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)
Related
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)
#client.event
async def on_message(message):
channel = client.get_channel(927625164038996019)
if message.channel == channel:
if message.author.id == 927033032882405517:
pass
else:
await message.add_reaction("✅")
await message.add_reaction("❌")
else:
pass
I would like to add:
if message.reactions == 2 await message.pin()
but it is not working, and every way I tried, it did not worked.
Don't use on_message it will be called once the message sent and it will not called for reacting the message with emoji. To tackle this issue you can use on_raw_reaction_add which will be called upon reacting the message with emoji.
Use the below code to solve this problem
#client.event
async def on_raw_reaction_add(payload):
channel=await client.fetch_channel(payload.channel_id)
message=await channel.fetch_message(payload.message_id)
user = await message.reactions[0].users().flatten()
if len(user)==2:
await message.pin()
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)
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.
#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.