-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpurgebot.py
764 lines (621 loc) · 24.4 KB
/
purgebot.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
import asyncio
import os
from datetime import datetime
from time import mktime
import dateparser
import disnake
import disnake.ui
from disnake import TextInputStyle, ButtonStyle
from disnake.ext import commands
from disnake.ui import Button, Modal, TextInput, View
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
BOT_IMAGE = os.getenv("BOT_IMAGE")
command_prefix = commands.when_mentioned
description = "Salutations! I'm here to help clean up your rooms!"
intents = disnake.Intents.default()
bot = commands.Bot(
case_insensitive=True,
command_prefix=command_prefix,
description=description,
help_command=None,
intents=intents,
test_guilds=[679218449024811067],
)
###################
## PURGE COMMAND ##
###################
@bot.slash_command(default_member_permissions=disnake.Permissions(manage_channels=True))
async def purge(inter: disnake.ApplicationCommandInteraction):
"""Deletes the current channel"""
# Build Modal UI
components = [
TextInput(
label="When? (leave blank if delete immediately)",
placeholder="Example: 'in two minutes', '1991-05-17', or similar",
custom_id="when",
style=TextInputStyle.short,
required=False,
),
TextInput(
label="Custom message",
placeholder="Custom message you want PurgeBot to say",
custom_id="custom_msg",
style=TextInputStyle.paragraph,
required=False,
),
TextInput(
label="Attachment",
placeholder="Link to image/GIF you want to attach to your custom message",
custom_id="attachment",
style=TextInputStyle.short,
required=False,
),
]
# Truncate channel name for modal title
channel_name = inter.channel.name
char_limit = 33
title = (
(channel_name[:char_limit] + "...")
if len(channel_name) > char_limit
else channel_name
)
modal = Modal(
title=f"Delete #{title}?",
custom_id="purge_modal",
components=components,
# timeout=5,
)
await inter.response.send_modal(modal=modal)
# @bot.slash_command(default_member_permissions=disnake.Permissions(manage_channels=True))
# async def purge(inter: disnake.ApplicationCommandInteraction):
# """Deletes the current channel"""
# async def timeout():
# bot_reply = f"Sorry, {inter.author.mention}. You did not respond in time. Try again next time!"
# await inter.edit_original_message(bot_reply)
# # Build Modal UI
# components = [
# TextInput(
# label="When? (leave blank if delete immediately)",
# placeholder="Example: 'in two minutes', '1991-05-17', or similar",
# custom_id="when",
# style=TextInputStyle.short,
# required=False,
# ),
# TextInput(
# label="Custom message",
# placeholder="Custom message you want PurgeBot to say",
# custom_id="custom_msg",
# style=TextInputStyle.paragraph,
# required=False,
# ),
# TextInput(
# label="Attachment",
# placeholder="Link to image/GIF you want to attach to your custom message",
# custom_id="attachment",
# style=TextInputStyle.short,
# required=False,
# ),
# ]
# modal = Modal(
# title=f"Delete #{inter.channel.name}?",
# custom_id="purge_modal",
# components=components,
# )
# # modal.on_timeout = timeout
# await inter.response.send_modal(modal=modal)
# try:
# modal_inter: disnake.ModalInteraction = await bot.wait_for(
# "modal_submit",
# check=lambda i: i.custom_id == "purge_modal"
# and i.author.id == inter.author.id,
# timeout=5,
# )
# except asyncio.TimeoutError:
# # Close modal on timeout here
# # await inter.followup.
# modal_inter = None
# if modal_inter is None:
# await inter.response.send_message("Cant process, timedout")
############################
## CHECK_CHANNELS COMMAND ##
############################
@bot.slash_command(default_member_permissions=disnake.Permissions(manage_guild=True))
async def check_channels(inter: disnake.ApplicationCommandInteraction):
"""List channels to be deleted"""
async def purge_callback(inter_purge: disnake.MessageInteraction):
content = ""
with open(get_to_delete_file(inter.guild_id), "r") as f:
lines = f.readlines()
for line in lines:
values = line.strip().split(",")
channel_id = int(values[0])
dt = values[1]
if not is_when_valid(dt):
channel = bot.get_channel(channel_id)
if channel is not None:
content = content + f"- #{channel.name}"
update_to_delete(inter.guild_id, channel_id)
await channel.delete()
embed = embed_builder("Mass-purged channels", content)
# Log event
await log_mass_delete(inter, content)
# Send embed
await inter_purge.send(embed=embed)
has_old_dates = False
# Build embed
content = ""
try:
with open(get_to_delete_file(inter.guild_id), "r") as f:
lines = f.readlines()
for line in lines:
values = line.strip().split(",")
dt = values[1]
if not is_when_valid(dt):
has_old_dates = True
if values[1] != "-":
dt = dt_to_discord_date_duration(values[1])
content = content + f"- <#{values[0]}>: {dt}\n"
except FileNotFoundError:
# Do nothing
pass
finally:
if content == "":
content = "None"
embed = embed_builder("Channels scheduled to be purged", content)
await inter.send(embed=embed)
# Build Button UI
btn_purge = Button(style=ButtonStyle.danger, label="Mass Purge", emoji="⚠️")
btn_purge.callback = purge_callback
view = View()
view.add_item(btn_purge)
if has_old_dates:
bot_reply = (
"Looks like there were still some old channels that need to be cleaned..."
)
await inter.send(bot_reply, view=view, ephemeral=True)
#############################
## SET_LOG_CHANNEL COMMAND ##
#############################
@bot.slash_command(default_member_permissions=disnake.Permissions(manage_guild=True))
async def set_log_channel(
inter: disnake.CommandInteraction,
channel: disnake.TextChannel = commands.Param(
description="The channel you want to send PurgeBot logs to"
),
):
"""Sets which channel to send PurgeBot logs"""
set_log_channel_id(inter.guild_id, channel.id)
await inter.send(f"PurgeBot logs will be sent to <#{channel.id}>!")
#################################
## ADD_DELETE_CATEGORY COMMAND ##
#################################
@bot.slash_command(default_member_permissions=disnake.Permissions(manage_guild=True))
async def add_delete_category(
inter: disnake.CommandInteraction,
category: disnake.CategoryChannel = commands.Param(
description="The category where the channels for deletion belong to"
),
):
"""Adds a category to the list of categories PurgeBot will delete channels from"""
append_delete_category_id(inter.guild_id, category.id)
await inter.send(
f"PurgeBot adds `{category.name}` to the list of categories it will only delete channels from!"
)
####################################
## REMOVE_DELETE_CATEGORY COMMAND ##
####################################
@bot.slash_command(default_member_permissions=disnake.Permissions(manage_guild=True))
async def remove_delete_category(
inter: disnake.CommandInteraction,
category: disnake.CategoryChannel = commands.Param(
description="The category where the channels for deletion belong to"
),
):
"""Removes a category to the list of categories PurgeBot will delete channels from"""
remove_delete_category_id(inter.guild_id, category.id)
await inter.send(
f"PurgeBot removes `{category.name}` from the list of categories it will only delete channels from!"
)
#############################
## PROTECT_CHANNEL COMMAND ##
#############################
@bot.slash_command(default_member_permissions=disnake.Permissions(manage_guild=True))
async def protect_channel(
inter: disnake.CommandInteraction,
channel: disnake.TextChannel = commands.Param(
description="A channel you want to protect from deletion"
),
):
"""Adds a channel to the list of channels protected from PurgeBot"""
protect_channel_id(inter.guild_id, channel.id)
await inter.send(f"<#{channel.id}> is protected from PurgeBot deletion!")
###############################
## UNPROTECT_CHANNEL COMMAND ##
###############################
@bot.slash_command(default_member_permissions=disnake.Permissions(manage_guild=True))
async def unprotect_channel(
inter: disnake.CommandInteraction,
channel: disnake.TextChannel = commands.Param(
description="A channel you want to remove from the protection list"
),
):
"""Removes a channel to the list of channels protected from PurgeBot"""
unprotect_channel_id(inter.guild_id, channel.id)
await inter.send(f"<#{channel.id}> is no longer protected from PurgeBot deletion!")
#########################
## VIEW_CONFIG COMMAND ##
#########################
@bot.slash_command(default_member_permissions=disnake.Permissions(manage_guild=True))
async def view_config(inter: disnake.CommandInteraction):
"""View current PurgeBot configuration"""
log_channel = "Not set"
log_channel_id = get_log_channel_id(inter.guild_id)
if log_channel_id:
log_channel = bot.get_channel(log_channel_id)
if log_channel:
log_channel = f"<#{log_channel.id}>"
else:
# Remove if it does not exist
set_log_channel_id(inter.guild_id, "")
delete_categories = ""
try:
with open(get_delete_categories_file(inter.guild_id), "r") as f:
ids = f.readlines()
for id in ids:
channel = bot.get_channel(int(id))
if channel:
delete_categories = delete_categories + f"- {channel.name}\n"
else:
# Remove if it does not exist
remove_delete_category_id(inter.guild_id, id)
except FileNotFoundError:
# Do nothing
pass
finally:
if delete_categories == "":
delete_categories = "None"
protected_channels = ""
try:
with open(get_protected_channels_file(inter.guild_id), "r") as f:
ids = f.readlines()
for id in ids:
channel = bot.get_channel(int(id))
if channel:
protected_channels = protected_channels + f"- <#{channel.id}>\n"
else:
# Remove if it does not exist
unprotect_channel_id(inter.guild_id, id)
except FileNotFoundError:
# Do nothing
pass
finally:
if protected_channels == "":
protected_channels = "None"
# Build embed
embed = embed_builder(
"PurgeBot Configuration", "Here are you current configurations"
)
embed.add_field(name="Log Channel", value=log_channel, inline=False)
embed.add_field(name="Delete Categories", value=delete_categories, inline=False)
embed.add_field(name="Protected Channels", value=protected_channels, inline=False)
await inter.send(embed=embed)
###################
## HELPER: PURGE ##
###################
def is_channel_valid_for_deletion(inter: disnake.MessageInteraction):
log_channel_id = get_log_channel_id(inter.guild_id)
if log_channel_id is None:
log_channel_id = 0
delete_category_ids = []
try:
with open(get_delete_categories_file(inter.guild_id), "r") as f:
ids = f.readlines()
for id in ids:
channel = bot.get_channel(int(id))
if channel:
delete_category_ids.append(channel.id)
else:
# Remove if it does not exist
remove_delete_category_id(inter.guild_id, id)
except FileNotFoundError:
# Do nothing
pass
protected_channel_ids = []
try:
with open(get_protected_channels_file(inter.guild_id), "r") as f:
ids = f.readlines()
for id in ids:
channel = bot.get_channel(int(id))
if channel:
protected_channel_ids.append(channel.id)
else:
# Remove if it does not exist
unprotect_channel_id(inter.guild_id, id)
except FileNotFoundError:
# Do nothing
pass
is_not_log_channel = inter.channel_id != log_channel_id
is_in_delete_category = inter.channel.category_id in delete_category_ids
is_not_protected = inter.channel_id not in protected_channel_ids
return is_not_log_channel and is_in_delete_category and is_not_protected
async def delete_channel(inter: disnake.MessageInteraction, when_dt_str):
# Set delay
if when_dt_str == "-":
await asyncio.sleep(3)
else:
when_to_delete = str_to_datetime(when_dt_str)
now = datetime.now()
delay_in_seconds = (when_to_delete - now).total_seconds()
await asyncio.sleep(delay_in_seconds)
try:
# Delete channel
await inter.channel.delete()
# Log event
await log_delete(inter)
except disnake.errors.NotFound:
# Do nothing
pass
#####################
## HELPER: LOGGING ##
#####################
def get_log_channel_file(guild_id):
return f"{guild_id}_log_channel.txt"
def get_log_channel_id(guild_id):
try:
with open(get_log_channel_file(guild_id), "r") as f:
channel_id = int(f.read())
except:
channel_id = None
finally:
return channel_id
def set_log_channel_id(guild_id, channel_id):
with open(get_log_channel_file(guild_id), "w+") as f:
f.write(str(channel_id))
def get_delete_categories_file(guild_id):
return f"{guild_id}_delete_categories.txt"
def append_delete_category_id(guild_id, category_id):
with open(get_delete_categories_file(guild_id), "a+") as f:
line = f"{category_id}\n"
f.write(line)
def remove_delete_category_id(guild_id, category_id):
filename = get_delete_categories_file(guild_id)
updated_ids = []
try:
with open(filename, "r") as f:
ids = f.readlines()
for id in ids:
if id.strip() != str(category_id):
updated_ids.append(id)
except FileNotFoundError:
# Do nothing
pass
finally:
with open(filename, "w+") as f:
f.writelines(updated_ids)
def get_protected_channels_file(guild_id):
return f"{guild_id}_protected_channels.txt"
def protect_channel_id(guild_id, channel_id):
with open(get_protected_channels_file(guild_id), "a+") as f:
line = f"{channel_id}\n"
f.write(line)
def unprotect_channel_id(guild_id, channel_id):
filename = get_protected_channels_file(guild_id)
updated_ids = []
try:
with open(filename, "r") as f:
ids = f.readlines()
for id in ids:
if id.strip() != str(channel_id):
updated_ids.append(id)
except FileNotFoundError:
# Do nothing
pass
finally:
with open(filename, "w+") as f:
f.writelines(updated_ids)
def get_to_delete_file(guild_id):
return f"{guild_id}_to_delete.txt"
def append_to_delete(guild_id, channel_id, when_dt_str):
with open(get_to_delete_file(guild_id), "a+") as f:
f.write(f"{channel_id},{when_dt_str}\n")
def update_to_delete(guild_id, channel_id):
filename = get_to_delete_file(guild_id)
updated_id_time_pairs = []
try:
with open(filename, "r") as f:
id_time_pairs = f.readlines()
for id_time in id_time_pairs:
values = id_time.strip().split(",")
if values[0] != str(channel_id):
updated_id_time_pairs.append(id_time)
except FileNotFoundError:
# Do nothing
pass
finally:
with open(filename, "w+") as f:
f.writelines(updated_id_time_pairs)
def embed_builder(title, description):
embed = disnake.Embed(title=title, description=description)
embed.set_author(name=bot.user.name, icon_url=BOT_IMAGE)
return embed
async def log_attempt_to_delete(
inter: disnake.MessageInteraction, message, channel_id, when_dt_str
):
append_to_delete(inter.guild_id, channel_id, when_dt_str)
log_channel_id = get_log_channel_id(inter.guild_id)
if log_channel_id:
log_channel = bot.get_channel(log_channel_id)
embed = embed_builder("PurgeBot will clean soon...", message)
embed.add_field(
name="Initiated by", value=f"{inter.author.mention}", inline=False
)
await log_channel.send(embed=embed)
async def log_delete(inter: disnake.MessageInteraction):
update_to_delete(inter.guild_id, inter.channel_id)
log_channel_id = get_log_channel_id(inter.guild_id)
if log_channel_id:
log_channel = bot.get_channel(log_channel_id)
embed = embed_builder(
"PurgeBot is done cleaning!", f"#{inter.channel.name} has been deleted!"
)
embed.add_field(
name="Initiated by", value=f"{inter.author.mention}", inline=False
)
await log_channel.send(embed=embed)
async def log_mass_delete(inter: disnake.MessageInteraction, content):
log_channel_id = get_log_channel_id(inter.guild_id)
if log_channel_id:
log_channel = bot.get_channel(log_channel_id)
embed = embed_builder("PurgeBot is done cleaning! [MASS PURGE]", content)
embed.add_field(
name="Initiated by", value=f"{inter.author.mention}", inline=False
)
await log_channel.send(embed=embed)
############
## HELPER ##
############
DATE_TIME_FORMAT = "%Y%m%d%H%M%S"
def to_unix(dt: datetime):
return int(mktime(dt.timetuple()))
def str_to_datetime(dt_str):
return datetime.strptime(dt_str, DATE_TIME_FORMAT)
def datetime_to_str(dt: datetime):
return dt.strftime(DATE_TIME_FORMAT)
def dt_to_discord_date_duration(dt_str):
dt = str_to_datetime(dt_str)
unix = to_unix(dt)
return f"<t:{unix}:F> (<t:{unix}:R>)"
def parse_when(when):
when_parsed = dateparser.parse(when)
if when_parsed:
return datetime_to_str(when_parsed)
else:
return None
def is_when_valid(dt_str):
if dt_str == "-":
return True
when_to_delete = str_to_datetime(dt_str)
now = datetime.now()
return "-" not in str(when_to_delete - now)
########################
## DISCORD BOT EVENTS ##
########################
@bot.event
async def on_modal_submit(inter: disnake.ModalInteraction):
if "purge_modal" == inter.custom_id:
when = inter.text_values.get("when")
when_dt_str = "-"
if when != "":
when_dt_str = parse_when(when)
custom_msg = inter.text_values.get("custom_msg")
attachment = inter.text_values.get("attachment")
if when_dt_str is None:
# If `when` cannot be parsed
bot_reply = f"Sorry, {inter.author.mention}. I don't understand when you want me to delete the channel. You said: `{when}`"
await inter.send(bot_reply, ephemeral=True)
else:
async def no_callback(inter_no: disnake.MessageInteraction):
bot_reply = f"Okay, {inter.author.mention}! I will **not** delete <#{inter.channel_id}>!"
await inter.edit_original_message(bot_reply, view=None)
await inter_no.response.defer()
async def yes_callback(inter_yes: disnake.MessageInteraction):
# Check if when is still valid
if is_when_valid(when_dt_str):
# Check if channel is valid for deletion
if is_channel_valid_for_deletion(inter):
message = (
f"Deleting #{inter.channel.name} (<#{inter.channel_id}>) "
)
if when_dt_str != "-":
embed_value = (
f"by {dt_to_discord_date_duration(when_dt_str)}"
)
# message = message + f"by {embed_value}"
else:
embed_value = "in a few seconds"
message = message + embed_value
# Log event
await log_attempt_to_delete(
inter, message, inter.channel_id, when_dt_str
)
# Send reply
bot_reply = (
f"Understood, {inter.author.mention}! {embed_value}..."
)
await inter.edit_original_message(bot_reply, view=None)
# Build embed
embed = embed_builder("Attention!", custom_msg)
embed.add_field(
name="This channel will be deleted",
value=embed_value,
inline=False,
)
embed.add_field(
name="Initiated by",
value=f"{inter.author.mention}",
inline=False,
)
if attachment:
embed.set_image(url=attachment)
await inter_yes.send(embed=embed)
# Delete channel
await delete_channel(inter, when_dt_str)
else:
bot_reply = f"Sorry, {inter.author.mention}. I'm not allowed to delete <#{inter.channel_id}>."
await inter.edit_original_message(bot_reply, view=None)
await inter_yes.response.defer()
else:
bot_reply = f"Sorry, {inter.author.mention}. The date/time you told me to delete the channel has already happened."
await inter.edit_original_message(bot_reply, view=None)
await inter_yes.response.defer()
async def timeout():
bot_reply = f"Sorry, {inter.author.mention}. You did not respond in time. Try again next time!"
await inter.edit_original_message(bot_reply, view=None)
# Build Button UI
btn_no = Button(style=ButtonStyle.secondary, label="No")
btn_no.callback = no_callback
btn_yes = Button(style=ButtonStyle.danger, label="Yes", emoji="⚠️")
btn_yes.callback = yes_callback
view = View(timeout=180) # 3 minutes
view.add_item(btn_no)
view.add_item(btn_yes)
view.on_timeout = timeout
# Build bot reply
bot_reply = f"{inter.author.mention}, are you sure you want to delete <#{inter.channel_id}>"
if when_dt_str != "-":
bot_reply = (
bot_reply + f" by {dt_to_discord_date_duration(when_dt_str)}"
)
bot_reply = (
bot_reply
+ "?\n\n*(Note: Once you click `Yes`, this cannot be cancelled!)*"
)
# Send response
await inter.send(bot_reply, view=view, ephemeral=True)
@bot.event
async def on_connect():
print(f"{bot.user.name} has connected to Discord!")
await bot.change_presence(
activity=disnake.Activity(
name="room purge! | /purge",
type=disnake.ActivityType.playing,
)
)
@bot.event
async def on_ready():
print(f"{bot.user.name} is live!")
#############
## TESTING ##
#############
# @bot.slash_command(guild_ids=[679218449024811067])
# async def test(inter: disnake.CommandInteraction):
# """TEST"""
# await inter.send(f"HELLO {get_duration()}")
#######################
## DISCORD BOT START ##
#######################
if __name__ == "__main__":
token = os.getenv("PURGEBOT_DISCORD_TOKEN")
bot.run(token)