【问题标题】:Problems with the discord.py botdiscord.py 机器人的问题
【发布时间】:2020-12-16 15:41:07
【问题描述】:

我有一个问题,当不是管理员的人执行命令“/cls”时,机器人没有发送它应该发送的消息,它所做的就是在 cmd 中给我一个错误。

我添加代码。

@bot.command()
@commands.has_permissions(manage_messages=True)
async def cls(ctx, Quantity= 10000):
    if ctx.message.author.guild_permissions.administrator:
        await ctx.channel.purge(limit=Quantity)
        await ctx.send('Successful removal!' + Quantity + ' mensajes.')
    else: 
        await ctx.send('You cannot run the '/cls' command because you are not admin')

这是当有人执行命令“/cls”时 cmd 告诉我的内容,我想要的是它给出了我放在那里的消息。

discord.ext.commands.errors.MissingPermissions:您缺少运行此命令的“管理消息”权限。

【问题讨论】:

  • 顺便说一句,您一次可以批量删除的最大消息是 200 条。所以10k太多了
  • 感谢您的回答。

标签: python-3.x discord.py


【解决方案1】:

删除has_permissions检查

以下是修改后的代码:

@bot.command()
async def cls(ctx, Quantity= 10000):
    if ctx.message.author.guild_permissions.administrator:
        await ctx.channel.purge(limit=int(Quantity))
        await ctx.send('Successful removal!' + Quantity + ' mensajes.')
    else: 
        await ctx.send('You cannot run the '/cls' command because you are not admin')

【讨论】:

  • 感谢您花时间回答这个问题,您帮了我很多。
【解决方案2】:

您可以按照 Poojan 的建议进行操作,也可以简单地在 has_permissions 装饰器中传递两个权限。

@bot.command()
@commands.has_permissions(administrator=True, manage_messages=True)
async def cls(ctx, quantity: int=10000): # I'm typehinting the quantity so it's already passed as an integer
    await ctx.channel.purge(limit=quantity) 
    await ctx.send("Succesful removal of {quantity} messages")


# If you want to send some message when the user doesn't have the needed perms:
@cls.error
async def cls_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send(f"You need {error.missing_perms} to use this command")
    elif isinstance(error, commands.BadArgument):
        await ctx.send("Please use only numbers")

【讨论】:

  • 感谢您花时间回答这个问题,您帮了我很多。
  • 不用担心,您可以通过接受和支持答案来回报我。
猜你喜欢
  • 2021-01-21
  • 2020-07-09
  • 1970-01-01
  • 2021-05-03
  • 2021-11-29
  • 1970-01-01
  • 2021-10-07
  • 2021-03-29
  • 2021-10-20
相关资源
最近更新 更多