I'm trying to get the id of an emoji in the reaction of a message but I don't know how to do it.
#client.event
async def on_message(message):
channal = client.get_channel(825523364844142601)
embeds = message.embeds
for embed in embeds:
if embed.author.name in wishlist:
reaction = get(message.reactions)
print(reaction)
await channal.send("yes")
else:
await channal.send("false")
context : im playing a discord's game, where a bot send an embed with the name of a character and on this embed, the bot automatically add a reaction. What i want is get the id of the bot's reaction and with the id add the same
You don't need to get the emoji again to react. You can just add the reaction from the Emoji class.
I also suggest waiting a little bit and getting the message again because reactions won't be on the Message object immediately.
#client.event
async def on_message(message):
channal = client.get_channel(825523364844142601)
embeds = message.embeds
for embed in embeds:
if embed.author.name in wishlist:
await asyncio.sleep(1.0) # Give time for reaction to update on cache
message = await message.channel.fetch_message(message.id) # Update message object
reaction = message.reactions[0] # Get first reaction of a message
emoji = reaction.emoji
emoji_id = emoji.id
await message.add_reaction(emoji) # You can just add with emoji, you don't need to get emoji again
await channal.send("yes")
else:
await channal.send("false")
See the discord.Reaction object.
Related
I'm trying to make a small matchmaking bot, and I want to get a list of people who reacted to a message the bot just sent so I can tag them in another message later. So far, I was able to get the message running and making something happen when it reaches 2 reactions:
import discord
import random
import os
from discord.utils import get
client = discord.Client(activity=discord.Game(name='Use !queue'))
#client.event
async def on_ready():
print('Logged in.'.format(client))
#client.event
async def on_message(message):
if message.content.startswith('!queue'):
await message.delete()
channel = client.get_channel(948024211736244255)
bot = await channel.send(
'<#&948034633373712445>\n\nA game is being set up for 8 players!\n\nTo join, react to this message with :white_check_mark:\n\nWhen 8 players have joined, maps will be voted upon and teams will be automatically balanced ingame.'
)
await bot.add_reaction(emoji='✅')
#client.event
async def on_raw_reaction_add(ctx):
ctx.channel_id == 948024211736244255
ctx.emoji.name == "✅"
channel = client.get_channel(948024211736244255)
message = await channel.fetch_message(ctx.message_id)
reaction = get(message.reactions, emoji=ctx.emoji.name)
count = reaction.count
if count == 2:
reactors = await message.reactions[0].users().flatten()
user = random.choice(reactors)
await channel.send("{} has reacted to the message.".format(user))
await message.delete()
client.run(os.getenv('TOKEN'))
But now I tried to get the bot to tag multiple people and I've been unsuccessful. Tried reading documentation and online help, but can't get anything to work.
Combining a user reacting with sending them a DM has had me stumped.
My project works by having an embed that has a reaction added at the bottom and when the user clicks the emoji then it will DM the user a set of instructions (for now I'm just using the word 'Test').
Here is what I have so far:
#client.event
async def on_message(message):
if message.content.startswith('.add'):
embed = discord.Embed(title="Embed Title", color=0x6610f2)
embed.add_field(name="__**Steps to get DM'd:**__", value="""
• Step 1
• Step 2
• Step 3
• Step 4
• Step 5
""")
msg = await message.channel.send(embed=embed)
await msg.add_reaction('\U0001F91D')
#client.event
async def on_raw_reaction_add(payload):
if payload.user_id == client.user.id:
return
if str(payload.emoji) == '\U0001F91D':
channel = await client.fetch_channel(payload.channel_id)
message = await channel.fetch_message(payload.message_id)
await message.author.send('Test')
I'm just not sure how to get the user's ID outside of the on_message() function because the only way I know how to make the on_reaction_add work is in its own client event. I'm not even sure if I'm handling the DM user aspect correctly.
Using the async version of Discord.py
In your payload, you should already get a discord.Member object you can work with. Try something like
#client.event
async def on_raw_reaction_add(payload):
if str(payload.emoji) == "\U0001F91D":
member = payload.member
await member.send('This is a DM to you!')
Because your code does not check for the message id, a reaction to ANY message (not just the one you sent) will lead to a response. To prevent this, you can check whether payload.message_id matches the id of your bot's message (which you could store somewhere when sending the message).
You also tried to prevent a response to the initial reaction of your bot. Instead of checking for payload.user_id, I would suggest
if member.bot:
return
which prevents a response to any bot reacting to your message, including your own.
So I am trying to add three different reactions (emojis), to a message that the bot has sent in a text channel.
The user fills in a form in their DM, and the message then get sent into a text-channel called "admin-bug", the admins of the server can then react to three different emojis:
fixed
will not be fixed
not-a-bug
And then, depending on what emoji the admin press on, the message will be transferred to a text channel.
But! I can't seem to figure out how you actually add the reactions to the message itself, I have done a bunch of googling, but can't find the answer.
code:
import discord
from discord.ext import commands
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)
Messagable.send returns the message it sends. So you can add reactions to it using that message object. Simply, you have to use a variable to define the message you sent by the bot.
embed = discord.Embed(title="Bug report")
embed.add_field(name="Name", value="value")
msg = await adminBug.send(embed=embed)
You can use msg to add reactions to that specific message
await msg.add_reaction("💖")
Read the discord.py documentation for detailed information.
Message.add_reaction
The discord.py docs have an FAQ post about adding reactions, it has multiple exampes and an indepth description, furthermore Messageable.send returns the message send so you can use Message.add_reaction on that. https://discordpy.readthedocs.io/en/neo-docs/faq.html#how-can-i-add-a-reaction-to-a-message
You need to save the embed as a variable, in this way you can add a reaction.
message = await adminBug.send(embed=embed) # save embed as "message"
await message.add_reaction('xxx') # add reaction to "message"
I'm not sure because I use nextcord (and it worked), but I think this can work :
#bot.command
async def testembed(ctx):
embed = discord.Embed(title='Test !', description='This is a test embed !')
msg = await ctx.send("", embed=embed)
msg = msg.fetch() # Notice this line ! It's important !
await msg.add_reaction('emoji_id')
i want send messages to all channel that bot has joined
def check(cnt):
while 1:
if hash_origin != str(subprocess.check_output(["md5sum",filename])[:-len(filename)-3])[2:-1] :
print("file destroyed alert!")
alert = 1
sleep(0.5)
i want send message to all discord channel that bot joined when specific file's hash result is different from original.
i know how to send respond message to channel
#client.event
async def on_message(message):
using this code, right?
but i want to send message to all of the channel that bot has joined when some event has happens.
In order to send a message to every channel that the bot is in, you must do:
#client.event
async def foo(bar): # change this to the event in which you wish to call it from
for guild in client.guilds:
for channel in guild.channels:
await channel.send(messagedata) # change messagedata to whatever it is you want to send.
#client.event
async def foo(bar): # change this to the event in which you wish to call it from
for guild in client.guilds:
for channel in guild.channels:
await channel.send(messagedata) # change messagedata to whatever it is you want to send.
when using this code directly, error has occurred
AttributeError: 'CategoryChannel' object has no attribute 'send'
because "guild.channels" object have list of "all channels"(including text,voice,etc..) that bot has joined.
so, if i want send text message to channel, using "guild.text_channels" instead.
anyway, thanks to Tylerr !!
I'm trying to make my discord bot say something when someone talks and if that person deletes their message the bot will delete it's response.
The Bot is for discord written in python using discord.py, trying to delete it's response when the person it is responding to deletes their message.
BOT_PREFIX = '.', '?'
message_list = {}
bot = commands.Bot(command_prefix=BOT_PREFIX, message_list={})
#bot.event
async def on_message_delete(message):
if message in message_list:
await message.channel.delete()
del message_list[message]
#bot.event
async def on_message(message):
print(message.author.id)
if message.author.id == 137351212856115200:
response = await message.channel.send('Message')
message_list[message] = response
await bot.process_commands(message)
else:
await bot.process_commands(message)
return
I expect it to detect when the person deletes their message it will delete its response, but it is trying to delete the person's deleted message.
Solved! Replaced await message.channel.delete() with await message_list[message].delete()