if client.user.mentioned_in(message):
await ctx.send("Hi!")
So I want my bot to respond to anyone who #mentions it. It does work as it should, but the only problem it'll respond to #everyone and #here pings. It's probably simple and I am overthinking it but I just want to know if there is any way to make the bot respond to messages it's actually mentioned in, not #everyone or #here pings?
Check whether the bot's user id is in the message content. Try the following:
if str(client.user.id) in message.content:
await ctx.send("Hi!")
Hmm Try This
#client.event
async def on_message(message):
if f"<#!{client.user.id}>" in message.content:
await ctx.send("Hi!")
You can use the
#client.event
async def on_message(message):
Then detect if the message has mentioned the bot:
if client.user.mentioned_in(message):
await message.channel.send("Hi!")
Documentation: https://docs.pycord.dev/en/master/api.html?highlight=mentioned#discord.ClientUser.mentioned_in
Related
Image Link I'm trying to make something where if the bot is pinged it says a message like "For a list of commands, type .help" and my code works except for it also says that when the bot is replied to.
This is my code - Using discord.py 1.7.3
#client.event
async def on_message(message):
if client.user.mentioned_in(message) and message.mention_everyone is False:
await message.channel.send("For a list of commands, type `.help`")
await client.process_commands(message)
You're looking for client.user.mention. You can also check if client.user.mention in message.content if you want to know whether the bot is mentioned somewhere in the message rather than checking if the mention comprises the entire message.
#client.event
async def on_message(message):
if client.user.mention == message.content and message.mention_everyone is False:
await message.channel.send("For a list of commands, type `.help`")
await client.process_commands(message)
Reference:
abstractmethod mention
I made this small discord bot to show a friend how to do it, exactly how I have done it before. But it doesnt answer my message in discord, and I cant find the error.
import discord
import os
client = discord.Client()
#client.event
async def on_ready():
print('Online as {0.user}'.format(client))
#client.event
async def in_message(message):
if message.author == client.user:
return
if message.content.startswith('Hello'):
await message.channel.send('Hello! {message.author.mention}')
client.run(os.getenv('TOKEN'))
Sorry if its obvious, I just cant see it.
Use on_message instead of in_message.
Format string 'Hello! {message.author.mention}' like f'Hello! {message.author.mention}'.
instead of using on_message events u can use
#client.command()
async def hello(ctx):
await ctx.send(f"Hello {ctx.author.mention}")
This is how u create actual Commands in discord.py
In my bot, I have an event for replying to when the bot is mentioned. The issue is, that it replies when u mention the bot through replying to it. How can I fix this? Here's my code:
#commands.Cog.listener()
async def on_message(self, msg):
guild = msg.guild
prefix = get_prefix(self.client, guild)
if msg.author == self.client.user:
return
if msg.mention_everyone:
return
if self.client.user.mentioned_in(msg):
embed = discord.Embed(description=f"The prefix for this server is `{prefix}` <:info:857962218150953000>", color=0x505050)
await msg.channel.send(embed=embed)
Change
if self.client.user.mentioned_in(msg):
to
if msg.guild.me.mention in msg.content:
Why this happens
discord messages use a special field in it's messages to specify if someone is mentioned or not, if a message has someone in that field then the message will ping that someone. mentioned_in works that way so you can figure out if a certain message pings a certain person. when we use the msg.guild.mention part, we manually check for the mention instead of discord telling us if someone was mentioned since discord counts replies as mentions
I've used the following code for similar applications.
Note - Replace 'client' in client.user.id with what you've used like bot.user.id
#client.event
async def on_message(message):
if message.content in [f'<#{client.user.id}', f'<#!{client.user.id}>']:
await message.channel.send("Hey! My prefix is ```,```")
await client.process_commands(message)
I made a bot for my discord, but I named it after myself. So sometimes the bot accidentally gets mentioned instead of me and I don't receive the notification. I wrote some code that if the bot is ever mentioned it will reply with my id so I get the mention.
My problem is: The bot also responds to #everyone, how can I prevent this?
#client.event
async def on_message(message):
if message.author == client.user:
return
if client.user.mentioned_in(message):
await message.channel.send(f'{message.author.mention} You probably meant that message for <#my_id>')
await client.process_commands(message)
You can make a prior if-statement to check if the message mentions everyone, and if so, ignore it.
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.mention_everyone:
return
if client.user.mentioned_in(message):
await message.channel.send(f'{message.author.mention} You probably meant that message for <#my_id>')
await client.process_commands(message)
TIA for your help and apologies, I'm a newbie so this may be a foolish question. I've searched through and can't find anything specific on how to make a discord bot (in Python) delete messages only within a specific channel. I want all messages sent to a specific channel to be deleted, their contents sent via PM to the user and the user's role changed.
Is there a way to use on_message and specify in an specific channel?
#client.event
async def on_message(message):
user = message.author
if message.content.startswith("Cluebot:"):
await message.delete()
await user.send("Yes?")
await user.remove_roles(get(user.guild.roles, "Investigator"))
The problem I'm having is I'm also using commands which now no longer work because the bot only responds if the message begins with "Cluebot:" Can I have the bot only look for "Cluebot:" in a specific channel?
Is it possible to make this work through a command instead of an event?
Thanks for your help. :)
The problem I'm having is I'm also using commands which now no longer work because the bot only responds if the message begins with "Cluebot:" Can I have the bot only look for "Cluebot:" in a specific channel?
About this problem the clue is:
await bot.process_commands(message)
as docs says Without this coroutine, none of the commands will be triggered.
about the main question you could try using get_channel by ID and then purge it:
eg.
#client.event
async def on_message(message):
purgeChannel = client.get_channel([CHANNEL ID]])
await purgeChannel.purge(limit=1)
you can add check to purge() to check for deleting specific message:
eg.:
def check(message):
return message.author.id == [author's ID]
#client.event
async def on_message(message):
purgeChannel = client.get_channel([CHANNEL ID]])
await purgeChannel.purge(limit=1, check=check)