-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
205 lines (155 loc) · 7.62 KB
/
main.py
File metadata and controls
205 lines (155 loc) · 7.62 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
from typing import List
import discord
from discord import app_commands
from dotenv import dotenv_values
import random
import requests
import scraper
print("main.py starting!")
env_vars = dotenv_values(".env")
# setup intents
intents = discord.Intents.default()
intents.message_content = True
# Open connection with discord and grab our command tree
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)
# Scrape classes
classlist: list = scraper.get_class_list(update=True)
@client.event
async def on_ready():
# print(classlist)
print(f'Logged on as {client.user}!')
# sync our commands globally
await tree.sync()
print("Ready!")
# Autocomplete decorator for school departments
async def school_autocomplete(interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]:
schools = ['ECE', 'M']
return [
app_commands.Choice(name=school, value=school)
for school in schools if current.lower() in school.lower()
]
# https://stackoverflow.com/questions/71165431/how-do-i-make-a-working-slash-command-in-discord-py
# noinspection PyUnresolvedReferences
@tree.command(name="addclass", description="Add a class to your roles")
@app_commands.autocomplete(school=school_autocomplete)
async def add_class(interaction: discord.Interaction, school: str, class_id: str):
# make a case-insensitive concat of the department and course number
class_full_name = (school + " " + class_id).upper()
USER = interaction.user
# Check for validity of the course, if not then quit
if not check_valid(class_full_name):
await interaction.response.send_message("Please enter a valid class", ephemeral=True)
return
# if the role already exists, then give it to the user
if discord.utils.get(interaction.guild.roles, name=class_full_name):
# if they already have it then we don't really need to do anything...
if discord.utils.get(user.roles, name=class_full_name):
await interaction.response.send_message("You already have this role.", ephemeral=True)
return
# assign the role
await user.add_roles(discord.utils.get(user.guild.roles, name=class_full_name))
await interaction.response.send_message("Done!", ephemeral=True)
return
# if it doesn't, then create the role and then add the user
role = await interaction.guild.create_role(name=class_full_name)
await user.add_roles(role)
await interaction.response.send_message("Done!", ephemeral=True)
return
# noinspection PyUnresolvedReferences
@tree.command(name="removeclass", description="Remove a class from your roles")
@app_commands.autocomplete(school=school_autocomplete)
async def remove_class(interaction: discord.Interaction, school: str, class_id: str):
# make a case-insensitive concat of the department and course number
class_full_name = (school + " " + class_id).upper()
user = interaction.user
# Check for validity of the course, if not then quit
if not check_valid(class_full_name):
await interaction.response.send_message("Please enter a valid class", ephemeral=True)
return
# if the role already exists, then remove it if they have it
if discord.utils.get(interaction.guild.roles, name=class_full_name):
role = discord.utils.get(interaction.guild.roles, name=class_full_name)
if discord.utils.get(user.roles, name=class_full_name):
await user.remove_roles(role)
await interaction.response.send_message("Done!", ephemeral=True)
return
await interaction.response.send_message("You don't have this role.", ephemeral=True)
return
await interaction.response.send_message("Failed ;_;.", ephemeral=True)
def randBonk():
return ['https://media.tenor.com/zdcbh9URQCsAAAAd/bonk-doge.gif',
'https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExYWQ0Nzk1NDFkYTBmNTZiODdlNmQ4YmZjNmQxODhhYTFkNTIxNTQ2NyZlcD12MV9pbnRlcm5hbF9naWZzX2dpZklkJmN0PWc/6d7YORJ4gNKJZ6eE4o/giphy-downsized-large.gif',
'https://media.tenor.com/Tg9jEwKCZVoAAAAd/bonk-mega-bonk.gif'][0 if random.randint(1, 100) > 25 else random.randint(1, 2)]
@tree.command(name="bonk", description="...bonk!")
async def bonk(interaction: discord.Interaction, user: discord.Member):
e = discord.Embed()
e.set_image(url=randBonk())
await interaction.response.send_message(interaction.user.mention + " bonked " + user.mention, embed=e)
"""
@tree.command(name="test")
async def test(interaction: discord.Interaction):
await interaction.response.send_message(f'Sorry, rate limited. Here\'s a cool video to pass the time!!\nhttps://www.youtube.com/watch?v={random.choice(["h73EYcyszf8", "itdpuGHAcpg", "22-Ji8_kDwg", "zsJpUCWfyPE", "21X5lGlDOfg"])}')
"""
@tree.command(name="apod", description="Display NASA's Astronomy Picture Of The Day!!")
async def apod(interaction: discord.Interaction):
try:
key = env_vars["APOD_KEY"]
print("ADD a NASA API KEY to .env!!")
except:
key = "DEMO_KEY"
r = requests.get(f'https://api.nasa.gov/planetary/apod?count=1&api_key={key}')
print(r.status_code)
if (r.status_code == 429):
await interaction.response.send_message(f'Sorry, rate limited. Here\'s a cool video to pass the time!!\nhttps://www.youtube.com/watch?v={random.choice(["h73EYcyszf8", "itdpuGHAcpg", "22-Ji8_kDwg", "zsJpUCWfyPE", "21X5lGlDOfg"])}')
return
try:
a = r.json()[0]["copyright"]
except:
a = "public domain"
e = discord.Embed(description=(r.json()[0]["explanation"]), title=(f"APOD {r.json()[0]['date']} - {r.json()[0]['title']}"))
e.set_author(name=a)
if (r.json()[0]["media_type"] == "image"):
e.set_image(url=str(r.json()[0]["url"]))
else:
e.url = str(r.json()[0]["url"])
await interaction.response.send_message(embed=e)
@tree.command(name="say")
async def say(interaction: discord.Interaction, msg: str):
await interaction.response.send_message(f"{interaction.user.mention} asked me to say: {msg}")
@tree.command(name="github", description="gets github link")
async def github(interaction: discord.Interaction):
e = discord.Embed(title="Frappe Bot Github:", description="https://github.com/yawn03/frappe")
await interaction.response.send_message(embed=e)
@tree.command(name="cupid", description="john cena busts it down")
async def cupid(interaction: discord.Interaction):
await interaction.response.send_message("https://www.rubylith.xyz/media/cupid.mp4")
fakeDB = {}
@tree.command(name="ece-moment",
description="What's the liklihood that you won't die alone")
async def ece_moment(interaction: discord.Interaction):
user_id = str(interaction.user.id)
if (user_id not in fakeDB):
# get weighted chance of lonliness
b = random.randint(-25, 100)
b = 0 if b < 0 else b
# apply to user
fakeDB[user_id] = b
else:
b = fakeDB[user_id]
m = f"There's a {b}% chance you will defeat the ECE major stereotype"
e = discord.Embed(description=m)
if (b == 0):
e.title="Quite unfortunate"
else:
e.title='"There\'s hope 🙏" -your grandma'
await interaction.response.send_message(embed=e)
if (b == 0):
await interaction.followup.send("Here's some music to comfort you in these trying times\n\nhttps://files.catbox.moe/76anah.mp4")
@tree.command(name="donk", description="...bonk!")
async def donk(interaction: discord.Interaction):
await interaction.response.send_message("donk!")
def check_valid(id: str):
return id in classlist
print("starting bot!")
client.run(env_vars["APPLICATION_KEY"])