How to get reaction from wait_for("reaction_add") - python

I am developing a little reaction roles bot in discord.py - for this I am trying to use the .wait_for() function with the "reaction_add" parameter. The problem is that I need to get the emoji from the reaction, which is not working as an error is shown:
AttributeError: 'tuple' object has no attribute 'user'
Seemingly there is something wrong with the way I am trying to get the emoji, but I couldn't find the proper way to get the emoji.
interaction = await client.wait_for('reaction_add')
if interaction.emoji == "🚨":
await interaction.user.add_roles(role)

emoji = "🚨"
message = await ctx.send("Test")
await message.add_reaction(emoji)
reaction, user = await bot.wait_for("reaction_add")
if user != bot.user:
if reaction.emoji == emoji:
await user.add_roles(user, role)
or
emoji = "🚨"
def check(reaction, user):
return user == ctx.message.author and reaction.emoji == emoji
message = await ctx.send("Test")
await message.add_reaction(emoji)
reaction, user = await bot.wait_for("reaction_add", check=check)
await bot.add_roles(user, role)

Related

Discord.py delete only the latest message by reaction

i want to delete a message by reacting with a trash emoji. My only problem is, that all messages by the one person get deleted when reacting.
So first it looks like this:
https://i.stack.imgur.com/JBBWh.png
And after reacting to the second "trash" emoji, all four message are deleted.
Thank you! :)
#client.event
async def on_message(message):
if not (message.author.bot):
embed = discord.Embed(title="Points earned!", description="+1 Points")
msg = await message.channel.send(embed=embed)
await message.add_reaction("πŸ—‘οΈ")
channel = message.channel
def check(reaction, user):
return user == message.author and str(reaction.emoji) == 'πŸ—‘οΈ'
try:
reaction, user = await client.wait_for('reaction_add', check=check)
except asyncio.TimeoutError:
await channel.send("This shouldnt happen")
else:
await msg.delete()
await message.delete()
You never checked for the correct message. When you react to one of them, it triggers the checks for both messages and both are deleted. What you want is this:
def check(reaction, user):
return user == message.author and str(reaction.emoji) == 'πŸ—‘οΈ' and reaction.message.id == message.id
This checks that the message you were reacting to is equal to the original message.
This way, the reaction will only trigger for the message that it reacted to.

how to make bot use reaction emoji for change embed using python

I'm trying to make a bot that uses emoji to change the embed message like owo or some kind of music bot pls show me how
I'm just a guy who is interested in coding and just begin making a bot
You could catch the on_reaction_add event.
#bot.event
async def on_reaction_add(reaction, user):
if (message.author.id == bot.user.id) and (str(reaction.message.id) == "id-of-message-to-be-edited"):
reaction.message.edit(embed=Embed(title="new embed content"))
Or if you're using this in the flow of a command or a pre-called event, use discord.Client.wait_for.
#bot.command()
async def foo(ctx):
await ctx.send('Say hello by reaction with πŸ‘!')
def check(reaction, user):
return user == message.author and str(reaction.emoji) == 'πŸ‘'
try:
reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check)
except asyncio.TimeoutError:
await channel.send('Bye πŸ‘Ž')
else:
await channel.send('Hello πŸ‘')

Discord py add role on_message

I've been trying to make a command that adds a role to the user when he/she speaks in a specific channel. Then the role mutes the user making it a one message channel.
Code:
#client.event
async def on_message(message):
if message.channel.name == 'πŸ’»γƒ»message-archive':
await client.process_commands(message)
elif "" in message.content:
role = discord.utils.get(message.guild.roles, name="one message")
await message.author.add_roles(role)
await client.process_commands(message)
The problem is that it adds a role in ANY channel. I want it to be used only on 1 channel.
I've tried using if and else if but it didn't work.
Simply check if the name/ID of the channel is the correct one:
if channel.id == 182739817283172:
await message.author.add_roles(role)
if channel.name == "whatever":
await message.author.add_roles(role)

Discord.py Giving role

#client.command()
#commands.has_permissions(manage_messages=True)
async def mute(ctx, member: discord.Member):
try:
guild = ctx.guild
rolecreate = "MutedByCloudy"
await guild.create_role(rolecreate, colour=discord.Colour.red())
if member.guild_permissions > ctx.author.guild_permissions or member.guild_permissions.administrator:
await ctx.send("I can't mute that user. User probably has more perms than me or you")
else:
if member.id == 739424025205538817:
await ctx.send("Nice try. I can't mute myself.")
else:
role = get(member.guild.roles, name='MutedByCloudy')
await member.add_roles(role)
embed=discord.Embed(title="User Muted! :white_check_mark:", description=f"**{member}** was muted by **{ctx.message.author}**!", color=0x2f3136)
await ctx.send(embed=embed)
except:
await ctx.send(":x: Something happened. I don't know what.")
So i have this command that creates a role, and gives it to the user. But It doesnt create the role and doesnt throw any errors, can you guys help? The giving role part works but the creating role doesn't.
Well you aren't creating the role, you could try something like this:
role = discord.utils.get(ctx.guild.roles, name='MutedByCloudy')
if not role:
role = await ctx.guild.create_role(name='MutedByCloudy', reason='Role for muting, auto generated by Cloudy')
for channel in ctx.guild.channels:
await channel.set_permissions(role, send_messages=False,
read_message_history=False,
read_messages=False)
You could also add an try and except discord.Forbidden to handle permission errors

Discord bot AttributeError: 'str' object has no attribute 'id'

I was messing around with making a bot, but I can't figure out how to ban someone...
if message.content.upper().startswith('CHILL BAN'):
if "447929519945416734" in [role.id for role in
message.author.roles] or "448191759739256842" in [role.id for role in
message.author.roles] or message.author.id == "218156043799101452":
await bot.kick(message.mentions[0].name)
await bot.send_message(message.channel, "Someone has been
banned!")
#["<#356065937318871041>"]
else:
await bot.send_message(message.channel, "You do not have
sufficient privileges.")
Don't use the on_message event for a ban command
use the command handler instead
#bot.command(pass_context=True)
async def ban(ctx, user: discord.Member):
await bot.ban(user)
await bot.say(f"{user.name} Just got banned")
# Do extra stuff here if you want

Categories

Resources