-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·479 lines (417 loc) · 17.5 KB
/
main.py
File metadata and controls
executable file
·479 lines (417 loc) · 17.5 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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
import json
import discord
from discord.ext import commands, tasks
import os
import asyncio
# from helpers import flags, pagination
# ka.keep_alive()
from webscraper import GetCourse, SearchCourse
token = "MTAwMDQ4MDk5Mjg2OTQ4MjUxMA.GbvjiX.ioDJbezM5RPW8Fg9coDEJkOQmhEl39hgd9qzeM"
intents = discord.Intents.default()
intents.members = True
intents.messages = True
intents.message_content = True
intents.reactions = True
intents.typing = True
intents.presences = True
bot = commands.Bot(command_prefix=".", intents=intents)
# Roles Construct
school = [
"School",
989074005619245086,
0,
["Pratt", 932231896572764200, "🛠️", 0],
["Trinity", 932232238865731645, "📚", 0],
["Bottos", 972204300447150220, "👍", 0],
]
focus = [
"Focus Groups",
989217923618971699,
1,
["cogneuro-and-law", 972763711221674014, "🧠", 1],
["ethics-leadership-and-global-citizenship", 972763864418615326, "🗣️", 1],
["geopolitics-and-culture", 972764119885287445, "🌐", 1],
["global-energy", 972764161652195338, "🔋", 1],
["global-health", 972764195382779934, "😷", 1],
["humanitarian-challenges", 972764237166444594, "👥", 1],
["service-of-society", 972764285841338368, "📚", 1],
["medicine-ethics-and-health-policy", 972764389071540284, "⚕️", 1],
["modeling-in-economic-and-social-sciences", 972764418846887967, "💹", 1],
["science-and-the-public", 972764462228598854, "🧪", 1],
["virtual-realities", 972764733725900870, "🎮", 1],
["renaissance", 972764498765185065, "🖼️", 1],
["music-and-the-arts", 972764641027575828, "🎼", 1],
["american-experiences", 972764608362323999, "🇺🇸", 1],
["visions-of-freedom", 972764764482719744, "📣", 1],
]
games = [
"Games", 989230831803445258, 1,
["minecraft", 982658318071922748, "<:mc:982657365570646046>", 1],
["league", 982658438318407730, "<:league:982657365524484127>", 1],
["tft", 982658471742824558, "<:tft:996259467362574357>", 1],
["csgo", 982658397402980423, "<:csgo:982657365042135110>", 1],
["valorant", 982658344487649280, "<:valorant:982657365402877982>", 1],
["genshin", 982665865302507550, "<:genshin:982666047964479538>", 1],
["btdbattles", 984839529145974845, "<:btd:989229054651678781>", 1],
["apex", 989227717662760960, "<:apex:989229052667768832>", 1],
["overwatch", 989227837162676294, "<:overwatch:989229056534937660>", 1],
["fortnite", 995357918281080862, "<:fortnite:995357810936262758>", 1],
["fallguys", 995357983263445053, "<:fallguys:995357793701863504>", 1],
["leetcode", 995598378320805908, "<:leetcode:995599166719934485>", 1]
]
courses = [
"Courses", 997216034828329041, 1, ["cs-201", 997214533858897961, "💻", 1],
["chem-101", 997217964795383818, "🧪", 1],
["psy-101", 997218047297323059, "🧠", 1],
["econ-201", 997224230326386728, "🪙", 1],
["math-221", 998794827615436912, "⚖️", 1],
["stats-199", 998794900466315356, "💵", 1]
]
rct = 988977829846859816
rc = 932214936573181963
# roles = [school,focus]
roles = [games, courses]
#Csgo report
# async def StartCsgoReport():
# while True:
# NewMatch = await CsgoReport()
# if NewMatch == True:
# channel = bot.get_channel(990482671379570748) #dukecs channel
# with open('csgo/report.jpg', 'rb') as f:
# picture = discord.File(f)
# await channel.send(file=picture)
#exec(open("webscraper.py").read())
@bot.command()
async def info(ctx, classnumber):
await ctx.send("Fetching course info for " + str(classnumber) + "...")
clist = await GetCourse(classnumber)
# print(clist)
if "error" in clist.keys():
await ctx.send(f"Can't find class with class number {classnumber}!")
return False
c = clist["section_info"]
details = c["class_details"]
# meetings = c["meetings"]
availability = c["class_availability"]
color = discord.Color.green(
) if availability["enrollment_available"] > 0 else discord.Color.red()
embed = discord.Embed(
title=str(details["subject"]) + " " + str(details["catalog_nbr"]) +
"\n" + str(details["course_title"]),
description=c["catalog_descr"]["crse_catalog_description"],
color=color)
if bot.user:
embed.set_author(name="Duke University", icon_url=bot.user.display_avatar.url)
embed.add_field(name="Course ID", value=details["course_id"], inline=True)
embed.add_field(name="Class Number",
value=details["class_number"],
inline=True)
embed.add_field(name="Class Section",
value=details["class_section"],
inline=True)
embed.add_field(name="Location",
value=c["meetings"][0]["room"],
inline=True)
embed.add_field(name="Meet Time",
value=c["meetings"][0]["meets"],
inline=True)
instructors = ""
for i in c["meetings"][0]["instructors"]:
instructors += i["name"] + "\n"
embed.add_field(name="Instructors", value=instructors[:-1], inline=False)
embed.add_field(name="Enrollment Notes",
value=c["enrollment_information"]["class_attributes"],
inline=False)
embed.add_field(name="Seats Open",
value=str(availability["enrollment_available"]) + "/" +
availability["class_capacity"],
inline=True)
embed.add_field(name="Waitlist Open",
value=str(
int(availability["wait_list_capacity"]) -
int(availability["wait_list_total"])) + "/" +
str(availability["wait_list_capacity"]),
inline=True)
await ctx.send(embed=embed)
return True
@bot.command()
async def search(ctx, *, keyword):
await ctx.send("Searching courses for keyword: " + str(keyword) + "...")
clist = await SearchCourse(keyword)
await ctx.send(clist)
@bot.command()
async def remind(ctx, choice, *, classnumber):
if choice in ["a", "add"]:
with open("dukehub/cid.json") as infile:
cid = json.load(infile)
with open("dukehub/uid.json") as infile:
uid = json.load(infile)
classes = classnumber.split(" ")
userid = str(ctx.author.id)
added = ""
for c in classes:
c = str(c)
if await info(ctx, c):
await ctx.send(
f"Are you sure you want to add course {c} to your reminders? You will be pinged when spots become available for that class! (y/N)"
)
def check(m):
return m.author == ctx.author and m.channel == ctx.channel
try:
response = await bot.wait_for('message',
check=check,
timeout=10.0)
except asyncio.TimeoutError:
return await ctx.send("Time's up. Aborted.")
if response.content.lower() not in ("yes", "y"):
return await ctx.send("Aborted.")
if await checkspots(ctx, int(c)) > 0:
await ctx.send(
"The class is currently available. It cannot be added onto your notifications!"
)
continue
if c not in cid.keys():
cid[c] = {}
if userid in cid[c].keys():
await ctx.send(f"{c} is already in your reminders!")
continue
cid[c][userid] = 1
if userid not in uid.keys():
uid[userid] = {}
uid[userid][c] = 1
added += c + " "
with open('dukehub/cid.json', 'w') as fp:
json.dump(cid, fp, indent=4)
with open('dukehub/uid.json', 'w') as fp:
json.dump(uid, fp, indent=4)
if added != "":
await ctx.send(f"Added {added}to your reminders.")
elif choice in ["r", "d", "remove", "delete", "del"]:
with open("dukehub/cid.json") as infile:
cid = json.load(infile)
with open("dukehub/uid.json") as infile:
uid = json.load(infile)
classes = classnumber.split(" ")
userid = str(ctx.author.id)
removed = ""
for c in classes:
c = str(c)
if await info(ctx, c):
await ctx.send(
f"Are you sure you want to **remove** course {c} from your reminders? You will **no longer** be pinged when spots become available for that class! (y/N)"
)
def check(m):
return m.author == ctx.author and m.channel == ctx.channel
try:
response = await bot.wait_for('message',
check=check,
timeout=10.0)
except asyncio.TimeoutError:
return await ctx.send("Time's up. Aborted.")
if response.content.lower() not in ("yes", "y"):
return await ctx.send("Aborted.")
if c in cid.keys() and userid in cid[c].keys():
cid[c].pop(userid)
if len(cid[c].keys()) == 0:
cid.pop(c)
if userid in uid.keys() and c in uid[userid].keys():
uid[userid].pop(c)
removed += c + " "
with open('dukehub/cid.json', 'w') as fp:
json.dump(cid, fp, indent=4)
with open('dukehub/uid.json', 'w') as fp:
json.dump(uid, fp, indent=4)
if removed != "":
await ctx.send(f"Removed {removed}from your reminders.")
elif choice in ["list", "all", "view", "v", "l"]:
desc = ""
with open("dukehub/uid.json") as infile:
uid = json.load(infile)
for c in uid[str(ctx.author.id)].keys():
desc += c + " "
embed = discord.Embed(title="My Reminders", description=desc)
await ctx.send(embed=embed)
else:
await ctx.send(f"Action of {choice} is invalid.")
@bot.command()
async def checkspots(ctx, classnumber):
await ctx.send("Fetching availabilities info for " + str(classnumber) +
"...")
clist = await GetCourse(classnumber)
# print(clist)
if "error" in clist.keys():
await ctx.send(f"Can't find class with class number {classnumber}!")
return False
c = clist["section_info"]
details = c["class_details"]
availability = c["class_availability"]
await ctx.send(
f"{details['subject']} {details['catalog_nbr']} {details['class_section']}: {availability['enrollment_available']} spots open out of {availability['class_capacity']} total spots."
)
return availability["enrollment_available"]
@bot.hybrid_command()
async def ping(ctx):
if ctx.author.id in [211237550487109632,491174779278065689]:
await ctx.send("Pong! " + str(bot.latency) + " ms")
@bot.command()
async def say(ctx, channel, *, msg):
if ctx.author.id in [211237550487109632,491174779278065689]:
chnl = bot.get_channel(int(channel))
if isinstance(chnl, discord.TextChannel):
await chnl.send(msg)
@bot.command()
async def test(ctx):
if ctx.author.id in [211237550487109632,491174779278065689]:
await ctx.send(bot.get_guild(923267323379474453).categories)
@bot.command()
async def rerole(ctx):
channel = bot.get_channel(rc)
guild = bot.get_guild(923267323379474453)
if guild and channel and isinstance(channel, discord.TextChannel):
for role in roles:
has_cat = ""
for cat in guild.categories:
# print(cat.name)
# print(role[0])
# print(role[0]==cat.name)
if role[0] == str(cat.name):
has_cat = cat
# print(has_cat)
if has_cat == "" and role[2] != 0:
await guild.create_category(name=role[0])
desc = ""
msg = await channel.fetch_message(role[1])
await msg.clear_reactions()
for i in range(3, len(role)):
has_ch = False
if role[i][3] != 0:
for ch in has_cat.channels:
# print(cat.name)
# print(role[0])
# print(role[0]==cat.name)
if role[i][0] == str(ch.name):
has_ch = True
# print(has_cat)
if not (has_ch):
mod = discord.utils.get(guild.roles, name="Moderator")
privrole = discord.utils.get(guild.roles, id=role[i][1])
overwrites = {
guild.default_role:
discord.PermissionOverwrite(read_messages=False),
mod:
discord.PermissionOverwrite(read_messages=True),
privrole:
discord.PermissionOverwrite(read_messages=True)
}
channel = await has_cat.create_text_channel(
role[i][0], overwrites=overwrites)
# await cat.create_text_channel(name=role[i][0],)
# if role[i][0] not in guild.roles:
# await guild.create_role(name=role[i][0])
desc += str(role[i][2]) + ": " + "<@&" + str(role[i][1]) + ">\n"
await msg.add_reaction(role[i][2])
embed = discord.Embed(title="Pick your " + str(role[0]) + "!",
description=desc,
color=0x00539b)
embed.set_footer(
text="React to the emojis below to self-assign the role.")
await msg.edit(embed=embed)
await ctx.send("Role adjustments complete.")
@bot.command()
async def sendembed(ctx, channel):
if ctx.author.id in [211237550487109632,491174779278065689]:
embed = discord.Embed(title="Embed!",
description="Nothing to see here.",
color=0x00539b)
await bot.get_channel(int(channel)).send(embed=embed)
@bot.command(pass_context=True)
async def nick(ctx, member: discord.Member, *, nick):
await member.edit(nick=nick)
await ctx.send(f'Nickname was changed for {member.mention} ')
@bot.event
async def on_raw_reaction_add(payload):
for role in roles:
if payload.message_id == role[1]:
# print(role[0])
# guild_id = payload.guild_id
# guild = discord.utils.find(lambda g : g.id == guild_id, bot.guilds)
# emoji = payload.emoji.name
member = payload.member
if member is not None and not (member.bot):
for i in range(3, len(role)):
if str(payload.emoji) == role[i][2]:
r = discord.utils.get(payload.member.guild.roles,
id=role[i][1])
# print(role)
await member.add_roles(r)
@bot.event
async def on_raw_reaction_remove(payload):
for role in roles:
# print(role)
if payload.message_id == role[1]:
# print("hi")
# print(role[0])
# guild_id = payload.guild_id
# guild = discord.utils.find(lambda g : g.id == guild_id, bot.guilds)
# emoji = payload.emoji.name
member = discord.utils.get(
bot.get_guild(923267323379474453).members, id=payload.user_id)
# print(member)
if member is not None and not (member.bot):
# print(member)
# print(role)
for i in range(3, len(role)):
# print(payload.emoji)
if str(payload.emoji) == role[i][2]:
r = discord.utils.get(member.guild.roles,
id=role[i][1])
# print(role)
await member.remove_roles(r)
# curr = [
# "Dani", "Frank", "Tahsimp", "Ellie", "Ari", "Kathleen", "Danny", "Yihong",
# "Mangler", "Arvin", "Sarah"
# ]
# c = 0
@tasks.loop(seconds=60) # repeat after every 10 seconds
async def myLoop():
# global curr
# global c
# c += 1
# if c >= len(curr):
# c = 0
with open("dukehub/cid.json") as infile:
cid = json.load(infile)
for key in cid.keys():
clist = await GetCourse(int(key))
print(f"Checking {key}")
if "error" in clist.keys():
return 0
c = clist["section_info"]
availability = c["class_availability"]
spots = availability["enrollment_available"]
if spots > 0:
for uid in cid[key]:
guild = bot.get_guild(923267323379474453)
member = guild.get_member(int(uid))
await member.send(f"Enrollment available for {key}!")
await asyncio.sleep(1)
# await bot.change_presence(activity=discord.Activity(
# type=discord.ActivityType.watching, name="bugs"))
@bot.command()
async def basic(ctx: commands.Context):
await ctx.send("hello world")
@bot.event
async def on_ready():
print("Ready!")
c = bot.get_channel(rct)
myLoop.start()
text = "Ready!"
if isinstance(c, discord.TextChannel):
text = await c.send(text)
# await GetCourse(3025)
await text.add_reaction('🏃')
# asyncio.Task(StartCsgoReport())
async def main():
async with bot:
await bot.start(token)
asyncio.run(main())