【问题标题】:How do I fix the following auto react role code, in discord.py?如何在 discord.py 中修复以下自动反应角色代码?
【发布时间】:2020-09-29 19:12:13
【问题描述】:

所以我希望机器人在用户对带有特定表情符号的消息做出反应时自动分配角色。但是我的代码不起作用。我该如何解决这个问题????

@client.event 
async def on_raw_reaction_add(payload):
    messgae_id = payload.message_id
    if messgae_id == "759765065380659261":
        user = payload.user_id
        for roles in user.roles:
            if roles in DefaultRoles:
                return
        if payload.emoji.name == "smiling_imp":
            role = discord.utils.get(user.server.roles, name = "Demon")
            await user.add_role(user, role)

【问题讨论】:

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


    【解决方案1】:

    对于标准的不和谐表情,你不能使用名称,你需要使用 unicode 符号。 所以不是'smiling_imp'而是'?'

    要获得 unicode 表情符号,您可以在发送之前在标准表情符号之前添加 \(不要认为它适用于移动设备)

    Discord.py 1.0 后,“server”改为“guild”

    
        if message_id == ________________: 
    
            # Find the guild name
            guild_id = payload.guild_id
            guild = discord.utils.find(lambda g :g.id == guild_id, client.guilds)
    
            # Get your guild role
            if payload.emoji.name == '?':
                role = discord.utils.get(guild.roles, name='Demon')
    
            # Find the member you are trying to assign role to
            member = discord.utils.find(lambda m : m.id == payload.user_id, guild.members)
    
            # Assign role
            await member.add_roles(role)
    
    
    

    【讨论】:

    • 我很抱歉。我最初是在我的手机上,但出于某种原因,它似乎只显示了您的部分代码。我对我的答案进行了编辑,并添加了一个代码示例。同样从 discord.py v1.0 及以后,“server”更改为“guild”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 1970-01-01
    • 2022-01-06
    • 2021-04-02
    • 2021-10-10
    相关资源
    最近更新 更多