Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discord.py - Make a bot react to its own message(s)

I am trying to make my discord bot react to its own message, pretty much. The system works like this:

A person uses the command !!bug - And gets a message in DM', she/she is supposed to answer those questions. And then whatever he/she answered, it will be transferred an embedded message to an admin text-channel.

But I need to add 3 emojis, or react with three different emojis. And depending on what the admin chooses, it will transfer the message once more. So if an admin reacts to an emoji that equals to "fixed", it will be moved to a "fixed" text-channel (the entire message).

I have done a lot of research about this, but only found threads about the old discord.py, meaning await bot.add_react(emoji) - But as I have understood it, that no longer works!

Here is my code:

import discord
from discord.ext import commands
import asyncio

TOKEN = '---'
bot = commands.Bot(command_prefix='!!')

reactions = [":white_check_mark:", ":stop_sign:", ":no_entry_sign:"]


@bot.event
async def on_ready():
    print('Bot is ready.')


@bot.command()
async def bug(ctx, desc=None, rep=None):
    user = ctx.author
    await ctx.author.send('```Please explain the bug```')
    responseDesc = await bot.wait_for('message', check=lambda message: message.author == ctx.author, timeout=300)
    description = responseDesc.content
    await ctx.author.send('````Please provide pictures/videos of this bug```')
    responseRep = await bot.wait_for('message', check=lambda message: message.author == ctx.author, timeout=300)
    replicate = responseRep.content
    embed = discord.Embed(title='Bug Report', color=0x00ff00)
    embed.add_field(name='Description', value=description, inline=False)
    embed.add_field(name='Replicate', value=replicate, inline=True)
    embed.add_field(name='Reported By', value=user, inline=True)
    adminBug = bot.get_channel(733721953134837861)
    await adminBug.send(embed=embed)
    # Add 3 reaction (different emojis) here

bot.run(TOKEN)
like image 909
simon sjöö Avatar asked Jul 17 '20 19:07

simon sjöö


People also ask

How do you make discord bot react to messages?

Take your cursor on the message and then select the “Add Reaction” button to use the react option for that message. Discord will display an emoji picker where you may select an emoji for your message response. From this menu, you can pick an emoji to add as a reaction to that message.

Is it better to make a discord bot with Python or js?

its better to go with discord. js then. In the end use whichever one you find simpler.


2 Answers

In discord.py@rewrite, you have to use discord.Message.add_reaction:

emojis = ['emoji 1', 'emoji_2', 'emoji 3']
adminBug = bot.get_channel(733721953134837861)
message = await adminBug.send(embed=embed)

for emoji in emojis:
    await message.add_reaction(emoji)

Then, to exploit reactions, you'll have to use the discord.on_reaction_add event. This event will be triggered when someone reacts to a message and will return a Reaction object and a User object:

@bot.event
async def on_reaction_add(reaction, user):
    embed = reaction.embeds[0]
    emoji = reaction.emoji

    if user.bot:
        return

    if emoji == "emoji 1":
        fixed_channel = bot.get_channel(channel_id)
        await fixed_channel.send(embed=embed)
    elif emoji == "emoji 2":
        #do stuff
    elif emoji == "emoji 3":
        #do stuff
    else:
        return

NB: You'll have to replace "emoji 1", "emoji 2" and "emoji 3" with your emojis. add_reactions accepts:

  • Global emojis (eg. 😀) that you can copy on emojipedia
  • Raw unicode (as you tried)
  • Discord emojis: \N{EMOJI NAME}
  • Custom discord emojis (There might be some better ways, I've came up with this after a small amount a research)
    async def get_emoji(guild: discord.Guild, arg):
         return get(ctx.guild.emojis, name=arg)
    
like image 191
MrSpaar Avatar answered Nov 13 '22 08:11

MrSpaar


When you want to add non custom emojis you need to add its id and name into a format that discord.py can read: <:emoji name:emoji id>

For example, the white and green check emoji that is a discord default emoji... you would need to write <:white_check_mark:824906654385963018> for discord.py to be able to identify it.

like image 1
SimpNotFound Avatar answered Nov 13 '22 07:11

SimpNotFound