How do I get bot to read mention? - discord.py

If someone mentions me how do I get the bot to read that? like if someone mentions me I want the bot to say something back. How would I do that?
#client.event
me = '<#user_id>'
async def on_message(message):
member = message.author.id
if message.content == me:
await message.channel.send('my master will be back shortly')
else:
return
await client.process_commands(message)

You can use discord.Message.mentions. It returns a list of discord.Member that were mentioned in message.
#client.event
async def on_message(message):
me = message.guild.get_member(<user id>)
if me in message.mentions:
await message.channel.send('my master will be back shortly')
await client.process_commands(message)
Reference
discord.Message.mentions

Related

How do i make a welcoming command in discord.py?

Im trying to make a welcome message for my bot but the message never works. I have all 3 intents enabled and i dont know whats wrong. Can someone please help?
#bot.event
async def on_member_join(member):
channel = bot.get_channel(906928559602421830)
embed=discord.Embed(title="Welcome!",description=f"{member.mention} Just Joined")
await channel.send(embed=embed)
What does the alfred stands for?
And for the code:
#bot.event
async def on_member_join(member):
guild = member.guild
channel = guild.get_channel(channel_id)
embed=discord.Embed(title="Welcome!",description=f"{member.mention} Just Joined")
await channel.send(embed=embed)

discord.py bot message deleting not working

my code is
#bot.command()
async def on_message(message):
if 'Congratulations' in message.content:
await message.delete(message)
await bot.process_commands(message)
However whenever I type Congratulations the message wont get deleted
Your code provided in your question is a command, it would not be listening for an on_message event, as the command must be invoked by a user.
This is an event applied to your code, it would always wait for a message, only if the message contains "Congratulations", will trigger the message delete
#bot.event
async def on_message(message):
if 'Congratulations' in message.content:
await message.delete()
await bot.process_commands(message)
Do await message.delete() instead of await message.delete(message).

auto mute a member if they say a specific word discord py rewrite

import discord
import re
from itertools import cycle
class Status(commands.Cog):
def __init__(self, client):
self.client = client
#commands.Cog.listener()
async def on_message(self, ctx):
if ctx.author.id == self.client.user.id:
return
if re.search("\.\.\.", ctx.content):
await ctx.send("you're... gonna get... muted... if you.. talk... like.. this...")
user = ctx.message.author
# print(str(user))
# print(str(message.content))
muted_role = discord.utils.get(ctx.author.guild.roles, name="Muted")
await self.client.add_roles(ctx.author, muted_role)
What I want is to temporarily mute a user if they use ellipses in a message they send. ctx.send doesn't work, the bot does not send a message to the channel. It says that self.client.add_roles doesn't exist.
Muted is a role that I have created that does not have any send message permissions.
Any idea why? Some help would be hugely appreciated. I'm using
AttributeError: 'Message' object has no attribute 'send' this is the error I get
[EDIT]
#commands.Cog.listener()
# #commands.command()
async def on_message(self, message):
if message.author.id == self.client.user.id:
return
if re.search("\.\.\.", message.content):
await message.channel.send("you're... gonna get... muted... if you.. talk... like.. this...")
user = message.author
muted_role = discord.utils.get(message.guild.roles, name="Muted")
await user.add_roles(muted_role, reason="you know what you did", atomic=True)
I took a look at the documentation and did this, it works, thank you for the support :)
There were many mistakes, please have a look at the documentation.
I have correct them for you -
#commands.Cog.listener()
async def on_message(self, message):
if message.author.id == self.client.user.id:
return
else:
await message.channel.send("you're... gonna get... muted... if you.. talk... like.. this...")
user = message.author
print(str(user))
print(str(message.content))
muted_role = discord.utils.get(message.guild.roles, name="Muted")
await self.user.add_roles(muted_role)
Let me know if you do still get any errors.
Last edit-
I tried this command for myself outside a cog, and it works perfectly fine :)
#client.event
async def on_message(message):
if message.author.id == client.user.id:
return
elif "test" in message.content:
await message.channel.send("you're... gonna get... muted... if you.. talk... like.. this...")
user = message.author
print(str(user))
print(str(message.content))
muted_role = discord.utils.get(message.guild.roles, name="Muted")
await user.add_roles(muted_role)
else:
return
await client.process_commands(message)

Discord.py - how to delete message if a specific user is mentioned

I'm trying to make an on_message event where if an admin is mentioned it will delete that message and then tell them they can't do that. Here is my code right now
#bot.event
async def on_message(message):
admin_id = "<#496186362886619138>"
if admin_id in message.content:
await message.delete()
await message.channel.send("You can't do that")
await bot.process_commands(message)
Took another look at the documentation and found mentioned_in() and Client.fetch_user().
My final code looks like this:
#bot.event
async def on_message(message):
# DELETE ADMIN MENTION
user = await bot.fetch_user(496186362886619138)
if user.mentioned_in(message):
await message.delete()
await message.channel.send("You can't do that")
await bot.process_commands(message)

Discord.py How to do an action depending on the user reaction

How can I check for the user reaction? I'm using that code:
#client.command()
async def react(ctx):
message = await ctx.send("Test")
await question.add_reaction("<💯>")
await question.add_reaction("<👍>")
How can I do an action if the user react to the message with 💯 and another action if the user react to the message with 👍? Thank you in advance
In the documentation you can find client.wait_for() which waits for an event to happen. The example from the documentation should help you out:
#client.event
async def on_message(message):
if message.content.startswith('$thumb'):
channel = message.channel
await channel.send('Send me that 👍 reaction, mate')
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('👎')
else:
await channel.send('👍')

Resources