【问题标题】:Check if the message author has a role discord.py检查消息作者是否有角色 discord.py
【发布时间】:2021-05-23 05:24:24
【问题描述】:

我正在为我的服务器中没有它的用​​户进行“硝基替换”,当有人发送动画表情符号 :test: 而他们没有硝基时,机器人会删除该消息,然后发送动画表情符号。

role = ["-『????』· ????????????????-????????????????????"]
@bot.listen("on_message")
async def nitro(msg):
        if ":" == msg.content[0] and ":" == msg.content[-1] and role in msg.author.roles:
            emoji_name = msg.content[1:-1]
            for emoji in msg.guild.emojis:
                if emoji_name == emoji.name:
                    await msg.channel.send(f"{msg.author.mention}{str(emoji)}")
                    await msg.delete()

机器人运行,但由于某种原因,当包含 and role in msg.author.roles: 部分时它不起作用。它只是留下了我的:test: 文本

机器人应该如何工作与它是如何工作的:

我做错了什么?

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    您将列表 (role) 与角色对象 (msg.author.roles) 进行比较。这种说法永远不会是真的。 您可以通过 getter 函数获得角色,例如

    role = msg.guild.get_role(id)
    

    或者,与角色名称进行比较

    role = "-『?』· ????-?????"
    @bot.listen("on_message")
    async def nitro(msg):
            if ":" == msg.content[0] and ":" == msg.content[-1] and role in [userRoles.name for userRoles in msg.author.roles]:
                emoji_name = msg.content[1:-1]
                for emoji in msg.guild.emojis:
                    if emoji_name == emoji.name:
                        await msg.channel.send(f"{msg.author.mention}{str(emoji)}")
                        await msg.delete()
    

    【讨论】:

    • 我如何使用多个角色来做到这一点。喜欢role = "-『?』· ????-?????", "⠀⠀⠀⠀『?』· ?????? ???????", "⠀⠀⠀⠀『?』· ?.?.? ???????", "⠀⠀⠀⠀『?』· ?.?.? ????", "⠀⠀⠀⠀『?』· ?.?.? ??????"
    • 试试any(rolesList in [userRoles.name for userRoles in msg.author.roles])
    猜你喜欢
    • 1970-01-01
    • 2019-11-17
    • 2021-02-22
    • 2021-10-10
    • 1970-01-01
    • 2019-02-01
    • 1970-01-01
    • 2021-01-30
    • 2021-07-01
    相关资源
    最近更新 更多