i want to create a discord bot using discord.py that when a user leave the bot will delete all the messages that user sent in all channels. I know to use the event handlers, but I don t have any idea on how to delete all messages from all channels of one user, I could just put the message id's in a dictionary, but maybe there is a better way.
And as reference is this bot: https://top.gg/bot/689790568154529792
async def on_member_remove(member):
for c in member.guild.channels:
await c.purge(limit=None, check=lambda m: m.author==member)
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
Hopefully this isn't a duplicate post but I've searched around and can't find anything on how to do this specifically.
I'm trying to create a Discord bot which can delete messages sent by a specific author, in a specific channel, but also checks previously sent messages.
At the moment I have the below, which adds new messages from chosen user to a list, checks if they're duplicates and if they are, deletes them.
I want to know:
A: How can I make this bot Channel specific
B: Can I have this program check old messages in that channel too and also delete them if they're duplicates?
Thanks in advance for any help and if any further info required, please let me know.
import discord
TOKEN = ('MY_TOKEN_LIVES_HERE')
client = discord.Client()
messagesSeen = []
#client.event
async def on_message(message):
if "News_Bot" in str(message.author):
if message.content in messagesSeen:
await message.delete()
else:
messagesSeen.append(message.content)
client.run(TOKEN)
Yes you can do both of those things. Since you are using on_message event you can just use message.channel to also check that channel for previously sent messages. I would also advise to change news_bot to the ID of the bot. The thing is one you implement this and clear the channel history for those messages, it won't be necessary anymore as the bot will catch the messages as soon as they are sent. So the message history is kind of a one time thing, which you can just create a cmd and run it in that channel.
messagesSeen = []
#client.event
async def on_message(message):
if 1234345 is message.author.id:
if message.content in messagesSeen:
await message.delete()
async for message in message.channel.history(limit=100):
if message.content in messagesSeen:
await message.delete()
else:
messagesSeen.append(message.content)
I was making a bot for my friend using discord.py
and I wanted to make it so that it would only work in channel which include the word ticket, made by another bot named Ticket Toll
How can I do so?
Relevant docs on text channels
Unfortunately, Discord's API does not keep track of who created the channel (which is why there's no such thing as channel.author).
One solution would be to have Ticket Toll create channels in a category, and only give your bot permissions to view this category.
However, you can easily have the bot ignore messages if the channel doesn't have "ticket" in the name, by checking channel.name. Here's an example with the on_message event:
#client.event
async def on_message(message):
if 'ticket' not in message.channel.name: return
# stuff to execute if message was sent in a channel with ticket in its name
Or as a command:
#client.command()
async def something(ctx, arg):
if "ticket" not in ctx.message.channel.name: return
# stuff to execute if the command was sent in a channel with ticket in its name
Only give the bot access to read channels where you want it to work.
In Discord.py how can I make my bot privately send messages to other people in a server. Like, if I want to message a person in the server who I have not friend requested yet, I can use this bot. I want it to use a slash command like
"/{user}{message}"
And the bot will display the message for them and make the user who it was sent to only see it.
How do I do this using discord.py?
You have to get the member and create a DM with them, then send whatever you want to the DM. See the following code:
member = discord.utils.get(ctx.guild.members, id = (member id)) # could also use client.get_user(ID)
dm = await member.create_dm()
await dm.send('this will be sent to the user!')
I currently have the following on_guild_join code:
#client.event
async def on_guild_join(guild):
embed = discord.Embed(title='Eric Bot', color=0xaa0000)
embed.add_field(name="What's up everyone? I am **Eric Bot**.", value='\nTry typing `/help` to get started.', inline=False)
embed.set_footer(text='Thanks for adding Eric Bot to your server!')
await guild.system_channel.send(embed=embed)
print(f'{c.bgreen}>>> {c.bdarkred}[GUILD JOINED] {c.black}ID: {guild.id} Name: {guild.name}{c.bgreen} <<<\n{c.darkwhite}Total Guilds: {len(client.guilds)}{c.end}')
(Ignore the c.color stuff, it's my formatting on the console)
It sends an embed with a bit of information to the system channel whenever someone adds the bot to a guild.
I want it to send a DM to whoever invited the bot (the account that used the oauth authorize link) the same message. The problem is that the on_guild_join event only takes 1 argument, guild, which does not give you any information about the person who used the authorize link to add the bot to the guild.
Is there a way to do this? Do I have to use a "cheat" method like having a custom website that logs the account that uses the invite?
Since bots aren't "invited", there's instead an audit log event for when a bot is added. This lets you iterate through the logs matching specific criteria.
If your bot has access to the audit logs, you can search for a bot_add event:
#client.event
async def on_guild_join(guild):
bot_entry = await guild.audit_logs(action=discord.AuditLogAction.bot_add).flatten()
await bot_entry[0].user.send("Hello! Thanks for inviting me!")
And if you wish to double-check the bot's ID against your own:
#client.event
async def on_guild_join(guild):
def check(event):
return event.target.id == client.user.id
bot_entry = await guild.audit_logs(action=discord.AuditLogAction.bot_add).find(check)
await bot_entry.user.send("Hello! Thanks for inviting me!")
References:
Guild.audit_logs()
AuditLogAction.bot_add
AsyncIterator.find()
From this post
With discord.py 2.0 you can get the BotIntegration of a server and with that the user who invited the bot.
Example
from discord.ext import commands
bot = commands.Bot()
#bot.event
async def on_guild_join(guild):
# get all server integrations
integrations = await guild.integrations()
for integration in integrations:
if isinstance(integration, discord.BotIntegration):
if integration.application.user.name == bot.user.name:
bot_inviter = integration.user# returns a discord.User object
# send message to the inviter to say thank you
await bot_inviter.send("Thank you for inviting my bot!!")
break
Note: guild.integrations() requires the Manage Server (manage_guild) permission.
References:
Guild.integrations
discord.BotIntegration