如何解决"discord.errors.ClientException:命令踢已被注册."错误? [英] How to fix "discord.errors.ClientException: Command kick is already registered." error?

查看:57
本文介绍了如何解决"discord.errors.ClientException:命令踢已被注册."错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在discord.py中制作了一个discord机器人,该机器人可以踢出发送特定字符串的任何成员,但出现错误"discord.errors.ClientException:命令踢已被注册."

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.

推荐答案

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

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)

这篇关于如何解决"discord.errors.ClientException:命令踢已被注册."错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆