How to fix “discord.errors.ClientException: Command kick is already registered.” error?

为君一笑 提交于 2021-02-11 12:40:16

问题


I'm making a discord bot in discord.py that kicks any member that sends a certain string, but I get the error "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)

Instead of kicking the member, I get this lovely traceback:

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:


Whenever the message !kick me is sent, you're re-registering the command. The command should be at the top level of your script or cog, not recreated every time the event is called.

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)


来源:https://stackoverflow.com/questions/55656497/how-to-fix-discord-errors-clientexception-command-kick-is-already-registered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!