它说没有找到命令,当它就在那里时,我也尝试过使用 on_message 事件,但这些也不起作用。
bot = commands.Bot(command_prefix='-', Help_Command=help, intents=discord.Intents.all())
intents=discord.Intents.all()
client = discord.Client(intents = intents)
activity = discord.Activity(type = discord.ActivityType.streaming, name="name", url = "twitch_url")
tree = app_commands.CommandTree(client)
bot.remove_command('help')
blue = discord.Color.from_rgb(0, 0, 200)
red = discord.Color.from_rgb(200, 0, 0)
green = discord.Color.from_rgb(0, 200, 0)
discord_blue = discord.Color.from_rgb(84, 102, 244)
guild_id= 1132535168247275600
@tree.command(name="sync", description="Make slash commands global.", guild=discord.Object(id=guild_id))
async def sync(int: discord.Interaction, member: discord.Member):
await tree.sync()
print('Command tree synced.')
@bot.command()
async def test(ctx):
await ctx.send("hello")
从 Python 3.11 和 Discord.py 2.3.2 开始,您可以通过 commands.Bot 使用前缀命令和斜杠命令。信用:雷克西
如果您选择使用discord.Client,
import discord
from discord import app_commands
client = discord.Client(...)
tree = app_commands.CommandTree(client)
@tree.command(name="mycommand")
async def command_callback(interaction: discord.Interaction):
await interaction.response.send_message(content="Hello World")
如果您选择使用commands.Bot,
import discord
from discord.ext import commands
bot = commands.Bot(...)
tree = bot.tree
# Same command code as discord.Client example
注意:您仍然可以将 on_message 等事件与应用程序命令树一起使用。
您还遇到同步问题。将
sync
函数放入斜杠命令中时,它不会同步,因为该命令本身尚未同步。
相反,您应该首先将其放入 on_ready 事件中,然后再删除它并使用
sync
命令。或者创建一个前缀同步命令,可以在重新启动时使用。
@bot.event
async def on_ready():
await bot.tree.sync()
@bot.command(name="sync", description="Sync the command tree.")
async def sync_callback(ctx):
await bot.tree.sync()
await ctx.reply(content="Synced!")
有关斜杠命令的更多信息,请参阅 discord.Interaction、app_commands.CommandTree。
更多关于 discord.Client、commands.Bot。
问题是您想要与树命令同步,该命令只有在同步后才会同步。你创造了一个悖论。而是使用普通命令或 on_ready() 事件来同步命令。