当用户对特定频道中的消息做出反应时,分配不和谐角色

时间:2020-09-18 02:44:06

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

我希望用户在我的“欢迎和角色”不和频道中选择某种反应时被分配一个角色。我到处都看过,无法在python中找到与discord.py最新版本兼容的代码。这是我到目前为止的内容:

import discord

client = discord.Client()

TOKEN = os.getenv('METABOT_DISCORD_TOKEN')


@client.event
async def on_reaction_add(reaction, user):
    role_channel_id = '700895165665247325'
    if reaction.message.channel.id != role_channel_id:
        return
    if str(reaction.emoji) == "<:WarThunder:745425772944162907>":
        await client.add_roles(user, name='War Thunder')


print("Server Running")

client.run(TOKEN)

1 个答案:

答案 0 :(得分:0)

使用on_raw_reaction_add而不是on_reaction_add,因为on_reaction_add仅在消息位于漫游器缓存中时才起作用,而on_raw_reaction_add则不管内部消息缓存的状态如何都起作用

所有IDS,角色ID,通道ID,消息ID ...都不是整数,这是您的代码不起作用的原因,因为它比较了INT和STR。

还需要获得角色,不能仅仅传递角色名称

下面是工作代码

@client.event
async def on_raw_reaction_add(payload):
    if payload.channel_id == 123131 and payload.message_id == 12121212: #channel and message IDs should be integer:
        if str(payload.emoji) == "<:WarThunder:745425772944162907>":
            role = discord.utils.get(payload.member.guild.roles, name='War Thunder')
            await payload.member.add_roles(role)

编辑:针对on_raw_reaction_remove

@client.event
async def on_raw_reaction_remove(payload):
    if payload.channel_id == 123131 and payload.message_id == 12121212: #channel and message IDs should be integer:
        if str(payload.emoji) == "<:WarThunder:745425772944162907>":
            #we can't use payload.member as its not a thing for on_raw_reaction_remove
            guild = bot.get_guild(payload.guild_id)
            member = guild.get_member(payload.user_id)
            role = discord.utils.get(guild.roles, name='War Thunder')
            await member.add_roles(role)