-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
341 lines (288 loc) · 13.1 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
# Dependencies
# pip install discord
# pip install -U discord-py-slash-command
# pip install --upgrade Pillow
# TODO
# DM when judge assigns points or comments or accepts
# Error checking on frontend
import discord
import sys
import json
import backend
from discord_slash import SlashCommand
from discord_slash.utils.manage_commands import create_option, create_choice
from discord_slash.model import SlashCommandOptionType
from PIL import Image, ImageDraw, ImageFont
import io
from discord import File
with open('keys.json', encoding='utf-8-sig') as k:
keys = json.load(k)
with open('constants.json', encoding='utf-8-sig') as c:
constants = json.load(c)
client = discord.Client(intents=discord.Intents.all())
slash = SlashCommand(client, sync_commands=True)
guildIDs = keys["guildIDs"]
colors = {
"purple": 0xb042f5,
"red": 0xeb402d,
"green": 0x00ff00,
"yellow": 0xfcd703,
"black": 0x1c1c1c,
"white": 0xfcfcfc,
}
@client.event
async def on_message(msg):
command = msg.content.split(" ")
print(command)
try:
if str(msg.channel.type) == "private":
if command[0] == "help" or command[0] == "/help":
await sendHelp(msg)
elif command[0] == "view" or command[0] == "/view":
await sendView(msg)
elif command[0] == "leaderboard" or command[0] == "/leaderboard":
await sendLeaderboard(msg)
elif command[0] == "status" or command[0] == "/status":
await sendStatus(msg, command[1], True)
except:
await msg.author.send(embed=errorEmbed(
"There was an error with your command. You most likely forgot a parameter.\nType /help for more information."))
@client.event
async def on_ready():
print("Username: " + client.user.name)
await changeSplash()
@client.event
async def on_member_join(member):
channel = discord.utils.get(member.guild.channels, name=constants["welcomeChannel"])
embedVar = discord.Embed(title="🎉 Welcome to the Scunt Discord " + member.display_name + "!", description="",
color=colors["purple"])
embedVar.add_field(name="Please use the /login <email> <code>", value="(same email as registration)",
inline=False)
await channel.send(embed=embedVar)
# ---------------------------------------------------------------------------------
# Test commands listed below. Use it to test basic functionality of the bot
# ---------------------------------------------------------------------------------
# @slash.slash(name="hello", guild_ids=guildIDs, description="Used to test your bot.", options = [])
# async def hello(ctx):
# embedVar = discord.Embed(title="Hello " + ctx.author.name, color=colors["purple"])
# await ctx.send(embed=embedVar)
# return
@slash.slash(name="login", guild_ids=guildIDs,
description="Used to get access to your Scunt team. Use the same email as registration.", options=[
create_option(
name="email",
description="Use your registration email for F!rosh week",
option_type=SlashCommandOptionType.STRING,
required=True
),
create_option(
name="code",
description="Use the code we've given you, you can find this by logging into the orientation or scunt site",
option_type=SlashCommandOptionType.STRING,
required=True
),
])
# todo - send error if email was entered and not in database
# todo - backend can send fullName field or preferred name if not empty
async def login(ctx, email, code):
print(ctx)
if getLogin(ctx):
print(getLogin(ctx))
await ctx.send(embed=errorEmbed("You have already logged in to Discord."))
return
if "@" in email:
loginResponse = backend.loginUser(email, code)
print(loginResponse)
print(ctx)
if 'errorMsg' in loginResponse:
print(loginResponse["errorMsg"])
embedVar = discord.Embed(title="Sent you a DM!", color=colors["purple"])
msg = await ctx.send(embed=embedVar)
# await msg.delete()
await ctx.author.send(embed=errorEmbed(loginResponse['errorMsg']))
return
if loginResponse['alreadyIn']:
print("Already logged in")
embedVar = discord.Embed(title="Sent you a DM!", color=colors["purple"])
msg = await ctx.send(embed=embedVar)
# await msg.delete()
await ctx.author.send(embed=errorEmbed("You have already logged in."))
return
try:
await ctx.author.add_roles(
discord.utils.get(ctx.author.guild.roles, name=constants["teamRoles"][int(loginResponse["team"]) - 1]))
await ctx.author.add_roles(discord.utils.get(ctx.author.guild.roles, name=constants["loggedInRole"]))
if loginResponse["type"] == 'leadur':
await ctx.author.add_roles(discord.utils.get(ctx.author.guild.roles, name=f"Team {loginResponse['team']} Leedur"))
await ctx.author.edit(nick=(loginResponse["fullName"] + " (" + loginResponse["pronoun"] + ")")[0:31])
except Exception as e:
print("error:")
print(e)
await ctx.send(embed=errorEmbed(str(e)))
else:
embedVar = discord.Embed(title="Sent you a DM!", color=colors["purple"])
print(embedVar)
msg = await ctx.send(embed=embedVar)
# await msg.delete()
embedVar = discord.Embed(title="Login successful, " + loginResponse["fullName"],
description="You are on team #" + str(loginResponse[
"team"]) + " and you now have access to the respective channels.",
color=colors["green"])
await ctx.author.send(embed=embedVar)
else:
await ctx.send(
embed=errorEmbed("Please enter a valid email and try again. Use the same email as you used to register."))
@slash.slash(name="status", guild_ids=guildIDs, description="View the status of a mission", options=[
create_option(
name="mission", description="The mission number",
option_type=SlashCommandOptionType.INTEGER,
required=True
),
])
async def status(ctx, mission):
await sendStatus(ctx, mission, False)
embedVar = discord.Embed(title="Sent you a DM!", color=colors["purple"])
msg = await ctx.send(embed=embedVar)
# await msg.delete()
async def sendStatus(ctx, mission, DM):
if (DM):
await sendMessage(ctx, errorEmbed('Please use this command on the Scunt Discord server only.'), DM)
return
if int(mission) <= 0:
await sendMessage(ctx, errorEmbed('Invalid Mission Number'), DM)
return
team = await getTeam(ctx)
if team is not False:
statusResponse = backend.status(mission, team, ctx.author.id)
print(statusResponse)
if 'errorMsg' in statusResponse:
await sendMessage(ctx, errorEmbed(statusResponse['errorMsg']), DM)
return
embedVar = discord.Embed(title="Challenge " + str(mission), description=createDescription([
{"title": "Name", "description": statusResponse["name"]},
{"title": "Category", "description": statusResponse["category"]},
{"title": "Status", "description": statusResponse["missionStatus"]},
{"title": "Points rewarded", "description": statusResponse["points"]},
]), color=colors["purple"])
await ctx.author.send(embed=embedVar)
else:
await ctx.send(embed=errorEmbed("You are not on a team! Please use /login"))
@slash.slash(name="help", guild_ids=guildIDs, description="See all the commands available.")
async def help(ctx):
await sendHelp(ctx)
embedVar = discord.Embed(title="Sent you a DM!", color=colors["purple"])
msg = await ctx.send(embed=embedVar)
# await msg.delete()
async def sendHelp(ctx):
embedVar = discord.Embed(title="Available Scunt " + constants["scuntYear"] + " commands", color=colors["purple"])
for command in constants["commands"]:
embedVar.add_field(name=command["title"], value=command["description"], inline=False)
await ctx.author.send(embed=embedVar)
@slash.slash(name="missions", guild_ids=guildIDs, description="See all the missions available.")
async def missions(ctx):
await sendMissions(ctx)
embedVar = discord.Embed(title="Sent you a DM!", color=colors["purple"])
msg = await ctx.send(embed=embedVar)
# await msg.delete()
async def sendMissions(ctx):
embedVar = discord.Embed(title="Scunt " + constants["scuntYear"] + " mission list",
description=constants["challengesLink"], color=colors["purple"])
await ctx.author.send(embed=embedVar)
@slash.slash(name="leaderboard", guild_ids=guildIDs, description="View the current leaderboard standings.")
async def leaderboard(ctx):
await sendLeaderboard(ctx)
embedVar = discord.Embed(title="Sent you a DM!", color=colors["purple"])
msg = await ctx.send(embed=embedVar)
# await msg.delete()
async def sendLeaderboard(ctx):
# For testing:
# teamPoints = [150,100,200,300,400,100,200,300,100,100,200,500]
leaderboardResponse = backend.leaderboard()
if 'errorMsg' in leaderboardResponse:
await sendMessage(ctx, errorEmbed(leaderboardResponse['errorMsg']), True)
return
teamPoints = leaderboardResponse["teamScores"]
if (len(constants["teamRoles"]) != len(teamPoints)):
print("There was a mismatch between teams and the length of the leaderboard!")
return
IMAGE_WIDTH = 800
heightTeam = 50
IMAGE_HEIGHT = len(constants["teamRoles"]) * heightTeam + 90
image = Image.new('RGB', (IMAGE_WIDTH, IMAGE_HEIGHT))
# create object for drawing
draw = ImageDraw.Draw(image)
# white background
draw.rectangle([0, 0, IMAGE_WIDTH, IMAGE_HEIGHT], fill=colors["white"])
# draw title
font = ImageFont.truetype('ComicSansMS3.ttf', 45)
text = "Scunt " + constants["scuntYear"] + " Leaderboard"
textWidth, textHeight = draw.textsize(text, font=font)
x = (IMAGE_WIDTH - textWidth) // 2
draw.text((x, 20), text, fill=colors["black"], font=font)
# draw text and bars
minBarWidth = 75
maxBarWidth = 500
offset = 35
teamNumber = 0
minValue = min(teamPoints)
maxValue = max(teamPoints)
range = maxValue - minValue
if range == 0:
range = 100
for team in constants["teamRoles"]:
font = ImageFont.truetype('ComicSansMS3.ttf', 26)
textWidth, textHeight = draw.textsize(team, font=font)
textWidth = 80
offset = offset + heightTeam
textPaddingLeft = 20
barPaddingLeft = 30
teamPointsPercent = (teamPoints[teamNumber] - minValue) / range
draw.rounded_rectangle((textWidth + textPaddingLeft + barPaddingLeft, offset + 3,
textWidth + textPaddingLeft + barPaddingLeft + (
teamPointsPercent * maxBarWidth + minBarWidth), offset + textHeight),
fill="purple", radius=7)
draw.text((textPaddingLeft, offset - 2), team, fill=(50, 0, 120), font=font)
font = ImageFont.truetype('ComicSansMS3.ttf', 20)
textWidthScore, textHeightScore = draw.textsize(str(teamPoints[teamNumber]), font=font)
draw.text((textWidth + textPaddingLeft + barPaddingLeft + (teamPointsPercent * maxBarWidth + minBarWidth + 10),
offset + 3), str(teamPoints[teamNumber]), fill=(50, 0, 120), font=font)
teamNumber = teamNumber + 1
buffer = io.BytesIO()
image.save(buffer, format='PNG')
buffer.seek(0)
await ctx.author.send(file=File(buffer, 'myimage.png'))
async def changeSplash():
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="you right now..."))
def errorEmbed(errorDescription):
return discord.Embed(title="Error", description=errorDescription, color=colors["red"])
def createDescription(fields, newline=False):
outputString = ""
for field in fields:
if (field["description"] == "" or field["description"] == False):
continue
if (str(field["title"]) != ""):
outputString += "**" + str(field["title"]) + ":** "
if (newline):
outputString += "\n"
outputString += str(field["description"])
outputString += "\n"
return outputString
async def getTeam(ctx):
for role in ctx.author.roles:
if ("team" in role.name.lower()):
return role.name.lower().replace("team", "").replace(" ", "")
return False
def getLogin(ctx):
for role in ctx.author.roles:
if (constants["loggedInRole"] == role.name):
return True
return False
def getDiscordTag(ctx):
print(ctx.author.name + "#" + str(ctx.author.discriminator))
return ctx.author.name + "#" + str(ctx.author.discriminator)
async def sendMessage(ctx, embed, DM):
if DM:
await ctx.author.send(embed=embed)
else:
await ctx.send(embed=embed)
client.run(keys["clientToken"])