-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.py
445 lines (350 loc) · 13.7 KB
/
bot.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
from json import load as json_load
from os import environ, getenv, listdir
from os.path import dirname
import discord
from discord.ext import commands
from dotenv import load_dotenv
from firebase_admin import credentials, db, initialize_app
from requests import get as requests_get
# Read environment variables set at ./.env
if not load_dotenv(f"{dirname(__file__)}/.env"):
raise RuntimeError(
f"Could not load env file. Make sure it is located at {dirname(__file__)}/.env"
)
debug = getenv("DEBUG") in {"TRUE", "true", "True"}
print(f"DEBUG={debug}")
def check_if_required_env_variables_are_present():
required_env_variables = {
"CURRENT_ACT",
"FIREBASE_DB",
"FIREBASE_STORAGE",
"BOT_TOKEN",
"ROOT_USERS",
}
if not all(env in environ for env in required_env_variables):
raise RuntimeError(
f"The following required environmental variables have not been set - {set(x for x in required_env_variables if x not in environ)}. Refer to code and Readme.MD for seeing what env keys are needed"
)
check_if_required_env_variables_are_present()
# Initialize Firebase app
creds = credentials.Certificate("firebase.json")
initialize_app(
creds,
{
"databaseURL": getenv("FIREBASE_DB"),
"storageBucket": getenv("FIREBASE_STORAGE"),
},
)
current_act = getenv("CURRENT_ACT") if getenv("CURRENT_ACT") else 6
"""The current leaderboard act"""
leaderboard_ref = (
db.reference("vitcc").child("owasp").child(f"leaderboard-act{current_act}")
)
# project_ref = base_ref.child("projects")
# certificate_ref = base_ref.child("certificates")
# ctf_ref = base_ref.child("ctf")
spam_bait_channel_id = getenv("SPAM_BAIT_CHANNEL_ID")
spam_log_channel_id = getenv("SPAM_LOG_CHANNEL_ID")
root_users = map(int, getenv("ROOT_USERS").split(","))
command_prefix = "!cyscom" if not debug else "!cyscom-dev"
bot = commands.Bot(
command_prefix=f"{command_prefix} ",
description="The official CYSCOM VITCC Discord Bot.",
activity=discord.Game(name="CYSCOM Leaderboard"),
intents=discord.Intents.all(),
)
CYSCOM_LOGO_URL = "https://cyscomvit.com/assets/images/logo.png"
def embed_generator(
ctx, description: str, name: str, rating: int = 0, contributions: int = 0
):
"""Returns a usable discord embed"""
embed = discord.Embed(
title=f"{ctx.guild.name}",
description=description,
color=discord.Color.blue(),
)
embed.add_field(name="Name", value=name)
embed.add_field(name="Rating", value=rating)
embed.add_field(name="Contributions", value=contributions)
embed.set_thumbnail(url=CYSCOM_LOGO_URL)
return embed
@bot.command()
async def ping(ctx):
"""Check to see if bot is working. Also returns path of the script"""
msg = f"I'm alive!"
print(msg)
await ctx.send(msg)
@bot.command()
async def doge(ctx):
"""Return a doge pic."""
try:
doge_pic_url = requests_get(
"https://shibe.online/api/shibes?count=1&urls=true"
).json()[0]
await ctx.send(doge_pic_url)
except Exception as e:
print(e)
ctx.send(str(e))
@bot.command()
async def sum(ctx, numOne: int, numTwo: int):
"""Return a sum of 2 numbers. !cyscom sum num1 num2"""
await ctx.send(numOne + numTwo)
@bot.command()
@commands.has_any_role("Cabinet Member")
async def add_member(ctx, name: str, rating: int = 0, contributions: int = 0):
"""Add data to the leaderboard. Call it by !cyscom add_member "name" rating contribution"""
try:
data = leaderboard_ref.get()
name = name.strip()
if not name:
ctx.send("No name given")
return None
if data != None:
for key, value in data.items():
if value["Name"].casefold() == name.casefold():
embed = embed_generator(
ctx,
"User already exists",
name,
value["Rating"],
value["Contributions"],
)
await ctx.send(embed=embed)
return None
# Insert name since it does not exist on the firebase server
leaderboard_ref.push(
{
"Name": name,
"Rating": int(rating),
"Contributions": int(contributions),
}
)
embed = embed_generator(
ctx,
"Added data to the CYSCOM Leaderboard.",
name,
rating,
contributions,
)
print(f"Added {name}")
await ctx.send(embed=embed)
except Exception as e:
ctx.send(e)
print(e.__traceback__)
@bot.command()
@commands.has_any_role("Cabinet Member")
async def add_recruits(ctx):
"""Add recruits by reading a members.txt file present in the same folder"""
# Place file in discord-bot folder.
try:
filename = "members.txt"
if filename not in listdir(dirname(__file__)):
await ctx.send(f"File {filename} not found in {dirname(__file__)}")
return None
else:
with open(filename, "r") as f:
members = f.read().split("\n")
print(members)
if members[0].casefold() == "name":
ctx.send(
f"Members file ({filename}) is supposed to only have 1 name on each line, and must have no headers!"
)
return None
for name in members:
await add_member(ctx, name)
except Exception as e:
ctx.send(e)
print(e.__traceback__)
@bot.command()
@commands.has_any_role("Cabinet Member")
async def set_points(ctx, name: str, rating=0, contributions=0):
"""Specifically set a member's points. Call it by !cyscom set_points "name" rating contribution"""
try:
data = leaderboard_ref.get()
name = name.strip()
if not name:
ctx.send("No name given")
return None
if data != None:
for key, value in data.items():
if value["Name"].casefold() == name.casefold():
selector = leaderboard_ref.child(key)
selector.update(
{
"Name": name,
"Rating": int(rating),
"Contributions": int(contributions),
}
)
embed = embed_generator(
ctx,
"Updated data on the CYSCOM Leaderboard.",
name,
rating,
contributions,
)
print(f"Updated {name}")
await ctx.send(embed=embed)
except Exception as e:
ctx.send(e)
print(e.__traceback__)
@bot.command()
@commands.has_any_role("Member", "Cabinet Member")
async def fetch_data(ctx, name):
"""Fetch data from the leaderboard. !cyscom fetch_data "name\" """
try:
data = leaderboard_ref.get()
if data != None:
for key, value in data.items():
if value["Name"].casefold() == name.casefold():
embed = embed_generator(
ctx,
"Fetched CYSCOM Leaderboard profile.",
name,
value["Rating"],
value["Contributions"],
)
await ctx.send(embed=embed)
except Exception as e:
print(e)
@bot.command()
@commands.has_any_role("Cabinet Member")
async def delete_data(ctx, name):
"""Delete someone from the leaderboard. !cyscom delete_data "name\" """
try:
data = leaderboard_ref.get()
if data != None:
for key, value in data.items():
if value["Name"].casefold() == name.casefold():
leaderboard_ref.child(key).set({})
embed = embed_generator(
ctx, "Deleted data from the Leaderboard", name
)
await ctx.send(embed=embed)
except Exception as e:
ctx.send(e)
print(e.__traceback__)
def fetch_points_for_each_task() -> dict[str, int]:
"""
Reads file at `points.json` to understand how many points to give for each task. If file is absent or error comes up, uses hardcoded values
"""
try:
with open(f"{dirname(__file__)}/points.json") as f:
points_dict: dict[str, int] = json_load(f)
if not isinstance(points_dict, dict) or not points_dict:
raise ValueError
return points_dict
except (FileNotFoundError, ValueError) as e:
print(
f"Could not read points.json in current directory. Error - {type(e)} {e}"
)
exit()
@bot.command()
@commands.has_any_role("Leaderboard", "Cabinet Member")
async def contribution(ctx, name, task):
"""Add contribution to a member. !cyscom contribution "name" "task\" """
try:
data = leaderboard_ref.get()
if data != None:
for key, value in data.items():
if value["Name"].casefold() == name.casefold():
selector = leaderboard_ref.child(key)
ref = selector.get()
rating = ref["Rating"] + points_dict[task.casefold()]
contributions = ref["Contributions"] + 1
selector.update(
{
"Rating": int(rating),
"Name": name,
"Contributions": int(contributions),
}
)
embed = embed_generator(
ctx,
"Added contribution to the CYSCOM Leaderboard.",
name,
rating,
contributions,
)
await ctx.send(embed=embed)
return None
await ctx.send("Name not present")
except Exception as e:
ctx.send(e)
print(e.__traceback__)
@bot.command()
@commands.has_any_role("Member", "Cabinet Member")
async def attendance(ctx, channel_name):
"""Attendance in a voice channel. !cyscom attendance "channel name\" """
f"""Mark attendance in a voice channel. Call by {command_prefix} attendance voice_channel_name"""
# Get the voice channel by name
channel = discord.utils.get(ctx.guild.voice_channels, name=channel_name)
if channel:
# Fetch all the members in the specified voice channel
members = channel.members
message = f"```Members in channel {channel.name} - {channel.id}\n\n"
if members:
message += f"There are {len(members)} member(s)\n\n"
# Print member names and IDs
for i, member in enumerate(members):
message += f"{i+1} - {member.name} - {member.id}\n"
else:
message += "There are no members in this voice channel.\n"
message += "```"
await ctx.send(message)
else:
await ctx.send("Voice channel not found.")
# Events
@bot.event
async def on_ready():
"""Message to be sent when bot is ready"""
print("CYSCOM VITCC Bot v1.0")
@bot.listen()
async def on_message(message):
"""Run every message in the server through this function to check for spam, whether someone is asking github link etc"""
if message.channel.id == spam_bait_channel_id:
ctx = bot.get_channel(spam_log_channel_id)
try:
await message.author.ban()
except:
await ctx.send(
f"USER cannot be banned due to permission issue User-<@{message.author.id}> \nmessage-{message.content}"
)
return
embed = discord.Embed(
title=f"{ctx.guild.name}",
description="Banned for typing in #SPAM_BAIT.",
color=discord.Color.blue(),
)
embed.add_field(name="Name", value=f"{message.author.id}")
embed.add_field(name="Message", value=f"{message.content}")
embed.set_thumbnail(url=CYSCOM_LOGO_URL)
await ctx.send(embed=embed)
return
elif "cyscom github" in message.content.lower():
await message.channel.send("Our GitHub is https://github.com/cyscomvit")
await bot.process_commands(message)
elif "cyscom website" in message.content.lower():
await message.channel.send("Our Website is https://cyscomvit.com")
await bot.process_commands(message)
def check_if_root_user(user_id: int | str):
return int(user_id) in root_users
def add_members_to_act(
act_num: int,
member_names: list[str],
discord_roles: list[str],
add_roles_to_discord: bool = False,
):
...
def fetch_spreadsheet(speadsheet_id: str):
...
# Fetch points for each task
points_dict: dict[str, int] = fetch_points_for_each_task()
# send this points list as message if the command is !cyscom points
@bot.command()
@commands.has_any_role("Cabinet Member")
async def points(ctx):
formatted_points = "\n".join([f"{task} - {points}" for task, points in points_dict.items()])
await ctx.send(f"```{formatted_points}```")
# Run the bot
bot.run(getenv("BOT_TOKEN"))