Python Discord Bot

posted on March 12, 2023, 6:24 a.m.


This project will always be a work in progress. I'll update it as my needs for its functionality change.
For now it can delete messages in a server that you have control over.
Most of the code was taken directly from docs at: https://discordpy.readthedocs.io/en/latest/api.html

Some docs on rate limits:
https://discord.com/developers/docs/topics/rate-limits

You need to first create a discord developer application at https://discord.com/developers/applications


  • add a bot and copy token / paste into your program

  • in the OAuth2 tab select bot under Scopes and then admin permissions under Bot Permissions

  • copy and paste the link in a new tab and add the bot to your desired server

  • run the program and you will see the bot is online in the server

  • type this to delete 5 messages:

  • .purge 5

  • It is indiscriminate and will delete every post


Changelog:

2
Improved existing functions, added function to purge member's messages older than fourteen days -- doesn't use bulk delete so the rate limit is not an issue, and added more descriptive comments.

1
Added a function to delete a specific users posts. It always starts from the newest post onward.
Also added an 'age" parameter to the chat purge function to choose between starting at the oldest or the newest.

import discord
from discord.ext.commands import Bot
from discord.ext import commands

client = commands.Bot(command_prefix= '.')

#purging all members messages
#p stands for purge
@client.command()
async def p(ctx, number, age):
if str(age)=='old':
deleted = await ctx.channel.purge(limit=int(number), oldest_first=True)
await ctx.channel.send('Deleted {} message(s)'.format(len(deleted)))
print('Deleted {} message(s)'.format(len(deleted)))
elif str(age)=='new':
deleted = await ctx.channel.purge(limit=int(number), oldest_first=False)
await ctx.channel.send('Deleted {} message(s)'.format(len(deleted)))
print('Deleted {} message(s)'.format(len(deleted)))
else:
await ctx.channel.send('Must choose old or new messages')

#purging a specific member's messages starting from newest
#mp stands for member purge
#there is a 100 message bulk delete limit imposed by discord so this stops at 99
#once it finishes, you can simply rerun the command and do it again
#channel.delete_messges only works for messages up to 14 days old
@client.command()
async def mp(self, member: discord.Member=None):
await self.message.delete()
msg = []

async for m in self.channel.history(limit=None):
if len(msg)==99:
break
else:
if m.author == member:
msg.append(m)


await self.channel.delete_messages(msg)
print('Deleted {} message(s)'.format(len(msg)))

#mpo stands for member purge old
#this loops through all messages one by one and deletes them
#this can be used when needing to delete messages older than 14 days old
#takes a lot longer
@client.command()
async def mpo(self, member: discord.Member=None):
#this simply deletes your command
await self.message.delete()
counter=0
async for m in self.channel.history(limit=None):

if m.author == member:
counter+=1
await m.delete()
print(counter)



#make the bot talk with .send "the message"
@client.command()
async def send(self, msg):
await self.message.delete()
await self.channel.send(msg)

client.run('paste bot token here')


File
SHA256
Download