当有人键入“!clear”没有权限时,使bot不协调发送消息

2024-04-26 12:05:00 发布

您现在位置:Python中文网/ 问答频道 /正文

@client.command(pass_context=True)
@commands.has_permissions(administrator=True)
async def clear(ctx,amount=1000):
    await ctx.channel.purge(limit=amount)
@clear.error
async def clear_error(ctx,error):
    if isinstance(error, MissingPermissions):
        text = "Sorry {}, you do not have permissions to do that!".format(ctx.message.author)
        await client.send_message(ctx.message.channel, text)

输出:(我希望它向没有权限管理的用户发送一条消息,并删除他的消息。我不理解这个错误,它发生在我尝试发送时。请修复它。泰,你能帮我修复吗?伙计们,谢谢,我是新来的。)

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke   
    await ctx.command.invoke(ctx)
  File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 855, in invoke  
    await self.prepare(ctx)
  File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 777, in prepare 
    if not await self.can_run(ctx):
  File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 1087, in can_run
    return await discord.utils.async_all(predicate(ctx) for predicate in predicates)
  File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\utils.py", line 348, in async_all
    for elem in gen:
  File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 1087, in <genexpr>    
    return await discord.utils.async_all(predicate(ctx) for predicate in predicates)
  File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 1790, in predicate    
    raise MissingPermissions(missing)
discord.ext.commands.errors.MissingPermissions: You are missing Administrator permission(s) to run this command.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 71, in wrapped        
    ret = await coro(*args, **kwargs)
  File "c:\Users\cyberx\Desktop\Paid projects\Discord bot\bot.py", line 25, in clear_error
    await client.send_message(ctx.message.channel, text)
AttributeError: 'Bot' object has no attribute 'send_message'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "c:\Users\cyberx\Desktop\Paid projects\Discord bot\bot.py", line 14, in on_message
    await client.process_commands(message)
  File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 976, in process_commands
    await self.invoke(ctx)
  File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 943, in invoke
    await ctx.command.dispatch_error(ctx, exc)
  File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 424, in dispatch_error    await injected(ctx, error)
  File "C:\Users\cyberx\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 77, in wrapped        
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Bot' object has no attribute 'send_message' 

Tags: inpyliblocallineawaitusersappdata
2条回答

您需要创建一个错误处理程序,如下所示:

@<commandname>.error
    async def <commandname>_error(self, ctx, error):
        if isinstance(error, commands.MissingPermissions):  #could be other type of error
            await ctx.send(f"Sorry {ctx.author.mention}, you do not have permissions to do that!")

如果您是新来者,我建议您阅读以下内容:https://vcokltfre.dev/tutorial/12-errors/

另外,要删除触发器,只需使用await ctx.message.delete()

首先,错误处理程序应该是commands.MissingPermissions而不是MissingPermissions

其次,client.send_message()在重写中被弃用

第三,我不认为可以在Python2.7中使用discord.py,也许可以升级到Python3

相关问题 更多 >