-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
1077 lines (1002 loc) · 56.3 KB
/
main.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 discord
from discord import ui, app_commands
import json
from discord.ext.commands.converter import PartialMessageConverter
from config import *
import chat_exporter
import io
import asyncio
import threading
import canvacord
import random
from colour import Color
class Client(discord.Client):
def __init__(self):
intents = discord.Intents.all()
activity = discord.CustomActivity("Look at me!")
super().__init__(intents=intents, activity=activity)
self.tree = app_commands.CommandTree(self)
async def on_ready(self):
print("Logged in as {0.user}".format(self))
await self.tree.sync()
async def setup_hook(self):
ticketsystem = app_commands.Group(name='tickets', description='Ticket commands')
ticketsystem.add_command(app_commands.Command(name = "setup", description="Setup the ticket system", callback = ticketsystem_setup_command))
ticketsystem.add_command(app_commands.Command(name = "resend", description="Send a specific ticket panel again", callback = ticketsystem_resend_command))
ticketsystem.add_command(app_commands.Command(name = "delete", description="Delete a specific ticket panel", callback = ticketsystem_delete_command))
self.tree.add_command(ticketsystem)
xpsystem = app_commands.Group(name='xp', description='XP/Leveling commands')
xpsystem.add_command(app_commands.Command(name = "setup", description = "Setup the xp/leveling system", callback = xpsystemsetup))
xpsystem.add_command(app_commands.Command(name = 'rank', description = "Get your current rank card", callback = xpsystemrankcard))
xpsystem.add_command(app_commands.Command(name = 'leaderboard', description = 'Display the current xp/leveling leaderboard', callback = xpsystemleaderboard))
self.tree.add_command(xpsystem)
announcements = app_commands.Group(name='announcements', description = 'Announcements for join, leave and ban')
announcements_add = app_commands.Command(name = 'add', description = 'Add a new announcement', callback = announcements_add_join)
announcements_remove = app_commands.Command(name = 'remove', description = 'Remove an announcement', callback = announcements_remove_join)
announcements.add_command(announcements_remove)
announcements.add_command(announcements_add)
self.tree.add_command(announcements)
# polls = app_commands.Group(name='polls', description = 'Just some polls')
# polls.add_command(app_commands.Command(name="start", description="Start a new poll", callback = startpoll))
# self.tree.add_command(polls)
# test = app_commands.Group(name='test', description='Currently in test')
# test.add_command(app_commands.Command(name = 'rank', description = 'Display your rank card', callback = testRankCard))
# self.tree.add_command(test)
client = Client()
@client.tree.command(name="say")
async def say_command(interaction, text: str = None):
"""Let Demon say something
Parameters
----------
text : str
Text to say"""
await interaction.response.defer()
if text:
await interaction.followup.send("You said: "+text)
else:
await interaction.followup.send("You said nothing.")
async def ticketsystem_setup_command(interaction, name: str, panel :discord.TextChannel, category: discord.CategoryChannel = None, mention: discord.Role = None):
"""Setup the Ticket system
Parameters
----------
name : str
A unique name for your tickets panel.
mention : discord.Role
Mention your everyone role [@everyone]
panel : discord.TextChannel
The channel where the ticket panel will appear
category : discord.CategoryChannel
The category where the tickets go in."""
await interaction.response.defer(ephemeral = True)
server_name = interaction.guild.name
ticketpanels = json.loads(open('ticketpanels.json', 'r').read())
staff = json.loads(open('staff.json', 'r').read())
if not interaction.guild:
embed = discord.Embed(title="Ticket system", description="This command can only be used in a server!", color = embed_color_error)
await interaction.followup.send(embed = embed)
return
isStaff = False
member = await interaction.guild.fetch_member(interaction.user.id)
if str(interaction.guild.id) in staff:
for role in member.roles:
if role.id in staff["manager"]:
isStaff = True
if member.guild_permissions.administrator or member.id == interaction.guild.owner.id:
isStaff = True
if not isStaff:
embed = discord.Embed(title="Ticket system", description="You don't have permission to use this command!", color = embed_color_error)
await interaction.followup.send(embed = embed)
return
if str(interaction.guild.id) + '-' + name in ticketpanels:
embed = discord.Embed(title="Ticket system", description="This ticket panel already exists!", color = embed_color_error)
await interaction.followup.send(embed = embed)
return
ticketpanels[str(interaction.guild.id) + '-' + name] = {
"name": name,
"guild_id": interaction.guild.id,
"category_id": category.id if category else None,
"message_id": None,
"description": "This is the ticket support of {server}.",
"title": "{server} Support",
"welcome_message": "Thanks for contacting {server} support.\nWe will be here for you shortly.",
"button_label": "🎟️ Create a Ticket",
}
open('ticketpanels.json', 'w').write(json.dumps(ticketpanels, indent = 4))
class ConfirmAndViewView(ui.View):
def __init__(self, guild_id, panel_name, mention, panel):
super().__init__(timeout = None)
self.guild_id = guild_id
self.panel_name = panel_name
self.mention = mention
self.panel = panel
@ui.button(label = "Send panel", style = discord.ButtonStyle.green)
async def send_panel(self, interaction, button):
await interaction.response.defer(ephemeral = True)
guild = client.get_guild(self.guild_id)
panel = ticketpanels[str(self.guild_id) + '-' + self.panel_name]
embed = discord.Embed(title = panel["title"].replace("{server}", guild.name), description = panel["description"].replace("{server}", guild.name), color = embed_color)
embed.set_footer(text = f"Click the button below to open a ticket")
button = discord.ui.Button(label = panel["button_label"], style = discord.ButtonStyle.blurple, custom_id = "create_ticket_"+panel["name"])
view = ui.View()
view.add_item(button)
if self.panel:
try:
if self.mention:
await self.panel.send(content = self.mention.mention, embed = embed, view = view)
else:
await self.panel.send(embed = embed, view = view)
except:
embed = discord.Embed(title = "Ticket system", description = "I don't have permission to send messages in <#"+str(self.panel.id)+">!", color = embed_color_error)
await interaction.followup.send(embed = embed)
else:
try:
if self.mention:
await interaction.channel.send(content = self.mention.mention, embed = embed, view = view)
else:
await interaction.channel.send(embed = embed, view = view)
except:
embed = discord.Embed(title = "Ticket system", description = "I don't have permission to send messages in this channel!", color = embed_color_error)
await interaction.followup.send(embed = embed)
@ui.button(label = "Preview", style = discord.ButtonStyle.red)
async def preview(self, interaction, button):
await interaction.response.defer(ephemeral = True)
guild = client.get_guild(self.guild_id)
panel = ticketpanels[str(self.guild_id) + '-' + self.panel_name]
embed = discord.Embed(title = panel["title"].replace("{server}", guild.name), description = panel["description"].replace("{server}", guild.name), color = embed_color)
embed.set_footer(text = f"Click the button below to open a ticket")
button = discord.ui.Button(label = panel["button_label"], style = discord.ButtonStyle.blurple, custom_id = "create_ticket_"+panel["name"], disabled = True)
view = ui.View()
view.add_item(button)
if self.mention:
await interaction.followup.send(content = self.mention.mention, embed = embed, view = view, ephemeral = True)
else:
await interaction.followup.send(embed = embed, view = view, ephemeral = True)
embed = discord.Embed(title="Ticket system", description="Tickets have been setup!", color=embed_color_success)
await interaction.followup.send(embed = embed, view=ConfirmAndViewView(guild_id = interaction.guild.id, panel_name = name, mention = mention, panel = panel))
async def complete_ticketsystem_panelname(interaction: discord.Interaction, current: str) -> list[app_commands.Choice[str]]:
ticketpanels = json.loads(open('ticketpanels.json', 'r').read())
guild_id = interaction.guild.id
return [app_commands.Choice(name = panel["name"], value = panel["name"]) for panel in ticketpanels.values() if panel["guild_id"] == guild_id and panel["name"].startswith(current)]
@app_commands.autocomplete(name = complete_ticketsystem_panelname)
async def ticketsystem_resend_command(interaction, name: str, channel: discord.TextChannel, mention: discord.Role = None):
"""Send a specific ticket panel again
Parameters
----------
name : str
A unique name for your tickets panel.
mention : discord.Role
Mention your everyone role [@everyone]
channel : discord.TextChannel
The channel where the ticket panel will appear"""
await interaction.response.defer(ephemeral = True)
ticketpanels = json.loads(open('ticketpanels.json', 'r').read())
staff = json.loads(open('staff.json', 'r').read())
if not interaction.guild:
embed = discord.Embed(title="Ticket system", description="This command can only be used in a server!", color = embed_color_error)
await interaction.followup.send(embed = embed)
return
isStaff = False
member = await interaction.guild.fetch_member(interaction.user.id)
if str(interaction.guild.id) in staff:
for role in member.roles:
if role.id in staff["manager"]:
isStaff = True
if member.guild_permissions.administrator or member.id == interaction.guild.owner.id:
isStaff = True
if not isStaff:
embed = discord.Embed(title="Ticket system", description="You don't have permission to use this command!", color = embed_color_error)
await interaction.followup.send(embed = embed)
return
guild = interaction.guild
try:
panel = ticketpanels[str(guild.id) + '-' + name]
except:
embed = discord.Embed(title = "Ticket system", description = "This ticket panel doesn't exist!", color = embed_color_error)
await interaction.followup.send(embed = embed)
return
embed = discord.Embed(title = panel["title"].replace("{server}", guild.name), description = panel["description"].replace("{server}", guild.name), color = embed_color)
embed.set_footer(text = f"Click the button below to open a ticket")
button = discord.ui.Button(label = panel["button_label"], style = discord.ButtonStyle.blurple, custom_id = "create_ticket_"+panel["name"])
view = ui.View()
view.add_item(button)
if channel:
try:
if mention:
await channel.send(content = mention.mention, embed = embed, view = view)
await interaction.followup.send('Sent')
else:
await channel.send(embed = embed, view = view)
await interaction.followup.send('Sent')
except:
embed = discord.Embed(title = "Ticket system", description = "I don't have permission to send messages in <#"+str(channel.id)+">!", color = embed_color_error)
await interaction.followup.send(embed = embed)
else:
try:
if mention:
await interaction.channel.send(content = mention.mention, embed = embed, view = view, ephemeral = True)
await interaction.followup.send('Sent')
else:
await interaction.channel.send(embed = embed, view = view, ephemeral = True)
await interaction.followup.send('Sent')
except:
embed = discord.Embed(title = "Ticket system", description = "I don't have permission to send messages in this channel!", color = embed_color_error)
await interaction.followup.send(embed = embed)
@app_commands.autocomplete(name = complete_ticketsystem_panelname)
async def ticketsystem_delete_command(interaction, name: str):
"""Delete a specific ticket panel
Parameters
----------
name : str
A unique name for your tickets panel.
"""
await interaction.response.defer(ephemeral = True)
ticketpanels = json.loads(open('ticketpanels.json', 'r').read())
staff = json.loads(open('staff.json', 'r').read())
if not interaction.guild:
embed = discord.Embed(title="Ticket system", description="This command can only be used in a server!", color = embed_color_error)
await interaction.followup.send(embed = embed)
return
isStaff = False
member = await interaction.guild.fetch_member(interaction.user.id)
if str(interaction.guild.id) in staff:
for role in member.roles:
if role.id in staff["manager"]:
isStaff = True
if member.guild_permissions.administrator or member.id == interaction.guild.owner.id:
isStaff = True
if not isStaff:
embed = discord.Embed(title="Ticket system", description="You don't have permission to use this command!", color = embed_color_error)
await interaction.followup.send(embed = embed)
return
guild = interaction.guild
try:
panel = ticketpanels[str(guild.id) + '-' + name]
except:
embed = discord.Embed(title = "Ticket system", description = "This ticket panel doesn't exist!", color = embed_color_error)
await interaction.followup.send(embed = embed)
return
view = ui.View()
button = discord.ui.Button(label = "Yes", style = discord.ButtonStyle.red, custom_id = "delete_ticket_panel_"+panel["name"])
view.add_item(button)
embed = discord.Embed(title = "Ticket system", description = "Are you sure you want to delete this ticket panel?", color = embed_color_warning)
await interaction.followup.send(embed = embed, view = view)
# wait for new button press
try:
interaction = await client.wait_for("interaction", check = lambda i: i.data['custom_id'] == "delete_ticket_panel_"+panel["name"], timeout = 120)
await interaction.response.defer(ephemeral = True)
await interaction.followup.send(content = "Deleting ticket panel...", ephemeral = True)
del ticketpanels[str(guild.id) + '-' + name]
json.dump(ticketpanels, open('ticketpanels.json', 'w'), indent = 4)
embed = discord.Embed(title = "Ticket system", description = "The ticket panel `"+name+"` has been deleted!", color = embed_color_success)
embed.set_footer(text="You'd have to delete the panel message yourself.")
await interaction.followup.send(embed = embed, ephemeral= True)
except Exception as es:
print(es)
embed = discord.Embed(title = "Ticket system", description = "You took too long to respond!", color = embed_color_error)
await interaction.followup.send(embed = embed, ephemeral = True)
@client.event
async def on_interaction(interaction):
if interaction.type == discord.InteractionType.component:
if interaction.data['custom_id'].startswith("create_ticket_"):
await interaction.response.defer(ephemeral = True)
ticketpanels = json.loads(open('ticketpanels.json', 'r').read())
staff = json.loads(open('staff.json', 'r').read())
guild = interaction.guild
try:
panel = ticketpanels[str(guild.id) + '-' + interaction.data['custom_id'].split('_')[2]]
except:
embed = discord.Embed(title = "Ticket system", description = "This ticket panel doesn't exist!", color = embed_color_error)
await interaction.followup.send(embed = embed, ephemeral = True)
return
for channel in interaction.guild.channels:
if channel.name == interaction.user.name:
embed = discord.Embed(title = "Ticket system", description = "You already have a ticket open in this server!", color = embed_color_error)
await interaction.followup.send(embed = embed, ephemeral = True)
return
channel = None
if panel["category_id"]:
category = guild.get_channel(panel["category_id"])
if category:
channel = await category.create_text_channel(name = interaction.user.name, topic = interaction.user.id, overwrites = {
guild.default_role: discord.PermissionOverwrite(read_messages = False),
interaction.user: discord.PermissionOverwrite(read_messages = True, send_messages = True, embed_links = True, attach_files = True, add_reactions = True, read_message_history = True),
guild.me: discord.PermissionOverwrite(read_messages = True, send_messages = True, embed_links = True, attach_files = True, add_reactions = True, read_message_history = True)
})
if not channel:
channel = await guild.create_text_channel(name = interaction.user.name, topic = interaction.user.id, overwrites = {
guild.default_role: discord.PermissionOverwrite(read_messages = False),
interaction.user: discord.PermissionOverwrite(read_messages = True, send_messages = True, embed_links = True, attach_files = True, add_reactions = True, read_message_history = True),
guild.me: discord.PermissionOverwrite(read_messages = True, send_messages = True, embed_links = True, attach_files = True, add_reactions = True, read_message_history = True),
})
# add all staffs to ticket
if str(interaction.guild.id) in staff:
staff = staff[str(interaction.guild.id)]
moderator_roles = staff['moderator']
manager_roles = staff['manager']
supporter_roles = staff['supporter']
for role in interaction.guild.roles:
if role.id in moderator_roles or role.id in manager_roles or role.id in supporter_roles:
await channel.set_permissions(role, read_messages = True, send_messages = True, embed_links = True, attach_files = True, add_reactions = True, read_message_history = True)
embed = discord.Embed(title = "Ticket system", description = "Your ticket has been created!", color = embed_color_success)
await interaction.followup.send(embed = embed, ephemeral = True)
ticket_embed = discord.Embed(title = panel["title"].replace("{server}", guild.name), description = panel["welcome_message"].replace("{server}", guild.name), color = embed_color)
ticket_embed.set_footer(text= "Close the ticket by pressing the button below")
try:
ticket_embed.set_thumnail(url = interaction.guild.icon.url)
except:
pass
view = ui.View()
view.add_item(ui.Button(label = "Close Ticket", style = discord.ButtonStyle.red, custom_id = "close_ticket"))
await channel.send(embed = ticket_embed, view = view)
if interaction.data['custom_id'] == "close_ticket":
await interaction.response.defer()
staff = json.loads(open('staff.json', 'r').read())
if str(interaction.guild.id) in staff:
staff = staff[str(interaction.guild.id)]
moderator_roles = staff['moderator']
manager_roles = staff['manager']
supporter_roles = staff['supporter']
else:
moderator_roles = []
manager_roles = []
supporter_roles = []
member = await interaction.guild.fetch_member(interaction.user.id)
userid = None
try:
userid = int(interaction.channel.topic)
except:
pass
for role in member.roles:
if role.id in moderator_roles or role.id in manager_roles or member.guild_permissions.administrator or member.id == interaction.guild.owner.id or member.id == userid:
# Do you really want to close this ticket? this can't be undone. (do with on_interaction and client.wait_for)
embed = discord.Embed(title = "Are you sure?", description = "Are you sure you want to close this ticket?", color = embed_color_warning)
view = ui.View()
view.add_item(ui.Button(label = "Yes", style = discord.ButtonStyle.green, custom_id = "close_ticket_yes"))
view.add_item(ui.Button(label = "No", style = discord.ButtonStyle.red, custom_id = "close_ticket_no"))
await interaction.followup.send(embed = embed, view = view)
close_ticket_interaction = await client.wait_for("interaction", check = lambda i: i.user.id == interaction.user.id and i.channel.id == interaction.channel.id and i.data["custom_id"] in ["close_ticket_yes", "close_ticket_no"])
await close_ticket_interaction.response.defer(ephemeral = True)
await close_ticket_interaction.message.delete()
if close_ticket_interaction.data['custom_id'] == "close_ticket_yes":
transcript = await chat_exporter.export(interaction.channel)
link = None
if transcript:
transcript_file = discord.File(
io.BytesIO(transcript.encode()),
filename = f"transcript-{interaction.channel.name}.html",
)
for guild in client.guilds:
if guild.id == support_server_id:
for channel in guild.channels:
if channel.id == channel_for_tickets:
message = await channel.send(file = transcript_file)
link = await chat_exporter.link(message)
msg = await interaction.followup.send(content = "Ticket will be closed in 5 seconds", ephemeral = True)
await asyncio.sleep(5)
for role in supporter_roles:
await interaction.channel.set_permissions(interaction.guild.get_role(role), read_messages = False, send_messages = False, embed_links = False, attach_files = False, add_reactions = False, read_message_history = False)
await interaction.channel.edit(name='closed-'+interaction.channel.name)
view = ui.View()
view.add_item(ui.Button(label = "Delete Ticket", style = discord.ButtonStyle.red, custom_id = "delete_ticket"))
await interaction.message.edit(view=view)
embed = discord.Embed(title = "Ticket system", description = "Ticket has been closed!", color = embed_color)
embed.add_field(name = "Closed by", value = f"{member.name}")
if link:
embed.add_field(name = "View transcript", value = f"[Click here]({link})")
await msg.delete()
await interaction.channel.send(embed = embed)
try:
user = await client.fetch_user(int(interaction.channel.topic))
member_of_user = await interaction.guild.fetch_member(user.id)
await interaction.channel.set_permissions(member_of_user, read_messages = False, send_messages = False, embed_links = False, attach_files = False, add_reactions = False, read_message_history = False)
if user.id == member.id:
embed = discord.Embed(title = "Ticket system", description = "Your ticket has been closed!", color = embed_color_success)
if link:
embed.add_field(name = "View transcript", value = f"[Click here]({link})")
await user.send(embed = embed)
else:
embed = discord.Embed(title = "Ticket system", description = "Your ticket in {} has been closed!".format(guild.name), color = embed_color_success)
embed.add_field(name = "Closed by", value = f"{member.name}")
if link:
embed.add_field(name = "View transcript", value = f"[Click here]({link})")
await user.send(embed = embed)
except:
pass
return
else:
await interaction.followup.send(content = "Ticket will not be closed", ephemeral = True)
return
else:
await interaction.followup.send(content = "You don't have permission to close this ticket!", ephemeral = True)
return
if interaction.data['custom_id'] == "delete_ticket":
await interaction.response.defer()
embed = discord.Embed(title = "Ticket system", description = "Are you sure you want to delete this ticket?", color = embed_color_warning)
view = ui.View()
view.add_item(ui.Button(label = "Yes", style = discord.ButtonStyle.red, custom_id = "delete_ticket_yes"))
view.add_item(ui.Button(label = "No", style = discord.ButtonStyle.gray, custom_id = "delete_ticket_no"))
await interaction.followup.send(embed = embed, view = view)
return
if interaction.data['custom_id'] == "delete_ticket_yes":
await interaction.response.defer(ephemeral= True)
embed = discord.Embed(title = "Ticket system", description = "Ticket will be deleted in 5 seconds", color = embed_color)
await interaction.followup.send(embed = embed)
await asyncio.sleep(5)
await interaction.channel.delete()
return
if interaction.data['custom_id'] == "delete_ticket_no":
await interaction.response.defer(ephemeral= True)
await interaction.message.delete()
await interaction.followup.send(content = "Ticket deletion cancelled", ephemeral = True)
@client.event
async def on_message(message):
if message.author.bot:
return
if message.guild:
try:
xpsettings = json.loads(open('xpranks/'+str(message.guild.id) + '-settings.json', 'r').read())
except Exception as es:
print(es)
xpsettings = None
if xpsettings:
if xpsettings['enabled']:
try:
serverxp = json.loads(open('xpranks/'+str(message.guild.id)+'-users.json', 'r').read())
except:
serverxp = {}
if not str(message.author.id) in serverxp.keys():
serverxp[str(message.author.id)] = {
"currentxp": 0,
"currentlevel": 1,
}
serverxp[str(message.author.id)]["currentxp"] += xpsettings["xppermessage"]
neededxp = xpsettings["firstlevelxp"] + (serverxp[str(message.author.id)]["currentlevel"] - 1) * xpsettings["xplevelincrement"]
json.dump(serverxp, open('xpranks/'+str(message.guild.id) + '-users.json', 'w'), indent = 4)
if serverxp[str(message.author.id)]["currentxp"] == neededxp:
serverxp[str(message.author.id)]["currentlevel"] += 1
serverxp[str(message.author.id)]["currentxp"] = 0
json.dump(serverxp, open('xpranks/'+str(message.guild.id) + '-users.json', 'w'), indent = 4)
if xpsettings["dolevelupmessage"]:
channel = None
if xpsettings["levelupchannel"]:
for channelnum in message.guild.channels:
if channelnum.id == xpsettings["levelupchannel"]:
channel = channelnum
if not channel:
channel = message.channel
if xpsettings["levelupmessagetype"] == 'embed':
embed = discord.Embed(title = message.author.name + " reached level "+str(serverxp[str(message.author.id)]["currentlevel"]),
description = "Cheers 🎉🎊", color = embed_color_premium)
try:
await channel.send(embed = embed)
except:
pass
else:
try:
await channel.send(message.author.name + " reached level "+str(serverxp[str(message.author.id)]["currentlevel"]))
except:
pass
async def xpsystemsetup(interaction, enabled: bool = True):
await interaction.response.defer(ephemeral= True)
try:
xpsettings = json.loads(open('xpranks/'+str(interaction.guild.id) + '-settings.json', 'r').read())
except:
xpsettings = None
if not xpsettings:
xpsettings = {
"enabled": False,
"xppermessage": 1,
"xplevelincrement": 120,
"xppercallminute": 1,
"firstlevelxp": 20,
"cardbackground": None,
"levelupchannel": None,
"dolevelupmessage": True,
"levelupmessagetype": "embed",
"roleasrank": False
}
xpsettings['enabled'] = enabled
json.dump(xpsettings, open('xpranks/'+str(interaction.guild.id) + '-settings.json', 'w'), indent = 4)
status = "enabled" if enabled else "disabled"
embed = discord.Embed(title = "XP/Leveling System", description = f"Your leveling system has been successfully {status}.", color = embed_color_success)
await interaction.followup.send(embed = embed)
async def xpsystemrankcard(interaction, user: discord.User = None):
"""Get your current xp rank
Parameters
----------
user : discord.User
The user to get the rank from"""
if not user:
user = interaction.user
await interaction.response.defer()
try:
xpsettings = json.loads(open('xpranks/'+str(interaction.guild.id) + '-settings.json', 'r').read())
except:
xpsettings = None
if not xpsettings or not xpsettings['enabled']:
embed = discord.Embed(title = "XP/Leveling System", description = "The XP/Leveling System is disabled on this server.", color = embed_color_error)
return await interaction.followup.send(embed= embed)
try:
serverxp = json.loads(open('xpranks/'+str(interaction.guild.id)+'-users.json', 'r').read())
except:
serverxp = {}
if not str(user.id) in serverxp.keys():
serverxp[str(user.id)] = {"currentxp": 0, "currentlevel": 1}
member = await interaction.guild.fetch_member(user.id)
username = user.name
currentxp = serverxp[str(user.id)]["currentxp"]
lastxp = 0
nextxp = xpsettings["firstlevelxp"] + (serverxp[str(user.id)]["currentlevel"] - 1) * xpsettings["xplevelincrement"]
current_level = serverxp[str(user.id)]['currentlevel']
if xpsettings["roleasrank"]:
member = await interaction.guild.fetch_member(user.id)
current_rank = member.roles[0].name
else:
sorted_members = sorted(serverxp.items(), key=lambda x: (x[1]['currentlevel'], x[1]['currentxp']), reverse=True)
current_rank = next((index + 1 for index, (member_id, member_data) in enumerate(sorted_members) if member_id == str(user.id)), None)
background = xpsettings["cardbackground"]
image = await canvacord.rankcard(user = member,
username = username,
currentxp = currentxp,
lastxp = lastxp,
nextxp = nextxp,
level = current_level,
rank = current_rank,
background = background, ranklevelsep = ':', xpsep = '-')
file = discord.File(filename = "rankcard.png", fp = image)
await interaction.followup.send(file = file)
async def xpsystemleaderboard(interaction):
await interaction.response.defer()
try:
xpsettings = json.loads(open('xpranks/'+str(interaction.guild.id) + '-settings.json', 'r').read())
except:
xpsettings = None
if not xpsettings or not xpsettings['enabled']:
return await interaction.followup.send('The XP//Leveling System is disabled on this server.')
try:
serverxp = json.loads(open('xpranks/' + str(interaction.guild.id) + '-users.json', 'r').read())
except:
return await interaction.followup.send('No leaderboard found on this server')
sorted_members = sorted(serverxp.items(), key=lambda x: (x[1]['currentlevel'], x[1]['currentxp']), reverse=True)
top_25 = sorted_members[:25]
embed = discord.Embed(title='XP/Leveling Leaderboard', color = embed_color)
for rank, (member_id, member_data) in enumerate(top_25, start=1):
member = await interaction.guild.fetch_member(int(member_id))
if member:
embed.add_field(name = f"#{rank} {member.display_name}", value=f"Level: {member_data['currentlevel']} | XP: {member_data['currentxp']}", inline=False)
if interaction.guild.icon:
embed.set_thumbnail(url = interaction.guild.icon.url)
await interaction.followup.send(embed= embed)
@client.event
async def on_member_join(member):
announcements = json.loads(open('announcements.json', 'r').read())
if not str(member.guild.id) in announcements:
announcements[str(member.guild.id)] = []
for announcement in announcements[str(member.guild.id)]:
try:
if announcement["action"]== "join":
if announcement["type"] == "banner":
for l in ["banner_avatarcolor", "banner_textcolor"]:
try:
Color(announcement[l])
except:
announcement[l] = 'white'
try:
background_color = Color(announcement['banner_background'])
background = None
except:
background_color = 'black'
background = announcement['banner_background']
image = await canvacord.welcomecard(user = member,
background = background,
avatarcolor = announcement['banner_avatarcolor'],
topcolor = announcement['banner_textcolor'],
bottomcolor=announcement['banner_textcolor'],
backgroundcolor=background_color,
font=None,
toptext=announcement['banner_toptext'],
bottomtext=announcement['banner_bottomtext'])
file = discord.File(filename = 'welcome.png', fp = image)
for channel in member.guild.channels:
if channel.id == announcement["channel"]:
await channel.send(file = file)
if announcement["type"] == "embed":
try:
join_embed_color = Color(n[0]['embed_color']).hex_l
except:
join_embed_color = Color('white').hex_l
join_embed_color = int(join_embed_color.replace('#',''), 16)
embed = discord.Embed(title = announcement["embed_title"].replace("{server}", member.guild.name).replace("{user_name}", member.name),
description = announcement["embed_description"].replace("{server}", member.guild.name).replace("{user_name}", member.name),
color = join_embed_color)
for channel in member.guild.channels:
if channel.id == announcement["channel"]:
await channel.send(embed = embed)
if announcement["type"] == "text":
for channel in member.guild.channels:
if channel.id == announcement["channel"]:
await channel.send(announcement["text_message"].replace("{server}", member.guild.name).replace("{user_name}", member.name))
except:
pass
@client.event
async def on_member_remove(member):
announcements = json.loads(open('announcements.json', 'r').read())
if not str(member.guild.id) in announcements:
announcements[str(member.guild.id)] = []
for announcement in announcements[str(member.guild.id)]:
try:
if announcement["action"]== "leave":
if announcement["type"] == "banner":
for l in ["banner_avatarcolor", "banner_textcolor"]:
try:
Color(announcement[l])
except:
announcement[l] = 'white'
try:
background_color = Color(announcement['banner_background'])
background = None
except:
background_color = 'black'
background = announcement['banner_background']
image = await canvacord.welcomecard(user = member,
background = background,
avatarcolor = announcement['banner_avatarcolor'],
topcolor = announcement['banner_textcolor'],
bottomcolor=announcement['banner_textcolor'],
backgroundcolor=background_color,
font=None,
toptext=announcement['banner_toptext'],
bottomtext=announcement['banner_bottomtext'])
file = discord.File(filename = 'welcome.png', fp = image)
for channel in member.guild.channels:
if channel.id == announcement["channel"]:
await channel.send(file = file)
if announcement["type"] == "embed":
try:
join_embed_color = Color(announcement['embed_color']).hex_l
except:
join_embed_color = Color('white').hex_l
join_embed_color = int(join_embed_color.replace('#',''), 16)
embed = discord.Embed(title = announcement["embed_title"].replace("{server}", member.guild.name).replace("{user_name}", member.name),
description = announcement["embed_description"].replace("{server}", member.guild.name).replace("{user_name}", member.name),
color = join_embed_color)
for channel in member.guild.channels:
if channel.id == announcement["channel"]:
await channel.send(embed = embed)
if announcement["type"] == "text":
for channel in member.guild.channels:
if channel.id == announcement["channel"]:
await channel.send(announcement["text_message"].replace("{server}", member.guild.name).replace("{user_name}", member.name))
except:
pass
async def complete_announcement_label(interaction: discord.Interaction, current: str) -> list[app_commands.Choice[str]]:
announcements = json.loads(open('announcements.json', 'r').read())
n = [i for i in announcements[str(interaction.guild.id)] if current in i["label"]]
guild_id = interaction.guild.id
return [app_commands.Choice(name = s["label"], value = s["label"]) for s in n]
@app_commands.autocomplete(label = complete_announcement_label)
async def announcements_remove_join(interaction, label: str):
"""Remove an announcement
Parameters
----------
label : str
The label of the announcement to remove"""
await interaction.response.defer(ephemeral = True)
staff = json.loads(open('staff.json', 'r').read())
if not interaction.guild:
embed = discord.Embed(title="Announcements", description="This command can only be used in a server!", color = embed_color_error)
await interaction.followup.send(embed = embed)
return
isStaff = False
member = await interaction.guild.fetch_member(interaction.user.id)
if str(interaction.guild.id) in staff:
for role in member.roles:
if role.id in staff[str(interaction.guild.id)]["manager"]:
isStaff = True
if member.guild_permissions.administrator or member.id == interaction.guild.owner.id:
isStaff = True
if not isStaff:
embed = discord.Embed(title="Announcements", description="You don't have permission to use this command!", color = embed_color_error)
await interaction.followup.send(embed = embed)
return
announcements = json.loads(open('announcements.json', 'r').read())
if not str(member.guild.id) in announcements:
announcements[str(member.guild.id)] = []
for n in announcements[str(member.guild.id)]:
if n["label"] == label:
announcements[str(member.guild.id)].remove(n)
json.dump(announcements, open('announcements.json', 'w'), indent = 4)
embed = discord.Embed(title="Announcements", description="The announcement with the label "+label+" got deleted.", color = embed_color_success)
await interaction.followup.send(embed=embed)
@app_commands.choices(announcement_type = [
app_commands.Choice(name="Banner", value="banner"),
app_commands.Choice(name="Embed", value="embed"),
app_commands.Choice(name="Text", value="text")
])
@app_commands.choices(action = [
app_commands.Choice(name="Join", value="join"),
app_commands.Choice(name="Leave", value="leave")
])
async def announcements_add_join(interaction, label: str, channel: discord.TextChannel, announcement_type: str, action: str):
"""Add a new announcement
Parameters
----------
label : str
Label this announcement
channel : discord.TextChannel
The channel to send the join announcement to
announcement_type : str
The type of the announcement to send
action : str
The event when this announcement shall be sent"""
await interaction.response.defer(ephemeral = True)
staff = json.loads(open('staff.json', 'r').read())
if not interaction.guild:
embed = discord.Embed(title="Announcements", description="This command can only be used in a server!", color = embed_color_error)
await interaction.followup.send(embed = embed)
return
isStaff = False
member = await interaction.guild.fetch_member(interaction.user.id)
if str(interaction.guild.id) in staff:
for role in member.roles:
if role.id in staff[str(interaction.guild.id)]["manager"]:
isStaff = True
if member.guild_permissions.administrator or member.id == interaction.guild.owner.id:
isStaff = True
if not isStaff:
embed = discord.Embed(title="Announcements", description="You don't have permission to use this command!", color = embed_color_error)
await interaction.followup.send(embed = embed)
return
announcements = json.loads(open('announcements.json', 'r').read())
if not str(member.guild.id) in announcements:
announcements[str(member.guild.id)] = []
n = [i for i in announcements[str(member.guild.id)] if i["label"] == label]
if len(n) !=0:
embed = discord.Embed(title="Announcements", description="An announcement with this label already exists in this server!", color = embed_color_error)
await interaction.followup.send(embed = embed)
return
if action == 'join':
announcements[str(member.guild.id)].append({
"type": announcement_type,
"text_message": "Welcome {user_name}\nEnjoy your stay in {server}!",
"embed_color": "purple",
"embed_title": "Welcome {user_name}",
"embed_description": "Enjoy your stay in {server}!",
"banner_background": "black",
"banner_avatarcolor": "white",
"banner_textcolor": "white",
"banner_toptext": "Welcome {user_name}",
"banner_bottomtext": "Enjoy your stay in {server}!",
"channel": channel.id,
"label": label,
"action": action
})
if action == 'leave':
announcements[str(member.guild.id)].append({
"type": announcement_type,
"text_message": "{user_name} left {server} 👋",
"embed_color": "purple",
"embed_title": "{user_name} left",
"embed_description": "{server} will miss you.",
"banner_background": "black",
"banner_avatarcolor": "white",
"banner_textcolor": "white",
"banner_toptext": "{user_name} left",
"banner_bottomtext": "{server} will miss you.",
"channel": channel.id,
"label": label,
"action": action
})
json.dump(announcements, open('announcements.json', 'w'), indent = 4)
n = [i for i in announcements[str(member.guild.id)] if i["label"] == label]
if announcement_type == 'banner':
for l in ["banner_avatarcolor", "banner_textcolor"]:
try:
Color(n[0][l])
except:
n[0][l] = 'white'
try:
background_color = Color(n[0]['banner_background'])
background = None
except:
background_color = 'black'
background = n[0]['banner_background']
image = await canvacord.welcomecard(user = member,
background = background,
avatarcolor = n[0]['banner_avatarcolor'],
topcolor = n[0]['banner_textcolor'],
bottomcolor=n[0]['banner_textcolor'],
backgroundcolor=background_color,
font=None,
toptext=n[0]['banner_toptext'],
bottomtext=n[0]['banner_bottomtext'])
file = discord.File(filename='welcome.png', fp=image)
class JoinAnnouncementBannerEditView(ui.View):
def __init__(self, member, label):
super().__init__(timeout = None)
self.member = member
self.label = label
@ui.button(label = "Edit banner", style = discord.ButtonStyle.green)
async def edit_banner(self, interaction, button):
class EditColorsModal(ui.Modal, title='Edit banner'):
announcements = json.loads(open('announcements.json', 'r').read())
n = [i for i in announcements[str(member.guild.id)] if i["label"] == label][0]
background = ui.TextInput(label = 'Background', default = n["banner_background"])
avatarcolor = ui.TextInput(label = 'Avatar color', default = n["banner_avatarcolor"])
textcolor = ui.TextInput(label = 'Text color', default = n["banner_textcolor"])
toptext = ui.TextInput(label = 'Top line', default = n["banner_toptext"])
bottomtext = ui.TextInput(label = 'Bottom line', default = n["banner_bottomtext"])
def __init__(self, member, label):
super().__init__(timeout = None)
self.label = label
self.member = member
async def on_submit(self, interaction):
await interaction.response.defer(ephemeral=True)
announcements = json.loads(open('announcements.json', 'r').read())
n = next((index for index, announcement in enumerate(announcements[str(member.guild.id)]) if announcement["label"] == label), None)
announcements[str(member.guild.id)][n]['banner_background'] = self.background.value
announcements[str(member.guild.id)][n]['banner_avatarcolor'] = self.avatarcolor.value
announcements[str(member.guild.id)][n]['banner_textcolor'] = self.textcolor.value
announcements[str(member.guild.id)][n]['banner_toptext'] = self.toptext.value
announcements[str(member.guild.id)][n]['banner_bottomtext'] = self.bottomtext.value
for l in ["banner_avatarcolor", "banner_textcolor"]:
try:
Color(announcements[str(member.guild.id)][n][l])
except:
announcements[str(member.guild.id)][n][l] = 'white'
json.dump(announcements, open('announcements.json', 'w'), indent = 4)
n = [i for i in announcements[str(member.guild.id)] if i["label"] == label]
try:
background_color = Color(n[0]['banner_background'])
background = None
except:
background_color = 'black'
background = n[0]['banner_background']
image = await canvacord.welcomecard(user = self.member,
background = background,
avatarcolor = n[0]['banner_avatarcolor'],
topcolor = n[0]['banner_textcolor'],
bottomcolor=n[0]['banner_textcolor'],
backgroundcolor=background_color,
font=None,
toptext=n[0]['banner_toptext'],
bottomtext=n[0]['banner_bottomtext'])
file = discord.File(filename='welcome.png', fp=image)
await interaction.followup.send(content = "Saved banner: ", file = file, view = JoinAnnouncementBannerEditView(member = self.member, label = self.label), ephemeral = True)
await interaction.response.send_modal(EditColorsModal(member = self.member, label = self.label))
await interaction.followup.send(content = "Saved banner: ", file = file, view = JoinAnnouncementBannerEditView(member = member, label = label))
elif announcement_type == 'embed':
try:
join_embed_color = Color(n[0]['embed_color']).hex_l
except:
join_embed_color = Color('white').hex_l
join_embed_color = int(join_embed_color.replace('#',''), 16)
embed = discord.Embed(title = n[0]["embed_title"].replace("{server}", interaction.guild.name).replace("{user_name}", member.name),
description = n[0]["embed_description"].replace("{server}", interaction.guild.name).replace("{user_name}", member.name),
color = join_embed_color)
if member.avatar:
embed.set_thumbnail(url = member.avatar.url)
class JoinAnnouncementEmbedEditView(ui.View):
def __init__(self, member, label):
super().__init__(timeout = None)
self.member = member
self.label = label
@ui.button(label = "Edit embed", style = discord.ButtonStyle.green)
async def edit_banner(self, interaction, button):
class EditColorsModal(ui.Modal, title='Edit embed'):
announcements = json.loads(open('announcements.json', 'r').read())
n = [i for i in announcements[str(member.guild.id)] if i["label"] == label][0]
color = ui.TextInput(label = 'Color', default = n["embed_color"])
etitle = ui.TextInput(label = 'Title', default = n["embed_title"])
description = ui.TextInput(label = 'Description', default = n["embed_description"], style=discord.TextStyle.long)
def __init__(self, member, label):
super().__init__(timeout = None)
self.label = label
self.member = member
async def on_submit(self, interaction):
await interaction.response.defer(ephemeral=True)
announcements = json.loads(open('announcements.json', 'r').read())
n = next((index for index, announcement in enumerate(announcements[str(member.guild.id)]) if announcement["label"] == label), None)
announcements[str(member.guild.id)][n]['embed_color'] = self.color.value
announcements[str(member.guild.id)][n]['embed_title'] = self.etitle.value
announcements[str(member.guild.id)][n]['embed_description'] = self.description.value
try:
join_embed_color = Color(announcements[str(member.guild.id)][n]['embed_color']).hex_l
print(join_embed_color)
except:
join_embed_color = Color('white').hex_l
join_embed_color = int(join_embed_color.replace('#',''), 16)
json.dump(announcements, open('announcements.json', 'w'), indent = 4)
n = [i for i in announcements[str(member.guild.id)] if i["label"] == label]
embed = discord.Embed(title = n[0]["embed_title"].replace("{server}", interaction.guild.name).replace("{user_name}", member.name),
description = n[0]["embed_description"].replace("{server}", interaction.guild.name).replace("{user_name}", member.name),
color = join_embed_color)
if member.avatar:
embed.set_thumbnail(url = member.avatar.url)
await interaction.followup.send(content = 'Saved embed: ', embed = embed, view = JoinAnnouncementEmbedEditView(member = self.member, label = self.label), ephemeral = True)
await interaction.response.send_modal(EditColorsModal(member = self.member, label = self.label))
await interaction.followup.send(content = 'Saved embed: ', embed = embed, view = JoinAnnouncementEmbedEditView(member = member, label = label))
else:
class JoinAnnouncementTextEditView(ui.View):
def __init__(self, member, label):
super().__init__(timeout = None)
self.member = member
self.label = label
@ui.button(label = "Edit text", style = discord.ButtonStyle.green)
async def edit_banner(self, interaction, button):
class EditColorsModal(ui.Modal, title='Edit text'):
announcements = json.loads(open('announcements.json', 'r').read())
n = [i for i in announcements[str(member.guild.id)] if i["label"] == label][0]
message = ui.TextInput(label = 'Message', default = n["text_message"], style=discord.TextStyle.long)
def __init__(self, member, label):
super().__init__(timeout = None)
self.label = label
self.member = member
async def on_submit(self, interaction):
await interaction.response.defer(ephemeral=True)
announcements = json.loads(open('announcements.json', 'r').read())
n = next((index for index, announcement in enumerate(announcements[str(member.guild.id)]) if announcement["label"] == label), None)