How to create and exception for a bot - python

#bot.event
async def on_message(message):
if '!' in message.content:
return
if message.content.startswith(muti):
await asyncio.sleep(3)
await message.delete()
else:
await message.delete()
await message.channel.send(muti)
if message.author.bot:
return
I am trying to create an exception where my bot will not delete another bot's message, but I don't know how to do it. I tried to use if message.author.(the other bot's id variable) but then I don't know how to set that variable up. The message.author.bot is for my bot to ignore its own messages.

Using member.bot which returns if the member is a bot documentation.
#bot.event
async def on_message(message):
if message.author.bot:
return
if '!' in message.content:
return
if message.content.startswith(muti):
await asyncio.sleep(3)
await message.delete()
else:
await message.delete()
await message.channel.send(muti)
if message.author.bot:
return
This will not execute any of the code below if the message was sent by a bot.

Related

Remove a specific reaction from message

I'm making a simple game in discord.py/pycord and I would like my bot to be able to delete a specific reaction. Is there a way to do this?
I want the reaction to delete after I click.
Like this:
Here is my code (I'm using pycord):
import discord
from discord.ext import commands
intents = discord.Intents().all()
bot = commands.Bot(intents=intents)
#bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content == 'test':
me = await message.reply('Is this cool?')
await me.add_reaction("πŸ‘Ž")
await me.add_reaction("πŸ‘")
try:
reaction, user = await bot.wait_for("reaction_add", check=lambda reaction, user:
user == message.author and reaction.emoji in ["πŸ‘Ž", "πŸ‘"], timeout=30.0)
except asyncio.TimeoutError:
await message.reply("Tmieout bro")
else:
if reaction.emoji == "πŸ‘":
await message.reply('Like it.')
await reaction.delete()
else:
await message.reply("NO like")
await reaction.delete()
You first get the reaction object, then you remove it. Docs
Code:
#bot.slash_command(name='removereaction', description="I'm helping someone with their Stack post")
async def removereaction(ctx, message: discord.Option(discord.Message)):
print(message)
for i in message.reactions:
async for user in i.users():
await i.remove(user)
await ctx.respond(message.reactions)
How this works is it gets the message from the parameter message that has type discord.Option. The discord.Option is set so that you can use a message from a link or ID. Then, it uses this to cycle through all of the message's reactions. Then it cycles through each user that reacted with said reaction. It must be async, because i.users() is async (See here).
The full code that may help you:
import discord
from discord.ext import commands
intents = discord.Intents().all()
bot = commands.Bot(intents=intents)
#bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content == 'test':
me = await message.reply('Is this cool?')
await me.add_reaction("πŸ‘Ž")
await me.add_reaction("πŸ‘")
try:
reaction, user = await bot.wait_for("reaction_add", check=lambda reaction, user:
user == message.author and reaction.emoji in ["πŸ‘Ž", "πŸ‘"], timeout=30.0)
except asyncio.TimeoutError:
await message.reply("Tmieout bro")
else:
if reaction.emoji == "πŸ‘":
await message.reply('Like it.')
async for user in reaction.users():
await reaction.remove(user)
else:
await message.reply("NO like")
async for user in reaction.users():
await reaction.remove(user)
If you want to remove JUST the bot's reactions change the following line:
async for user in reaction.users():
await reaction.remove(user)
To:
await reaction.remove(bot.user)

Commands don't work. They used to work but they don't any more

I added the bot status and after that the
Commands don't work. Added a answerers answer but it still no work ( help works not but not hello ;-;)
import discord
from KeepAlive import keep_alive
client=discord.Client()
#client.event
async def on_ready():
await client.change_presence(status=discord.Status.online,activity=discord.Game('Hey There! Do €help to start!'))
print('We have logged in as {0.user}'.format(discord.Client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
if message.content.startswith('$help'):
await message.channel.send('no help here!')
await bot.process_commands(message)
keep_alive()
client.run('wont say token :)')
If you are talking about commands and not "commands" that you run with on_message then you have to add await client.process_commands(message) (check this issue in documentation). If your on_message event is not working then it's probably only because of missing _ in on_message event.
#client.event
async def on_message(message): # your forgot "_"
if message.author == client.user: # discord.Client won't work. Use client.user instead
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
if message.content.startswith('$help'):
await message.channel.send('no help here!')
await client.process_commands(message) # add this line at the end of your on_message event
the problem is your message function, it has async def onmessage(message): but the correct one is:
#client.event
async def on_message(message):
And I recommend defining the prefix and then separating it from the message so you don't have to keep typing $ in every if, and save the elements written after the command for future functions:
PREFIX = "$"
#client.event
async def on_message(message):
msg = message.content[1:].split(' ')
command = msg[0]
if command == "hello":
await message.channel.send('Hello!')

Mod mail command

Someone know how to make the bot close the modmail? Like the member start spamming on the bot dms and you want to like block him
here is my modmail code
#client.event
async def on_message(message):
channel = client.get_channel(909853337510371449)
if message.author.id == client.user.id:
return
if message.author != message.author.bot:
if not message.guild:
await channel.send(f"[{message.author.mention}] {message.content}")
await client.process_commands(message)

How to detect a message from a specific user and delete it

I'm trying to detect a message from a specific user (me), delete it and send the same message again.
#client.event
async def on_message(message):
if message.author.id == 881583268569436200:
await message.delete(message)
await client.process_commands(message)
I don't know what I'm doing wrong. Please help
You don't have to use await message.delete(message) you already got the message after await statement. Just use await message.delete().
#client.event
async def on_message(message):
if message.author.id == 881583268569436200:
msg = message.content
await message.delete()
await message.channel.send(msg)
await client.process_commands(message)

Why I can't run my bot command (discord.py)?

I have some problem when I try to create a bot
Here is a part of my code:
#client.event
async def on_message(message):
if message.author == client.user:
return
await client.process_commands(message)
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
general_channel = client.get_channel(874180398475804695)
await general_channel.send('I am ready',delete_after=10)
#client.event
async def on_message(message):
if any(word in message.content for word in rude_word):
await message.reply('Hey man, that word is not allowed here .',delete_after=2)
await message.delete()
#client.command(name='ping')
async def ping(message):
await message.reply('pong')
When I type command, the bot does not work.
BUT, if I remove the part of:
#client.event
async def on_message(message):
if any(word in message.content for word in rude_word):
await message.reply('Hey man, that word is not allowed here .',delete_after=2)
await message.delete()
The command works again.
Another situation, I remove :
#client.command(name='ping')
async def ping(message):
await message.reply('pong')
The bot still can works(delete message). However, when I combine these two, the #client.command doesn't works. I have try my best to figure it out but still have no idea. What can I do to let the commands work?
The issue is with on_message. You need to add await client.process_commands(message) at the end of on_message as mentioned here in the discord.py FAQ.

Categories