【问题标题】:Discord slash commands using python使用python的不和谐斜线命令
【发布时间】:2022-10-20 15:21:56
【问题描述】:

我一直在查看 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 = commands.Bot”替换“bot = discord.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!")

但是不和谐上没有出现斜杠命令

【问题讨论】:

标签: python discord bots pycord


【解决方案1】:

过时的

Discord.py 不再更新。

如果你想使用更新版本的 discord 模块,你必须安装 pycord https://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!")

【讨论】:

猜你喜欢
  • 2021-09-27
  • 2021-11-20
  • 1970-01-01
  • 1970-01-01
  • 2022-12-19
  • 1970-01-01
  • 2021-10-05
  • 2022-11-05
  • 1970-01-01
相关资源
最近更新 更多