不和谐清除命令仅限于所有者

时间:2018-09-30 10:35:23

标签: python bots discord discord.py

对于不和谐的机器人来说是非常新的。我希望限制一个清除命令,仅将消息删除给所有者。如果调用该命令的用户没有权限,我还想返回一条简单消息“您没有使用此命令的权限”。我目前编写了以下内容:

async def is_owner(ctx):

    return ctx.author.id == *my userid*

@client.command(pass_context=True)

@commands.check(is_owner)

async def clear(ctx, amount=5):

    channel = ctx.message.channel

    messages = []

    async for message in client.logs_from(channel, limit=int(amount)):

        messages.append(message)

    await client.delete_messages(messages)

    await client.say('Messages deleted')

尽管此功能起作用,但当前运行它的任何用户都可以执行命令。不知道我在想什么。感谢您提供纠正此问题的任何指导或建议。

对def is_owner(ctx)进行更正后的错误如下:

Ignoring exception in on_message
Traceback (most recent call last):
  File "F:\Python\lib\site-packages\discord\client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "F:\Python\lib\site-packages\discord\ext\commands\bot.py", line 857, in on_message
    yield from self.process_commands(message)
  File "F:\Python\lib\site-packages\discord\ext\commands\bot.py", line 846, in process_commands
    yield from command.invoke(ctx)
  File "F:\Python\lib\site-packages\discord\ext\commands\core.py", line 367, in invoke
    yield from self.prepare(ctx)
  File "F:\Python\lib\site-packages\discord\ext\commands\core.py", line 344, in prepare
    self._verify_checks(ctx)
  File "F:\Python\lib\site-packages\discord\ext\commands\core.py", line 338, in _verify_checks
    if not self.can_run(ctx):
  File "F:\Python\lib\site-packages\discord\ext\commands\core.py", line 438, in can_run
    return all(predicate(context) for predicate in predicates)
  File "F:\Python\lib\site-packages\discord\ext\commands\core.py", line 438, in <genexpr>
    return all(predicate(context) for predicate in predicates)
  File "f:\Discord Bots\First\bot.py", line 19, in is_owner
    return ctx.author.id == 'MY USERID'
AttributeError: 'Context' object has no attribute 'author'

5 个答案:

答案 0 :(得分:1)

您的函数应该使用 @commands.is_owner()

不要尝试使用该命令删除您的消息

这是删除消息的 discord.py 解决方案:

@commands.is_owner()
async def clear(ctx, amount=5):
    deleted = await ctx.channel.purge(limit=amount)

有关清除的更多信息,请查看 discord.py reference

答案 1 :(得分:0)

您的装饰器@commands.check(is_owner)可能包含此问题。尝试查看该装饰器是否可以运行以及是否可以运行,然后检查是否存在我所说的任何问题。此外,Discord API文档指出,超过2周的邮件无法删除,因为它会“干扰Discord的邮件数据库”。

答案 2 :(得分:0)

您可能会看到类似RuntimeWarning: coroutine 'is_owner' was never awaited的错误,这意味着您在不希望使用协程时给了一些协程,因此它的价值被忽略了。

在异步分支上,commands.check仅对协程不起作用。通过删除is_owner

async协程更改为函数
def is_owner(ctx):
    return ctx.message.author.id == *my userid*

答案 3 :(得分:0)

这是我的做法

@client.command()
async def clear(ctx, amount=1):
    if ctx.message.author.id(YOUR ID HERE)
        await ctx.channel.purge(limit = amount + 1) 
        # this is because we want to include the message that calls the command
    else:
        ctx.send("I'm sorry, you cannot do that.") 

玩得开心编码,如果这不起作用,请随时给我发邮件:官员 grr#6609

答案 4 :(得分:0)

试试这个:

@client.command() @commands.has_any_role(YourRoleID)

    async def clear(self, ctx, limit: int=None):
        success = 0
        failed = 0
        async for msg in ctx.message.channel.history(limit=limit):
            if ctx.message.author != client.user:
                try:
                    await msg.delete()
                    success += 1
                except:
                    failed += 1

        print(f"Removed {success} messages with {failed} errors")