-
Notifications
You must be signed in to change notification settings - Fork 1
/
lehrbot.py
160 lines (134 loc) · 4.7 KB
/
lehrbot.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
from collections import defaultdict
from typing import Dict, List
import os
import discord
from discord import Member
from discord.channel import TextChannel
from discord.guild import Guild
from discord.role import Role
from discord.message import Message
from discord.embeds import Embed
import classes
client: discord.Client = discord.Client()
queues: Dict[str, List[Member]] = defaultdict(list)
def get_all_classes(guild: Guild) -> List[str]:
roles: List[Role] = guild.roles
r: Role
return [r.name[6:] for r in roles if r.name.startswith('Class-')]
async def showclasses(user: Member, channel: TextChannel) -> None:
out = user.mention + " List of classes:\n"
out += "\n".join(get_all_classes(channel.guild))
await channel.send(out)
async def joinqueue(user: Member, channel: TextChannel, cls: str) -> None:
if cls not in get_all_classes(channel.guild):
await channel.send(
user.mention + ' Section {} does not exist.'.format(cls)
)
return
if user in queues[cls]:
queues[cls].remove(user)
await channel.send(
user.mention + ' has left the {} queue.'.format(cls)
)
else:
queues[cls].append(user)
await channel.send(
user.mention + ' has joined the {} queue.'.format(cls)
)
async def showqueue(caller: Member, channel: TextChannel, cls: str) -> None:
user: Member
if len(queues[cls]) == 0:
await channel.send(
'{} Queue "{}" is empty.'.format(caller.mention, cls)
)
else:
out: str = '{} Members in "{}" queue:\n'.format(caller.mention, cls)
for user in queues[cls]:
out += user.display_name + '\n'
await channel.send(out)
@classes.check_admin
async def ready(mentor: Member, channel: TextChannel, cls: str) -> None:
student: Member = queues[cls].pop(0)
await channel.send(
mentor.mention + " is ready for " + student.mention + "."
)
async def help(channel: TextChannel) -> None:
embedVar = Embed(
title="Help!",
description="Possible commands:",
color=0xf76902
)
embedVar.add_field(
name="$joinqueue <class>",
value="Add yourself to an existing queue.",
inline=False
)
embedVar.add_field(
name="$showqueue <class>",
value="Show the people currently in queue.",
inline=False
)
embedVar.add_field(
name="$showclasses",
value="Lists all classes.",
inline=False
)
embedVar.add_field(
name="$ready (admin only)",
value="Move to the next student in the queue.",
inline=False
)
embedVar.add_field(
name="$makeclass <name> (admin only)",
value="Create a class.",
inline=False
)
embedVar.add_field(
name="$deleteclass <name> (admin only)",
value="Deletes a class.",
inline=False
)
embedVar.add_field(
name="$clear <class> (admin only)",
value="Clears the queue of the specified class.",
inline=False
)
await channel.send(embed=embedVar)
@client.event
async def on_ready() -> None:
print('We have logged in as {0.user}'.format(client))
await client.change_presence(activity=discord.Game("$help"))
@classes.check_admin
async def clear(caller: Member, channel: TextChannel, cls: str) -> None:
queues[cls] = list()
await channel.send(caller + " has cleared the queue for " + cls + ".")
@client.event
async def on_message(message: Message) -> None:
if message.author == client.user:
return
if message.content.startswith('$'):
tokens: List[str] = message.content[1:].split(' ')
if tokens[0] == 'joinqueue':
await joinqueue(message.author, message.channel, tokens[1])
elif tokens[0] == 'showclasses':
await showclasses(message.author, message.channel)
elif tokens[0] == 'ready':
await ready(message.author, message.channel, tokens[1])
elif tokens[0] == 'showqueue':
await showqueue(message.author, message.channel, tokens[1])
elif tokens[0] == 'help':
await help(message.channel)
elif tokens[0] == 'makeclass':
await classes.makeclass(message.author, message.channel, tokens[1])
elif tokens[0] == 'deleteclass':
await classes.deleteclass(
message.author, message.channel, tokens[1]
)
elif tokens[0] == 'clear':
await clear(message.author, message.channel, tokens[1])
else:
await message.channel.send(
"Invalid command. Type $help if you need somebody."
)
if __name__ == "__main__":
client.run(os.getenv('LEHRBOT_API_KEY', ''))