使用python不协调斜线命令

k2fxgqgv  于 2023-01-16  发布在  Python
关注(0)|答案(2)|浏览(161)

我一直在看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%20will%20appearly.我得到了这个错误:
追溯(最近调用最后调用):文件"/home/container/www.example.com ",第3行,在bot = discord中。机器人程序(命令前缀="!")属性错误:bot.py", line 3, in bot = discord.Bot(command_prefix="!") AttributeError: module 'discord' has no attribute '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上没有出现斜杠命令

wdebmtf2

wdebmtf21#

出现此错误是因为您使用的是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,代码将有所不同)

m528fe3b

m528fe3b2#

过时

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!")

相关问题