-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoderation_utils.py
942 lines (842 loc) · 43.9 KB
/
moderation_utils.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
import math
import discord
from discord.ext import commands
import re
import asyncio
import pymorphy2
from datetime import datetime
from discord.ext.commands.core import has_permissions
from discord.ext.commands.errors import CheckFailure
from db import setup_db
def get_permissions_embed(ctx: commands.Context):
title_error = ":x: | Вам не хватает прав"
permissions_error = "Вам нужны следующие права:\n> Администратор"
permissions_error_embed = discord.Embed(
title=title_error, description=permissions_error, color=discord.Color.red())
permissions_error_embed.set_footer(
icon_url=ctx.author.avatar_url, text=str(ctx.author))
return permissions_error_embed
def get_error_embed(ctx: commands.Context):
embed = discord.Embed(title=":x: | Что-то пошло не так",
description="> Похоже, что возникла ошибка!", color=discord.Color.red())
embed.set_footer(icon_url=ctx.author.avatar_url, text=str(ctx.author))
return embed
# ------------------------------------------------------------------------------------------------------------------
def parse_time(time: str):
days = 0
hours = 0
minutes = 0
seconds = 0
if bool(re.match("(\d+)d$", time)):
days = int(time.split('d')[0])
elif bool(re.match("(\d+)h$", time)):
hours = int(time.split('h')[0])
elif bool(re.match("(\d+)m$", time)):
minutes = int(time.split('m')[0])
elif bool(re.match("(\d+)s$", time)):
seconds = int(time.split('s')[0])
elif bool(re.match("(\d+)d(\d+)h$", time)):
time = time.split('d')
days = int(time[0])
hours = int(time[1].split('h')[0])
elif bool(re.match("(\d+)d(\d+)m$", time)):
time = time.split('d')
days = int(time[0])
minutes = int(time[1].split('m')[0])
elif bool(re.match("(\d+)d(\d+)s$", time)):
time = time.split('d')
days = int(time[0])
seconds = int(time[1].split('s')[0])
elif bool(re.match("(\d+)h(\d+)m$", time)):
time = time.split('h')
hours = int(time[0])
minutes = int(time[1].split('m')[0])
elif bool(re.match("(\d+)h(\d+)s$", time)):
time = time.split('h')
hours = int(time[0])
seconds = int(time[1].split('s')[0])
elif bool(re.match("(\d+)m(\d+)s$", time)):
time = time.split('m')
minutes = int(time[0])
seconds = int(time[1].split('s')[0])
elif bool(re.match("(\d+)d(\d+)h(\d+)m$", time)):
time = time.split('d')
days = int(time[0])
time = time[1].split('h')
hours = int(time[0])
minutes = int(time[1].split('m')[0])
elif bool(re.match("(\d+)d(\d+)h(\d+)s$", time)):
time = time.split('d')
days = int(time[0])
time = time[1].split('h')
hours = int(time[0])
seconds = int(time[1].split('s')[0])
elif bool(re.match("(\d+)h(\d+)m(\d+)s$", time)):
time = time.split('h')
hours = int(time[0])
time = time[1].split('m')
minutes = int(time[0])
seconds = int(time[1].split('s')[0])
elif bool(re.match("(\d+)d(\d+)h(\d+)m(\d+)s$", time)):
time = time.split('d')
days = int(time[0])
time = time[1].split('h')
hours = int(time[0])
time = time[1].split('m')
minutes = int(time[0])
seconds = int(time[1].split('s')[0])
return 24*60*60*days + 60*60*hours + 60*minutes + seconds
def get_word(word: str, num: int):
morph = pymorphy2.MorphAnalyzer()
word = morph.parse(word)[0]
return word.make_agree_with_number(num).word
def parse_time_for_embed(timer: int):
parsed = ""
days = int(timer / (24*60*60))
timer %= (24*60*60)
hours = int(timer / (60*60))
timer %= (60*60)
minutes = int(timer / 60)
timer %= 60
seconds = int(timer)
flag = 0
if days > 0:
parsed += f"{days} " + str(get_word("день", days))
flag = 1
if hours > 0:
if flag == 1:
parsed += ", "
parsed += f"{hours} " + str(get_word("час", hours))
flag = 1
if minutes > 0:
if flag == 1:
parsed += ", "
parsed += f"{minutes} " + str(get_word("минута", minutes))
flag = 1
if seconds > 0:
if flag == 1:
parsed += ", "
parsed += f"{seconds} " + str(get_word("секунда", seconds))
return parsed
# ------------------------------------------------------------------------------------------------------------------
class Mute(commands.Cog):
def __init__(self, bot):
self.bot = bot
def get_mute_help_embed(ctx: commands.Context):
help_title = ":books: | О команде `!mute`"
help_description = "> Забирает у участника права отправлять сообщения в большинстве каналов"
help_embed = discord.Embed(
title=help_title, description=help_description, color=discord.Color.from_rgb(106, 118, 238))
help_embed.add_field(
name="Шаблон", value=f"> `!mute Участник Длительность(по желанию) Причина(по желанию)`", inline=False)
help_embed.add_field(
name="Пример", value=f"> `!mute @Tom 1h Спам`", inline=False)
help_embed.set_footer(
icon_url=ctx.author.avatar_url, text=str(ctx.author))
return help_embed
def get_mute_time_error_embed(ctx: commands.Context):
title_error = ":x: | Неточность"
time_error = "Аргумент **mute** должен обозначать период времени(как минимум 1 секунду) в формате `0d0h0m0s`\n\
`d` - дни, `h` - часы, `m` - минуты, `s` - секунды\n \
Например, 1h30m значит 1 час и 30 минут."
time_error_embed = discord.Embed(
title=title_error, description=time_error, color=discord.Color.red())
time_error_embed.set_footer(
icon_url=ctx.author.avatar_url, text=str(ctx.author))
return time_error_embed
def get_mute_embed(ctx: commands.Context, member: discord.Member, reason: str, timer: int = None):
if timer is None:
description = "**Длительность:** Неопределённый срок\n"
description += f"**Модератор:** {str(ctx.author)}\n"
description += f"**Причина:** {reason}"
else:
description = f"**Длительность:** {parse_time_for_embed(timer)}\n"
description += f"**Модератор:** {str(ctx.author)}\n"
description += f"**Причина:** {reason}"
embed = discord.Embed(
title=f":lock: | {str(member)} был замьючен", description=description, color=discord.Color.from_rgb(143, 0, 19))
return embed
@commands.command()
@has_permissions(administrator=True)
async def mute(self, ctx: commands.Context, member: discord.Member = None, time: str = None, *, reason="Без причины"):
mute_role = discord.utils.get(ctx.guild.roles, name="Muted")
if not mute_role:
mute_role = await ctx.guild.create_role(name="Muted")
for channel in ctx.guild.channels:
await channel.set_permissions(mute_role, speak=True, send_messages=False, read_message_history=True, read_messages=True)
db = setup_db([x.id for x in ctx.guild.members])
if member is None:
bot_answer = await ctx.send(embed=Mute.get_mute_help_embed(ctx))
if ctx.message.content.startswith("!mute"):
await asyncio.sleep(5)
await ctx.message.delete()
await asyncio.sleep(60)
await bot_answer.delete()
elif time is None:
await member.add_roles(mute_role)
db.mute_user(member.id, datetime.now().strftime(
"%H:%M, %d.%m.%y"), "Неопределённый срок")
await ctx.send(embed=Mute.get_mute_embed(ctx, member, reason))
if ctx.message.content.startswith("!mute"):
await asyncio.sleep(5)
await ctx.message.delete()
else:
timer = parse_time(time)
if timer is None or timer <= 0:
bot_answer = await ctx.send(embed=Mute.get_mute_time_error_embed(ctx))
if ctx.message.content.startswith("!mute"):
await asyncio.sleep(5)
await ctx.message.delete()
await asyncio.sleep(5)
await bot_answer.delete()
else:
await member.add_roles(mute_role)
db.mute_user(member.id, datetime.now().strftime(
"%H:%M, %d.%m.%y"), parse_time_for_embed(timer))
await ctx.send(embed=Mute.get_mute_embed(ctx, member, reason, timer))
if ctx.message.content.startswith("!mute"):
await asyncio.sleep(5)
await ctx.message.delete()
await asyncio.sleep(timer)
await member.remove_roles(mute_role)
db.unmute_user(member.id)
await ctx.send(embed=Unmute.get_unmute_embed(ctx, member))
@mute.error
async def mute_error(self, ctx: commands.Context, error):
if isinstance(error, CheckFailure):
bot_answer = await ctx.send(embed=get_permissions_embed(ctx))
else:
bot_answer = await ctx.send(embed=get_error_embed(ctx))
if ctx.message.content.startswith("!mute"):
await asyncio.sleep(5)
await ctx.message.delete()
await asyncio.sleep(5)
await bot_answer.delete()
# ------------------------------------------------------------------------------------------------------------------
class Unmute(commands.Cog):
def __init__(self, bot):
self.bot = bot
def get_unmute_embed(ctx: commands.Context, member: discord.Member):
description = f"**Модератор:** {str(ctx.author)}"
embed = discord.Embed(
title=f":unlock: | {str(member)} был размьючен", description=description, color=discord.Color.green())
return embed
def get_unmute_help_embed(ctx: commands.Context):
help_title = ":books: | О команде `!unmute`"
help_description = "> Возвращает участнику права отправлять сообщения в большинстве каналов"
help_embed = discord.Embed(
title=help_title, description=help_description, color=discord.Color.from_rgb(106, 118, 238))
help_embed.add_field(
name="Шаблон", value=f"> `!unmute Участник`", inline=False)
help_embed.add_field(
name="Пример", value=f"> `!unmute @Tom`", inline=False)
help_embed.set_footer(
icon_url=ctx.author.avatar_url, text=str(ctx.author))
return help_embed
def get_unmute_no_role_embed(ctx: commands.Context, member: discord.Member):
description = f"**Модератор:** {str(ctx.author)}"
embed = discord.Embed(
title=f":unlock: | {str(member)} не был замьючен", description=description, color=discord.Color.green())
return embed
@commands.command()
@has_permissions(administrator=True)
async def unmute(self, ctx: commands.Context, member: discord.Member = None):
mute_role = discord.utils.get(ctx.guild.roles, name="Muted")
if not member:
bot_answer = await ctx.send(embed=Unmute.get_unmute_help_embed(ctx))
await asyncio.sleep(60)
await bot_answer.delete()
if ctx.message.content.startswith("!unmute"):
await asyncio.sleep(5)
await ctx.message.delete()
else:
if not mute_role or mute_role not in member.roles:
bot_answer = await ctx.send(embed=Unmute.get_unmute_no_role_embed(ctx, member))
await asyncio.sleep(5)
await bot_answer.delete()
if ctx.message.content.startswith("!unmute"):
await asyncio.sleep(5)
await ctx.message.delete()
else:
db = setup_db([x.id for x in ctx.guild.members])
await member.remove_roles(mute_role)
db.unmute_user(member.id)
await ctx.send(embed=Unmute.get_unmute_embed(ctx, member))
if ctx.message.content.startswith("!unmute"):
await asyncio.sleep(5)
await ctx.message.delete()
@unmute.error
async def unmute_error(self, ctx: commands.Context, error):
if isinstance(error, CheckFailure):
bot_answer = await ctx.send(embed=get_permissions_embed(ctx))
else:
bot_answer = await ctx.send(embed=get_error_embed(ctx))
if ctx.message.content.startswith("!unmute"):
await asyncio.sleep(5)
await ctx.message.delete()
await asyncio.sleep(5)
await bot_answer.delete()
# ------------------------------------------------------------------------------------------------------------------
class Ban(commands.Cog):
def __init__(self, bot):
self.bot = bot
def get_ban_help_embed(ctx: commands.Context):
help_title = ":books: | О команде `!ban`"
help_description = "> Банит участника на сервере"
help_embed = discord.Embed(
title=help_title, description=help_description, color=discord.Color.from_rgb(106, 118, 238))
help_embed.add_field(
name="Шаблон", value=f"> `!ban Участник Длительность(по желанию) Причина(по желанию)`", inline=False)
help_embed.add_field(
name="Пример", value=f"> `!ban @Tom 1h Спам`", inline=False)
help_embed.set_footer(
icon_url=ctx.author.avatar_url, text=str(ctx.author))
return help_embed
def get_ban_time_error_embed(ctx: commands.Context):
title_error = ":x: | Неточность"
time_error = "Аргумент **ban** должен обозначать период времени(как минимум 1 секунду) в формате `0d0h0m0s`\n\
`d` - дни, `h` - часы, `m` - минуты, `s` - секунды\n \
Например, 1h30m значит 1 час и 30 минут."
time_error_embed = discord.Embed(
title=title_error, description=time_error, color=discord.Color.red())
time_error_embed.set_footer(
icon_url=ctx.author.avatar_url, text=str(ctx.author))
return time_error_embed
def get_ban_self_error_embed(ctx: commands.Context):
title_error = ":x: | Неточность"
user_error = "Нельзя забанить себя"
user_error_embed = discord.Embed(
title=title_error, description=user_error, color=discord.Color.red())
user_error_embed.set_footer(
icon_url=ctx.author.avatar_url, text=str(ctx.author))
return user_error_embed
def get_ban_embed(ctx: commands.Context, member: discord.Member, reason: str, timer: int = None):
if timer is None:
description = "**Длительность:** Неопределённый срок\n"
description += f"**Модератор:** {str(ctx.author)}\n"
description += f"**Причина:** {reason}"
else:
description = f"**Длительность:** {parse_time_for_embed(timer)}\n"
description += f"**Модератор:** {str(ctx.author)}\n"
description += f"**Причина:** {reason}"
embed = discord.Embed(
title=f":no_entry: | {str(member)} был забанен", description=description, color=discord.Color.from_rgb(143, 0, 19))
return embed
@commands.command()
@has_permissions(administrator=True)
async def ban(self, ctx: commands.Context, member: discord.Member = None, time: str = None, *, reason="Без причины"):
if member is None:
bot_answer = await ctx.send(embed=Ban.get_ban_help_embed(ctx))
if ctx.message.content.startswith("!ban"):
await asyncio.sleep(5)
await ctx.message.delete()
await asyncio.sleep(60)
await bot_answer.delete()
elif ctx.author.id == member.id:
bot_answer = await ctx.send(embed=Ban.get_ban_self_error_embed(ctx))
if ctx.message.content.startswith("!ban"):
await asyncio.sleep(5)
await ctx.message.delete()
await asyncio.sleep(5)
await bot_answer.delete()
elif time is None:
await ctx.guild.ban(user=member, delete_message_days=0, reason=reason)
await ctx.send(embed=Ban.get_ban_embed(ctx, member, reason))
if ctx.message.content.startswith("!ban"):
await asyncio.sleep(5)
await ctx.message.delete()
else:
timer = parse_time(time)
if timer is None or timer <= 0:
bot_answer = await ctx.send(embed=Ban.get_ban_time_error_embed(ctx))
if ctx.message.content.startswith("!ban"):
await asyncio.sleep(5)
await ctx.message.delete()
await asyncio.sleep(5)
await bot_answer.delete()
else:
await ctx.guild.ban(user=member, delete_message_days=0, reason=reason)
await ctx.send(embed=Ban.get_ban_embed(ctx, member, reason, timer))
if ctx.message.content.startswith("!ban"):
await asyncio.sleep(5)
await ctx.message.delete()
await asyncio.sleep(timer)
await ctx.guild.unban(user=member)
await ctx.send(embed=Unban.get_unban_embed(ctx, member))
@ban.error
async def ban_error(self, ctx: commands.Context, error):
if isinstance(error, CheckFailure):
bot_answer = await ctx.send(embed=get_permissions_embed(ctx))
else:
bot_answer = await ctx.send(embed=get_error_embed(ctx))
if ctx.message.content.startswith("!ban"):
await asyncio.sleep(5)
await ctx.message.delete()
await asyncio.sleep(5)
await bot_answer.delete()
# ------------------------------------------------------------------------------------------------------------------
class Unban(commands.Cog):
def __init__(self, bot):
self.bot = bot
def get_unban_embed(ctx: commands.Context, member: discord.Member):
description = f"**Модератор:** {str(ctx.author)}"
embed = discord.Embed(
title=f":shield: | {str(member)} был разбанен", description=description, color=discord.Color.green())
return embed
def get_unban_help_embed(ctx: commands.Context):
help_title = ":books: | О команде `!unban`"
help_description = "> Разбанивает участника на сервере"
help_embed = discord.Embed(
title=help_title, description=help_description, color=discord.Color.from_rgb(106, 118, 238))
help_embed.add_field(
name="Шаблон", value=f"> `!unban Участник`", inline=False)
help_embed.add_field(
name="Пример", value=f"> `!unban Tom#1234`", inline=False)
help_embed.set_footer(
icon_url=ctx.author.avatar_url, text=str(ctx.author))
return help_embed
def get_unban_user_error_embed(ctx: commands.Context):
title_error = ":x: | Неточность"
user_error = "Аргумент **unban** должен обозначать пользователя Discord в формате `Name#1234` или ID пользователя\nНапример, `Tom#3024`"
user_error_embed = discord.Embed(
title=title_error, description=user_error, color=discord.Color.red())
user_error_embed.set_footer(
icon_url=ctx.author.avatar_url, text=str(ctx.author))
return user_error_embed
def get_unban_unknown_user_embed(ctx: commands.Context):
title_error = ":x: | Неточность"
user_error = "Указанный пользователь никогда не был на сервере"
user_error_embed = discord.Embed(
title=title_error, description=user_error, color=discord.Color.red())
user_error_embed.set_footer(
icon_url=ctx.author.avatar_url, text=str(ctx.author))
return user_error_embed
def get_unban_no_ban_embed(ctx: commands.Context, member: discord.Member):
description = f"**Модератор:** {str(ctx.author)}"
embed = discord.Embed(
title=f":shield: | {str(member)} не был забанен", description=description, color=discord.Color.green())
return embed
@commands.command()
@has_permissions(administrator=True)
async def unban(self, ctx: commands.Context, *, member: str = None):
if member is None:
bot_answer = await ctx.send(embed=Unban.get_unban_help_embed(ctx))
if ctx.message.content.startswith("!unban"):
await asyncio.sleep(5)
await ctx.message.delete()
await asyncio.sleep(60)
await bot_answer.delete()
else:
banned_users = await ctx.guild.bans()
if "#" in member:
member_name = member.split("#")
if len(member_name) != 2 or len(member_name[1]) != 4:
bot_answer = await ctx.send(embed=Unban.get_unban_user_error_embed(ctx))
if ctx.message.content.startswith("!unban"):
await asyncio.sleep(5)
await ctx.message.delete()
await asyncio.sleep(5)
await bot_answer.delete()
else:
banned = False
for ban_entry in banned_users:
user = ban_entry.user
if (user.name, user.discriminator) == (member_name[0], member_name[1]):
banned = True
await ctx.guild.unban(user)
await ctx.send(embed=Unban.get_unban_embed(ctx, user))
if ctx.message.content.startswith("!unban"):
await asyncio.sleep(5)
await ctx.message.delete()
if banned == False:
if (member_name[0], member_name[1]) in [(x.name, x.discriminator) for x in ctx.guild.members]:
bot_answer = await ctx.send(embed=Unban.get_unban_no_ban_embed(ctx, member))
else:
bot_answer = await ctx.send(embed=Unban.get_unban_unknown_user_embed(ctx))
if ctx.message.content.startswith("!unban"):
await asyncio.sleep(5)
await ctx.message.delete()
await asyncio.sleep(5)
await bot_answer.delete()
else:
try:
user_id = int(member)
banned = False
for ban_entry in banned_users:
user = ban_entry.user
if user.id == user_id:
banned = True
await ctx.guild.unban(user)
await ctx.send(embed=Unban.get_unban_embed(ctx, user))
if ctx.message.content.startswith("!unban"):
await asyncio.sleep(5)
await ctx.message.delete()
if banned == False:
if user_id in [x.id for x in ctx.guild.members]:
bot_answer = await ctx.send(embed=Unban.get_unban_no_ban_embed(ctx, member))
else:
bot_answer = await ctx.send(embed=Unban.get_unban_unknown_user_embed(ctx))
if ctx.message.content.startswith("!unban"):
await asyncio.sleep(5)
await ctx.message.delete()
await asyncio.sleep(5)
await bot_answer.delete()
except:
bot_answer = await ctx.send(embed=Unban.get_unban_user_error_embed(ctx))
if ctx.message.content.startswith("!unban"):
await asyncio.sleep(5)
await ctx.message.delete()
await asyncio.sleep(5)
await bot_answer.delete()
@unban.error
async def unban_error(self, ctx: commands.Context, error):
if isinstance(error, CheckFailure):
bot_answer = await ctx.send(embed=get_permissions_embed(ctx))
else:
bot_answer = await ctx.send(embed=get_error_embed(ctx))
if ctx.message.content.startswith("!unban"):
await asyncio.sleep(5)
await ctx.message.delete()
await asyncio.sleep(5)
await bot_answer.delete()
# ------------------------------------------------------------------------------------------------------------------
class Warn(commands.Cog):
def __init__(self, bot):
self.bot = bot
def get_warn_help_embed(ctx: commands.Context):
help_title = ":books: | О команде `!warn`"
help_description = "> Кидает участнику предупреждение"
help_embed = discord.Embed(
title=help_title, description=help_description, color=discord.Color.from_rgb(106, 118, 238))
help_embed.add_field(
name="Наказания", value=f"> Второе предупреждение - `mute`\n> Четвёртое предупреждение - `ban`", inline=False)
help_embed.add_field(
name="Шаблон", value=f"> `!warn Участник Причина(по желанию)`", inline=False)
help_embed.add_field(
name="Пример", value=f"> `!warn @Tom Спам`", inline=False)
help_embed.set_footer(
icon_url=ctx.author.avatar_url, text=str(ctx.author))
return help_embed
def get_warn_embed(ctx: commands.Context, member: discord.Member, warns, reason: str):
description = f"**Текущие предупреждения:** {warns}\n"
description += f"**Модератор:** {str(ctx.author)}\n"
description += f"**Причина:** {reason}"
embed = discord.Embed(
title=f":warning: | {str(member)} получил предупреждение", description=description, color=discord.Color.from_rgb(143, 0, 19))
return embed
@commands.command()
@has_permissions(administrator=True)
async def warn(self, ctx: commands.Context, member: discord.Member = None, *, reason: str = "Без причины"):
if member is None:
bot_answer = await ctx.send(embed=Warn.get_warn_help_embed(ctx))
if ctx.message.content.startswith("!warn"):
await asyncio.sleep(5)
await ctx.message.delete()
await asyncio.sleep(60)
await bot_answer.delete()
else:
db = setup_db([x.id for x in ctx.guild.members])
warns = db.warn_user(member.id)
await ctx.send(embed=Warn.get_warn_embed(ctx, member, warns, reason))
if warns == 2:
ctx.author = self.bot.user
cmd = self.bot.get_command("mute")
await ctx.invoke(cmd, member, None, **{"reason": "Второе предупреждение"})
elif warns == 4:
ctx.author = self.bot.user
cmd = self.bot.get_command("ban")
await ctx.invoke(cmd, member, None, **{"reason": "Четвёртое предупреждение"})
if ctx.message.content.startswith("!warn"):
await asyncio.sleep(5)
await ctx.message.delete()
db.disconnect()
@warn.error
async def warn_error(self, ctx: commands.Context, error):
if isinstance(error, CheckFailure):
bot_answer = await ctx.send(embed=get_permissions_embed(ctx))
else:
bot_answer = await ctx.send(embed=get_error_embed(ctx))
if ctx.message.content.startswith("!warn"):
await asyncio.sleep(5)
await ctx.message.delete()
await asyncio.sleep(5)
await bot_answer.delete()
# ------------------------------------------------------------------------------------------------------------------
class Unwarn(commands.Cog):
def __init__(self, bot):
self.bot = bot
def get_unwarn_help_embed(ctx: commands.Context):
help_title = ":books: | О команде `!unwarn`"
help_description = "> Снимает с участника предупреждения"
help_embed = discord.Embed(
title=help_title, description=help_description, color=discord.Color.from_rgb(106, 118, 238))
help_embed.add_field(
name="Шаблон", value=f"> `!unwarn Участник Количество(по желанию)`", inline=False)
help_embed.add_field(
name="Пример", value=f"> `!unwarn @Tom 2`", inline=False)
help_embed.set_footer(
icon_url=ctx.author.avatar_url, text=str(ctx.author))
return help_embed
def get_unwarn_num_error_embed(ctx: commands.Context):
title_error = ":x: | Неточность"
num_error = "Аргумент **unwarn** должен обозначать количество предупреждений в числовом формате\n\
Например, 2."
num_error_embed = discord.Embed(
title=title_error, description=num_error, color=discord.Color.red())
num_error_embed.set_footer(
icon_url=ctx.author.avatar_url, text=str(ctx.author))
return num_error_embed
def get_unwarn_embed(ctx: commands.Context, member: discord.Member, warns):
if warns > 0:
title = f":pushpin: | С {str(member)} сняты предупреждения"
description = f"**Текущие предупреждения:** {warns}\n"
description += f"**Модератор:** {str(ctx.author)}\n"
else:
title = f":pushpin: | С {str(member)} сняты все предупреждения"
description = f"**Модератор:** {str(ctx.author)}\n"
embed = discord.Embed(
title=title, description=description, color=discord.Color.green())
return embed
@commands.command()
@has_permissions(administrator=True)
async def unwarn(self, ctx: commands.Context, member: discord.Member = None, num: str = "all"):
if member is None:
bot_answer = await ctx.send(embed=Unwarn.get_unwarn_help_embed(ctx))
if ctx.message.content.startswith("!unwarn"):
await asyncio.sleep(5)
await ctx.message.delete()
await asyncio.sleep(60)
await bot_answer.delete()
else:
db = setup_db([x.id for x in ctx.guild.members])
if not db.unwarn_user(member.id, num):
bot_answer = await ctx.send(embed=Unwarn.get_unwarn_num_error_embed(ctx))
if ctx.message.content.startswith("!unwarn"):
await asyncio.sleep(5)
await ctx.message.delete()
await asyncio.sleep(5)
await bot_answer.delete()
else:
warns = db.get_user_warns(member.id)
await ctx.send(embed=Unwarn.get_unwarn_embed(ctx, member, warns))
if ctx.message.content.startswith("!unwarn"):
await asyncio.sleep(5)
await ctx.message.delete()
db.disconnect()
@unwarn.error
async def unwarn_error(self, ctx: commands.Context, error):
if isinstance(error, CheckFailure):
bot_answer = await ctx.send(embed=get_permissions_embed(ctx))
else:
bot_answer = await ctx.send(embed=get_error_embed(ctx))
if ctx.message.content.startswith("!unwarn"):
await asyncio.sleep(5)
await ctx.message.delete()
await asyncio.sleep(5)
await bot_answer.delete()
# ------------------------------------------------------------------------------------------------------------------
class Sinners(commands.Cog):
def __init__(self, bot):
self.bot = bot
def get_sinners_help_embed(ctx: commands.Context):
help_title = ":books: | О команде `!sinners`"
help_description = "> Показывает список грешников"
help_embed = discord.Embed(
title=help_title, description=help_description, color=discord.Color.from_rgb(106, 118, 238))
help_embed.add_field(
name="Режимы", value=f"> Показать всех грешников - `all`\n> Показать 10 замьюченных пользователей - `mute`\n\
> Показать 10 предупреждений - `warn`", inline=False)
help_embed.add_field(
name="Шаблон", value=f"> `!sinners Режим Страница(по желанию)`", inline=False)
help_embed.add_field(
name="Пример", value=f"> `!sinners mute 2`", inline=False)
help_embed.set_footer(
icon_url=ctx.author.avatar_url, text=str(ctx.author))
return help_embed
def get_sinners_error_embed(ctx: commands.Context):
title_error = ":x: | Неточность"
num_error = "Аргумент **sinners** должен обозначать один из трёх режимов:\n\
`all` - показать всех грешников\n\
`mute` - показать замьюченных пользователей\n\
`warn` - показать предупреждения"
num_error_embed = discord.Embed(
title=title_error, description=num_error, color=discord.Color.red())
num_error_embed.set_footer(
icon_url=ctx.author.avatar_url, text=str(ctx.author))
return num_error_embed
def get_muted_embed(ctx: commands.Context, mutes, pagenum):
if len(mutes) == 0:
embed = discord.Embed(
title=f":pushpin: | На сервере никто не замьючен", color=discord.Color.green())
embed.set_footer(icon_url=ctx.author.avatar_url,
text=str(ctx.author))
return embed
else:
pages = math.ceil(len(mutes) / 10)
description = ""
for i in range(min(10, len(mutes) - (pagenum - 1) * 10)):
description += f"> **{ctx.bot.get_user(mutes[i + (pagenum - 1) * 10]['id']).mention}:** \
`{mutes[i + (pagenum - 1) * 10]['date']}` на `{mutes[i + (pagenum - 1) * 10]['timer']}`\n"
embed = discord.Embed(title=f":pushpin: | Список замьюченных пользователей",
description=description, color=discord.Color.blue())
return embed
def get_warned_embed(ctx: commands.Context, warns, pagenum):
if len(warns) == 0:
embed = discord.Embed(
title=f":pushpin: | На сервере ни у кого нет предупреждений", color=discord.Color.green())
embed.set_footer(icon_url=ctx.author.avatar_url,
text=str(ctx.author))
return embed
else:
pages = math.ceil(len(warns) / 10)
description = ""
for i in range(min(10, len(warns) - (pagenum - 1) * 10)):
description += f"> **{ctx.bot.get_user(warns[i + (pagenum - 1) * 10]['id']).mention}:** `{warns[i + (pagenum - 1) * 10]['warns']}`\n"
embed = discord.Embed(title=f":pushpin: | Список предупреждений [{pagenum}/{pages}]",
description=description, color=discord.Color.blue())
return embed
def get_sinners_embed(ctx: commands.Context, mutes, warns):
embed = discord.Embed(title=f":pushpin: | Список грешников",
color=discord.Color.blue())
if len(mutes) == 0:
embed.add_field(name="Замьюченные пользователи",
value="> Никто не замьючен", inline=False)
else:
muted = ""
for mute in mutes:
muted += f"> **{ctx.bot.get_user(mute['id']).mention}:** `{mute['date']}` на `{mute['timer']}`\n"
embed.add_field(name="Замьюченные пользователи",
value=muted, inline=True)
if len(warns) == 0:
embed.add_field(name="Предупреждения",
value="> Ни у кого нет предупреждений", inline=False)
else:
warned = ""
for warn in warns:
warned += f"> **{ctx.bot.get_user(warn['id']).mention}:** `{warn['warns']}`\n"
embed.add_field(name="Предупреждения", value=warned, inline=True)
return embed
@commands.command()
async def sinners(self, ctx: commands.Context, arg: str = None, pagenum: int = 1):
if arg is None:
bot_answer = await ctx.send(embed=Sinners.get_sinners_help_embed(ctx))
if ctx.message.content.startswith("!sinners"):
await asyncio.sleep(5)
await ctx.message.delete()
await asyncio.sleep(60)
await bot_answer.delete()
else:
db = setup_db([x.id for x in ctx.guild.members])
warns = db.get_warns()
mutes = db.get_mutes()
if arg.lower() == "all":
await ctx.send(embed=Sinners.get_sinners_embed(ctx, mutes[:5], warns[:5]))
if ctx.message.content.startswith("!sinners"):
await asyncio.sleep(5)
await ctx.message.delete()
elif arg.lower() == "mute":
await ctx.send(embed=Sinners.get_muted_embed(ctx, mutes, pagenum))
if ctx.message.content.startswith("!sinners"):
await asyncio.sleep(5)
await ctx.message.delete()
elif arg.lower() == "warn":
await ctx.send(embed=Sinners.get_warned_embed(ctx, warns, pagenum))
if ctx.message.content.startswith("!sinners"):
await asyncio.sleep(5)
await ctx.message.delete()
else:
bot_answer = await ctx.send(embed=Sinners.get_sinners_error_embed(ctx))
await ctx.message.delete()
await asyncio.sleep(5)
await bot_answer.delete()
# ------------------------------------------------------------------------------------------------------------------
class Points(commands.Cog):
def __init__(self, bot):
self.bot = bot
def get_points_help_embed(ctx: commands.Context):
help_title = ":books: | О команде `!set_points`"
help_description = "> Устанавливает новое значение очков пользователя"
help_embed = discord.Embed(
title=help_title, description=help_description, color=discord.Color.from_rgb(106, 118, 238))
help_embed.add_field(
name="Режимы", value=f"> Изменить очки в чате - `text`\n> Изменить очки в войсе - `voice`", inline=False)
help_embed.add_field(
name="Шаблон", value=f"> `!set_points Режим Участник Очки`", inline=False)
help_embed.add_field(
name="Пример", value=f"> `!set_points text @Tom 100`", inline=False)
help_embed.set_footer(
icon_url=ctx.author.avatar_url, text=str(ctx.author))
return help_embed
def get_points_error_embed(ctx: commands.Context):
title_error = ":x: | Неточность"
mode_error = "Аргумент **set_points** должен обозначать один из двух режимов:\n\
`text` - изменить очки в чате\n\
`voice` - изменить очки в войсе"
mode_error_embed = discord.Embed(
title=title_error, description=mode_error, color=discord.Color.red())
mode_error_embed.set_footer(
icon_url=ctx.author.avatar_url, text=str(ctx.author))
return mode_error_embed
def get_points_member_error_embed(ctx: commands.Context):
title_error = ":x: | Неточность"
mode_error = "Аргумент **set_points** должен обозначать участника сервера\n\
Например, `@Tom`"
mode_error_embed = discord.Embed(
title=title_error, description=mode_error, color=discord.Color.red())
mode_error_embed.set_footer(
icon_url=ctx.author.avatar_url, text=str(ctx.author))
return mode_error_embed
def get_points_points_error_embed(ctx: commands.Context):
title_error = ":x: | Неточность"
mode_error = "Аргумент **set_points** должен обозначать количество очков\n\
Например, `100`"
mode_error_embed = discord.Embed(
title=title_error, description=mode_error, color=discord.Color.red())
mode_error_embed.set_footer(
icon_url=ctx.author.avatar_url, text=str(ctx.author))
return mode_error_embed
def get_points_embed(ctx: commands.Context, mode: str, member: discord.Member, points: int):
title = f":rocket: | Обновление очков для {str(member)}"
description = f"> **Модератор:** `{ctx.author}`\n"
description += f"> **Режим:** `{mode}`\n"
description += f"> **Очки:** `{points}`"
embed = discord.Embed(
title=title, description=description, color=discord.Color.from_rgb(239, 165, 55))
return embed
@commands.command()
@has_permissions(administrator=True)
async def set_points(self, ctx: commands.Context, mode: str = None, member: discord.Member = None, points: int = None):
if mode is None:
bot_answer = await ctx.send(embed=Points.get_points_help_embed(ctx))
await asyncio.sleep(60)
await bot_answer.delete()
elif member is None:
bot_answer = await ctx.send(embed=Points.get_points_member_error_embed(ctx))
await asyncio.sleep(5)
await bot_answer.delete()
elif points is None:
bot_answer = await ctx.send(embed=Points.get_points_points_error_embed(ctx))
await asyncio.sleep(5)
await bot_answer.delete()
elif mode != "text" and mode != "voice":
bot_answer = await ctx.send(embed=Points.get_points_error_embed(ctx))
await asyncio.sleep(5)
await bot_answer.delete()
else:
db = setup_db([x.id for x in ctx.guild.members])
if mode == "text":
db.set_text_rating(member.id, points)
else:
db.set_voice_rating(member.id, points)
await ctx.send(embed=Points.get_points_embed(ctx, mode, member, points))
await ctx.message.delete()
# ------------------------------------------------------------------------------------------------------------------
def setup_moderation_commands(bot):
bot.add_cog(Mute(bot))
bot.add_cog(Unmute(bot))
bot.add_cog(Ban(bot))
bot.add_cog(Unban(bot))
bot.add_cog(Warn(bot))
bot.add_cog(Unwarn(bot))
bot.add_cog(Sinners(bot))
bot.add_cog(Points(bot))