This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathevents.py
1117 lines (1005 loc) · 49.5 KB
/
events.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import json
import logging
import uuid
import discord
from classes import components
from classes.bot import SnedBot
from discord.ext import commands
from classes.errors import UserInputError
async def has_owner(ctx):
return await ctx.bot.custom_checks.has_owner(ctx)
logger = logging.getLogger(__name__)
class PersistentEventView(discord.ui.View):
def __init__(self, bot: SnedBot, buttons: list = None):
super().__init__(timeout=None)
self.bot = bot
if buttons:
for button in buttons:
self.add_item(button)
class SignUpCategoryButton(discord.ui.Button):
"""This button handles signing up a user & modifying the event-board embed"""
def __init__(
self,
entry_id: int,
category_name: str,
emoji: discord.PartialEmoji,
style: discord.ButtonStyle,
label: str = None,
):
super().__init__(
style=style,
label=label,
emoji=emoji,
custom_id=f"{entry_id}:{category_name}",
)
self.entry_id = entry_id
self.category_name = category_name
async def refresh_embed_field(
self,
guild: discord.Guild,
member_ids: list[int],
embed: discord.Embed,
field_name: str,
member_cap: int = None,
) -> discord.Embed:
"""Refresh an embed field to accurately depict current participants"""
insert_at = None
inline = False
member_cap = member_cap if member_cap else "∞"
names = [guild.get_member(member_id).display_name for member_id in member_ids]
for i, name in enumerate(names):
if len(name) > 16:
names[i] = name[:16] + ".."
names_str = "\n".join(names)
names_str = names_str[:1020] + "`..`" if len(names_str) > 1024 else names_str
names_str = f">>> {names_str}" if len(names_str) > 0 else "-"
for i, field in enumerate(embed.fields):
if field.name.startswith(field_name):
inline = field.inline
insert_at = i
break
embed.insert_field_at(
insert_at,
name=f"{field_name} ({len(member_ids)}/{member_cap})",
value=names_str,
inline=inline,
)
embed.remove_field(insert_at + 1)
return embed
# Called whenever the button is called
async def callback(self, interaction: discord.Interaction):
"""Add, remove, or move to/from the category that this button is configured for"""
if interaction.guild_id:
records = await self.view.bot.caching.get(
table="events",
guild_id=interaction.guild_id,
msg_id=interaction.message.id,
channel_id=interaction.channel.id,
)
categories = json.loads(records[0]["categories"])
embed = interaction.message.embeds[0]
remove_from = None
state = "added"
state_msgs = {
"added": f"Added to category: **{self.category_name}**",
"removed": f"Removed from category: **{self.category_name}**",
"moved": f"Moved to category: **{self.category_name}**",
}
guild = self.view.bot.get_guild(interaction.guild_id)
if interaction.user.id in categories[self.category_name]["members"]:
# If removing
categories[self.category_name]["members"].remove(interaction.user.id)
embed = await self.refresh_embed_field(
guild,
categories[self.category_name]["members"],
embed,
self.category_name,
categories[self.category_name]["member_cap"],
)
state = "removed"
else: # If adding
for category, data in categories.items():
if category == self.category_name:
if data["member_cap"] and data["member_cap"] <= len(data["members"]):
return await interaction.response.send_message("This category is full!", ephemeral=True)
elif records[0]["permitted_roles"] and not any(
role_id in [role.id for role in interaction.user.roles]
for role_id in records[0]["permitted_roles"]
):
return await interaction.response.send_message(
"You do not have permission to sign up to this event.",
ephemeral=True,
)
else:
categories[self.category_name]["members"].append(interaction.user.id)
embed = await self.refresh_embed_field(
guild,
categories[self.category_name]["members"],
embed,
self.category_name,
categories[self.category_name]["member_cap"],
)
for (
category,
data,
) in categories.items(): # Check if user is already in a team
if interaction.user.id in data["members"] and category != self.category_name:
state = "moved"
categories[category]["members"].remove(interaction.user.id)
embed = await self.refresh_embed_field(
guild,
categories[category]["members"],
embed,
category,
categories[category]["member_cap"],
)
break
async with self.view.bot.pool.acquire() as con:
await con.execute(
"""
UPDATE events SET categories = $1 WHERE guild_id = $2 AND entry_id = $3""",
json.dumps(categories),
guild.id,
self.entry_id,
)
await self.view.bot.caching.refresh(table="events", guild_id=guild.id)
try:
if embed.to_dict() != interaction.message.embeds[0]:
webhook = interaction.followup
await interaction.response.edit_message(embed=embed)
await webhook.send(state_msgs[state], ephemeral=True)
else:
await interaction.response.send_message(state_msgs[state], ephemeral=True)
except discord.Forbidden:
await interaction.response.send_message(
"Failed to register event due to a permissions issue. Please contact an administrator!",
ephemeral=True,
)
class EditMainView(discord.ui.View):
"""Main View for edit menu"""
def __init__(self, ctx):
super().__init__()
self.value = None
self.ctx = ctx
async def interaction_check(self, interaction: discord.Interaction) -> bool:
return self.ctx.author.id == interaction.user.id
@discord.ui.button(emoji="#️⃣", label="Title", style=discord.ButtonStyle.blurple, row=0)
async def title(self, button: discord.ui.Button, interaction: discord.Interaction):
self.value = "title"
await interaction.response.defer()
self.stop()
@discord.ui.button(emoji="📚", label="Description", style=discord.ButtonStyle.blurple, row=0)
async def desc(self, button: discord.ui.Button, interaction: discord.Interaction):
self.value = "description"
await interaction.response.defer()
self.stop()
@discord.ui.button(emoji="🕘", label="Timestamp", style=discord.ButtonStyle.blurple, row=0)
async def timestamp(self, button: discord.ui.Button, interaction: discord.Interaction):
self.value = "timestamp"
await interaction.response.defer()
self.stop()
@discord.ui.button(emoji="➕", label="Category", style=discord.ButtonStyle.green, row=1)
async def add_category(self, button: discord.ui.Button, interaction: discord.Interaction):
self.value = "add_category"
await interaction.response.defer()
self.stop()
@discord.ui.button(emoji="➖", label="Category", style=discord.ButtonStyle.red, row=1)
async def del_category(self, button: discord.ui.Button, interaction: discord.Interaction):
self.value = "del_category"
await interaction.response.defer()
self.stop()
@discord.ui.button(emoji="🗑️", label="Delete", style=discord.ButtonStyle.red, row=1)
async def delete(self, button: discord.ui.Button, interaction: discord.Interaction):
self.value = "delete"
await interaction.response.defer()
self.stop()
@discord.ui.button(emoji="🚪", label="Exit", style=discord.ButtonStyle.grey, row=2)
async def exit_menu(self, button: discord.ui.Button, interaction: discord.Interaction):
self.value = "exit"
await interaction.response.defer()
self.stop()
class Events(commands.Cog):
"""
Create and manage events that users can sign up to.
"""
def __init__(self, bot: SnedBot):
self.bot = bot
self.button_styles = {
"Blurple": discord.ButtonStyle.primary,
"Grey": discord.ButtonStyle.secondary,
"Green": discord.ButtonStyle.success,
"Red": discord.ButtonStyle.danger,
}
async def cog_check(self, ctx):
return await ctx.bot.custom_checks.has_permissions(
ctx, "events"
) or await ctx.bot.custom_checks.has_permissions(ctx, "mod_permitted")
@commands.Cog.listener()
async def on_ready(self):
await self.events_init()
async def events_init(self):
"""Re-acquire all persistent buttons"""
await self.bot.wait_until_ready()
logger.info("Adding persistent views to events...")
async with self.bot.pool.acquire() as con:
records = await con.fetch("""SELECT * FROM events""")
add_to_persistent_views = {}
for record in records:
for key, data in json.loads(record.get("categories")).items():
button = SignUpCategoryButton(
record.get("entry_id"),
key,
discord.PartialEmoji.from_str(data["emoji"]),
style=self.button_styles[data["buttonstyle"]],
label=data["buttonlabel"],
)
if record.get("msg_id") not in add_to_persistent_views.keys():
add_to_persistent_views[record.get("msg_id")] = [button]
else:
add_to_persistent_views[record.get("msg_id")].append(button)
for msg_id, buttons in add_to_persistent_views.items():
self.bot.add_view(PersistentEventView(self.bot, buttons), message_id=msg_id)
logger.info("Events ready!")
@commands.Cog.listener()
async def on_event_timer_complete(self, timer):
"""Event expiry"""
entry_id = timer.notes
record = await self.bot.caching.get(table="events", guild_id=timer.guild_id, entry_id=entry_id)
guild = self.bot.get_guild(timer.guild_id)
channel = guild.get_channel(timer.channel_id)
if guild and channel and record:
try:
message = await channel.fetch_message(record[0]["msg_id"])
except discord.NotFound:
return
else:
await message.edit(view=None)
paginator = commands.Paginator(prefix="", suffix="")
paginator.add_line(f"Event **'{message.embeds[0].title}'** is starting now!\n")
for category, data in json.loads(record[0]["categories"]).items():
members = [(guild.get_member(member_id)) for member_id in data["members"]]
members = list(filter(None, members))
paginator.add_line(f"**{category}: {', '.join([member.mention for member in members])}**")
async with self.bot.pool.acquire() as con:
await con.execute(
"""DELETE FROM events WHERE guild_id = $1 AND entry_id = $2""",
guild.id,
entry_id,
)
await self.bot.caching.refresh(table="events", guild_id=guild.id)
try:
for page in paginator.pages:
await channel.send(page)
except (discord.Forbidden, discord.HTTPException):
pass
async def add_category(
self,
ctx: commands.Context,
invoke_msg: discord.Message,
categories: dict = None,
first: bool = False,
loop: bool = False,
) -> dict:
"""
Helper function that interactively adds a new category to an event
invoke_msg - Message that this was invoked from, will be edited to reflect current state
categories - Dict of all current categories - do not pass this directly!!
first - If user should be greeted on start as a first-time user or not
loop - If true, will continue to add categories until the user interrupts
Raises UserInputError when invalid values are passed by a user.
"""
if not categories:
categories = {}
def idcheck(payload):
return payload.author == ctx.author and payload.channel.id == ctx.channel.id
def confirmemoji(reaction, user):
return reaction.message.id == invoke_msg.id and user.id == ctx.author.id
if first:
embed = discord.Embed(
title="🛠️ Event Categories setup",
description="Excellent! Now we will begin setting up the first category for this event! Please type the category's name below!\nMaximum **25** characters!\nExamples: `Red Team` or `Attackers`",
color=self.bot.embed_blue,
)
else:
embed = discord.Embed(
title="🛠️ Event Categories setup",
description="Type the name of the category below!\nMaximum **25** characters!",
color=self.bot.embed_blue,
)
await invoke_msg.edit(embed=embed, view=None)
message = await self.bot.wait_for("message", timeout=180.0, check=idcheck)
if len(message.content) <= 25 or message.content not in categories.keys():
category_name = message.content
elif message.content in categories.keys():
embed = discord.Embed(
title="❌ Error: Duplicate key",
description="You already have a category with the same name. Operation cancelled.",
color=self.bot.error_color,
)
await invoke_msg.edit(embed=embed)
raise UserInputError("Duplicate key!")
else:
embed = discord.Embed(
title="❌ Error: Title too long",
description="Category name cannot exceed **25** characters. Operation cancelled.",
color=self.bot.error_color,
)
await invoke_msg.edit(embed=embed)
raise UserInputError("Title too long!")
await message.delete()
embed = discord.Embed(
title="🛠️ Event Categories setup",
description="React **to this message** with the emoji you want to appear on the sign-up button! This can be any emoji, be it custom or Discord default!",
color=self.bot.embed_blue,
)
await invoke_msg.edit(embed=embed)
reaction, user = await self.bot.wait_for("reaction_add", timeout=60.0, check=confirmemoji)
emoji = reaction.emoji
await invoke_msg.clear_reactions()
view = components.AuthorOnlyView(ctx)
options = []
for name in self.button_styles.keys():
options.append(discord.SelectOption(label=name))
view.add_item(components.CustomSelect(placeholder="Select a style!", options=options))
embed = discord.Embed(
title="🛠️ Event Categories setup",
description="Select the style of the sign-up button!",
color=self.bot.embed_blue,
)
await invoke_msg.edit(embed=embed, view=view)
await view.wait()
if view.value:
buttonstyle = view.value["values"][0]
else:
raise asyncio.exceptions.TimeoutError
embed = discord.Embed(
title="🛠️ Event Categories setup",
description="Type in how many people should be able to join this category as a positive integer! If you do not wish to limit this, type `skip`.",
color=self.bot.embed_blue,
)
await invoke_msg.edit(embed=embed, view=None)
message = await self.bot.wait_for("message", timeout=180.0, check=idcheck)
await message.delete()
try:
member_cap = int(message.content) if message.content.lower() != "skip" else None
if member_cap and (member_cap <= 0 or member_cap > 100):
raise ValueError
except ValueError:
embed = discord.Embed(
title="❌ Error: Invalid value",
description="Value must be positive integer below 100. Operation cancelled.",
color=self.bot.error_color,
)
await invoke_msg.edit(embed=embed)
raise UserInputError("Invalid value for member_cap!")
categories[category_name] = {
"emoji": str(emoji),
"buttonlabel": category_name,
"buttonstyle": buttonstyle,
"member_cap": member_cap,
"members": [],
}
if len(categories) < 9 and loop:
embed = discord.Embed(
title="🛠️ Event Categories setup",
description="Category added! Would you like to add another?",
color=self.bot.embed_blue,
)
create_another = await ctx.confirm(embed=embed, delete_after=True)
if create_another == True:
return await self.add_category(ctx, invoke_msg, categories, first=False, loop=True)
elif create_another == False:
return categories
else:
raise asyncio.exception.TimeoutError
else:
return categories
@commands.group(
aliases=["events"],
help="Manages events.",
description="Lists all events created in this guild, if any. Subcommands allow you to remove or set additional ones.",
usage="buttonrole",
invoke_without_command=True,
case_insensitive=True,
)
@commands.guild_only()
async def event(self, ctx):
records = await self.bot.caching.get(table="events", guild_id=ctx.guild.id)
if records:
text = ""
for record in records:
text = f"{text}**{record['entry_id']}** - {ctx.guild.get_channel(record['channel_id']).mention}\n"
embed = discord.Embed(
title="📅 Events active in this server:",
description=text,
color=self.bot.embed_blue,
)
await ctx.send(embed=embed)
else:
embed = discord.Embed(
title="❌ Error: No active events",
description=f"There are no active events in this server. Create one with `{ctx.prefix}event create`",
color=self.bot.error_color,
)
await ctx.channel.send(embed=embed)
@event.command(
name="delete",
aliases=["del", "remove"],
help="Deletes an event by ID.",
description="Deletes an event via it's ID. You can get the ID via the `event` command.",
usage="event delete <ID>",
)
@commands.guild_only()
async def event_delete(self, ctx, id: str):
records = await self.bot.caching.get(table="events", guild_id=ctx.guild.id, entry_id=id)
if records:
channel = ctx.guild.get_channel(records[0]["channel_id"])
try:
message = await channel.fetch_message(records[0]["msg_id"]) if channel else None
if message:
await message.delete()
except discord.NotFound:
pass
async with self.bot.pool.acquire() as con:
await con.execute(
"""DELETE FROM events WHERE guild_id = $1 AND entry_id = $2""",
ctx.guild.id,
id,
)
await self.bot.caching.refresh(table="events", guild_id=ctx.guild.id)
embed = discord.Embed(
title="✅ Event deleted",
description="Event has been successfully deleted!",
color=self.bot.embed_green,
)
await ctx.channel.send(embed=embed)
else:
embed = discord.Embed(
title="❌ Error: Not found",
description="There is no event by that ID.",
color=self.bot.error_color,
)
await ctx.channel.send(embed=embed)
@event.command(
name="edit",
aliases=["modify"],
help="Modifies an event by ID.",
description="Modifies the event by the provided ID. You can get the ID via the `event` command.",
)
@commands.guild_only()
@commands.max_concurrency(1, per=commands.BucketType.guild, wait=False)
async def event_edit(self, ctx, id: str):
def check(payload):
return payload.author == ctx.author and payload.channel.id == ctx.channel.id
records = await self.bot.caching.get(table="events", guild_id=ctx.guild.id, entry_id=id)
if records:
channel = ctx.guild.get_channel(records[0]["channel_id"])
try:
event_message = await channel.fetch_message(records[0]["msg_id"])
except discord.NotFound:
async with self.bot.pool.acquire() as con:
await con.execute(
"""DELETE FROM events WHERE guild_id = $1 AND entry_id = $2""",
ctx.guild.id,
records[0]["entry_id"],
)
await self.bot.caching.refresh(table="events", guild_id=ctx.guild.id)
embed = discord.Embed(
title="❌ Error: Not found",
description="There is no event by that ID.",
color=self.bot.error_color,
)
await ctx.channel.send(embed=embed)
else:
event_embed = event_message.embeds[0]
embed = discord.Embed(
title=f"🛠️ Editing {event_embed.title}",
description="Please select below what you would like to change!",
color=self.bot.embed_blue,
)
view = EditMainView(ctx)
setup_msg = await ctx.send(embed=embed, view=view)
await view.wait()
if view.value:
if view.value == "title":
embed = discord.Embed(
title=f"🛠️ Editing {event_embed.title}",
description="Enter a new title! Please note that your title cannot exceed **100** characters.",
color=self.bot.embed_blue,
)
await setup_msg.edit(embed=embed, view=None)
message = await self.bot.wait_for("message", timeout=180.0, check=check)
if len(message.content) <= 100:
event_embed.title = message.content
else:
embed = discord.Embed(
title="❌ Error: Title too long",
description="Title cannot exceed **100** characters. Operation cancelled.",
color=self.bot.error_color,
)
await setup_msg.edit(embed=embed)
return
await message.delete()
await event_message.edit(embed=event_embed)
embed = discord.Embed(
title=f"🛠️ Editing {event_embed.title}",
description="✅ Title edited successfully!",
color=self.bot.embed_green,
)
await setup_msg.edit(embed=embed, view=None)
elif view.value == "description":
embed = discord.Embed(
title=f"🛠️ Editing {event_embed.title}",
description="Enter a new description! Please note it cannot exceed **2500** characters.",
color=self.bot.embed_blue,
)
await setup_msg.edit(embed=embed, view=None)
message = await self.bot.wait_for("message", timeout=300.0, check=check)
if len(message.content) <= 2500:
event_embed.description = message.content
else:
embed = discord.Embed(
title="❌ Error: Title too long",
description="Title cannot exceed **2500** characters. Operation cancelled.",
color=self.bot.error_color,
)
await setup_msg.edit(embed=embed)
return
await message.delete()
await event_message.edit(embed=event_embed)
embed = discord.Embed(
title=f"🛠️ Editing {event_embed.title}",
description="✅ Description edited successfully!",
color=self.bot.embed_green,
)
await setup_msg.edit(embed=embed, view=None)
elif view.value == "add_category":
categories = json.loads(records[0]["categories"])
if len(categories) <= 9:
try:
new_categories = await self.add_category(ctx, setup_msg)
categories.update(new_categories)
except UserInputError:
return
else:
first = list(new_categories.keys())[0] # lol
event_embed.add_field(
name=f"{new_categories[first]['buttonlabel']} (0/{new_categories[first]['member_cap']})",
value="-",
inline=True,
)
buttons = []
for category, data in categories.items():
button = SignUpCategoryButton(
entry_id=records[0]["entry_id"],
category_name=category,
emoji=discord.PartialEmoji.from_str(data["emoji"]),
style=self.button_styles[data["buttonstyle"]],
label=data["buttonlabel"],
)
buttons.append(button)
event_view = PersistentEventView(self.bot, buttons)
await event_message.edit(embed=event_embed, view=event_view)
await self.bot.caching.execute(
"""UPDATE events SET categories = $1 WHERE guild_id = $2 AND entry_id = $3""",
json.dumps(categories),
ctx.guild.id,
records[0]["entry_id"],
)
embed = discord.Embed(
title=f"🛠️ Editing {event_embed.title}",
description="✅ Category added!",
color=self.bot.embed_green,
)
await setup_msg.edit(embed=embed, view=None)
else:
embed = discord.Embed(
title="❌ Error: Category limit exceeded",
description="The amount of categories cannot exceed **9**. Operation cancelled.",
color=self.bot.error_color,
)
await setup_msg.edit(embed=embed, view=None)
return
elif view.value == "del_category":
categories = json.loads(records[0]["categories"])
if len(categories) > 1:
view = components.AuthorOnlyView(ctx)
options = []
for category in categories:
options.append(discord.SelectOption(label=category))
view.add_item(components.CustomSelect(placeholder="Select a category!", options=options))
embed = discord.Embed(
title=f"🛠️ Editing {event_embed.title}",
description="Select which category you would like to delete! Please note this will also remove all users who signed up for this category.",
color=self.bot.embed_blue,
)
await setup_msg.edit(embed=embed, view=view)
await view.wait()
value = view.value["values"][0] if view.value else None
if value:
categories.pop(value)
for i, field in enumerate(event_embed.fields):
if field.name.startswith(value):
event_embed.remove_field(i)
buttons = []
for category, data in categories.items():
button = SignUpCategoryButton(
entry_id=records[0]["entry_id"],
category_name=category,
emoji=discord.PartialEmoji.from_str(data["emoji"]),
style=self.button_styles[data["buttonstyle"]],
label=data["buttonlabel"],
)
buttons.append(button)
event_view = PersistentEventView(self.bot, buttons)
await event_message.edit(embed=event_embed, view=event_view)
await self.bot.caching.execute(
"""UPDATE events SET categories = $1 WHERE guild_id = $2 AND entry_id = $3""",
json.dumps(categories),
ctx.guild.id,
records[0]["entry_id"],
)
embed = discord.Embed(
title=f"🛠️ Editing {event_embed.title}",
description="✅ Category removed!",
color=self.bot.embed_green,
)
await setup_msg.edit(embed=embed, view=None)
else:
raise asyncio.exceptions.TimeoutError
else:
embed = discord.Embed(
title="❌ Error: Too few categories",
description="An event needs to have at least **1** category. Operation cancelled.",
color=self.bot.error_color,
)
await setup_msg.edit(embed=embed, view=None)
return
elif view.value == "delete":
await event_message.delete()
async with self.bot.pool.acquire() as con:
await con.execute(
"""DELETE FROM events WHERE guild_id = $1 AND entry_id = $2""",
ctx.guild.id,
records[0]["entry_id"],
)
await self.bot.caching.refresh(table="events", guild_id=ctx.guild.id)
embed = discord.Embed(
title=f"🛠️ Editing {event_embed.title}",
description="✅ Event deleted!",
color=self.bot.embed_green,
)
await setup_msg.edit(embed=embed, view=None)
elif view.value == "timestamp":
embed = discord.Embed(
title=f"🛠️ Editing {event_embed.title}",
description="""Type in a new end date! Formatting help:
**Absolute:**
`YYYY-MM-dd hh:mm`
`YYYY-MM-dd`
**Note:** Absolute times must be in UTC. [This website](https://www.timeanddate.com/worldclock/timezone/utc) may be of assistance.
**Relative:**
Examples:
`in 5 days`
`1 week`
`2M`
For more information about time and date formatting, please refer to the [documentation](https://sned.hypersden.com/docs/modules/reminders.html).
""",
color=self.bot.embed_blue,
)
await setup_msg.edit(embed=embed, view=None)
message = await self.bot.wait_for("message", timeout=300.0, check=check)
try:
new_expiry, string = await self.bot.get_cog("Timers").converttime(message.content)
except ValueError as error:
embed = discord.Embed(
title="❌ Error: Date formatting error",
description=f"Failed reading date. Operation cancelled.\n**Error:** ```{error}```",
color=self.bot.error_color,
)
await setup_msg.edit(embed=embed)
return
else:
await message.delete()
async with self.bot.pool.acquire() as con:
timer_records = await con.fetch(
"""
SELECT id FROM timers
WHERE event = 'event' AND guild_id = $1 AND channel_id = $2 AND notes = $3""",
ctx.guild.id,
records[0]["channel_id"],
str(records[0]["entry_id"]),
)
if timer_records:
await self.bot.get_cog("Timers").update_timer(
new_expiry, timer_records[0].get("id"), ctx.guild.id
)
for i, field in enumerate(event_embed.fields):
if field.name == "Event start":
insert_at = i
break
event_embed.insert_field_at(
insert_at,
name="Event start",
value=f"{discord.utils.format_dt(new_expiry, style='F')} ({discord.utils.format_dt(new_expiry, style='R')})",
)
event_embed.remove_field(insert_at + 1)
await event_message.edit(embed=event_embed)
embed = discord.Embed(
title=f"🛠️ Editing {event_embed.title}",
description="✅ Timestamp updated!",
color=self.bot.embed_green,
)
await setup_msg.edit(embed=embed)
else:
await setup_msg.delete()
else:
raise asyncio.exceptions.TimeoutError
else:
embed = discord.Embed(
title="❌ Error: Not found",
description="There is no event by that ID.",
color=self.bot.error_color,
)
await ctx.channel.send(embed=embed)
@event.command(
name="create",
aliases=["new", "setup", "add"],
help="Initializes setup to create and schedule an event.",
description="Initializes a setup to help you add a new event. Takes no arguments.",
usage="event create",
)
@commands.guild_only()
@commands.max_concurrency(1, per=commands.BucketType.guild, wait=False)
async def event_setup(self, ctx):
"""
Here is where end-users would set up an event for their server
"""
records = await self.bot.caching.get(table="events", guild_id=ctx.guild.id)
if records and len(records) >= 10:
embed = discord.Embed(
title="❌ Error: Too many events",
description="A server can only have up to **10** running events at a time.",
color=self.bot.error_color,
)
await ctx.channel.send(embed=embed)
return
def idcheck(payload):
return payload.author == ctx.author and payload.channel.id == ctx.channel.id
def confirmemoji(reaction, user):
return reaction.message.id == setup_msg.id and user.id == ctx.author.id
options = []
for channel in ctx.guild.channels:
if channel.type in [discord.ChannelType.text, discord.ChannelType.news]:
options.append(discord.SelectOption(label=f"#{channel.name}", value=channel.id))
embed = discord.Embed(
title="🛠️ Event setup",
description="Please specify the channel where you want the event message to be sent!",
color=self.bot.embed_blue,
)
value, asked, setup_msg = await components.select_or_ask(
ctx, options=options, placeholder="Select a channel", embed=embed
)
if value and not asked:
event_channel = ctx.guild.get_channel(int(value["values"][0]))
elif value and asked:
try:
event_channel = await commands.GuildChannelConverter().convert(ctx, value)
if event_channel.type not in [
discord.ChannelType.news,
discord.ChannelType.text,
]:
embed = discord.Embed(
title="❌ Error: Invalid channel",
description="Channel must be of type `text` or `news`. Operation cancelled.",
color=self.bot.error_color,
)
await setup_msg.edit(embed=embed)
return
except commands.ChannelNotFound:
embed = discord.Embed(
title="❌ Error: Channel not found.",
description="Unable to locate channel. Operation cancelled.",
color=self.bot.error_color,
)
await setup_msg.edit(embed=embed)
return
else:
raise asyncio.exceptions.TimeoutError
embed = discord.Embed(
title="🛠️ Event setup",
description="What is the title of the event? Type it below! Please note that your title cannot exceed **100** characters.",
color=self.bot.embed_blue,
)
await setup_msg.edit(embed=embed, view=None)
message = await self.bot.wait_for("message", timeout=180.0, check=idcheck)
if len(message.content) <= 100:
event_title = message.content
else:
embed = discord.Embed(
title="❌ Error: Title too long",
description="Title cannot exceed **100** characters. Operation cancelled.",
color=self.bot.error_color,
)
await setup_msg.edit(embed=embed)
return
await message.delete()
embed = discord.Embed(
title="🛠️ Event setup",
description="Great! Now type the description of the event down below! Please note there is a maximum length of **2500** characters.",
color=self.bot.embed_blue,
)
await setup_msg.edit(embed=embed, view=None)
message = await self.bot.wait_for("message", timeout=300.0, check=idcheck)
if len(message.content) <= 2500:
event_description = message.content
else:
embed = discord.Embed(
title="❌ Error: Description too long",
description="Description cannot exceed **2500** characters. Operation cancelled.",
color=self.bot.error_color,
)
await setup_msg.edit(embed=embed)
return
await message.delete()
embed = discord.Embed(
title="🛠️ Event setup",
description="""When should the event end? Type it in below in one of the following formats:
**Absolute:**
`YYYY-MM-dd hh:mm`
`YYYY-MM-dd`
**Note:** Absolute times must be in UTC. [This website](https://www.timeanddate.com/worldclock/timezone/utc) may be of assistance.
**Relative:**
Examples:
`in 5 days`
`1 week`
`2M`
For more information about time and date formatting, please refer to the [documentation](https://sned.hypersden.com/docs/modules/reminders.html).
""",
color=self.bot.embed_blue,
)
await setup_msg.edit(embed=embed, view=None)
message = await self.bot.wait_for("message", timeout=300.0, check=idcheck)
try:
event_expiry, string = await self.bot.get_cog("Timers").converttime(message.content)
except ValueError as error:
embed = discord.Embed(
title="❌ Error: Date formatting error",
description=f"Failed reading date. Operation cancelled.\n**Error:** ```{error}```",
color=self.bot.error_color,
)
await setup_msg.edit(embed=embed)
return
await message.delete()
"""Category adding"""
try:
categories = await self.add_category(ctx, setup_msg, first=True, loop=True)
except UserInputError:
return
"""Role restrictions adding"""
role_options = []
for role in ctx.guild.roles:
if role.name != "@everyone":
rolename = role.name if len(role.name) <= 25 else role.name[:20] + "..."
role_options.append(discord.SelectOption(label=rolename, value=role.id))
event_permitted_roles = None
class RolesView(discord.ui.View):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.skipping = False
self.data = None
async def interaction_check(self, interaction: discord.Interaction) -> bool:
return ctx.author.id == interaction.user.id
max_values = 5 if len(role_options) > 5 else len(role_options)
@discord.ui.select(
placeholder="Select some roles!",
min_values=1,
max_values=max_values,