【问题标题】:Discord.py commands always says as existing command, even when it's completely randomDiscord.py 命令总是显示为现有命令,即使它是完全随机的
【发布时间】:2021-02-08 15:44:49
【问题描述】:

我正在尝试在 discord.py 中编写一个仅响应命令的 Discord 机器人,例如当您键入 =!firewall 时,它会显示 Hello!这是我的代码:

import discord
from discord.ext import commands

TOKEN = ('')

client = discord.Client()
bot = commands.Bot(command_prefix='=!')

@bot.event
async def on_ready():
    print('Bot ready')

@bot.event
async def on_message(message):
    if message.author == client.user:
        return

    @bot.command(pass_context=True)
    async def chickennuggets(ctx):
        await message.channel.send("Hello!")
    await bot.process_commands(message)

bot.run(TOKEN)

但是,当我尝试在 Discord 中使用 =!firewall 运行命令时,控制台会返回:

Ignoring exception in on_message
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "/Users/pixdoet/Code/python/firewall/bottest.py", line 19, in on_message
    async def chickennuggets(ctx):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/ext/commands/core.py", line 1257, in decorator
    self.add_command(result)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/ext/commands/core.py", line 1143, in add_command
    raise CommandRegistrationError(command.name)
discord.ext.commands.errors.CommandRegistrationError: The command chickennuggets is already an existing command or alias.

很明显,chickennuggets 不是默认命令,也不是 Python 中任何其他库使用的命令。我已经在互联网上搜索过这个但没有用。请帮忙。谢谢!

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    首先,您在帖子中使用不同的名称引用了命令chickennuggets,这告诉我您在代码中使用了该命令的别名。但是,您尚未显示别名或定义它的方式。您在代码中的缩进也不正确。试试这个:

    import discord
    from discord.ext import commands
    
    client = discord.Client()
    bot = commands.Bot(command_prefix='=!')
    
    @bot.event
    async def on_ready():
        print('Bot ready')
    
    @bot.event
    async def on_message(message):
        if message.author == client.user:
            return
        await bot.process_commands(message)
    
    @bot.command(pass_context=True, aliases = ['firewall'])
    async def chickennuggets(ctx):
        await ctx.send("Hello!")
    
    TOKEN = ('') # insert your token here
    bot.run(TOKEN)
    

    另外,请不要将您的机器人令牌泄露到任何公共论坛,因为其他人可以使用您的机器人令牌通过您的机器人运行他们的代码。现在,您必须在 Discord 的开发者门户中访问您的应用程序并生成一个新令牌。这将使您之前的令牌无效,因此没有人可以使用它在您的机器人上运行他们的代码。

    【讨论】:

    • 感谢您的回答,但实际上在client.user中使用了客户端
    • 糟糕,忽略了这一点。
    • 不要使用client.user,而是使用bot.user。您正在运行机器人实例,而不是客户端。另外为什么要定义两者?这样做没有意义
    猜你喜欢
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2021-03-26
    • 1970-01-01
    • 2021-11-05
    • 2022-06-11
    相关资源
    最近更新 更多