I use on_raw_message_delete and i want to print the name of the person who deleted the message.
I can print the deleted message and user who writed the message but I don't know how to print the name of the person who deleted the deleted message.
#commands.Cog.listener()
async def on_raw_message_delete(self, payload):
channel = self.bot.get_channel(channel_id)
embed = nextcord.Embed(title="message deleted",
description=f"Message deleted in <#{payload.cached_message.channel.id}>\n Message deleted by {payload.name}")
It is not part of the message delete payload, so you'll need to check the audit logs.
You also may want to check the date of the entry since a user deleting their own message is not logged to audit logs.
#commands.Cog.listener()
async def on_message_delete(self, message: nextcord.Message):
async for entry in message.guild.audit_logs(action=nextcord.AuditLogAction.message_delete, limit=1):
log = entry
# if deleted more than a second ago, use the author instead
deleter = log.user if (datetime.now() - log.created_at).seconds > 1 else message.author
Note that this may be unreliable if you have messages deleted frequently in your guild.
This is untested, but you can modify as needed.
Related
How do I make a discord.py bot not react to commands from the bot's DMs? I only want the bot to respond to messages if they are on a specific channel on a specific server.
If you wanted to only respond to messages on a specific channel and you know the name of the channel, you could do this:
channel = discord.utils.get(ctx.guild.channels, name="channel name")
channel_id = channel.id
Then you would check if the id matched the one channel you wanted it to be in. To get a channel or server's id, you need to enable discord developer mode. After than you could just right click on the server or channel and copy the id.
To get a server's id you need to add this piece of code as a command:
#client.command(pass_context=True)
async def getguild(ctx):
id = ctx.message.guild.id # the guild is the server
# do something with the id (print it out)
After you get the server id, you can delete the method.
And to check if a message is sent by a person or a bot, you could do this in the on_message method:
def on_message(self, message):
if (message.author.bot):
# is a bot
pass
You Can Use Simplest And Best Way
#bot.command()
async def check(ctx):
if not isinstance(ctx.channel, discord.channel.DMChannel):
Your Work...
So just to make the bot not respond to DMs, add this code after each command:
if message.guild:
# Message comes from a server.
else:
# Message comes from a DM.
This makes it better to separate DM from server messages. You just now have to move the "await message.channel.send" function.
I assume that you are asking for a bot that only listens to your commands. Well, in that case, you can create a check to see if the message is sent by you or not. It can be done using,
#client.event
async def on_message(message):
if message.author.id == <#your user id>:
await message.channel.send('message detected')
...#your code
#client.event
async def on_raw_reaction_add(payload):
#the embedded msg that was sent
global msg
message_id1 = payload.message_id
#checks if the message that was sent is the message the reaction was added to
if message_id1 == msg.id:
..code..
I also told the bot to add reactions (all the reactions from 1 to 9) to the message but when I said:
if payload.emoji.name == '1️⃣':
..code..
it did whatever the code said after that but I want it to ignore the bot's reaction and just take user reaction and later on I want it to only use 2 user's reactions (the ones who called the function).
Is there some kind of syntax that checks the id or username of the people who reacted so I can add it to the if statement at the start?
The payload is the type: RawReactionActionEvent, which has the attributes of member. You can do,
if payload.member.id == bot.user.id:
return
So I need my bot to print in which channel it sents a message. This is my code:
#client.command()
async def test(ctx):
await ctx.reply('hello', mention_author=True)
channel = #what goes here????
print('i said hello in', (channel))
I've tried all of these:
user = bot.get_user(user_id)
message.author.username
username = client.get_user(user_id)
The things you tried are referencing user id. I have not used discord.py in a while, but you will have to reference the channel id and not the user id. Maybe try:
ctx.message.channel.id
I can't remember if this returns an object or a string so you may have to format further to get the string.
#client.command()
async def clear(ctx, amount=2):
await ctx.channel.purge(limit=amount)
await ctx.send(f'Note:I have cleared the previous two messages\nDeleted messages:{(ctx)}')
I am trying to make the bot show which messages it has deleted, but I can't figure out how to do so.
For this use, channel.history. This is better because while channel.purge deletes at once, channel.history go to each and every message before deleting which means you can get the content.
#client.command()
async def clear(ctx, amount:int=2):
messagesSaved = [] # Used to save the messages that are being deleted to be shown later after deleting everything.
async for msg in ctx.channel.history(limit=amount, before=ctx.message): # Before makes it that it deletes everything before the command therfore not deleting the command itself.
await msg.delete() # Delets the messages
messagesSaved.append(msg.content) # Put the message in the list
await ctx.send(f'Note: I have cleared the previous {amount} messages.\nDeleted messages:\n'+'\n'.join(str(message) for message in messagesSaved))
Saving the message in the list instead of saying it after deleting is good so we can send it all at once instead of deleteing and sending the message right after deleting as this could cause many notifications especially if deleleting a lot of message.
I'm programming a bot for discord and trying to delete messages but only the one of the bots since I'm doing some commands that make him spam quite a lot.
So what I've found works well to bulk delete would be
#bot.command(pass_context = True)
async def purge(ctx,msglimit : int):
deleted = await bot.purge_from(ctx.message.channel, limit=msglimit)
await bot.say("Cleared **{}** Messages".format(len(deleted)))
but the documentation shows this command
def is_me(m):
return m.author == client.user
deleted = await client.purge_from(channel, limit=100, check=is_me)
await client.send_message(channel, 'Deleted {} message(s)'.format(len(deleted)))
But i can't really guet it to work if someone has an idea
def is_me(m):
return m.author == bot.user
#bot.command(pass_context = True)
async def purge(ctx):
deleted = await bot.purge_from(ctx.message.channel, check=is_me)
await bot.say("Cleared **{}** Messages".format(len(deleted)))
This deletes a certain number of messages (100 by default).
The purge_from method takes an argument called check which is a function that takes a message and returns whether the message should be deleted.
The is_me function will return true if the message author is the bot.
Which means that calling purge will delete the bot that picked up this command. If you need the bot to delete a different user's messages, you will need to change the condition.