Discord.py 反应角色

时间:2021-03-11 19:04:53

标签: python python-3.x discord.py

我正在制作一个反应角色频道,如果你对一个表情符号做出反应,它会给你相应的角色。我在我的验证系统中使用了这个完全相同的代码,它运行良好。但是由于某种原因,当我用相应的表情符号对消息做出反应时,我没有得到角色,没有回溯显示,并且我没有在控制台中收到一条消息,说“给某人一个角色”。

代码:

async def roles(ctx):
  embed=discord.Embed(title="Reaction Roles", description="React with the corresponding emoji to get the role", color=0xff0000, timestamp=datetime.now())
  embed.set_author(name="Official Gnag Discord", icon_url="https://upload.wikimedia.org/wikipedia/commons/thumb/2/2e/Exclamation_mark_red.png/50px-Exclamation_mark_red.png")
  embed.add_field(name="⛓️", value="Role 1", inline=True)
  embed.add_field(name="?", value="Role 2", inline=True)
  embed.add_field(name="?", value="Role 3", inline=True)
  embed.add_field(name="?️‍?", value="Role 4", inline=True)
  embed.add_field(name="?", value="Role 5", inline=True)
  embed.add_field(name="?", value="Role 6", inline=True)
  embed.add_field(name="?", value="Role 7", inline=True)
  embed.add_field(name="✔️", value="Role 8", inline=True)
  embed.add_field(name="?", value="Role 9", inline=True)
  embed.add_field(name="?", value="Role 10", inline=True)
  embed.add_field(name="♂️", value="Role 11", inline=True)
  embed.set_footer(text="Official Gnag Discord Bot Made By @Lukeee#6032")
  msg = await ctx.send(embed=embed)
  await msg.add_reaction('?')
  await msg.add_reaction('✔️')
  await msg.add_reaction('?')
  await msg.add_reaction('?')
  await msg.add_reaction('♂️')
  await msg.add_reaction('⛓️')
  await msg.add_reaction('?')
  await msg.add_reaction('?')
  await msg.add_reaction('?️‍?')
  await msg.add_reaction('?')
  await msg.add_reaction('?')


@bot.event
async def on_raw_reaction_add(payload):
    ourMessageID = 819597141743763467

    if ourMessageID == payload.message_id:
      member = payload.member
      guild = member.guild

      emoji = payload.emoji.name
      if emoji == '?':
        mrole = discord.utils.get(guild.roles, name='Role 7')
        await member.add_roles(mrole)
        print ('gave a role to someone')

1 个答案:

答案 0 :(得分:1)

嘿,虽然你没有说你的问题是我想命令不能正常工作。我建议做这样的事情:

async def roles(ctx):
      embed=discord.Embed(title="Reaction Roles", description="React with the corresponding emoji to get the role", color=0xff0000)
      embed.set_author(name="Official Gnag Discord", icon_url="https://upload.wikimedia.org/wikipedia/commons/thumb/2/2e/Exclamation_mark_red.png/50px-Exclamation_mark_red.png")
      embed.add_field(name="⛓️", value="Role 1", inline=True)
      embed.add_field(name="?", value="Role 2", inline=True)
      embed.add_field(name="?", value="Role 3", inline=True)
      embed.add_field(name="?️‍?", value="Role 4", inline=True)
      embed.add_field(name="?", value="Role 5", inline=True)
      embed.add_field(name="?", value="Role 6", inline=True)
      embed.add_field(name="?", value="Role 7", inline=True)
      embed.add_field(name="✔️", value="Role 8", inline=True)
      embed.add_field(name="?", value="Role 9", inline=True)
      embed.add_field(name="?", value="Role 10", inline=True)
      embed.add_field(name="♂️", value="Role 11", inline=True)
      embed.set_footer(text="Official Gnag Discord Bot Made By @Lukeee#6032")
      msg = await ctx.send(embed=embed)
      await msg.add_reaction('?')
      await msg.add_reaction('✔️')
      await msg.add_reaction('?')
      await msg.add_reaction('?')
      await msg.add_reaction('♂️')
      await msg.add_reaction('⛓️')
      await msg.add_reaction('?')
      await msg.add_reaction('?')
      await msg.add_reaction('?️‍?')
      await msg.add_reaction('?')
      await msg.add_reaction('?')

      def check(reaction, user):

            return user == ctx.user and reaction.message == msg and str(reaction.emoji) in ['?','✔','?','?','♂','⛓','?','?','?️‍?','?','?']

      while True:
           try:
               reaction, user = await client.wait_for("reaction_add", check=check)

               if str(reaction.emoji) == '?':
                    mrole = discord.utils.get(ctx.guild.roles, name='Role 7')
                    await user.add_roles(mrole)
                    print ('gave a role to someone')
               

Ps 在复制和粘贴它之前,请阅读它。它甚至可能不适合你。

编辑:您也可以加入 discord.py 服务器以获得更多帮助discord.py