首页 > 解决方案 > 嵌入的 Discord.py 反应角色不起作用

问题描述

我正在尝试制作一个不和谐的机器人,它会发送带有反应的嵌入消息,如果用户对该消息做出反应,他/她将获得一个角色。这是针对服务器规则的。

问题是,嵌入消息被发送但没有反应。如果我手动对其做出反应,并让其他人也做出反应,他/她将没有任何角色。(嵌入消息是该频道中唯一的消息)。我在控制台中也没有错误。

'channel_id_will_be_here' 总是替换为正确的频道 ID。

谢谢你。

import discord
from discord.ext import commands

client = discord.Client()

@client.event
async def on_ready():
    Channel = client.get_channel('channel_id_will_be_here')
    print("Ready as always chief")

@client.event
async def on_message(message):
    if message.content.find("|rules12345654323") != -1:
        embedVar = discord.Embed(title="**Rules**", description="The Rules Everybody needs to follow.", colour=discord.Colour.from_rgb(236, 62, 17))
        embedVar.add_field(name="Rule 1", value="Be nice etc", inline=False)
       
        await message.channel.send(embed=embedVar)

async def on_reaction_add(reaction, user):
    Channel = client.get_channel('channel_id_will_be_here')
    if reaction.message.channel.id != Channel:
        return
    if reaction.emoji == ":white_check_mark:":
        Role = discord.utils.get(user.server.roles, name="Player")
        await client.add_roles(user, Role)

标签: discord.py

解决方案


if reaction.message.channel.id != Channel您将 id 与 Channel 对象进行比较,而不是 Channel.id。

您不需要在那里使用该对象,只需 id(首先用于创建通道对象)就可以了

if reaction.message.channel.id != channel_id_will_be_here:
    return

您还可以使用消息 id 之类的(因此它只会在对该确切消息做出反应时触发):

if reaction.message.id != rules_message_id_will_be_here:
    return

您进行检查的方式也很奇怪,为什么要检查以使函数在 False 时返回?为什么不让它在 True 时添加角色?


async def on_reaction_add(reaction, user):
    if reaction.message.channel.id == channel_id_will_be_here and reaction.emoji == ":white_check_mark:":
        Role = discord.utils.get(user.server.roles, name="Player")
        await client.add_roles(user, Role)

if reaction.emoji == ":white_check_mark:":如果它是该频道中唯一的消息/表情反应,您甚至可以省略该部分


推荐阅读