对用户角色的反应不起作用-discord.py
我只是用discord.py 为我的discord 服务器构建一个discord 机器人。 我写了一个代码,但它不起作用。它创建消息,您可以做出反应。甚至出现了第一条调试消息:用户对消息做出了反应。
但它不会给我这个角色。 这是我的代码:
async def reactMsg(ctx):
send_sub = await ctx.message.channel.send("React to this message to subscribe NPT.")
reactions = ['✅']
for i in reactions:
await send_sub.add_reaction(i)
@client.event
async def on_raw_reaction_add(payload):
#You forgot to await the bot.get_channel
channel = client.get_channel(payload.channel_id)
message = await channel.fetch_message(payload.message_id)
guild = client.get_guild(payload.guild_id)
#Put the following Line
member = guild.get_member(payload.user_id)
reaction = discord.utils.get(message.reactions, emoji=payload.emoji.name)
print("Debug: User reacted to message")
# only work if it is the client
if payload.user_id == client.user.id:
return
if payload.message_id == 943634886642765874 and reaction.emoji == '✅':
roles = discord.utils.get(guild.roles, name='TrackerSub')
await member.add_roles(roles)
await reaction.remove(payload.member)
print("Debug: Give role to user")
有什么想法我做错了吗?
I'm just building a discord bot for my discord server with discord.py.
I made a code, but it won't work. It creates the message and you can react. Even the first debug message comes: User reacted to message.
But it won't give me the role.
This is my code:
async def reactMsg(ctx):
send_sub = await ctx.message.channel.send("React to this message to subscribe NPT.")
reactions = ['✅']
for i in reactions:
await send_sub.add_reaction(i)
@client.event
async def on_raw_reaction_add(payload):
#You forgot to await the bot.get_channel
channel = client.get_channel(payload.channel_id)
message = await channel.fetch_message(payload.message_id)
guild = client.get_guild(payload.guild_id)
#Put the following Line
member = guild.get_member(payload.user_id)
reaction = discord.utils.get(message.reactions, emoji=payload.emoji.name)
print("Debug: User reacted to message")
# only work if it is the client
if payload.user_id == client.user.id:
return
if payload.message_id == 943634886642765874 and reaction.emoji == '✅':
roles = discord.utils.get(guild.roles, name='TrackerSub')
await member.add_roles(roles)
await reaction.remove(payload.member)
print("Debug: Give role to user")
Any ideas what I made wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新的代码
我发现有一些问题,让我们看一下您可以进行的一些更改,以使其变得更好:
请记住,
拥有良好的
on_command_error
对于 Discord 机器人开发非常重要。当调试这样的事情时,使用回溯总是一个好朋友。Updated Code
I see a couple things wrong, let's walk through some changes you can make to make this a bit better:
Remember
Having a good
on_command_error
is important to Discord bot development. Using the traceback is always going to be a good friend when debugging things like this.