-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
388 lines (325 loc) · 14.8 KB
/
bot.py
File metadata and controls
388 lines (325 loc) · 14.8 KB
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
import os
import re
import json
import base64
import asyncio
import discord
import logging
from systemd.journal import JournaldLogHandler
from discord.ext import commands
from dotenv import load_dotenv
from mm import MortalManager
from tasker import Tasker
# Config should be readonly!
def getConfig():
with open ("conf.json","r") as f:
return json.loads(f.read())
#def saveConfig():
# with open ("conf.json","w") as f:
# f.write(json.dumps(config))
# TODO: Add real database support
EMPTY_DB = {"discords": {}, "mortals": []}
def getDb():
if not os.path.isfile("db.json"):
logging.info("New database!")
return EMPTY_DB
with open ("db.json","r") as f:
return json.loads(f.read())
def saveDb():
with open ("db.json","w") as f:
f.write(json.dumps(db))
def isGod(uid):
return (str(uid) in config["userapi"]["admins"])
def recovery(discord_id):
user=db["discords"][str(discord_id)]
return serverManager.password_reset(user)
async def getMentionedUsers(ctx, db):
# return all users mentioned individually and from ranks
# if only word "all" in command - return all users in database
users = []
if ''.join(ctx.message.content.lower().split()[1:]) == "all":
for userid in db["discords"]:
users.append(await bot.fetch_user(int(userid)))
return users
users.extend(ctx.message.mentions)
for rank in ctx.message.role_mentions:
users.extend(rank.members)
return users
# --------------- Initial setup ---------------
# Logs
# get an instance of the logger object this module will use
logger = logging.getLogger("adminbot")
# instantiate the JournaldLogHandler to hook into systemd
journald_handler = JournaldLogHandler()
# set a formatter to include the level name
journald_handler.setFormatter(logging.Formatter(
'[%(levelname)s] %(asctime)s - %(message)s'
))
# add the journald handler to the current logger
logger.addHandler(journald_handler)
# optionally set the logging level
logger.setLevel(logging.INFO)
logging.info("Starting new session...")
# Queue
mainQueue = Tasker() # for tasks that change users data (register, kill, password, etc..)
secondQueue = Tasker() # for reading-only tasks (whois, etc..)
# Discord bot
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
bot = commands.Bot(command_prefix='$')
# Other
config = getConfig()
db = getDb()
serverManager = MortalManager.from_save(config, db)
# --------------- Bot commands ---------------
@commands.cooldown(1,10)
@bot.command(help="Tworzy nowe konto użytkownika")
async def register(ctx):
""" Create account """
if not isGod(ctx.author.id):
await ctx.message.add_reaction('🛑')
await ctx.send("Nie dla psa! Dla Adminów to!")
return
await ctx.message.add_reaction('⌛')
await mainQueue.addJob(registerCoro(ctx))
async def registerCoro(ctx):
for user in await getMentionedUsers(ctx, db):
# check if user already exists
if str(user.id) in db["discords"]:
await ctx.message.add_reaction('⚠')
await ctx.send(f"Ten użytkownik ma już konto: {db['discords'][str(user.id)]}")
continue
out = None
try:
out = serverManager.create_mortal()
except Exception as e:
logging.exception(f"Exception while creating user: {e}")
out = None
pass # TODO: Add exception handling
if out:
# Update db
db["mortals"] = list(serverManager.mortals)
db["discords"][str(user.id)] = out
saveDb()
# Message success
logging.info(f"Created user: {out}")
await ctx.message.add_reaction('📬')
await ctx.send(f"Utworzono użytkownika: {out}")
newdata = recovery(user.id)
embed=discord.Embed(title="Tryton", url="https://tryton.vlo.gda.pl", description="Sleep less, code more!", color=0x11ff00)
embed.add_field(name="Utworzono dla Ciebie konto na serwerze Tryton", value="https://tryton.vlo.gda.pl", inline=False)
embed.add_field(name="Login", value=f"```{out}```", inline=False)
embed.add_field(name="Hasło", value=f"```{newdata[0]}```", inline=False)
embed.add_field(name="Nazwa bazy danych", value=f"```db{out}```", inline=False)
embed.add_field(name="Hasło bazy danych", value=f"```{newdata[1]}```", inline=False)
embed.set_footer(text="Jeśli kiedyś zapomnisz hasła, użyj komendy $password")
await user.send(embed=embed)
else:
await ctx.message.add_reaction('⚠')
await ctx.send(f"Nie można utworzyć konta dla: {user}")
await ctx.message.remove_reaction('⌛', bot.user)
@commands.cooldown(1,10)
@bot.command(help="Usuwa konto użytkownika wraz ze wszystkimi danymi")
async def kill(ctx):
""" Remove account """
if not isGod(ctx.author.id):
await ctx.message.add_reaction('🛑')
await ctx.send("Nie dla psa! Dla Adminów to!")
return
await ctx.message.add_reaction('⌛')
await mainQueue.addJob(killCoro(ctx))
async def killCoro(ctx):
# Remove by discord username
for user in ctx.message.mentions:
try:
serverManager.remove_mortal(db["discords"][str(user.id)])
# Update config
db["mortals"] = list(serverManager.mortals)
db["discords"].pop(str(user.id),None)
saveDb()
# Message success
logging.info(f"Deleted user: {user.display_name}")
await ctx.send(f"Usunięto konto: {user.display_name}")
except Exception as e:
logging.exception(f"Exception while killing user: {e}")
await ctx.message.add_reaction('⚠')
await ctx.send(f"Nie udało się usunąć konta użytkownika {user.display_name}")
# Remove by server username (s1, s2, etc..)
for user in ctx.message.content.split()[1:]:
try:
if "@" not in user and user.lower() != "all":
serverManager.remove_mortal(user)
# Update db
db["mortals"] = list(serverManager.mortals)
for i in db["discords"]:
if db["discords"][i]==user:
db["discords"].pop(i)
break
saveDb()
# Message success
logging.info(f"Removed user: {user}")
await ctx.send(f"Usunięto konto: {user}")
except Exception as e:
logging.exception(f"Exception while killing user: {e}")
await ctx.message.add_reaction('⚠')
await ctx.send(f"Nie udało się usunąć konta {user}")
await ctx.message.remove_reaction('⌛', bot.user)
@commands.cooldown(1,10)
@bot.command(help="Zmienia hasło użytkownika")
async def password(ctx):
""" Reset caller's password """
await ctx.message.add_reaction('⌛')
await mainQueue.addJob(passwordCoro(ctx))
async def passwordReset(ctx, user, single=False):
""" Reset provided user's password and send according message """
try:
newdata = recovery(user.id)
logging.info(f"Resetted password: {db['discords'][str(user.id)]}")
if single:
await ctx.message.add_reaction('📬')
embed=discord.Embed(title="Tryton", url="https://tryton.vlo.gda.pl", description="Sleep less, code more!", color=0x44ff00)
embed.add_field(name="Przywracanie dostępu do konta", value="Twoje hasła zostały zresetowane", inline=False)
embed.add_field(name="Nowe hasło", value=f"```{newdata[0]}```", inline=False)
embed.add_field(name="Nowe hasło bazy danych", value=f"```{newdata[1]}```", inline=False)
await user.send(embed=embed)
await ctx.send(f"Pomyślnie ustawiono nowe hasła dla: {db['discords'][str(user.id)]}")
#await ctx.author.send(f"Nowe hasło do przesyłania plików: `{newdata[0]}`\nNowe hasło do bazy danych: `{newdata[1]}`")
except Exception as e:
logging.exception(f"Password reset failed: {e}")
if single:
await ctx.message.add_reaction('❌')
await ctx.send("Nie udało się zresetować hasła. Prawdopodobnie nie masz jeszcze konta na serwerze Tryton.")
else:
await ctx.send(f"Nie udało się zresetować hasła dla użytkownika {user.display_name}. Prawdopodobnie nie ma on jeszcze konta na serwerze Tryton.")
async def passwordCoro(ctx):
if len(ctx.message.content.split()) == 1:
# no mentioned users - reset author's password
await passwordReset(ctx, ctx.author, single=True)
else:
# some mentions - check permissions
if not isGod(ctx.author.id):
await ctx.message.add_reaction('🛑')
await ctx.send("Normalni użytkownicy nie mogą resetować haseł innych osób.\nJeżeli próbujesz zmienić swoje hasło to użyj samej komendy bez oznaczania nikogo.")
return
# reset by discord username
for user in await getMentionedUsers(ctx, db):
await passwordReset(ctx, user)
# reset by server username (s1, s2, etc..)
for user in ctx.message.content.split()[1:]:
if "@" not in user and user.lower() != "all":
found = False
for i in db["discords"]:
if db["discords"][i]==user:
found = True
res = await bot.fetch_user(int(i))
await passwordReset(ctx, res)
break
if not found:
await ctx.message.add_reaction('⚠')
await ctx.send(f"Użytkownik {user} nie istnieje.")
await ctx.message.remove_reaction('⌛', bot.user)
@commands.cooldown(1,10)
@bot.command(help="Sprawdza, które konta są powiązane z danymi użytkownikami")
async def whois(ctx):
""" Identify discord user by server username and vice versa """
if not isGod(ctx.author.id):
await ctx.message.add_reaction('🛑')
await ctx.send("Nie dla psa! Dla Adminów to!")
return
await ctx.message.add_reaction('⌛')
await secondQueue.addJob(whoisCoro(ctx))
async def whoisCoro(ctx):
# check by discord username
for user in await getMentionedUsers(ctx, db):
try:
nick = db["discords"][str(user.id)]
perm="👑 Admin" if isGod(user.id) else "👨 Użytkownik"
embed=discord.Embed(title=user.display_name, url=f"https://tryton.vlo.gda.pl/u/{nick}", description=perm)
embed.add_field(name="Login na serwerze:", value=nick, inline=False)
embed.add_field(name="Baza danych:", value=f"db{nick}", inline=False)
await ctx.send(embed=embed)
except Exception as e:
logging.exception(f"Whois lookup failed: {e}")
await ctx.message.add_reaction('⚠')
await ctx.send(f"Użytkownik {user.display_name} nie posiada konta na serwerze.")
# Check by server username (s1, s2, etc..)
for user in ctx.message.content.split()[1:]:
if "@" not in user and user.lower() != "all":
found = False
for i in db["discords"]:
if db["discords"][i]==user:
res = await bot.fetch_user(int(i))
perm="👑 Admin" if isGod(i) else "👨 Użytkownik"
embed=discord.Embed(title=res.display_name, url=f"https://tryton.vlo.gda.pl/u/{user}", description=perm)
embed.add_field(name="Login na serwerze:", value=user, inline=False)
embed.add_field(name="Baza danych:", value=f"db{user}", inline=False)
await ctx.send(embed=embed)
found=True
break
if not found:
await ctx.message.add_reaction('⚠')
await ctx.send(f"Użytkownik {user} nie istnieje.")
await ctx.message.remove_reaction('⌛', bot.user)
@commands.cooldown(1,10)
@bot.command(help="Sprawdza, które konto należy do Ciebie")
async def whoami(ctx):
""" Check which account is owned by user """
await ctx.message.add_reaction('⌛')
await secondQueue.addJob(whoamiCoro(ctx))
async def whoamiCoro(ctx):
# check by author id
user=ctx.author
try:
nick = db["discords"][str(user.id)]
perm="👑 Admin" if isGod(user.id) else "👨 Użytkownik"
embed=discord.Embed(title=ctx.author.display_name, url=f"https://tryton.vlo.gda.pl/u/{nick}", description=perm)
embed.add_field(name="Login na serwerze:", value=nick, inline=False)
embed.add_field(name="Baza danych:", value=f"db{nick}", inline=False)
embed.set_footer(text="Jeśli zapomniałeś swoich haseł, wpisz $password")
await ctx.send(embed=embed)
except Exception as e:
logging.exception(f"Whoami does not know who are You: {e}")
await ctx.message.add_reaction('❌')
await ctx.send(f"Nie utworzono dla Ciebie żadnego konta. Jeśli chcesz posiadać konto, skontaktuj się z administracją.")
await ctx.message.remove_reaction('⌛', bot.user)
@commands.cooldown(1,10)
@bot.command(help="Pokazuje pełną listę użytkowników")
async def users(ctx):
""" Show full users list """
if not isGod(ctx.author.id):
await ctx.message.add_reaction('🛑')
await ctx.send("Nie dla psa! Dla Adminów to!")
return
await ctx.message.add_reaction('⌛')
await secondQueue.addJob(usersCoro(ctx))
async def usersCoro(ctx):
em=discord.Embed(title="Wykaz użytkowników",description="Oto wszyscy zarejestrowani na serwerze Tryton:")
fields=0
for i in db["discords"]:
res = await bot.fetch_user(int(i))
login=db["discords"][str(i)]
em.add_field(name=res.display_name,value=f"https://tryton.vlo.gda.pl/u/{login}",inline=False)
fields+=1
if fields>=25:
await ctx.send(embed=em)
em=discord.Embed()
fields=0
await ctx.send(embed=em)
await ctx.message.remove_reaction('⌛', bot.user)
@bot.event
async def on_command_error(ctx,error):
await ctx.message.add_reaction('❌')
if isinstance(error, commands.CommandOnCooldown):
await ctx.send("Nie spamuj! Możesz ponownie użyć tej komendy dopiero za {:.1f}s".format(error.retry_after))
elif isinstance(error, commands.CommandNotFound):
await ctx.send("Nieprawidłowe polecenie. Wpisz `$help`, aby uzyskać listę dostępnych poleceń.")
else:
await ctx.send("Wystąpił problem, proszę skontaktuj się z administracją.")
def main():
# Run queues and bot
asyncio.get_event_loop().run_until_complete(mainQueue.start())
asyncio.get_event_loop().run_until_complete(secondQueue.start())
asyncio.get_event_loop().run_until_complete(bot.start(TOKEN))
if __name__ == "__main__":
main()
logging.warning("Execution ended (that shouldn't be possible)")