简体   繁体   中英

How to make a discord bot that gives a role on reaction of all messages on chat?

I'm trying to make a discord bot to automate the giving of a certain role, the members receive the role when they react to all messages on a especific chat.

I started making bots in python recently and i have no clue of how to do it

Works only for Discord.py 2.o

@bot.command()
async def reactionroles(ctx):
    msg=await ctx.send("Reaction Roles!")
    await msg.add_reaction('🎂')
    await msg.add_reaction('🐶')


@bot.event
async def on_raw_reaction_add(payload):
    channel = await bot.fetch_channel(payload.channel_id)
    message = await channel.fetch_message(payload.message_id)
    user = await bot.fetch_user(payload.user_id)
    emoji = payload.emoji
    emoemoji=f"{emoji}"
    if payload.user_id != 941728904694075442:#your bot id
     if emoemoji=='🎂':
      role=ctx.guild.get_role(981534767185002506)#cake role id
      if role in user.roles:
         await user.remove_roles(role)
         emd=discord.Embed(title="Removed the Cake role",color=0x4582e6)
      else:
         await user.add_roles(role)
         emd=discord.Embed(title="Added the Cake role",color=0x4582e6)
      emd.set_author(name=user.display_name, icon_url=user.display_avatar.url)
      await message.send(embed=emd)
     if emoemoji=='🐶':
      role=ctx.guild.get_role(981534767185002506)#dog role id
      if role in user.roles:
         await user.remove_roles(role)
         emd=discord.Embed(title="Removed the Dog role",color=0x4582e6)
      else:
         await user.add_roles(role)
         emd=discord.Embed(title="Added the Dog role",color=0x4582e6)
      emd.set_author(name=user.display_name, icon_url=user.display_avatar.url)
      await message.send(embed=emd)



@bot.event
async def on_raw_reaction_remove(payload):
    channel = await bot.fetch_channel(payload.channel_id)
    message = await channel.fetch_message(payload.message_id)
    user = await bot.fetch_user(payload.user_id)
    emoji = payload.emoji
    emoemoji=f"{emoji}"
    if payload.user_id != 941728904694075442:#your bot id
     if emoemoji=='🎂':
      role=ctx.guild.get_role(981534767185002506)#cake role id
      if role in user.roles:
         await user.remove_roles(role)
         emd=discord.Embed(title="Removed the Cake role",color=0x4582e6)
      else:
         await user.add_roles(role)
         emd=discord.Embed(title="Added the Cake role",color=0x4582e6)
      emd.set_author(name=user.display_name, icon_url=user.display_avatar.url)
      await message.send(embed=emd)
     if emoemoji=='🐶':
      role=ctx.guild.get_role(981534767185002506)#dog role id
      if role in user.roles:
         await user.remove_roles(role)
         emd=discord.Embed(title="Removed the Dog role",color=0x4582e6)
      else:
         await user.add_roles(role)
         emd=discord.Embed(title="Added the Dog role",color=0x4582e6)
      emd.set_author(name=user.display_name, icon_url=user.display_avatar.url)
      await message.send(embed=emd)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM