Python Discord Bot 斜杠命令不起作用

问题描述 投票:0回答:1

我有这个 python 代码,但斜杠命令不起作用。当我运行代码时,机器人显示在线,但斜杠命令不起作用,并且控制台中没有记录任何命令:

import discord #Imports the discord module.
from discord.ext import commands #Imports discord extensions.

#The below code verifies the "client".
client = commands.Bot(command_prefix='-t', intents=discord.Intents.all())
#The below code stores the token.
token = "[TOKEN]"

'''
The below code displays if you have any errors publicly. This is useful if you don't want to display them in your output shell.
'''
@client.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.MissingRequiredArgument):
        await ctx.send('Please pass in all requirements :rolling_eyes:.')
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("You dont have all the requirements :angry:")

#The below code bans player.
@client.command()
@commands.has_permissions(ban_members = True)
async def ban(ctx, member : discord.Member, *, reason = None):
    await member.ban(reason = reason)

#The below code unbans player.
@client.command()
@commands.has_permissions(administrator = True)
async def unban(ctx, *, member):
    banned_users = await ctx.guild.bans()
    member_name, member_discriminator = member.split("#")

    for ban_entry in banned_users:
        user = ban_entry.user

        if (user.name, user.discriminator) == (member_name, member_discriminator):
            await ctx.guild.unban(user)
            await ctx.send(f'Unbanned {user.mention}')
            return

#The below code runs the bot.
client.run(token)

我尝试在开始时删除

intents=intents
块,但它给出了错误,当我将其更改为现在的样子时,没有任何变化。

机器人拥有所有特权网关意图[attachment 1 - Image Of Enabled Privileged Gateway Intents]

python discord discord.py bots
1个回答
0
投票

为什么这不起作用?

这与意图完全“毫无关系”。您实际上只是创建一些文本命令并期望它们像斜杠命令一样工作,但事实并非如此。 你说过:

但是斜杠命令不起作用

但这就是因为您正在创建文本命令。每次使用时:

@client.command()

它正在创建一个基于文本的命令。这方面的文档说:

一个快捷方式装饰器,调用 command() 并通过 add_command() 将其添加到内部命令列表。

command()

调用“将函数转换为

Command
”。它的定义是:

实现机器人
文本命令协议的类。

该怎么办:

参见

this

Stack Overflow 帖子。 来源

    @命令装饰器
  • discord.ext.commands.Command
© www.soinside.com 2019 - 2024. All rights reserved.