-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
323 lines (255 loc) · 11 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
# bot.py
import os
import discord
import random
import asyncio
import typing
import re
import datetime
from dotenv import load_dotenv
from discord.ext import commands, tasks
from timedQueue import QueueUser, TimedQueue
from helper_functions import *
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
GUILD = os.getenv("DISCORD_GUILD")
WORKING_CHANNEL = os.getenv("WORKING_CHANNEL_ID")
JACKBOX_VOICECHANNEL_CATEGORY = os.getenv("JACKBOX_VOICECHANNEL_CATEGORY")
DEFAULT_QUEUE_NAME = os.getenv("DEFAULT_QUEUE_NAME")
DEFAULT_QUEUE_MINIMUM_REQ = 2
# client = discord.Client()
bot = commands.Bot(command_prefix="!")
bot.remove_command("help")
# maximum amount of jackbox voicechannels
maximum_channel_number = 20
# maximum size of one jackbox voicechannel
maximum_channel_size = 16
# minimum size of one jackbox voicechannel
minimum_channel_size = 2
# the maximum amount of seconds a user should be in a queue
max_user_livetime = 60*30 # 30 mins
wtp_queues = []
##################### HELPER FUNCTIONS ###########################
def not_in_dms():
async def wrapper_for_check(ctx, *args):
print(ctx.guild)
if ctx.guild == None:
return False
return True
return commands.check(wrapper_for_check)
def get_current_guild():
return discord.utils.find(lambda g: GUILD == g.name, bot.guilds)
def get_current_jackbox_vcs():
return discord.utils.find(lambda c: JACKBOX_VOICECHANNEL_CATEGORY == c.name, get_current_guild().categories).voice_channels
# FIXME dont forget that I subtracted the deltatime of 2 hrs, to get the same time, as the discord UI
async def manage_channels(refreshrate_sec: int):
while not bot.is_closed():
jack_calls = get_current_jackbox_vcs()
for vc in jack_calls:
if vc.name == "jackbox 1" or vc.name == "jackbox 2":
continue
else:
if len(vc.members) == 0 and (vc.created_at + datetime.timedelta(minutes=10) < datetime.datetime.now() - datetime.timedelta(hours=2)):
await vc.delete(reason="Empty")
await update_queue_user(refreshrate_sec)
await asyncio.sleep(refreshrate_sec)
async def update_queue_user(time_to_add: int):
for queue in wtp_queues:
for queue_user in queue.userlist:
queue_user.livetime += time_to_add
if queue_user.livetime > max_user_livetime:
queue.remove(queue_user)
if queue.status() == IndividQueue.EMPTY and queue.name != DEFAULT_QUEUE_NAME:
wtp_queues.remove(queue)
def queue_name_is_valid(channelname: str):
if re.match(r"jackbox \d+", channelname.lower()) or channelname.lower() == DEFAULT_QUEUE_NAME.lower():
return False
else:
return True
def user_already_in_any_queue(queue_user_to_check: discord.Member):
for queue in wtp_queues:
for queue_user in queue.userlist:
if queue_user_to_check == queue_user.member:
return queue_user
return None
def get_queue(queue_name: str):
if queue_name == DEFAULT_QUEUE_NAME:
return wtp_queues[0]
for queue in wtp_queues:
if queue_name == queue.name:
return queue
return None
def add_user_to_queue(member: discord.Member, queuename: str):
queue = get_queue(queuename)
new_user = QueueUser(member, queue)
queue.append(new_user)
return queue
def add_queue(originator: discord.Member, queuename: str, min_req: int):
new_originator = QueueUser(originator, None)
queue = IndividQueue(new_originator, queuename, min_req)
new_originator.queue = queue
wtp_queues.append(queue)
return queue
def queue_full_response(queue: IndividQueue):
response = "Hey "
for queue_user in queue.userlist:
response += f"{queue_user.member.mention}, "
response += "finally you are enough players for a new party. Have fun!!!"
return response
def delete_queue(queue: IndividQueue):
queue.userlist.clear()
wtp_queues.remove(queue)
def get_smallest_available_channelnumber():
vcs = get_current_jackbox_vcs()
channelnumbers = []
for vc in vcs:
groups = re.match(r"jackbox (\d+)", vc.name.lower())
channelnumbers.append(int(groups.group(1)))
channelnumbers.sort()
if len(channelnumbers) == 0:
return 1
elif len(channelnumbers) == 1:
return channelnumbers[0] + 1
else:
for i in range(1, len(channelnumbers)):
if channelnumbers[i - 1] == channelnumbers[i] - 1:
continue
else:
return channelnumbers[i] - 1
# we return len + 1, because the first channel starts at 1
return len(channelnumbers) + 1
##################### EVENTS AND COMMANDS #######################################
@bot.event
async def on_ready():
wtp_queues.append(IndividQueue(
None, DEFAULT_QUEUE_NAME, DEFAULT_QUEUE_MINIMUM_REQ))
bot.loop.create_task(manage_channels(60))
print("bot started")
@bot.event
async def on_member_join(member):
welcome_messages = [
f"What's up {member.name}. Welcome to this Discord server, where everybody shares the same interest in playing Jackbox games."
]
await member.create_dm()
await member.dm_channel.send(random.choice(welcome_messages))
# jedes mal, wenn reaction hinzugefügt wird, schauen, ob in den channels genug leute sind und mach dann entsprechende channeladministrationen
@bot.event
async def on_reaction_add(reaction, user):
print(f"added {reaction.message} reaction, by {user.name}")
await reaction.message.channel.send(f"added {reaction.emoji} reaction, by {user.name}")
@bot.event
async def on_voice_state_update(member, before, after):
# switch
if before.channel != after.channel and before.channel is not None and after.channel is not None:
print(
f"user {member.name} switched from {before.channel.name} to channel {after.channel.name}")
# leave
elif before.channel is not None and after.channel is None:
print(f"user {member.name} left channel {before.channel.name}")
# join
elif before.channel is None and after.channel is not None:
print(f"user {member.name} joined channel {after.channel.name}")
@bot.event
async def on_command_error(ctx, error):
print(f"Seems like the command was wrong: {error}")
@bot.command(name="wtp")
@not_in_dms()
@bot_command_channel("searching-for-players")
async def want_to_play(ctx, channelname: str = None, channelsize=10):
if len(get_current_jackbox_vcs()) >= maximum_channel_number:
await ctx.send(f"Sorry {ctx.author.mention}, but the maximum amount of voicechats is reached, but you can try to join an existing one or try later")
return
if channelsize > maximum_channel_size:
channelsize = 16
await ctx.send(f"Your channelsize exceeded the maximum size and was set to 16")
if channelsize < minimum_channel_size:
channelsize = 2
await ctx.send(f"Your channelsize is below the minimum and was set to 2")
user = user_already_in_any_queue(ctx.author)
# check, if user is already in any queue
if user is not None:
await ctx.send(f"{ctx.author.mention} you are already in the **{user.queue.name}** queue")
return
# check if the queue already exists
if channelname is None:
curr_queue = add_user_to_queue(ctx.author, DEFAULT_QUEUE_NAME)
if curr_queue.status() == IndividQueue.FULL:
current_smallest_number = get_smallest_available_channelnumber()
await ctx.guild.create_voice_channel(f"jackbox {current_smallest_number}", category=discord.utils.find(lambda c: JACKBOX_VOICECHANNEL_CATEGORY == c.name, get_current_guild().categories), user_limit=channelsize)
await ctx.send(queue_full_response(curr_queue))
curr_queue.userlist.clear()
else:
await ctx.send(f"There are currently **{len(get_queue(DEFAULT_QUEUE_NAME))}** people waiting in **{get_queue(DEFAULT_QUEUE_NAME).name}** queue")
else:
opt_queue = get_queue(channelname.lower())
if opt_queue is None:
if queue_name_is_valid(channelname):
add_queue(ctx.author, channelname, channelsize)
await ctx.send(f"{ctx.author.mention} created the queue: **{channelname}**. Feel free to join!")
else:
await ctx.send(f"Sorry {ctx.author.mention}, but the channelname **{channelname}** is not allowed")
else:
curr_queue = add_user_to_queue(ctx.author, channelname)
if curr_queue.status() == IndividQueue.FULL:
await ctx.guild.create_voice_channel(channelname, category=discord.utils.find(lambda c: JACKBOX_VOICECHANNEL_CATEGORY == c.name, get_current_guild().categories), user_limit=channelsize)
await ctx.send(queue_full_response(curr_queue))
delete_queue(curr_queue)
else:
await ctx.send(f"There are currently **{len(get_queue(channelname))}** people waiting in the **{get_queue(channelname).name}** queue")
@bot.command(name="wtp-info")
@bot_command_channel("searching-for-players")
async def want_to_play_info(ctx, queue_name: str = None):
response = "```"
response += "The following queues are currently available\n\n"
for index, queue in enumerate(wtp_queues):
response += f"{index + 1}. {queue.name}:\n"
for user in queue.userlist:
response += f"{user.member.name}\n"
response += f"waiting for {queue.min_req - len(queue)} more people to start\n\n"
response += "```"
await ctx.send(response)
@bot.command(name="wtp-leave")
@bot_command_channel("searching-for-players")
async def leave_queue(ctx):
user = user_already_in_any_queue(ctx.author)
if user is not None:
user.queue.remove(user)
if user.queue.status() == IndividQueue.EMPTY and user.queue.name != DEFAULT_QUEUE_NAME:
wtp_queues.remove(user.queue)
await ctx.send(f"**{ctx.author}** left the {user.queue} queue")
user.queue = None
# TODO help command has to be send by DM
@bot.command(name="help")
@not_in_dms()
async def help(ctx):
embed = discord.Embed(colour=discord.Colour.gold())
embed.set_author(
name="Help table"
)
embed.add_field(
name="wtp-help",
value="shows a table with all commands",
inline=False
)
embed.add_field(
name="<font color=\"red\">This</font></HTML>",
value="""
wtp: enter a waiting queue
wtp [queuename]: joins a queue with the name [queuename] or creates a new one if not existing
wtp [queuename] [queuelength]: create a new queue with [queuename] and a maximal size of [queuelength].
""",
inline=False
)
embed.add_field(
name="wtp-leave",
value="leaves a queue, if already in one",
inline=False
)
embed.add_field(
name="wtp-info",
value="shows information about existing queues and personal belongings"
)
embed.description = ""
await ctx.author.send(embed=embed)
await ctx.send(f"{ctx.author.mention}, DM sent")
bot.run(TOKEN)