【问题标题】:Ban and Unban commands to work in all servers禁止和取消禁止命令在所有服务器中工作
【发布时间】:2019-09-03 02:00:52
【问题描述】:

我一直在编写一个不和谐的机器人,对于它的禁令命令,这是我使用的代码:

@client.command(aliases=['Ban'])
async def ban(ctx, member: discord.Member, days: int = 1):
    if get(ctx.author.roles, id=548841535223889923):
        await member.ban(delete_message_days=days)
        await ctx.send("Banned {}".format(ctx.author))
    else:
        await ctx.send(ctx.author.mention + " you don't have permission to use this command.".format(ctx.author))

但是,如果我尝试在其他服务器上使用它,它只会告诉我我没有所需的角色.. 那么如何获取机器人自动加入的新服务器的角色 ID,或者是否有其他方法可以实现这一点?

【问题讨论】:

  • 每个角色都有唯一的ID,即使是同名的角色也有不同的ID,但单独的角色来自不同的服务器。您现在的操作方式只能允许 ban 命令为 1 个角色工作,仅此而已。这也意味着它仅适用于具有该 1 个角色的 1 个服务器。
  • 我已经解决了,你可以使用:@commands.has_permissions(<kick/ban)_members=True)

标签: discord.py


【解决方案1】:

您可以使用check for permissions 而不是查找特定的角色 ID

import discord
from discord.ext import commands

@client.command(aliases=["Ban"])
@commands.has_permission(ban_members=True)
async def...

【讨论】:

    【解决方案2】:

    您可以使用 has_permission,如下所示:

    @client.command(aliases=["Ban"])
    @commands.has_permission(administrator=True)
    async def ban(ctx, member: discord.Member, days: int = 1):
        await member.ban(delete_message_days=days)
        await ctx.send("Banned {}".format(ctx.author))
    

    然后您可以添加缺少权限的错误处理程序:

    @client.event
    async def on_command_error(ctx, error):
      if isinstance(error, commands.MissingPermissions):
        await ctx.send("You do not have the permissions required for this command.")
        return
    
      raise error
    

    【讨论】:

      猜你喜欢
      • 2021-09-15
      • 2021-06-03
      • 2021-06-01
      • 2021-06-20
      • 2021-10-06
      • 2020-10-20
      • 1970-01-01
      • 2021-05-11
      • 2018-11-07
      相关资源
      最近更新 更多