我如何将我的机器人从使用基于文本/前缀的命令更改为disonce.py中的斜杠命令和按钮



我看到大多数开发人员正在将他们的机器人程序切换为使用斜杠命令,而不是通常的前缀命令。如何使用斜杠命令重写我的机器人程序?我不知道如何开始使用斜杠命令。

您需要通过安装最新版本pip install -U git+https://github.com/Rapptz/discord.py来安装discord.py v2.0。

基本示例(自由功能命令(

import asyncio
from discord.ext import commands
from discord import app_commands
# define Bot with **needed** parameters
bot = commands.Bot(command_prefix="some_prefix", intents=some_intents_definition)
# You can now use `@bot.tree.command()` as a decorator:
@bot.tree.command()
async def my_command(interaction: discord.Interaction) -> None:
await interaction.response.send_message("Hello from my command!")
### NOTE: the above is a global command, see the `main()` func below:

async def main():
async with bot:
# do you setup stuff if you need it here, then:
bot.tree.copy_global_to(guild=discord.Object(id=...))  # we copy the global commands we have to a guild, this is optional
await bot.start(MY_TOKEN)

您可以在此处找到更多示例:https://gist.github.com/AbstractUmbra/a9c188797ae194e592efe05fa129c57f


参考文献:
  • CommandTree.command()
  • discord.Interaction
  • CommandTree.copy_global_to

最新更新