Right, I want to make it so when I do "/ban #user reason" the bot responds with an embed saying are you sure you want to ban this user and reacts to its message with a tick, and awaits for the reaction off of the person who made the ban.
Try this:
#bot.command(name="ban")
async def ban(ctx, member, reason=""):
if not ctx.message.mentions:
await ctx.channel.send("You must mention a user to use this command")
embed = discord.Embed(
title="Confirm ban",
description=f"Are you sure you want to ban {member.mention}",
color=0xff0000
)
message = await ctx.channel.send(embed=embed)
await message.add_reaction(u"\U0001F44D")
def check(pay):
pay.member == member and pay.message_id == ctx.message.id
await bot.wait_for("raw_reaction_add", check=check)
await member.ban(reason=reason)
Related
I want the message to get sent to a specific channel
async def on_message_edit(before, after):
await before.channel.send(
f'{before.author} edited a message.\n'
f'Before: {before.content}\n'
f'After: {after.content}'
)
To send a message in a specific channel you need first to fetch the choosed channel by using his id by using get_channel() like this:
specificChannel = bot.get_channel(int(YOUR_CHANNEL_ID_HERE))
Then your event block should look like this:
#client.event
async def on_message_edit(before, after):
specificChannel = bot.get_channel(int(YOUR_CHANNEL_ID_HERE))
await specificChannel.send(
f'{before.author} edited a message.\n'
f'Before: {before.content}\n'
f'After: {after.content}'
)
Useful link:
get_channel()
Discord.py documentation
maybe this can help u :)
#client.event
async def on_member_join(member:Member):
if member.bot:
guild:discord.Guild = member.guild
role = guild.get_role(config.botRoleID)
channel:discord.TextChannel = guild.get_channel(YOUR_CHANNEL_ID)
await member.add_roles(role)
await channel.send(f'Set Bot role to "{member.display_name}" 😌')
async def on_reaction_add(reaction, user):
emoji = reaction.emoji
if emoji == "💌":
await user.channel.send("HI")
I got problem here with user.
I want to use here with ctx like ctx.channel.send.
but also it occured error how to use ctx in here?
Instead of using the on_reaction_add event, it's better in this case to use a wait_for command event. This would mean the event can only be triggered once and only when the command was invoked. However with your current event, this allows anyone to react to a message with that emoji and the bot would respond.
By using client.wait_for("reaction_add"), this would allow you to control when a user can react to the emoji. You can also add checks, this means only the user would be able to use the reactions on the message the bot sends. Other parameters can be passed, but it's up to you how you want to style it.
In the example below shows, the user can invoke the command, then is asked to react. The bot already adds these reactions, so the user would only need to react. The wait_for attribute would wait for the user to either react with the specified emojis and your command would send a message.
#client.command()
async def react(ctx):
message = await ctx.send('React to this message!')
mail = '💌'
post = '📮'
await message.add_reaction(mail)
await message.add_reaction(post)
def check(reaction, user):
return user == ctx.author and str(
reaction.emoji) in [mail, post]
member = ctx.author
while True:
try:
reaction, user = await client.wait_for("reaction_add", timeout=10.0, check=check)
if str(reaction.emoji) == mail:
await ctx.send('Hi you just received')
if str(reaction.emoji) == post:
await ctx.send('Hi you just posted...')
You need to use reaction.message.channel.send
async def on_reaction_add(reaction, user):
emoji = reaction.emoji
if str(emoji) == "💌": await reaction.message.channel.send("HI")
Im Trying to make a discord bot which sends an embed message with a reaction, and if a user reacts to that message he/she would get a role. This is for the Rules of the Server.
The Problem is, the Embed message gets sent but there is no reaction. If I manually react to it, and get someone else to react too he/she will get no role. (The embed message is the only message in that channel). I also get no Errors in the console.
The 'channel_id_will_be_here' is always replaced with the correct channel Id.
Thank you.
import discord
from discord.ext import commands
client = discord.Client()
#client.event
async def on_ready():
Channel = client.get_channel('channel_id_will_be_here')
print("Ready as always chief")
#client.event
async def on_message(message):
if message.content.find("|rules12345654323") != -1:
embedVar = discord.Embed(title="**Rules**", description="The Rules Everybody needs to follow.", colour=discord.Colour.from_rgb(236, 62, 17))
embedVar.add_field(name="Rule 1", value="Be nice etc", inline=False)
await message.channel.send(embed=embedVar)
async def on_reaction_add(reaction, user):
Channel = client.get_channel('channel_id_will_be_here')
if reaction.message.channel.id != Channel:
return
if reaction.emoji == ":white_check_mark:":
Role = discord.utils.get(user.server.roles, name="Player")
await client.add_roles(user, Role)
In if reaction.message.channel.id != Channel you compare the id to the Channel object, not the Channel.id.
You don't need to use the object there, just the id (which you use to create the channel object in the first place) would be fine
if reaction.message.channel.id != channel_id_will_be_here:
return
You could also use the message id like(So it'll only trigger when reacting to that exact message):
if reaction.message.id != rules_message_id_will_be_here:
return
The way you do your check is pretty strange too, why check to make the function return if False? Why not just make it add the role when True?
async def on_reaction_add(reaction, user):
if reaction.message.channel.id == channel_id_will_be_here and reaction.emoji == ":white_check_mark:":
Role = discord.utils.get(user.server.roles, name="Player")
await client.add_roles(user, Role)
You could even leave out the if reaction.emoji == ":white_check_mark:": part if it is the only message/emote reaction in that channel
I'm new to discord bot making, and I'd like to have the bot add a reaction to my message that, once pressed, allows the user reacting to pick up a role. What would be the code to allow me to do this? Thank you.
This is a very well written and formatted question! In order for the bot to detect that the reaction is on a specific message, you can do many ways. The easiest way would be to be by ID, so I'm just going to do this with a simple command.
messageIDs = []
#client.event
async def on_raw_reaction_add(payload):
global messageIDs
for messageID in messageIDs:
if messageID == payload.message_id:
user = payload.member
role = "roleName"
await user.add_roles(discord.utils.get(user.guild.roles, name = role))
#client.command()
async def addMessage(ctx, messageID):
global messageIDs
emoji = "👍"
channel = ctx.message.channel
try:
msg = await channel.fetch_message(messageID)
except:
await ctx.send("Invalid Message ID!")
return
await msg.add_reaction(emoji)
messageIDs.append(messageID)
I created a lot of moderation commands, and only allowed admins to use them.I also really want to make such a scheme:If a person who does not have the right to use the command writes it,the bot sends a message that you do not have the right to use this command.Please help me figure this out!
#client.command()
#commands.has_permissions(view_audit_log=True)
async def ban(ctx,member:discord.Member,reason):
emb = discord.Embed(title="ban",color=0xff0000)
emb.add_field(name='Модератор',value=ctx.message.author.mention,inline=False)
emb.add_field(name='Нарушитель',value=member.mention,inline=False)
emb.add_field(name='Причина',value=reason,inline=False)
await member.ban()
await channel.send(embed = emb)
You can use a error handler.
#client.command()
#commands.has_permissions(view_audit_log=True)
async def ban(ctx,member:discord.Member,reason):
emb = discord.Embed(title="ban",color=0xff0000)
emb.add_field(name='Модератор',value=ctx.message.author.mention,inline=False)
emb.add_field(name='Нарушитель',value=member.mention,inline=False)
emb.add_field(name='Причина',value=reason,inline=False)
await member.ban()
await channel.send(embed = emb)
#ban.error
async def ban_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send('You do not have the required permissions to use this command.')