-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
407 lines (331 loc) · 15.6 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
from datetime import datetime, timedelta
from bs4 import BeautifulSoup
import requests
import asyncio
import discord
import random
import yaml
import json
import time
import math
import os
import re
from typing import Optional, Tuple
from functools import lru_cache
@lru_cache(maxsize=512)
def is_locked_pastebin(url):
resp = requests.get(url)
if resp.status_code != 200:
return False
soup = BeautifulSoup(resp.text, "html.parser")
title_elem = soup.find("title")
title = title_elem.text.strip() if title_elem else ""
return title == "Pastebin.com - Locked Paste"
class NPSTBot(discord.Client):
api_endpoints = {
"scoreboard": "https://wiarnvnpsjysgqbwigrn.supabase.co/rest/v1/scoreboard?select=*",
"snabel-a": "https://wiarnvnpsjysgqbwigrn.supabase.co/rest/v1/messages?select="
"id%2Crelease_at%2Csender%2Crecipient%2Ccc%2Ctopic%2Ccontent%2Crelease_after_solve%28flag%29"
}
def __init__(self, *args, config_file="config.yaml", **kwargs):
if "intents" not in kwargs:
kwargs["intents"] = discord.Intents.all()
with open(config_file, "r", encoding="utf-8") as fr:
self.config = yaml.load(fr, Loader=yaml.FullLoader)
self.scoreboard_cache = None
self.mail_channel: Optional[discord.TextChannel] = None
self.mod_channel: Optional[discord.TextChannel] = None
self.mail_acknowledged = []
super().__init__(*args, **kwargs)
async def on_ready(self):
print(f"Bot started as {self.user}")
if not self.config.get("mail-channel"):
return
if not self.config.get("login") or not (self.config["login"].get("email") or self.config["login"].get("password")):
return
if os.path.exists("mail-acknowledge.json"):
with open("mail-acknowledge.json", "r", encoding="utf-8") as fr:
self.mail_acknowledged = json.load(fr)
if not self.mail_acknowledged:
return
else:
self.mail_acknowledged = []
self.mail_channel = await self.fetch_channel(self.config["mail-channel"])\
if self.config.get("mail-channel") else None
self.mod_channel = await self.fetch_channel(self.config["moderator-channel"])\
if self.config.get("moderator-channel") else None
if not self.mail_channel:
return
while True:
print("Checking mail")
session = self.get_login_session()
resp = requests.get(self.api_endpoints["snabel-a"], headers={
"apikey": self.config["api_key"],
"authorization": f"Bearer {session['access_token']}"
})
if resp.status_code != 200:
print(f"Failed to check mail, error code:", resp.status_code)
else:
for mail in resp.json():
if mail["id"] in self.mail_acknowledged:
continue
self.mail_acknowledged.append(mail["id"])
attachments = []
for match in re.finditer(r"\[(.*)]\((https?://.*)\)", mail["content"]):
fname, url = match.groups()
attachments.append(url)
date = datetime.strptime(
mail["release_at"], "%Y-%m-%dT%H:%M:%S+00:00") + timedelta(hours=2)
title = (f"Fra: {mail['sender']}\n" +
f"Sendt: {datetime.strftime(date, '%d %b %H:%M:%S')}\n" +
f"Til: Alle\n" +
(f"CC: {mail['cc']}\n" if mail['cc'] else '') +
f"Emne: {mail['topic']}")
description = mail["content"].replace(
"{{brukernavn}}", "hjelpere")
mail_msg = await self.mail_channel.send(
embed=discord.Embed(
title=title,
description=description,
color=0xff0000
)
)
# Publish the message to other servers if it's in a news channel
if mail_msg.channel.type == discord.ChannelType.news:
await mail_msg.publish()
with open("mail-acknowledge.json", "w", encoding="utf-8") as fw:
json.dump(self.mail_acknowledged, fw)
time_now = datetime.now()
next_challenge = datetime(
year=time_now.year, month=time_now.month, day=time_now.day, hour=18)
seconds_from_new_challenge = (time_now - next_challenge).seconds
if abs(seconds_from_new_challenge) > 5:
await asyncio.sleep(min(self.config.get("mail-check-delay", 60), seconds_from_new_challenge - 5))
else:
await asyncio.sleep(1)
async def audit_message(self, msg: discord.Message):
if not self.config.get("moderate", False):
return
print("Auditing:", msg)
if isinstance(msg.channel, discord.DMChannel):
return
if msg.author.id == self.user.id:
return
if msg.channel.name == "cryptobins" or msg.channel.name == "løsninger":
passed_checks = True
all_urls = re.findall(
r"(https?://[^\s]+)", msg.content)
if len(all_urls) == 0:
passed_checks = False
for url in all_urls:
if url.startswith("https://cryptobin.co"):
continue
elif url.startswith("https://pastebin.com"):
if not is_locked_pastebin(url):
passed_checks = False
break
continue
else:
passed_checks = False
break
print(f"{passed_checks=}")
if not passed_checks:
await msg.delete()
temp_msg = await msg.channel.send("Vennligst kun send cryptobins eller låste pastebins i denne kanalen!")
await asyncio.sleep(5)
await temp_msg.delete()
# Match for CTF flags
if re.match(r"((N?PST)|(EGG)|(NSM)|(KRIPOS)){[^}]{6,}}", msg.content, re.IGNORECASE):
await msg.delete()
temp_msg = await msg.channel.send("Vennligst ikke send flagg!")
await asyncio.sleep(5)
await temp_msg.delete()
if self.mod_channel:
await self.mod_channel.send(
f"Flagg sendt i {msg.channel.mention} av {msg.author.mention}:"
f"```{msg.author}: {discord.utils.escape_mentions(msg.content)}```")
async def on_message_edit(self, _before, after):
await self.audit_message(after)
await self.handle_potential_command(after)
async def on_message(self, msg: discord.Message):
await self.audit_message(msg)
await self.handle_potential_command(msg)
async def handle_potential_command(self, msg: discord.Message):
if not msg.content.startswith(self.config.get("prefix", "!")):
return
command_name, * \
command_args = msg.content[len(self.config.get("prefix", "!")):].split(" ")
command_name = command_name.lower()
try:
await self.handle_command(msg, command_name, command_args)
except Exception as _:
await msg.reply("`Noe gikk galt 🤖`")
raise
async def handle_command(self, msg: discord.Message, command_name: str, command_args: list):
if command_name == "ping":
await self.command_ping(msg, command_args)
elif command_name == "score":
await self.command_score(msg, command_args)
elif command_name == "purgemail":
await self.command_purgemail(msg, command_args)
elif command_name == "topp":
await self.command_topp(msg, command_args)
elif command_name == "hjelp" or command_name == "help":
prefix = self.config.get("prefix", "!")
await msg.reply(
"```" +
"Kommandoer:\n" +
f"{prefix}topp - sjekk hvor mange som har toppscore og hvor mange som er på scoreboardet\n" +
f"{prefix}score - se scoreboard\n" +
f"{prefix}score #[plass] - se scoreboard fra en plassering\n" +
f"{prefix}score [person] - se score og plassering til en person\n" +
f"{prefix}flagg - få dagens flagg\n" +
f"{prefix}regler - få reglene\n" +
f"{prefix}ping - sjekk om boten er oppe\n" +
"```"
)
elif command_name == "regler" or command_name == "rules":
await msg.reply("<#652630206804131902>")
elif command_name == "flagg":
await msg.reply("PST{finn_det_selv}")
elif command_name == "egg":
await msg.reply(random.choice([
"https://tenor.com/view/you-win-an-egg-ostrich-egg-egg-giant-egg-massive-egg-gif-15032525",
"https://tenor.com/view/egg-frank-can-i-offer-you-a-nice-egg-in-this-trying-time-gif-13802522"
]))
@staticmethod
def unpack_score(score, n_solves) -> Tuple[int, int]:
flags = (score - n_solves) / 9
eggs = n_solves - flags
assert math.floor(flags) == flags
return int(flags), int(eggs)
@staticmethod
def format_score(person, flags=True, eggs=True):
if person["flags"] is None:
return f"{person['score']} poeng"
return (
(f"🚩 {person['flags']} " if person['flags'] and flags else "") +
(f"⭐ {person['eggs']}" if person['eggs'] and eggs else "")
)
@staticmethod
def format_user(person):
placement = person["index"] + 1
return f"#{placement}{' ' if placement != 10 else ''} {'👑 ' if placement == 1 else ''}" +\
f"**{NPSTBot.clean_username(person['username'])}**: {NPSTBot.format_score(person)}"
@staticmethod
def clean_username(username):
return discord.utils.escape_markdown(username.replace(":crown:", "💩").replace("👑", "💩"))
@staticmethod
async def command_ping(msg: discord.Message, _):
await msg.reply("Pong!")
async def command_purgemail(self, msg: discord.Message, _):
if not msg.author.guild_permissions.administrator:
await msg.add_reaction("❌")
return
if self.mail_channel is None:
await msg.reply("Please wait")
return
print("Purging mail channel")
await self.mail_channel.purge(limit=100)
self.mail_acknowledged = []
with open("mail-acknowledge.json", "w", encoding="utf-8") as fw:
json.dump(self.mail_acknowledged, fw)
async def command_topp(self, msg: discord.Message, _):
scoreboard = self.get_scoreboard()
best_score_person = max(
[person for person in scoreboard], key=lambda person: person["score"] or 0)
most_flags_person = max(
[person for person in scoreboard], key=lambda person: person["flags"] or 0)
most_eggs_person = max([person for person in scoreboard],
key=lambda person: person["eggs"] or 0)
n_best_score = len(
[person for person in scoreboard if person["score"] == best_score_person["score"]])
n_most_flags = len(
[person for person in scoreboard if person["flags"] == most_flags_person["flags"]])
n_most_eggs = len(
[person for person in scoreboard if person["eggs"] == most_eggs_person["eggs"]])
pct_best_score = "{:10.2f}".format(
100 * n_best_score / len(scoreboard))
pct_most_flags = "{:10.2f}".format(
100 * n_most_flags / len(scoreboard))
pct_most_eggs = "{:10.2f}".format(100 * n_most_eggs / len(scoreboard))
await msg.reply(embed=discord.Embed(
description=(
"\n".join(set(
(
f"**{n_best_score} av {len(scoreboard)}** ({pct_best_score}%) på scoreboardet har " +
self.format_score(best_score_person) + "\n" +
f"**{n_most_flags} av {len(scoreboard)}** ({pct_most_flags}%) på scoreboardet har " +
self.format_score(most_flags_person, eggs=False) + "\n" +
f"**{n_most_eggs} av {len(scoreboard)}** ({pct_most_eggs}%) på scoreboardet har " +
self.format_score(most_eggs_person, flags=False)).split("\n")
)
)
)
))
def get_scoreboard(self):
if self.scoreboard_cache is None or time.time() - self.scoreboard_cache["time"] > 5:
resp = requests.get(self.api_endpoints["scoreboard"], headers={
"apikey": self.config["api_key"]})
assert resp.status_code == 200
scoreboard = resp.json()
for i, person in enumerate(scoreboard):
person["index"] = i
try:
person["flags"], person["eggs"] = self.unpack_score(
person["score"], person["num_solves"])
except AssertionError:
person["flags"], person["eggs"] = None, None
self.scoreboard_cache = {
"scoreboard": scoreboard, "time": time.time()}
else:
scoreboard = self.scoreboard_cache["scoreboard"]
return scoreboard
async def command_score(self, msg: discord.Message, args):
scoreboard = self.get_scoreboard()
if len(args) >= 1:
if args[0].startswith("#") or args[0].startswith("*"):
try:
scoreboard = scoreboard[max(int(args[0][1:]) - 1, 0):]
except ValueError:
pass
else:
scoreboard = [person for person in scoreboard if person["username"].lower(
).startswith(args[0].lower())]
if len(scoreboard) == 0:
await msg.reply("Ingen brukere funnet")
else:
await msg.reply(embed=discord.Embed(description="\n".join([
self.format_user(person) for i, person in enumerate(scoreboard[:10])
])))
def get_login_session(self):
session = None
if os.path.exists("session.json"):
with open("session.json", "r", encoding="utf-8") as fr:
session_data = json.load(fr)
if time.time() - session_data["session_created"] < session_data["session"]["expires_in"]:
session = session_data["session"]
print("Using existing login session")
else:
print("Login session expired!")
if session is None:
print("Creating new login session!")
resp = requests.post("https://wiarnvnpsjysgqbwigrn.supabase.co/auth/v1/token?grant_type=password", headers={
"apikey": self.config["api_key"]
}, json={
"email": self.config["login"]["email"],
"password": self.config["login"]["password"]
})
session = resp.json()
assert "error" not in session, session["error"]
with open("session.json", "w", encoding="utf-8") as fw:
json.dump(
{"session": session, "session_created": time.time()}, fw, indent=4)
return session
def run(self, *args, **kwargs):
super().run(self.config["bot_key"], *args, **kwargs)
def main():
bot = NPSTBot()
bot.run()
if __name__ == "__main__":
main()