React 对用户角色不起作用 - discord.py
React for user role doesn't work - 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")
有什么想法我做错了吗?
更新代码
我发现有几处不对劲,让我们来看看您可以进行的一些更改以使其变得更好:
async def react_msg(ctx):
# Send a message, no need for `ctx.message.channel.send`, it's redundant.
message = await ctx.send("React to this message to subscribe NPT.")
reactions = ['✅']
for i in reactions:
await message.add_reaction(i)
@client.event
async def on_raw_reaction_add(payload):
# We don't want to do operations if the bot is the one who reacted
if payload.user_id == client.user.id:
return
channel = client.get_channel(payload.channel_id)
if channel is None: # This channel wasn't found
return None
guild = channel.guild
if guild is None: # This reaction was not in a guild
return
member = payload.member
if member is None:
try:
member = guild.get_member(payload.user_id) or (await guild.fetch_member(payload.user_id))
except discord.NotFound: # This member wasn't found
return None
message = await channel.fetch_message(payload.message_id)
reaction = discord.utils.get(message.reactions, emoji=payload.emoji.name)
if reaction is None: # This reaction was not found
return
# The message ID is not right or the emoji was wrong
if payload.message_id != 943634886642765874 or str(reaction.emoji) != '✅':
return
roles = discord.utils.get(guild.roles, name='TrackerSub')
if roles is None:
# This role was not found, return.
return
await member.add_roles(roles)
await reaction.remove(member)
记得
拥有良好的 on_command_error
对 Discord 机器人开发很重要。在调试这样的事情时,使用回溯总是一个好朋友。
我正在使用 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")
有什么想法我做错了吗?
更新代码
我发现有几处不对劲,让我们来看看您可以进行的一些更改以使其变得更好:
async def react_msg(ctx):
# Send a message, no need for `ctx.message.channel.send`, it's redundant.
message = await ctx.send("React to this message to subscribe NPT.")
reactions = ['✅']
for i in reactions:
await message.add_reaction(i)
@client.event
async def on_raw_reaction_add(payload):
# We don't want to do operations if the bot is the one who reacted
if payload.user_id == client.user.id:
return
channel = client.get_channel(payload.channel_id)
if channel is None: # This channel wasn't found
return None
guild = channel.guild
if guild is None: # This reaction was not in a guild
return
member = payload.member
if member is None:
try:
member = guild.get_member(payload.user_id) or (await guild.fetch_member(payload.user_id))
except discord.NotFound: # This member wasn't found
return None
message = await channel.fetch_message(payload.message_id)
reaction = discord.utils.get(message.reactions, emoji=payload.emoji.name)
if reaction is None: # This reaction was not found
return
# The message ID is not right or the emoji was wrong
if payload.message_id != 943634886642765874 or str(reaction.emoji) != '✅':
return
roles = discord.utils.get(guild.roles, name='TrackerSub')
if roles is None:
# This role was not found, return.
return
await member.add_roles(roles)
await reaction.remove(member)
记得
拥有良好的 on_command_error
对 Discord 机器人开发很重要。在调试这样的事情时,使用回溯总是一个好朋友。