我一直在查看 stackoverflow 帖子和很多地方,但仍然找不到适合我的这个问题的答案。如何使 python 斜线命令不一致? 关注这篇文章:https://stackoverflow.com/questions/71165431/how-do-i-make-a-working-slash-command-in-discord-py#:~:text=import%20discord%20from% 20discord.ext%20import%20commands%20create%20you,ids%20in%20which%20the%20slash%20command%20will%20appear。 我收到错误:
回溯(最近一次调用最后一次): 文件“/home/container/bot.py”,第 3 行,位于 bot = discord.Bot(command_prefix="!") AttributeError:模块“discord”没有属性“Bot”
使用此代码:
import discord
from discord.ext import commands
bot = discord.Bot(command_prefix="!")
@bot.slash_command(name="first_slash") #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 =discord.Bot”替换为“bot =commands.Bot”,但这也不起作用
我发现的唯一没有错误的代码是:
import discord
from discord.ext import commands
from discord_slash import SlashCommand, SlashContext
bot = commands.Bot(command_prefix="!")
slash = SlashCommand(bot)
@slash.slash(name="test")
async def _test(ctx: SlashContext):
await ctx.send("Hello World!")
但是discord上没有出现斜线命令
您出现此错误是因为您使用discord 的客户端而不是discord.ext 的。
bot = commands.Bot(command_prefix='!',intents=discord.Intents.all()) #gets all intents for the bot to work
接下来,将不再使用斜杠(变量和事件)。替换为此事件:
@bot.hybrid_command(put same args as the actual code)
完成此操作后,更新命令树,在 Discord 上应该不会出现任何问题。链接在这里:https://discordpy.readthedocs.io/en/stable/interactions/api.html#commandtree
希望有帮助。 :D
编辑:试试这个,这是你的代码,但我修改了它(如果它适合我,它也会适合你):
main.py:
import os
import discord
from discord.ext import commands
bot = discord.Bot(command_prefix="!",intents=discord.Intents.all()) #intents are required depending on what you wanna do with your bot
@bot.hybrid_command(name="first_slash")
async def first_slash(ctx):
await ctx.send("You executed the slash command!") #respond no longer works, so i changed it to send
@bot.event
async def on_ready():
await bot.sync() #sync the command tree
print("Bot is ready and online")
bot.run("Put your token here")
现在它应该在同步后一小时内出现。希望这有更多帮助。 (有点精度:如果你使用Pycord,代码会有所不同)
@TheUnknownYT 像这样使用它会给我一个错误:
回溯(最近一次调用最后一次): 文件“C:\Users\steph\OneDrive\Desktop\Coding\Discordbot\ModNPC estcode.py”,第 4 行,位于 bot = discord.Bot(command_prefix="!",intents=discord.Intents.all()) #intents 是否需要取决于你想用你的机器人做什么 ^^^^^^^^^^^ AttributeError:模块“discord”没有属性“Bot”
这不起作用...
已过时
Discord.py 不再更新。
如果你想使用更新版本的discord模块,你必须安装pycordhttps://guide.pycord.dev/installation
为此,首先您必须使用以下命令卸载discord.py模块:
pip uninstall discord.py
然后使用以下命令安装 pycord:
pip install py-cord
你的脚本将像这样工作:
import discord
from discord.ext import commands
bot = discord.Bot(debug_guilds=["YOUR TEST GUILD'S ID HERE"])
@bot.slash_command(name="first_slash")
async def first_slash(ctx):
await ctx.respond("You executed the slash command!")