Discord.py default emoji reactions - discord.py

This code works for adding roles through reactions but I can only use custom emotes for reactions in the payload.emoji.name so when I set it equal to electric_plug it doesn't work. Is there any way around it?
async def on_raw_reaction_add(payload):
message_id = payload.message_id
if message_id == 799030902302834698:
guild_id = payload.guild_id
guild = discord.utils.find(lambda g : g.id == guild_id, client.guilds)
if payload.emoji.name == 'electric_plug':
role = discord.utils.get(guild.roles, name='Electrical')
if role is not None:
member = payload.member
if member is not None:
await member.add_roles(role)
else:
print('no member')
else:
print('no role')

Replace the "electric_plug" name for the unicode emoji. Unicode emojis look like this: πŸ‘
You can get a unicode emoji in two ways.
First, you can just look through emojipedia and search for your wanted emoji. After that, just copy the one they provide (the box should look like this):
The second option is going to discord, typing out your wanted unicode emoji normally (ex. ":thumbsup:"), and then placing a backslash (\) before it. Similar to this:
After that, send it, and copy the emoji that shows up. It should look different than the version of the emoji you usually see on Discord.
Once you've obtained your emoji, your code should look like this:
if payload.emoji.name == "πŸ”Œ":

Related

trying to make a choose command (like pls choose)

I've been trying to code a choose command like how dank memer has one but without the slash commands but the problem is i dont have that much coding knowledge to make the bot choose beetween 2 things so i did a code like this:
#bot.command(pass_context=True)
async def choose(ctx, *, message1, message2):
possible_responses = [
f"i choose {message1}",
f"i choose {message2}",
]
await ctx.send(random.choice(possible_responses) + " ")
the code worked but it only send "i choose" so how can i make my bot choose beetween 2 words and send the answer?
This is a very simple way of doing it,
#bot.command()
async def choose(ctx, option1, option2, option3):
choices = [option1, option2, option3]
await ctx.send(f"I choose, {random.choice(choices)}")

Discord.py Send a message after user reacts to a message

I'm trying to make my discord bot send a message as soon as a reaction has been added to a specific message.
This is my code:
#client.event
async def on_raw_reaction_add(message, reaction):
message_id = message.message_id
channel_id = 830438815595364372
if message_id == 830439131083046952:
if reaction.emoji == 'πŸ‘':
await channel.send('OK')
I'm not sure what I'm doing wrong, when I react to the message, nothing happens.
Thanks in advance.
L.
Your code seems a bit messed up to me. You have to use payload to get it working.
Have a look at the following code:
#client.event
async def on_raw_reaction_add(payload):
# channel and message IDs should be integer:
if payload.channel_id == ChannelID and payload.message_id == MessageID:
if str(payload.emoji) == "YourReaction": # Use a string
channel1 = client.get_channel(MessageChannel)
await channel1.send("Test")
What did we do?
Used payload
Defined the channel and message ID
Defined the channel where to send the message after the reaction
Note that the on_raw_reaction_add() event reference takes only one argument, payload. The payload argument is a RawReactionActionEvent object, which has a few key attributes for solving this issue: message_id and emoji.
The message_id attribute of the RawReactionActionEvent object can be compared to the target Message ID. RawReactionActionEvent.emoji is a PartialEmoji object, which has an attribute name, which returns the name of a custom emoji or the unicode representation of a default emoji. This can be compared to the target emoji, whether if it's the emoji copy-pasted or the unicode representation of the emoji.
Finally, Client objects have a method fetch_channel which takes the target Channel ID as its only argument and returns a GuildChannel object, which has the send() method which we all know and love.
#client.event
async def on_raw_reaction_add(payload):
message_id = 830439131083046952
channel_id = 830438815595364372
if payload.message_id == message_id and payload.emoji.name == "πŸ‘": # OR paylod.emoji.name == u"\U0001F44D"
channel = await client.fetch_channel(channel_id)
await channel.send("OK")

Trying to make a Discord bot send a message that uses multiple randomly chosen words

I wanted to make a Discord bot with discord.py that sends a message made out of randomly chosen predetermined words. I've tried inserting another variable after the first one, but that just gave a syntax error. Here's my code by the way.
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('f!generate'):
variable1 = [
'test',
'test2',
'test3',]
variable2 = [
'test',
'test2',
'test3',]
await message.channel.send((random.choice(variable1)),(random.choice(variable2)))
How to fix the problem?
Messageable.send only takes 1 positional argument, you're passing 2. Simply add the two strings together and it should work
await message.channel.send(random.choice(variable1) + random.choice(variable2))

how to make bot be able to show link for any emoji from any server

so this command works and all but...
#commands.command()
async def se(self, ctx, emoji: discord.Emoji):
await ctx.send(f"**Name:**Illdo this later **Link:**{emoji.url}")
it only works for the emojis from the server that the bot is in.
does anyone know how to make it be able to get the link for any servers emojis? even if the bot isnt in it
and if you wanna, also i need help with the showing the name of the emoji
the kind of thing i am going for is
thank you!
You need to get the id of the emoji and make the url itself
You can get its id by some split() and also you need to check if its animated so we can use .gif and .png accordingly
Below is the code:
#commands.command()
async def se(self, ctx, *, msg):
_id = msg.split(":") # split by ":"
if "<a" == _id[0]: # animated emojis structure <a:name:id>
ext = "gif"
else:
ext = "png" # normal emojis structure <name:id>
e_id = _id[2].split(">")[0].strip()# get the id
# url for a emoji is like this, try yourself if you want to check by manually copying any emoji's url
url = f"https://cdn.discordapp.com/emojis/{e_id}.{ext}"
await ctx.send(f"**Name**: :{_id[1]}: **Link**: {url}")

Discord python how can I get all the banned users?

I was looking at the API Reference and I found fetch_ban(user). How can I check if the user is banned from the server, I was reading that it returns the the BanEntry, and get a boolean? Can I use member as well or I need to get the user?
Thank you for any reply.
Tip: Always link what you're talking about.
fetch_ban
BanEntry (discord.py source code)
If you go through the source code you will very quickly find this in the first lines:
BanEntry = namedtuple('BanEntry', 'reason user')
Returned is a BanEntry object if the user is banned, otherwise it returns a NotFound Exception.
So to check if a user is banned just do:
async def is_banned(guild, user):
try:
entry = await guild.fetch_ban(user)
except discord.NotFound:
return False
return True
This will also work with members, as they are basically user objects with a bit of extra.
BanEntry is a named tuple (if you need a refresher on those here).
if you wanna command that sending list of banned users
async def banlist(self, ctx):
bans = await ctx.guild.bans()
loop = [f"{u[1]} ({u[1].id})" for u in bans]
_list = "\r\n".join([f"[{str(num).zfill(2)}] {data}" for num, data in enumerate(loop, start=1)])
await ctx.send(f"```ini\n{_list}```")
it gives list like this
[01] ε°Έδ»ι•Ώδ»ˆδΉƒε†‚δ»¨#0529 (269800030300033098)
[02] Yako#1001 (294113773333557952)
[03] Ping#9216 (46804048093530418)
[04] Vasky#6978 (494069478291921344)

Resources