-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot.py
273 lines (237 loc) · 11.6 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
import discord
import os
from discord.ext import commands
from dotenv import load_dotenv
import sys
from classes.Event import Event
client = commands.Bot(command_prefix='.')
id_increment = 0
servers = {}
def check_events_server(ctx):
return ctx.guild.id in servers
@client.event
async def on_ready():
print('Bot is ready.')
@client.command()
async def ping(ctx):
"""Returns the latency of the bot"""
print(f"Ping command sent from server: {ctx.message.guild.name} by {ctx.message.author}")
sys.stdout.flush()
await ctx.send(f'Pong! {round(client.latency * 1000)}ms')
@client.command()
@commands.has_role('Host')
async def begin(ctx, *, event_name):
"""Only for hosts: begins an even with <event_name>"""
print(f"Begin command sent from server: {ctx.message.guild.name} by {ctx.message.author}")
sys.stdout.flush()
global id_increment
event = Event(id_increment, event_name, ctx.author)
if not check_events_server(ctx):
servers[ctx.guild.id] = {}
server_events = servers[ctx.guild.id]
server_events[event.id] = event
id_increment += 1
await ctx.send(f'{event.host.display_name} started {event.eventName}. Use id {event.id} to enter queue.')
@client.command(aliases=['liste', 'listevents'])
async def le(ctx):
"""Lists all events currently taking place"""
print(f"List events command sent from server: {ctx.message.guild.name} by {ctx.message.author}")
sys.stdout.flush()
if not check_events_server(ctx) or len(servers[ctx.guild.id]) == 0:
embed_var = discord.Embed(title="There are currently no events taking place at this time",
description="Once a host starts an event you'll find them "
"all listed here!", color=0x902020)
await ctx.send(embed=embed_var)
else:
server_events = servers[ctx.guild.id]
embed_var = discord.Embed(title="Current events",
description="To ask a question in an event, type "
"`.enter [event_id] [question_topic]`",
color=0x902020)
for event in server_events:
embed_var.add_field(name=server_events[event].eventName,
value=f'Hosted by: {server_events[event].host.display_name}. '
f'Event ID: {server_events[event].id}. '
f'There are currently {len(server_events[event].queue)} in queue', inline=False)
await ctx.send(embed=embed_var)
@client.command(aliases=['q'])
async def queue(ctx, eid):
"""Displays queue for a given <queue_id>"""
print(f"Queue command sent from server: {ctx.message.guild.name} by {ctx.message.author}")
sys.stdout.flush()
if not check_events_server(ctx):
await ctx.send("Sorry, I wasn't able to find any events")
else:
server_events = servers[ctx.guild.id]
event_id = int(eid)
if event_id not in server_events:
print(server_events)
await ctx.send("Sorry, I wasn't able to find that event")
else:
count = 1
embed_var = discord.Embed(title=f"{server_events[event_id].eventName}",
description=f"Hosted by: {server_events[event_id].host.display_name}",
color=0x902020)
for entry in server_events[event_id].queue:
embed_var.add_field(name=f"{count}. {entry.topic}",
value=f"Question asked by {entry.author.display_name}", inline=False)
count += 1
await ctx.send(embed=embed_var)
@client.command()
async def enter(ctx, eid, *, topic="Topic N/A"):
"""Enters a queue with a given <queue_id>"""
print(f"Enter command sent from server: {ctx.message.guild.name} by {ctx.message.author}")
sys.stdout.flush()
if not check_events_server(ctx):
await ctx.send("Sorry, I wasn't able to find any events")
else:
server_events = servers[ctx.guild.id]
event_id = int(eid)
if event_id not in server_events:
await ctx.send(f"Sorry, I wasn't able to find any event with ID: {event_id}")
else:
await ctx.send(f"{ctx.author.display_name} was added to queue {event_id}"
if server_events[event_id].enter_queue(ctx.author, topic) else
"You've exceeded the maximum amount of question in a queue, wait for the host "
"to resolve one of your questions")
@client.command()
async def leave(ctx, eid, question_id):
"""Leaves the queue with a given <queue_id> and a given <question_id>"""
print(f"Leave command sent from server: {ctx.message.guild.name} by {ctx.message.author}")
sys.stdout.flush()
if not check_events_server(ctx):
await ctx.send("Sorry I wasn't able to find any events")
else:
server_events = servers[ctx.guild.id]
event_id = int(eid)
if event_id not in server_events:
await ctx.send(f"Sorry I wasn't able to find any event with ID: {event_id}")
else:
current = server_events[event_id].get_question_at(int(question_id) - 1)
if current.author != ctx.author:
await ctx.send("You can only remove yourself from the queue")
else:
server_events[event_id].resolve(int(question_id) - 1)
await ctx.send(f"{ctx.author.display_name} has left the queue {event_id}")
@client.command()
@commands.has_role('Host')
async def clear(ctx, eid):
"""Only for hosts: clears the queue of a given <event_id>"""
print(f"Clear command sent from server: {ctx.message.guild.name} by {ctx.message.author}")
sys.stdout.flush()
if not check_events_server(ctx):
await ctx.send("Sorry, I wasn't able to find any events")
else:
server_events = servers[ctx.guild.id]
event_id = int(eid)
if event_id not in server_events:
await ctx.send(f"Sorry, I wasn't able to find any event with ID: {event_id}")
elif ctx.author != server_events[event_id].host:
await ctx.send("You are not the host for this event. Only the host can clear the queue for this event.")
else:
server_events[event_id].clear_queue()
await ctx.send(f"The queue has been cleared for {server_events[event_id].eventName}.")
@client.command()
@commands.has_role('Host')
async def end(ctx, eid, *, leave_message="Thanks for attending!"):
"""Only for hosts: ends event with id <event_id>"""
print(f"End command sent from server: {ctx.message.guild.name} by {ctx.message.author}")
sys.stdout.flush()
if not check_events_server(ctx):
await ctx.send("Sorry, I wasn't able to find any events")
else:
server_events = servers[ctx.guild.id]
event_id = int(eid)
if event_id not in server_events:
await ctx.send(f"Sorry, I wasn't able to find any event with ID: {event_id}")
elif ctx.author != server_events[event_id].host:
await ctx.send("You are not the host for this event. Only the host can end their own event.")
else:
await ctx.send(f"{ctx.author.display_name}, the host for the event"
f" '{server_events[event_id].eventName}' has ended the event!")
await ctx.send(leave_message)
server_events.pop(event_id)
@client.command()
@commands.has_role('Host')
async def move(ctx, eid, old_pos, new_pos):
"""Only for hosts: Moves person at <old_position> to <new_position> with event id <event_id>"""
print(f"Move command sent from server: {ctx.message.guild.name} by {ctx.message.author}")
sys.stdout.flush()
if ctx.guild.id not in servers:
await ctx.send("Sorry, I wasn't able to find any events")
else:
server_events = servers[ctx.guild.id]
event_id = int(eid)
user_to_move = server_events[event_id].queue[int(old_pos) - 1].author.display_name
if event_id not in server_events:
await ctx.send(f"Sorry, I wasn't able to find any event with ID: {event_id}")
elif ctx.author != server_events[event_id].host:
await ctx.send("You are not the host for this event. Only the host can move people for this event.")
else:
server_events[event_id].move_user(int(old_pos) - 1, int(new_pos) - 1)
await ctx.send(f"{user_to_move} got moved from position {old_pos} to {new_pos}.")
@client.command()
@commands.has_role('Host')
async def resolve(ctx, eid, question_index=1):
"""Only for hosts: Resolves current user's question
(or question at index <index>) and mentions next user in queue"""
print(f"Resolve command sent from server: {ctx.message.guild.name} by {ctx.message.author}")
sys.stdout.flush()
if not check_events_server(ctx):
await ctx.send("Sorry, I wasn't able to find any events")
else:
server_events = servers[ctx.guild.id]
event_id = int(eid)
if event_id not in server_events:
await ctx.send(f"Sorry, I wasn't able to find any event with ID: {event_id}")
elif ctx.author != server_events[event_id].host:
await ctx.send("You are not the host for this event. Only the host can resolve for this event.")
else:
server_events[event_id].resolve(int(question_index) - 1)
await ctx.send(f"Resolved. {server_events[event_id].currently_served().mention}, you're up next!")
@client.command()
@commands.has_role('Host')
async def ready(ctx, eid):
"""Only for hosts: Mentions next user in queue of <event_id>"""
print(f"Ready command sent from server: {ctx.message.guild.name} by {ctx.message.author}")
sys.stdout.flush()
if not check_events_server(ctx):
await ctx.send("Sorry, I wasn't able to find any events")
else:
server_events = servers[ctx.guild.id]
event_id = int(eid)
if event_id not in server_events:
await ctx.send(f"Sorry, I wasn't able to find any event with ID: {event_id}")
elif ctx.author != server_events[event_id].host:
await ctx.send("You are not the host for this event. Only the host can run this command for this event.")
else:
await ctx.send(f"{server_events[event_id].currently_served().mention}, "
f"{server_events[event_id].host.display_name} is ready for you!")
@client.command()
async def rename(ctx, eid, ind, *, new_question):
"""Renames question at <ind> to <new_question>"""
print(f"Rename command sent from server: {ctx.message.guild.name} by {ctx.message.author}")
sys.stdout.flush()
if not check_events_server(ctx):
await ctx.send("Sorry, I wasn't able to find any events")
else:
server_events = servers[ctx.guild.id]
event_id = int(eid)
question_index = int(ind)
if event_id not in server_events:
await ctx.send(f"Sorry, I wasn't able to find any event with ID: {event_id}")
else:
current = server_events[event_id].get_question_at(int(question_index) - 1)
if current.author != ctx.author:
await ctx.send("You can only rename a topic that you entered.")
else:
current.topic = new_question
await ctx.send("The topic was renamed")
@client.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandNotFound):
print(f"Error sent from server: {ctx.message.guild.name} by {ctx.message.author}")
sys.stdout.flush()
load_dotenv()
ACCESS_TOKEN = os.getenv("ACCESS_TOKEN")
client.run(ACCESS_TOKEN)