-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
136 lines (111 loc) · 5.3 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
# bot.py
import json
import os
import re
from collections import deque
from datetime import timedelta
import aiohttp
import asyncio
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
SEND_ERRORS_TO = int(os.getenv('SEND_ERRORS_TO'))
LOG_CHANNEL = int(os.getenv('LOG_CHANNEL'))
intents = discord.Intents.default()
intents.members = True
intents.messages = True
intents.message_content = True
client = discord.Client(intents=intents)
new_members = deque()
harmful_tlds = set()
harmful_phrases = set()
json_scammer_lists = ["https://raw.githubusercontent.com/Discord-AntiScam/scam-links/main/list.json"]
plain_scammer_lists = ["https://raw.githubusercontent.com/BuildBot42/discord-scam-links/main/list.txt",
"https://raw.githubusercontent.com/Qwasyx/DiscordScamDetectionList/main/known-scammer-domains.txt"]
plain_keyword_lists = ["https://raw.githubusercontent.com/Qwasyx/DiscordScamDetectionList/main/known-scammer-phrases.txt"]
async def send_error(msg):
user = client.get_user(SEND_ERRORS_TO)
await user.create_dm()
await user.dm_channel.send("Error: " + msg)
async def update_harmful_tlds():
global harmful_tlds, harmful_phrases
while True:
print("Updating harmful tlds...")
new_harmful_tlds = set()
new_harmful_phrases = set()
async with aiohttp.ClientSession() as session:
for url in json_scammer_lists:
async with session.get(url) as response:
if response.status != 200:
await send_error("Can't connect to {} anymore, error code {}".format(url, response.status))
else:
new_harmful_tlds.update(json.loads(await response.text()))
for url in plain_scammer_lists:
async with session.get(url) as response:
if response.status != 200:
await send_error("Can't connect to {} anymore, error code {}".format(url, response.status))
else:
text = await response.text()
for line in text.splitlines():
new_harmful_tlds.add('http://{}/'.format(line))
new_harmful_tlds.add('https://{}/'.format(line))
for url in plain_keyword_lists:
async with session.get(url) as response:
if response.status != 200:
await send_error(
"Can't connect to {} anymore, error code {}".format(url, response.status))
else:
text = await response.text()
new_harmful_phrases.update(map(str.lower, text.splitlines()))
harmful_tlds = new_harmful_tlds
harmful_phrases = new_harmful_phrases
print("Done updating harmful tlds...")
await asyncio.sleep(60 * 60 * 2) # wait for 2 hours
def is_guaranteed_harmful_message(message):
msg = message.content
link_regex = re.compile(r"((https?):((//)|(\\\\))+[\w\d:#@%;$()~_?+-.0-=\\.&]*/)", re.MULTILINE | re.UNICODE)
urls = link_regex.findall(msg)
for url in urls:
if url[0] in harmful_tlds or (url[0] + '/') in harmful_tlds:
return True
return False
def is_probably_harmful_message(message):
msg = message.content
link_regex = re.compile(r"((https?):((//)|(\\\\))+[\w\d:#@%;$()~_?+-.0-=\\.&]*/)", re.MULTILINE | re.UNICODE)
urls = link_regex.findall(msg)
if len(urls) > 0:
lower_content = message.content.lower()
for phrase in harmful_phrases:
if phrase in lower_content:
return True
return False
@client.event
async def on_ready():
print(f'{client.user.name} has connected to Discord!')
client.loop.create_task(update_harmful_tlds())
@client.event
async def on_message(message):
if message.type != discord.MessageType.default:
return
if message.author == client.user.id:
return
if is_guaranteed_harmful_message(message):
log_channel = client.get_channel(LOG_CHANNEL)
embed = discord.Embed(title="Banned scammer", color=discord.Color.red())
embed.add_field(name="Message", value=message.clean_content)
embed.set_author(name=str(message.author), icon_url=message.author.display_avatar)
embed.set_footer(text=str(message.author.id))
await log_channel.send(embed=embed)
await message.author.ban(delete_message_days=1, reason="Scammer (auto-detected by bot)")
elif is_probably_harmful_message(message):
log_channel = client.get_channel(LOG_CHANNEL)
embed = discord.Embed(title="Deleted possible scam message and added 1hr timeout (consider banning them)", color=discord.Color.orange())
embed.add_field(name="Message", value=message.clean_content)
embed.set_author(name=str(message.author), icon_url=message.author.display_avatar)
embed.set_footer(text=str(message.author.id))
await log_channel.send(embed=embed)
await message.author.timeout(timedelta(hours=24), reason="Scammer (auto-detected by bot)")
await message.delete()
if __name__ == '__main__':
client.run(TOKEN)