-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
147 lines (134 loc) · 5.41 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import discord
import asyncio
import sys
import argparse
import re
# Set your defaults below, or specify with the respective arguments
prefix = str("#DEL")
token = str("")
heartbeat = int(86400)
serverpurge =str("#PS")
nooutput = bool(True)
MessagesToDEL = int()
counter = int(0)
def has_numbers(inputString):
return bool(re.search(r'\d', inputString))
class MyClient(discord.Client):
async def on_message(self, message):
if(message.author!=self.user):
return
channels=[]
containsnumbers = False
if(message.content==serverpurge):
channels=message.channel.guild.channels
elif(prefix in message.content): # check if the prefix is present in the message
tmpStrConv = message.content.replace(prefix, '')
tmpStrConv = tmpStrConv.strip()
containsnumbers = has_numbers(tmpStrConv)
if containsnumbers == True:
MessagesToDEL = int(str(tmpStrConv))
channels.append(message.channel)
else:
return
for channel in channels:
if nooutput==False:
print(channel)
if containsnumbers == True:
try:
async for mss in channel.history(limit=MessagesToDEL +1):
if(mss.author==self.user):
if nooutput==False:
print(mss.content)
try:
await mss.delete()
except:
if nooutput==False:
print("Can't delete!\n") # this shouldn't happen unless you call purge multiple time
except:
if args.output==False:
print("Can't read history!\n")
else:
try:
async for mss in channel.history(limit=None):
# fetch all messages, you might want to purge channel by channel to speed up if the server is old and big
if(mss.author==self.user):
if nooutput==False:
print(mss.content)
try:
await mss.delete()
except:
if nooutput==False:
print("Can't delete!\n") # this shouldn't happen unless you call purge multiple time
except:
if args.output==False:
print("Can't read history!\n")
# Create arguments
parser = argparse.ArgumentParser(description='')
parser.add_argument('-t','--token', dest='token', type=str, help='Token to use with message purger')
parser.add_argument('-p','--prefix', dest='prefix', type=str, help='Prefix to use with message purger')
parser.add_argument('-s','--serverpurge', dest='serverpurge', type=str, help='Specify a prefix to use for Server Purge')
parser.add_argument('-b','--heartbeat', dest='heartbeat', type=int, help='Heartbeat timeout to use')
parser.add_argument('-o', '--output', action='store_true', help='Enable console output of deleted messages (Good for debugging)')
args = parser.parse_args()
# Token
if args.token is not None:
token = args.token
# Prefix
if args.prefix is not None:
prefix = args.prefix
else:
prefix = "#DEL"
# Serverpurge prefix
if args.serverpurge is not None:
serverpurge = args.serverpurge
else:
serverpurge = "PS"
# Hearbeat
if args.heartbeat is not None:
heartbeat = args.heartbeat
else:
heartbeat = 86400
# output
if args.output == True:
nooutput = False
else:
token = input("Please input a Token: ")
# Prefix
if args.prefix is not None:
prefix = args.prefix
else:
prefix = input("Please input a prefix (leave blank for the default '#DEL'): ")
if prefix == "":
prefix = "#DEL"
# Serverpurge prefix
if args.serverpurge is not None:
serverpurge = args.serverpurge
else:
serverpurge = input("Please input a server purge prefix (leave blank for the default '#PS'): ")
if serverpurge == "":
serverpurge = "#PS"
# Heartbeat
if args.heartbeat is not None:
heartbeat = args.heartbeat
else:
heartbeat = input("Please input a heartbeat timeout (leave blank for the default 86400): ")
if heartbeat == "":
heartbeat = 86400
# output
if args.output == True:
nooutput = False
else:
nop = input("Do you want to log console output? (Y/N): ")
if nop == "" or nop == "n" or nop == "N" or nop == "No" or nop == "NO":
nooutput = True
elif nop == "y" or nop == "Y" or nop == "Yes" or nop == "YES":
nooutput = False
print("\nTo delete all messages in one channel, type: " + prefix + " in Discord, \nor delete a set amount of messages by adding a number after the prefix\n")
print("To delete all messages from the server type: " + serverpurge + " in Discord.")
if sys.platform == "Darwin" or sys.platform == "darwin":
print("\nTo stop the program, press " + u"\u2318" + " + C in the console.")
else:
print("\nTo stop the program, press CTRL + C in the console.")
# Run the self-bot and await prefix
client=MyClient(heartbeat_timeout=heartbeat, guild_subscriptions=False)
client.run(token)