如何修复“discord.errors.ClientException:Command kick 已注册。”错误?

Posted

技术标签:

【中文标题】如何修复“discord.errors.ClientException:Command kick 已注册。”错误?【英文标题】:How to fix "discord.errors.ClientException: Command kick is already registered." error? 【发布时间】:2019-09-03 12:09:03 【问题描述】:

我在 discord.py 中创建了一个不和谐机器人,它会踢任何发送特定字符串的成员,但我收到错误“discord.errors.ClientException: Command kick is already registered。”

bot = commands.Bot(command_prefix=',')
@client.event
async def on_message(message):
    if message.author == client.user:
    return    

    if "kick me"in message.content:
        @bot.command(name="kick", pass_context=True)
        @has_permissions(kick_members=True)
        async def _kick(ctx, member: Member):
            await bot.kick(member)

我没有踢成员,而是得到了这个可爱的回溯:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\Jason\AppData\Local\Programs\Python\Python35\lib\site-packages\discord\client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "C:\Users\Jason\AppData\Local\Programs\Python\Python35\PrawnBot.py", line 66, in on_message
    async def _kick(ctx, member: Member):
  File "C:\Users\Jason\AppData\Local\Programs\Python\Python35\lib\site-packages\discord\ext\commands\core.py", line 574, in decorator
    self.add_command(result)
  File "C:\Users\Jason\AppData\Local\Programs\Python\Python35\lib\site-packages\discord\ext\commands\core.py", line 487, in add_command
    raise discord.ClientException('Command 0.name is already registered.'.format(command))
discord.errors.ClientException: Command kick is already registered.

【问题讨论】:

【参考方案1】:

每当发送消息!kick me 时,您都在重新注册该命令。该命令应位于脚本或 cog 的顶层,而不是每次调用事件时都重新创建。

bot = commands.Bot(command_prefix=',')

@bot.event
async def on_message(message)
    ...
    await bot.process_commands(mesage)

@bot.command(name="kick", pass_context=True)
@has_permissions(kick_members=True)
async def _kick(ctx, member: Member):
    await bot.kick(member)

【讨论】:

以上是关于如何修复“discord.errors.ClientException:Command kick 已注册。”错误?的主要内容,如果未能解决你的问题,请参考以下文章

如何修复漏洞

如何修复WMI

PHP网站漏洞怎么修复 如何修补网站程序代码漏洞

如何修复这些漏洞? (npm audit fix 无法修复这些漏洞)

如何修复AppScan漏洞

如何在DOS环境下修复系统