-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
93 lines (75 loc) · 3.09 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
# Delete messages from certain users based on filters without a bot or banning them
# uninstall discord.py first or run this in a venv and only then install requirements.txt
import discord
from datetime import datetime
import json
import inotify.adapters
import threading
import os
import re
TOKEN = os.getenv("TOKEN")
if TOKEN is None:
print("You need to set the TOKEN env variable in order to run this selfbot")
exit(1)
allow_filters = json.load(open("allow.json", "r"))
deny_filters = json.load(open("deny.json", "r"))
# true = pass, false = filtered
def filter_message(message):
if message.guild is None:
return True # we don't care about messages in DMs
for allow in allow_filters:
if allow["guild"] == str(message.guild.id) or allow["guild"] == "*":
if allow["user"] == str(message.author.id) or allow["user"] == "*":
if (
allow["channel"] == str(message.channel.id)
or allow["channel"] == "*"
):
if "content" not in allow:
return True
if allow["content"] == "*":
return True
if re.match(allow["content"], message.content):
return True
for deny in deny_filters:
if deny["guild"] == str(
message.guild.id
): # guild cannot be "*" for safety reasons
if deny["user"] == str(message.author.id) or deny["user"] == "*":
if deny["channel"] == str(message.channel.id) or deny["channel"] == "*":
if "content" not in deny:
return False
if deny["content"] == "*":
return False
if re.match(deny["content"], message.content):
return False
return True
class MessageDeleterClient(discord.Client):
async def on_ready(self):
print("Logged in as", self.user)
async def on_message(self, message):
if not filter_message(message):
await message.delete()
print(
f"Deleted message from {message.author.name} in {message.guild.name}/{message.channel.name} at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
)
print(
f"Message was (truncated): {message.content[:100]}"
+ ("..." if len(message.content) > 100 else "")
)
def updater():
global allow_filters
global deny_filters
i = inotify.adapters.Inotify()
i.add_watch(".")
for event in i.event_gen(yield_nones=False):
(_, type_names, path, filename) = event # type: ignore
if "IN_CLOSE_WRITE" in type_names:
if filename == "allow.json":
allow_filters = json.load(open(filename, "r"))
print("Allow filters updated")
if filename == "deny.json":
deny_filters = json.load(open(filename, "r"))
print("Deny filters updated")
threading.Thread(target=updater).start()
client = MessageDeleterClient()
client.run(TOKEN)