【发布时间】:2019-08-09 00:53:15
【问题描述】:
当用户对帖子做出反应时,我在添加角色时遇到问题。
我希望它如何发挥作用是,当用户加入 Discord 服务器时,机器人将使用 on_join 事件发送消息(现在我使用命令 test 进行测试)。
下一步是on_reaction_add 事件,当用户对此消息做出反应时,机器人会将角色添加到该用户。
这是我正在使用的。但是,我已经对此进行了测试,但没有得到理想的结果。
(在 discord.py 重写时)
@commands.command(pass_context=True)
async def test(self, user: discord.Member): #lets change this to the command 'test' for now and change on on_join event later.
guildName = user.guild.name
embed = discord.Embed(colour=discord.Color(random.randint(0x000000, 0xFFFFFF)))
embed.title = "Welcome to {} {}!".format(guildName, user)
embed.description = "Head over to <#443133907102203912> to have a read up on our rules. Once you have read them please reaction to this post with a :thumbsup:"
embed.set_image(url='')
await user.send(embed=embed)
async def on_reaction_add(reaction, user):
channelid = '555844758778544160' #The text-channel where the react should happen
role = discord.utils.get(user.guild.roles, name="Members") #The role applied when the post is reacted to
if reaction.message.channel.id != channelid:
return #So it only happens in the specified channel
if str(reaction.emoji) == "<:thumbsup:555844758778544160>":#The reaction (not sure if this should be raw in discord.py rewrite)
await user.add_roles(user, role)#Adds role when post is reacted to
【问题讨论】:
-
这段代码是在 cog 还是在扩展中?您使用的是什么版本的 discord.py?最近的一项更改破坏了扩展和 cogs 工作方式的许多向后兼容性,因此您可能正在查看过时的示例。您需要在
user之前有一个ctx参数,因为正在传入上下文,并且您可能还需要on_reaction_add中的self(假设此代码在一个齿轮中)。 -
这是一个齿轮,我正在使用 discord.py 重写
-
什么版本号?你上次更新是什么时候?几周前进行了重大更改,因此如果您使用新代码但遵循可能解释您的问题的旧教程/示例。
-
@PatrickHaugh discord.py 重写 1.0.0a
-
@PatrickHaugh 谢谢你的回复我已经发布了一个答案,所以我可以告诉你我已经尝试过这个(添加了一些更改)不幸的是对用户消息的反应仍然没有申请这个角色我在这里错过了什么吗?
标签: python python-3.x discord discord.py discord.py-rewrite