Discord.py Simple reaction role - discord

I'm looking for a simple way to make a reaction role with my code setup. Every tutorial I've seen does it differently so I'm confused
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$krole'):
embed = discord.Embed(title="lorem ipsum",
description="lorem ipsum",
)
I need that when someone reacts to the embed with a specific emoji it will assign them a role

I suggest listening for on_raw_reaction_add And then looking at the str(payload.emoji) and comparing it to your desired emoji, and then comparing payload.channel_id and payload.message_id to the channel and message id of the reaction role.

Related

How to set up a discord bot that replies upon mention to messages which contain certain words without prefix command?

I'm trying to create a simple discord bot that replies with mention to messages that contain certain words.
Also, I want the bot to respond only when it gets mentioned and without a prefix.
This was my try, but the bot didn't reply at all to the messages.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='')
#client.event
async def on_message(message):
if client.user.mentioned_in(message){
if message.content == "Hi"{
await message.channel.send(f"Hello {message.author.mention}")
}
if message.content == "How are you?"{
await message.channel.send(f"I'm good {message.author.mention}")
}
}
client.run("TOKEN")
message.content contains the entirety of the message text so when you do #mybot Hi, the message.content will be something like: <#MY_BOT_ID> hi. Your code is checking that the message.content is exactly equals to either Hi or How are you? and that is not going to be the case.
You could use Python's in operator to check for certain text:
#client.event
async def on_message(message):
if client.user.mentioned_in(message):
# making the text lowercase here to make it easier to compare
message_content = message.content.lower()
if "hi" in message_content:
await message.channel.send(f"Hello {message.author.mention}")
if "how are you?" in message_content:
await message.channel.send(f"I'm good {message.author.mention}")
client.run("TOKEN")
Though, this isn't perfect. The bot will also respond to anything with the characters hi in. You could split the content of the messages by word and check that or you could use regular expressions. See this post for more info.
Additionally, your python code had some {} brackets - which is invalid syntax for if statements - I've corrected that in my example. Consider looking up python syntax.

Discord.py doesn't update on member remove

I am making a discord bot that changes a voice channels' name when a user joins or leaves to the amount of members on the server. My issue is that when a user leaves, it doesn't update. Any help is appreciated.
#bot.event
async def on_raw_member_remove(member):
channel = discord.utils.get(member.guild.channels, id=973603264639668248)
await channel.edit(name=f'Member Count: {member.guild.member_count}')
The right event for this would be on_member_remove.
You can also get the channel in a much easier way and edit it.
See a possible new code:
#bot.event
async def on_member_remove(member: discord.Member):
channel = bot.get_channel(Channel_ID_here)
await channel.edit(name=f"Member Count: {len(member.guild.members)}")
The reason it doesn't work is because you are using on_raw_member_remove
You should be using on_member_leave as the member is not getting removed.

A specific role only reaction

I'm trying to get it so a specific role can react to the message to be able to ban the user, I have made the ability to be able to react to the message, and the author of the command can ban the user, but I want to make it so another role can do it as well
Here is what I have currently
def check(rctn, user):
return user.id == ctx.author.id and str(rctn) == '<:tick:837398197931868190>'
reaction, user = await bot.wait_for('reaction_add', check=check)
You can use #commands.has_permissions
For example you could make it:
#bot.command()
#commands.has_permissions(ban_members=True)
# rest of your code
This way only roles with the permissions to ban_members can use the command

discord.py sending message to channel name instead of ID

I am a beginner to this programming stuff, and I have a quick question. I am trying to make a logs channel for multiple servers, and I want to be able to look for a channel that has the word “logs” in it, so can you help me?
Code:
#client.event
async def on_command_completion(ctx):
channel = client.get_channel('829067962316750898')
embed = discord.Embed(colour=discord.Color.green(),
title="Command Executed")
embed.add_field(name="Command:", value=f"`,{ctx.command}`")
embed.add_field(name="User:", value=f"{ctx.author.mention}", inline=False)
embed.add_field(name="Channel:",
value=f"{ctx.channel} **( <#{ctx.channel.id}> )**")
await channel.send(embed=embed)
You need to get the channel. I suggest using discord.utils.get.
Also, try using {ctx.channel.mention} to mention the channel instead.
#client.event
async def on_command_completion(ctx):
channel = discord.utils.get(ctx.guild.text_channels, name='logs')
if channel is None:
return # If there is no logs channel, do nothing (or what you want to do)
embed = discord.Embed(colour=discord.Color.green(),
title="Command Executed")
embed.add_field(name="Command:", value=f"`,{ctx.command}`")
embed.add_field(name="User:", value=f"{ctx.author.mention}", inline=False)
embed.add_field(name="Channel:",
value=f"{channel.name} **( {channel.mention} )**")
await channel.send(embed=embed)
See the discord.TextChannel docs and discord.utils.get()

Adding reaction if message contains certain content

I wanted to ask how do I make my bot react to a message if it contains something, for example:
if the message contains "Hi" the bot will react with :wave:.
so how do I do this? any help is appreciated, I am new to the forum so sorry if I made any mistakes. :)
You should try this
#client.event
async def on_message(message):
if 'Hi' in message.content.split():
await message.add_reaction("👋")
It will probably work
if you use cogs :
#commands.Cog.listener()
async def on_message(self,message):
if 'Hi' in message.content.split():
await message.add_reaction("👋")

Resources