如何修复discord.py中的以下自动反应角色代码?

2024-04-19 01:17:10 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我希望机器人在用户对带有特定表情符号的消息做出反应时自动分配角色。但是我的代码不起作用。我怎样才能解决这个问题

@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)

Tags: 用户nameinaddid消息角色if
1条回答
网友
1楼 · 发布于 2024-04-19 01:17:10

对于标准的discord表情符号,不能使用名称,需要使用unicode符号。 所以不是“微笑”而是“微笑”😈'

要获取unicode表情符号,您可以在发送标准表情符号之前添加\项(不要认为它在手机上有效)

Discord.py 1.0之后,“服务器”更改为“公会”


    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)


相关问题 更多 >