【问题标题】:discord.py Set a command to only work if the user doesn't have a certain rolediscord.py 将命令设置为仅在用户没有特定角色时才起作用
【发布时间】:2021-07-29 03:15:32
【问题描述】:

我正在尝试设置一个验证命令,该命令在使用时会提供一个角色,但我想设置它,以便用户只能在他们还没有角色的情况下使用该命令。这是我的代码:

@client.command()
async def verify(ctx):
  if str(ctx.guild.id) in verify_roles:
    guild = ctx.message.author.guild
    rolegiven = ctx.message.author.guild.get_role(verify_roles.get(str(guild.id)))
    await ctx.message.author.add_roles(rolegiven)

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:

    您可以做的一件事是在代码的最开头检索rolegiven。然后您可以通过member.roles 检查ctx.author 是否具有此角色。这是解释的简化版本。

    @client.command()
    async def verify(ctx):
        await ctx.message.delete() # deletes the message the author sent
        if str(ctx.guild.id) in verify_roles:
            verify_role = ctx.guild.get_role(verify_roles.get(str(guild.id)))
            # check if the role is not in the author's current roles
            if verify_role not in ctx.author.roles:
                await ctx.author.add_roles(verify_role)
                await ctx.send("You have been verified!")
            else:
                await ctx.send(f"You already have {verify_role.name}")
    

    【讨论】:

    • 还有,你知道命令执行时如何删除命令触发器吗?
    • @QuantumCode 我相应地编辑了我的问题,但您可以使用message.delete()
    猜你喜欢
    • 2019-04-04
    • 2021-01-19
    • 2018-08-27
    • 2021-08-05
    • 1970-01-01
    • 2020-12-03
    • 2021-04-08
    • 1970-01-01
    • 2020-10-11
    相关资源
    最近更新 更多