【发布时间】:2022-10-07 21:05:54
【问题描述】:
我正在尝试使用 discord.py 制作一个斜杠命令我已经尝试了很多它似乎不起作用的东西。帮助将不胜感激。
【问题讨论】:
标签: discord discord.py
我正在尝试使用 discord.py 制作一个斜杠命令我已经尝试了很多它似乎不起作用的东西。帮助将不胜感激。
【问题讨论】:
标签: discord discord.py
注意:我将在最后包含一个 pycord 版本,因为我认为它更简单,也是原始答案。
首先确保您安装了最新版本的 discord.py。 在您的代码中,您首先导入库:
import discord
from discord import app_commands
然后你定义你的客户和树:
intents = discord.Intents.default()
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)
该树包含您的所有应用程序命令。然后你可以定义你的命令:
@tree.command(name = "commandname", description = "My first application Command", guild=discord.Object(id=12417128931)) #Add the guild ids in which the slash command will appear. If it should be in all, remove the argument, but note that it will take some time (up to an hour) to register the command if it's for all guilds.
async def first_command(interaction):
await interaction.response.send_message("Hello!")
然后,您还必须在客户端准备好后将命令同步到不和谐,因此我们在 on_ready 事件中执行此操作:
@client.event
async def on_ready():
await tree.sync(guild=discord.Object(id=Your guild id))
print("Ready!")
最后我们必须运行我们的客户端:
client.run("token")
要安装 py-cord,首先运行 pip uninstall discord.py,然后运行 pip install py-cord。
然后在你的代码中,首先导入库
import discord
from discord.ext import commands
创建你的机器人类
bot = commands.Bot()
并使用创建您的斜杠命令
@bot.slash_command(name="first_slash", guild_ids=[...]) #Add the guild ids in which the slash command will appear. If it should be in all, remove the argument, but note that it will take some time (up to an hour) to register the command if it's for all guilds.
async def first_slash(ctx):
await ctx.respond("You executed the slash command!")
然后使用您的令牌运行机器人
bot.run(TOKEN)
【讨论】:
他们正在向 discord.py 添加斜杠命令,但您可以在 https://gist.github.com/Rapptz/c4324f17a80c94776832430007ad40e6 中看到一些示例您似乎正在使用我没有使用过的 discord_slash。
这些东西的主要文档是https://discordpy.readthedocs.io/en/master/interactions/api.html?highlight=dropdown#decorators,但主要的“方法”是你必须创建一个“树”,将命令附加到该树,并同步你的树以显示命令。 discord.ext.Bot 创建自己的树,这就是为什么我使用它而不是客户端,我认为默认情况下它不会创建树。
如果您指定公会,命令同步会立即发生,但如果您不指定公会,我认为更新或类似的东西需要一个小时,因此请指定公会,直到您准备好部署。
我不太确定如何在 cogs 中执行此操作,因为我有我的组,但基本上我正在做的是主 bot 文件中的 @bot.tree.command() 和单独文件中的几个组的组合.
所以这是我的主文件
import discord
import simplegeneralgroup
from config import TOKEN
MY_GUILD = discord.Object(id=1234567890)
class MyBot(discord.ext.commands.Bot):
async def on_ready(self):
await self.tree.sync(guild=MY_GUILD)
bot: discord.ext.commands.Bot = MyBot
@bot.tree.command(guild=MY_GUILD)
async def slash(interaction: discord.Interaction, number: int, string: str):
await interaction.response.send_message(f'Modify {number=} {string=}', ephemeral=True)
bot.tree.add_command(simplegeneralgroup.Generalgroup(bot), guild=MY_GUILD)
if __name__ == "__main__":
bot.run(TOKEN)
然后是 simplegeneralgroup 文件
import discord
from discord import app_commands as apc
class Generalgroup(apc.Group):
"""Manage general commands"""
def __init__(self, bot: discord.ext.commands.Bot):
super().__init__()
self.bot = bot
@apc.command()
async def hello(self, interaction: discord.Interaction):
await interaction.response.send_message('Hello')
@apc.command()
async def version(self, interaction: discord.Interaction):
"""tells you what version of the bot software is running."""
await interaction.response.send_message('This is an untested test version')
应该有三个命令:/slash,将提示用户输入数字和字符串,/generalgroup hello 和 /generalgroup 版本
【讨论】:
discord.py 不支持斜线命令,并且永远不会添加对斜线命令的支持(因为它已关闭)因此我推荐 disnake(一个流行的 fork)。特别是disnake,因为在所有分叉中disnake 似乎是更聪明的一个。
【讨论】:
disnake 更好,因为它不需要完全重写您当前的机器人并且使用与原始库相同的语法。