discord.py - 嵌入的 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)
解决方案
在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:":
如果它是该频道中唯一的消息/表情反应,您甚至可以省略该部分
推荐阅读
- c# - MediatR - Handling current user in query
- java - Why is my look and feel look very weird when I launch my program?
- json - Clojure:遍历 json 并返回 true 或 false,具体取决于给定文档中每个 key:value 对的 contion 检查结果
- shell - 使用 docker run 命令“执行格式错误”
- python - 基于最大元素创建新数据框
- html - 为什么 css 中的溢出会延迟工作?
- html - 如何在 HTML CSS 网格上定位“覆盖矩形”,但从列边界水平偏移?(根据我的图像)
- message - RosAria 节点不接收消息
- laravel - lararave intervention image upload slow code review
- python - json 编码器 json.dump 和 json.dumps 的不同结果