-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcog.py
1249 lines (1114 loc) · 48.5 KB
/
cog.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 constants
import nextcord
import asyncio
from typing import Union
from utils import sheets_constants
from nextcord.ext import commands
from utils import discord_utils, logging_utils, command_predicates
"""
Discord Channel management module. Bundle of all discord functions and commands related to managing a specific discord channel and managing it.
Code copied/adapted from DenverCoder1's professor-vector-discord-bot repo - https://github.com/DenverCoder1/professor-vector-discord-bot
"""
class DiscordChannelManagementCog(commands.Cog, name="Discord Channel Management"):
"""
For managing channels on discord.
"""
def __init__(self, bot):
self.bot = bot
####################
# CHANNEL COMMANDS #
####################
@command_predicates.is_verified()
@commands.command(name="movechan", aliases=["movechannel"])
async def movechannel(
self, ctx, category_name: str, *args: Union[nextcord.TextChannel, str]
):
"""Command to move channels to category with given name
Permission Category : Verified Roles only.
Usage: `~movechannel "CatA"` (Moves current channel to CatA)
Usage: `~movechannel "CatA" #chan1 "chan2" "chan3"` (Moves all listed channels to CatA. Note - This does not move current channel unless listed)
Usage: `~movechannel "CatA" all` (Moves all channels in current category to CatA.)
Note that channels may be mentioned or named, but a channel is named "all", then it must be mentioned to avoid issues.
"""
await logging_utils.log_command(
"movechannel", ctx.guild, ctx.channel, ctx.author
)
embed = discord_utils.create_embed()
# get current channel
channel = ctx.channel
# get new category
new_category = await discord_utils.find_category(ctx, category_name)
if new_category is None:
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Could not find category `{category_name}`",
)
await ctx.send(embed=embed)
return
channelstomove = []
# No arg given. Move only current channel
if len(args) == 0:
channelstomove.append(channel)
# Only one arg given, "All". Move all channels in category
elif len(args) == 1 and args[0] == "all":
for chan in ctx.channel.category.channels:
channelstomove.append(chan)
# One or more args given. All processed as channels.
else:
# Process as N channels then add
for unclean_chan in args:
if isinstance(unclean_chan, nextcord.TextChannel):
chan = unclean_chan
else:
embed.add_field(
name="Error Finding Channel!",
value=f"Could not find channel `{unclean_chan}`. Perhaps check your spelling and try again.",
inline=False,
)
continue
channelstomove.append(chan)
channels_moved = []
for chan in channelstomove:
if chan.category == new_category:
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Channel {chan.mention} is already in Category `{new_category.name}`.",
inline=False,
)
continue
if discord_utils.category_is_full(new_category):
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Could not move channel {chan.mention}. Category `{new_category.name}` is already full, max limit is 50 channels.",
inline=False,
)
continue
# Move the channels
try:
await chan.edit(category=new_category)
channels_moved.append(chan)
except nextcord.Forbidden:
embed.insert_field_at(
0,
name=f"{constants.FAILED}!",
value=f"Forbidden! Have you checked if the bot has the required permisisons?",
inline=False,
)
await ctx.send(embed=embed)
return
if len(channels_moved) < 1:
embed.insert_field_at(
0,
name="Complete!",
value=f"Did not move any channels to `{new_category.name}`.",
inline=False,
)
else:
embed.add_field(
name=f"{constants.SUCCESS}!",
value=f"Moved these channels to `{new_category.name}` : {', '.join([chan.mention for chan in channels_moved])}",
inline=False,
)
embeds = discord_utils.split_embed(embed)
msgs = []
for e in embeds:
msgs.append(await ctx.send(embed=e))
@command_predicates.is_verified()
@commands.command(name="renamechan", aliases=["renamechannel", "renamethread"])
async def renamechannel(
self,
ctx,
chan_a: Union[nextcord.TextChannel, nextcord.Thread, str],
chan_b: Union[nextcord.TextChannel, nextcord.Thread, str] = "",
):
"""Changes current channel name to whatever is asked.
Permission Category : Verified Roles only.
Usage: `~renamechannel newname` (Renames current channel)
Usage: `~renamechan #old-chan newname`
Can rename threads as well.
Note that if you use more than 2 channel renaming commands quickly, Discord automatically stops any more channel-name changes for 10 more minutes.
Those channels will have to be renamed manually, or wait for the full 10 mins.
"""
# log command in console
await logging_utils.log_command(
"renamechannel", ctx.guild, ctx.channel, ctx.author
)
embed = discord_utils.create_embed()
if chan_b == "":
old_channel_name = ctx.channel.name
new_channel_name = chan_a
else:
old_channel_name = chan_a
new_channel_name = chan_b
old_channel = await discord_utils.find_chan_or_thread(ctx, old_channel_name)
if old_channel is None:
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Channel `{old_channel_name}` was not found.",
)
await ctx.send(embed=embed)
return
# If user managed to tag a channel name instead of typing
if not isinstance(new_channel_name, str):
new_channel_name = new_channel_name.name
try:
# rename channel
await old_channel.edit(name=new_channel_name)
except nextcord.Forbidden:
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Forbidden! Have you checked if the bot has the required permisisons?",
)
await ctx.send(embed=embed)
return
embed.add_field(
name=f"{constants.SUCCESS}!",
value=f"Renamed `{old_channel_name}` to `{new_channel_name}`: {ctx.channel.mention}!",
inline=False,
)
await ctx.send(embed=embed)
@command_predicates.is_verified()
@commands.command(name="makethread", aliases=["createthread"])
async def createthread(self, ctx, name: str):
"""Command to create thread in same category with given name
Permission Category : Verified Roles only.
Usage: `~createthread new-thread-name`
"""
# log command in console
await logging_utils.log_command(
"createthread", ctx.guild, ctx.channel, ctx.author
)
embed = discord_utils.create_embed()
if await discord_utils.is_thread(ctx.channel):
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Invalid! You cannot make a thread from inside another thread!",
)
await ctx.send(embed=embed)
return
channel = await discord_utils.createthreadgeneric(
ctx, ctx.message, ctx.channel, name
)
# Send status (success or fail)
if channel:
embed.add_field(
name=f"{constants.SUCCESS}",
value=f"Created channel {channel.mention} in `{channel.category.name}`!",
)
else:
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Forbidden! Have you checked if the bot has the required permisisons?",
)
await ctx.send(embed=embed)
@command_predicates.is_verified()
@commands.command(
name="createchan", aliases=["makechannel", "makechan", "createchannel"]
)
async def createchannel(
self, ctx, name: str, category_arg: Union[nextcord.CategoryChannel, str] = ""
):
"""Command to create channel in a given category. If given no category name, it defaults to current category.
Permission Category : Verified Roles only.
Usage: `~createchannel new-channel-name` (creates new-channel-name in the category it's called from)
Usage: `~createchannel new-channel-name "Category Name"`
"""
# log command in console
await logging_utils.log_command(
"createchannel", ctx.guild, ctx.channel, ctx.author
)
embed = discord_utils.create_embed()
if category_arg == "":
category = ctx.channel.category
else:
category = category_arg
category = await discord_utils.find_category(ctx, category)
if category is None:
if category_arg != "":
embed.add_field(
name="ERROR: Cannot find category",
value=f"Sorry, I cannot find a category with name {category_arg}. "
f"Please make sure the spelling and capitalization are correct!",
inline=False,
)
await ctx.send(embed=embed)
return
else:
embed.add_field(
name="ERROR: Cannot find category",
value=f"Sorry, I cannot find the category for the channel {ctx.channel.mention}. "
f"Are you sure you are in a category?",
inline=False,
)
await ctx.send(embed=embed)
return
# Category channel limit
if discord_utils.category_is_full(category):
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Category `{category.name}` is already full, max limit is 50 channels.",
)
await ctx.send(embed=embed)
return None
# Server channel limit
if discord_utils.server_is_full(category.guild):
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Guild `{category.guild.name}` is completely full! Max limit is 500 channels/categories/... Please contact a mod for help.",
)
# reply to user
await ctx.send(embed=embed)
return None
channel = await discord_utils.createchannelgeneric(ctx.guild, category, name)
# Send status (success or fail)
if channel:
embed.add_field(
name=f"{constants.SUCCESS}",
value=f"Created channel {channel.mention} in `{channel.category.name}`!",
)
else:
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Forbidden! Have you checked if the bot has the required permisisons?",
)
await ctx.send(embed=embed)
@command_predicates.is_verified()
@commands.command(name="clonechan", aliases=["clonechannel", "chanclone"])
async def clonechannel(
self,
ctx,
chan_a: Union[nextcord.TextChannel, str],
chan_b: str = "",
origRoleorUser: Union[nextcord.Role, nextcord.Member, str] = None,
targetRoleorUser: Union[nextcord.Role, nextcord.Member, str] = None,
):
"""Command to create channel in same category with given name.
If user/role is specified, then syncs permissions as well.
The channel created is just below the channel being cloned
If making role/user changes, both old channel and new channel is necessary.
Permission Category : Verified Roles only.
Usage: `~clonechannel #channel-to-clone new-channel-name`
Usage: `~clonechannel new-channel-name` (Clones current channel)
Usage: `~clonechannel #chan1 chan2 @roleA @userB` (clones then syncs permission of A in chan1 with B in chan2)
"""
# log command in console
await logging_utils.log_command(
"clonechannel", ctx.guild, ctx.channel, ctx.author
)
embed = discord_utils.create_embed()
if chan_b == "":
old_channel_name = ctx.channel
new_channel_name = chan_a
else:
old_channel_name = chan_a
new_channel_name = chan_b
old_channel = await discord_utils.find_chan_or_thread(ctx, old_channel_name)
if old_channel is None:
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Channel `{old_channel_name}` was not found.",
)
await ctx.send(embed=embed)
return
if isinstance(origRoleorUser, str):
origUser = await discord_utils.find_user(ctx, origUser)
if origUser is None:
origRole = await discord_utils.find_role(ctx, origRoleorUser)
if origRole is None:
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Role/User `{origRoleorUser}` does not exist. Please use @ to tag them.",
)
await ctx.send(embed=embed)
return
else:
origRoleorUser = origRole
else:
origRoleorUser = origUser
if isinstance(targetRoleorUser, str):
targetUser = await discord_utils.find_user(ctx, targetRoleorUser)
if targetUser is None:
targetRole = await discord_utils.find_role(ctx, targetRoleorUser)
if targetRole is None:
try:
targetRoleorUser = await ctx.guild.create_role(
name=targetRoleorUser
)
await targetRoleorUser.edit(mentionable=True)
embed.add_field(
name=f"Created role {targetRoleorUser}",
value=f"Could not find role `{targetRoleorUser}`, so I created it.",
inline=False,
)
except nextcord.Forbidden:
embed.add_field(
name=f"{constants.FAILED}!",
value=f"I couldn't find role `{targetRoleorUser}`, so I tried to make it. But I don't have "
f"permission to add a role in this server. Do I have the `add_roles` permission?",
inline=False,
)
await ctx.send(embed=embed)
return
else:
targetRoleorUser = targetRole
else:
targetRoleorUser = targetUser
# get guild and category
guild = old_channel.guild
category = old_channel.category
# Category limit
if discord_utils.category_is_full(category):
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Category `{category.name}` is already full, max limit is 50 channels.",
inline=False,
)
await ctx.send(embed=embed)
return
# Server channel limit
if discord_utils.server_is_full(guild):
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Guild `{guild.name}` is completely full! Max limit is 500 channels/categories/... Please contact a mod for help.",
)
# reply to user
await ctx.send(embed=embed)
return
try:
# create channel
new_channel = await guild.create_text_channel(
new_channel_name, category=category, overwrites=old_channel.overwrites
)
await new_channel.edit(position=old_channel.position + 1)
except nextcord.Forbidden:
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Forbidden! Have you checked if the bot has the required permisisons?",
inline=False,
)
await ctx.send(embed=embed)
return
embed.add_field(
name=f"{constants.SUCCESS}!",
value=f"Created channel {new_channel.mention} in `{category}` as a clone of {old_channel.mention}!",
inline=False,
)
# If roles exist, add them.
if origRoleorUser and targetRoleorUser:
overwrites = old_channel.overwrites
if overwrites.get(origRoleorUser) is None:
embed.add_field(
name=f"{constants.FAILED}!",
value=f"{origRoleorUser.mention} is not in {old_channel.mention} overwrites. Skipping!",
inline=False,
)
else:
try:
overwrite = overwrites.get(origRoleorUser)
await new_channel.set_permissions(origRoleorUser, overwrite=None)
await new_channel.set_permissions(
targetRoleorUser, overwrite=overwrite
)
embed.add_field(
name=f"{constants.SUCCESS}!",
value=f"Synced permissions of {origRoleorUser.mention} in {old_channel.mention} with that of {targetRoleorUser.mention} in {new_channel.mention}.",
inline=False,
)
except nextcord.Forbidden:
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Forbidden! Have you checked if the bot has the required permisisons?",
inline=False,
)
await ctx.send(embed=embed)
return
await ctx.send(embed=embed)
@command_predicates.is_verified()
@commands.command(name="shiftchan", aliases=["shiftchannel"])
async def shiftchannel(
self,
ctx,
chan_a_name: Union[nextcord.TextChannel, str],
chan_b_name: Union[nextcord.TextChannel, str] = "",
):
"""Shifts a channel to below another channel in the same category.
Does not work for Voice Channels, just Text Channels.
Channels may be mentioned or named, but better to mention (in case of multiple channels with same name).
Note : Use "0" or "top" instead to say "Top of the category". But if a channel in the server is named #top or #0 then the respective argument wont work
Permission Category : Verified Roles only.
Usage: `~shiftchan #chana #chanb` (Shifts Chan A to just below Chan B)
Usage: `~shiftchan "chanb"` (Shifts the current channel to just below Chan B)
Usage: `~shiftchan "chana" top` (Shifts ChanA to top of category)
Usage: `~shiftchan 0` (Shifts the current channel to top of category)
"""
await logging_utils.log_command(
"shiftchannel", ctx.guild, ctx.channel, ctx.author
)
embed = discord_utils.create_embed()
pos_to_shift_to = -1
chan_to_shift = None
if chan_b_name == "" and chan_a_name in ["top", "0"]:
# Shift CurrChan to top
chan_to_shift = ctx.channel
pos_to_shift_to = 0
elif chan_b_name in ["top", "0"]:
# Shift Chan A to top
chan_to_shift = chan_a_name
pos_to_shift_to = 0
else:
if chan_b_name == "":
# Shift CurrChan to ChanA
chan_to_shift = ctx.channel
chan_shifting_to = chan_a_name
else:
# Shift ChanA to ChanB
chan_to_shift = chan_a_name
chan_shifting_to = chan_b_name
if not isinstance(chan_to_shift, nextcord.TextChannel):
embed.add_field(
name=f"{constants.FAILED}",
value=f"I cannot find channel `{chan_to_shift}`. Perhaps check your spelling and try again.",
)
await ctx.send(embed=embed)
return
# Not top, so position needs to be given
if pos_to_shift_to == -1:
if not isinstance(chan_shifting_to, nextcord.TextChannel):
embed.add_field(
name=f"{constants.FAILED}",
value=f"I cannot find channel `{chan_shifting_to}`. Perhaps check your spelling and try again.",
)
await ctx.send(embed=embed)
return
if chan_shifting_to.category != chan_to_shift.category:
# Different categories for channel to shift to
embed.add_field(
name=f"{constants.FAILED}",
value=f"The channel to be shifted {chan_to_shift.mention} is in category `{chan_to_shift.category}` but it's trying to shift to channel {chan_shifting_to.mention}, which is in category `{chan_shifting_to.category}`"
f"\nUse `~movechan` to move the channel across categories first.",
)
await ctx.send(embed=embed)
return
# No errors
pos_to_shift_to = chan_shifting_to.position + 1
# Move channels
try:
await chan_to_shift.edit(position=pos_to_shift_to)
except nextcord.Forbidden:
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Forbidden! Have you checked if the bot has the required permisisons?",
)
await ctx.send(embed=embed)
return
if pos_to_shift_to == 0:
embed.add_field(
name=f"{constants.SUCCESS}!",
value=f"Succesfully moved channel {chan_to_shift.mention} to top of category {chan_to_shift.category}",
)
else:
embed.add_field(
name=f"{constants.SUCCESS}!",
value=f"Succesfully moved channel {chan_to_shift.mention} to just below {chan_shifting_to.mention}",
)
await ctx.send(embed=embed)
##########################
# VOICE CHANNEL COMMANDS #
##########################
@command_predicates.is_verified()
@commands.command(name="renamevc", aliases=["renamevoicechan", "renamevoice"])
async def renamevoicechan(self, ctx, new_name: str):
"""Command to rename the Voice Channel in which the user currently is
Permission Category : Verified Roles only.
Usage: `~renamevc "VC-Name"`
"""
# log command in console
await logging_utils.log_command(
"renamevoicechan", ctx.guild, ctx.channel, ctx.author
)
embed = discord_utils.create_embed()
voice_chan_list = ctx.guild.voice_channels
calling_user = ctx.author
voice_chan_to_rename = None
for vc in voice_chan_list:
if calling_user in vc.members:
voice_chan_to_rename = vc
break
if voice_chan_to_rename is None:
embed.add_field(
name=f"{constants.FAILED}!",
value=f"User {calling_user.mention} needs to be in a Voice Channel to use `~renamevc`!",
inline=False,
)
await ctx.send(embed=embed)
return
try:
oldvcname = voice_chan_to_rename.name
await voice_chan_to_rename.edit(name=new_name)
except nextcord.Forbidden:
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Forbidden! Have you checked if the bot has the required permisisons?",
)
await ctx.send(embed=embed)
return
embed.add_field(
name=f"{constants.SUCCESS}!",
value=f"Renamed voice channel `{oldvcname}` in category `{voice_chan_to_rename.category}` to {voice_chan_to_rename.mention}",
inline=False,
)
# reply to user
await ctx.send(embed=embed)
#####################
# CATEGORY COMMANDS #
#####################
@command_predicates.is_verified()
@commands.command(
name="sortcat",
aliases=["categorysort", "catsort", "sortcategory", "reorderchannels"],
)
async def categorysort(self, ctx, cat_name: str = ""):
"""Sort all channels in a category. Specifically for puzzle hunts, `solved-`, `backsolved-`, and `solvedish-`
prefixes will be put behind channels without a prefix.
Permission Category : Verified Roles only.
Usage: `~categorysort`
"""
await logging_utils.log_command(
"categorysort", ctx.guild, ctx.channel, ctx.author
)
embed = discord_utils.create_embed()
if cat_name == "":
category_to_sort_name = ctx.channel.category
else:
category_to_sort_name = cat_name
category = await discord_utils.find_category(ctx, category_to_sort_name)
if category is None:
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Error! The category named `{category_to_sort_name}` not found. Perhaps check your spelling and try again.",
)
# reply to user
await ctx.send(embed=embed)
return
channel_list = self.sort_channels(category.text_channels)
start_embed = discord_utils.create_embed()
start_embed.add_field(
name=f"Sort Started",
value=f"Your sort of category `{category.name}` has begun! "
f"This may take a while. If I run into any errors, I'll let you know.",
)
start_embed_msg = await ctx.send(embed=start_embed)
for idx, channel in enumerate(channel_list):
# Skip channels already in correct place.
if channel.position == idx:
continue
try:
await channel.edit(position=idx)
except nextcord.Forbidden:
if start_embed_msg:
await start_embed_msg.delete()
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Unable to sort `{channel.mention}`. Do I have the correct `manage_channel` positions?",
)
await ctx.send(embed=embed)
return
if start_embed_msg:
await start_embed_msg.delete()
embed.add_field(
name=f"{constants.SUCCESS}!",
value=f"Sorted the channels in `{category.name}`!",
)
await ctx.send(embed=embed)
def sort_channels(
self,
channel_list: list,
prefixes: list = sheets_constants.solved_prefixes,
) -> list:
"""Sort channels according to some prefixes"""
channel_list_sorted = sorted(channel_list, key=lambda x: x.name)
prefixes = [x + "-" for x in prefixes]
channel_list_prefixes = []
for prefix in prefixes:
channel_list_prefixes += list(
filter(lambda x: x.name.startswith(prefix), channel_list_sorted)
)
unsolved = channel_list_sorted
unsolved = list(filter(lambda x: x not in channel_list_prefixes, unsolved))
return unsolved + channel_list_prefixes
@command_predicates.is_verified()
@commands.command(name="renamecat", aliases=["renamecategory"])
async def renamecategory(self, ctx, cat_a_name: str, cat_b_name: str = ""):
"""Renames the given category to whatever is asked
Note that if you use more than 2 category renaming commands quickly, Discord automatically stops any more channel-name changes for 10 more minutes.
Those categories will have to be renamed manually, or wait for the full 10 mins.
Permission Category : Verified Roles only.
Usage: `~renamecat newname` (Changes current category name to newname)
Usage: `~renamecat "CatA" CatB` (Changes CatA name to CatB)
"""
# log command in console
await logging_utils.log_command(
"renamecategory", ctx.guild, ctx.channel, ctx.author
)
embed = discord_utils.create_embed()
if cat_b_name == "":
old_category_name = ctx.channel.category.name
new_category_name = cat_a_name
else:
old_category_name = cat_a_name
new_category_name = cat_b_name
old_category = await discord_utils.find_category(ctx, old_category_name)
if old_category is None:
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Error! The category named `{old_category_name}` not found. Perhaps check your spelling and try again.",
inline=False,
)
# reply to user
await ctx.send(embed=embed)
return
try:
# rename category
await old_category.edit(name=new_category_name)
except nextcord.Forbidden:
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Forbidden! Have you checked if the bot has the `manage_channels` permisisons?",
inline=False,
)
# reply to user
await ctx.send(embed=embed)
return
embed.add_field(
name=f"{constants.SUCCESS}!",
value=f"Renamed `{old_category_name}` to `{new_category_name}`!",
inline=False,
)
await ctx.send(embed=embed)
@command_predicates.is_trusted()
@commands.command(name="synccat", aliases=["synccategory", "catsync"])
async def synccategory(self, ctx, cat_name: str = ""):
"""Changes permissions of all channels in Current Category to be synced to Cat-permissions.
So any channel with different role permissions set up is reverted.
Permission Category : Trusted Roles only.
Usage: `~synccat` (Syncs current category)
Usage: `~synccat "CatA"` (Syncs given category)
"""
await logging_utils.log_command(
"synccategory", ctx.guild, ctx.channel, ctx.author
)
embed = discord_utils.create_embed()
if cat_name == "":
category_to_sync_name = ctx.channel.category
else:
category_to_sync_name = cat_name
category = await discord_utils.find_category(ctx, category_to_sync_name)
if category is None:
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Error! The category named `{category_to_sync_name}` not found. Perhaps check your spelling and try again.",
inline=False,
)
# reply to user
await ctx.send(embed=embed)
return
start_embed = discord_utils.create_embed()
start_embed.add_field(
name="Sync Started",
value=f"Your syncing of category `{category.name}`"
f" has begun! This may take a while. If I run into "
f"any errors, I'll let you know.",
inline=False,
)
start_msg = await ctx.send(embed=start_embed)
try:
for channel in category.channels:
await channel.edit(sync_permissions=True)
except nextcord.Forbidden:
if start_msg:
await start_msg.delete()
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Forbidden! Have you checked if the bot has the required permisisons?",
inline=False,
)
# reply to user
await ctx.send(embed=embed)
return
if start_msg:
await start_msg.delete()
embed.add_field(
name=f"{constants.SUCCESS}!",
value=f"All channels in category `{category.name}` successfully synced to Category!",
inline=False,
)
await ctx.send(embed=embed)
@command_predicates.is_verified()
@commands.command(
name="shiftcat", aliases=["shiftcategory", "movecategory", "movecat"]
)
async def shiftcategory(self, ctx, cat_a_name: str, cat_b_name: str = ""):
"""Shifts a category to below another category.
Note : Use "0" or "top" instead to say "Top of the server". But if a category in the server is named "top" or "0" then the respective argument wont work
Permission Category : Verified Roles only.
Usage: `~shiftcat "Category A" "Category B"` (Shifts Cat A to just below Cat B)
Usage: `~shiftcat "Category B"` (Shifts the current category to just below Category B)
Usage: `~shiftcat "Category A" 0` (Shifts Cat A to the top)
Usage: `~shiftcat top` (Shifts the current category to the top)
"""
await logging_utils.log_command(
"shiftcategory", ctx.guild, ctx.channel, ctx.author
)
embed = discord_utils.create_embed()
pos_to_shift_to = -1
cat_to_shift_name = None
if cat_b_name == "" and cat_a_name in ["top", "0"]:
# Shift Currcat to top
cat_to_shift_name = ctx.channel.category
pos_to_shift_to = 0
elif cat_b_name in ["top", "0"]:
# Shift cat A to top
cat_to_shift_name = cat_a_name
pos_to_shift_to = 0
else:
if cat_b_name == "":
# Shift Currcat to catA
cat_to_shift_name = ctx.channel.category
cat_shifting_to_name = cat_a_name
else:
# Shift catA to catB
cat_to_shift_name = cat_a_name
cat_shifting_to_name = cat_b_name
if cat_to_shift_name is None:
embed.add_field(
name=f"{constants.FAILED}",
value=f"The current channel {ctx.channel.mention} does not exist in a category I can move. Check `~help shiftcat`.",
inline=False,
)
await ctx.send(embed=embed)
return
cat_to_shift = await discord_utils.find_category(ctx, cat_to_shift_name)
if cat_to_shift is None:
embed.add_field(
name=f"{constants.FAILED}",
value=f"I cannot find category `{cat_to_shift_name}`. Perhaps check your spelling and try again.",
inline=False,
)
await ctx.send(embed=embed)
return
# Not top, so position needs to be given
if pos_to_shift_to == -1:
cat_shifting_to = await discord_utils.find_category(
ctx, cat_shifting_to_name
)
if cat_shifting_to is None:
embed.add_field(
name=f"{constants.FAILED}",
value=f"I cannot find category `{cat_shifting_to_name}`. Perhaps check your spelling and try again.",
inline=False,
)
await ctx.send(embed=embed)
return
pos_to_shift_to = cat_shifting_to.position + 1
try:
await cat_to_shift.edit(position=pos_to_shift_to)
except nextcord.Forbidden:
embed.add_field(
name=f"{constants.FAILED}",
value=f"I was unable to shift category `{cat_to_shift}`. Do I have the `manage_channels` permission?",
inline=False,
)
await ctx.send(embed=embed)
return
if pos_to_shift_to == 0:
embed.add_field(
name=f"{constants.SUCCESS}!",
value=f"Succesfully moved Category `{cat_to_shift}` to top of the server.",
inline=False,
)
else:
embed.add_field(
name=f"{constants.SUCCESS}!",
value=f"Succesfully moved Category `{cat_to_shift}` to just below Category `{cat_shifting_to}`",
inline=False,
)
await ctx.send(embed=embed)
@command_predicates.is_verified()
@commands.command(
name="clonecat",
aliases=["copycategory", "clonecategory", "copycat", "catclone"],
)
async def clonecategory(
self,
ctx,
origCatName: str,
targetCatName: str,
origRole: Union[nextcord.Role, str] = None,
targetRole: Union[nextcord.Role, str] = None,
):
"""Clones one category as another.
If roles are given, takes OrigRole's perms in OrigCat and clones them for Targetrole in TargetCat.
Creates targetCat if it doesn't exist already.
Create targetRole if it doesn't exist already (with same server permissions). See `~clonerole` for example.
Permission Category : Verified Roles only.
Usage: `~clonecategory "Category A" "Category B"` (Clones Cat A as Cat B)
Usage: `~clonecategory "Category A" "Category B" @RoleC @RoleD` (Clones Cat A as Cat B. Takes RoleC permission on Cat A, and replicates it with RoleD and Cat B)
"""
await logging_utils.log_command(
"clonecategory", ctx.guild, ctx.channel, ctx.author
)
embed = discord_utils.create_embed()
# Input parsing I guess
# First, make sure origCat exists
origCat = await discord_utils.find_category(ctx, origCatName)
if origCat is None:
embed.add_field(
name=f"{constants.FAILED}",
value=f"I cannot find category {origCatName}. Perhaps check your spelling and try again.",
inline=False,
)
await ctx.send(embed=embed)
return
# Either neither origRole nor targetRole are supplied, or both are. If XOR, that's a fail.
if (
origRole is not None
and targetRole is None
or origRole is None
and targetRole is not None
):
embed.add_field(
name=f"{constants.FAILED}",
value=f"Next time, please supply both `origRole` and `targetRole`, or neither.",
inline=False,
)
await ctx.send(embed=embed)
return
origRole_or_none = await discord_utils.find_role(ctx, origRole)
targetRole_or_none = await discord_utils.find_role(ctx, targetRole)
# If we have looped over all the roles and still can't find an origRole, then that's an error
if origRole_or_none is None and origRole is not None:
embed.add_field(
name=f"{constants.FAILED}",