-
Notifications
You must be signed in to change notification settings - Fork 0
/
bruhbot.py
977 lines (881 loc) · 39.4 KB
/
bruhbot.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
import asyncio
from contextlib import suppress
from datetime import date, datetime
from io import BytesIO
import json
import os
from random import choice, random
import re
import string
import traceback
from dateutil.relativedelta import relativedelta
import discord
from discord.ext import commands
from discord.utils import escape_markdown
from dotenv import load_dotenv
from bruhbot import ResponseBackup, ErrorLogger, ForzaSeason
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="$", intents=intents)
bot.remove_command("help")
load_dotenv(".\\bruhbot\\.env")
token = os.getenv("TOKEN")
owner_id = int(os.getenv("OWNER_ID"))
ignored_channel = int(os.getenv("IGNORED_CHANNEL"))
here = os.path.dirname(__file__)
async def check_age():
def plural(num: int):
return 0 if num == 1 else 1
while True:
now = date.today()
bday = date(2020, 6, 20)
rdelta = relativedelta(now, bday)
year = ["year", "years"]
if rdelta.months == 0 and rdelta.days == 0:
await bot.change_presence(
activity=discord.CustomActivity(
name=f"Been a stinky dingus for {rdelta.years} {year[plural(rdelta.years)]}",
emoji=discord.PartialEmoji.from_str("🎂"),
)
)
else:
await bot.change_presence(status=discord.Status.online)
await asyncio.sleep(3600)
async def auto_backup():
try:
with open(f"{here}\\bruhbot\\data.json", "r+") as f:
data = json.load(f)
now = date.today()
last = datetime.strptime(data["lastbackup"], "%Y-%m-%d")
rdelta = relativedelta(now, last)
if rdelta.months >= 1:
ResponseBackup.run()
data["lastbackup"] = now.strftime("%Y-%m-%d")
f.seek(0)
json.dump(data, f, indent=4)
else:
return
except Exception:
owner = await bot.fetch_user(owner_id)
await owner.send("Error logged in auto backup.")
ErrorLogger.run(traceback.format_exc())
return
async def get_color(ctx):
color = ctx.guild.get_member(bot.user.id).top_role.color if ctx.guild else 0xE74C3C # red
return color
async def send_image(ctx, response: str):
try:
image = response.replace(" - image", "")
await ctx.reply(file=discord.File(f"{here}\\images\\{image}"), mention_author=False)
except Exception: # missing image
await ctx.reply("Error logged in send_image.")
ErrorLogger.run(traceback.format_exc())
return
@bot.event
async def on_ready():
servers = list(bot.guilds)
print(f"{bot.user.name}(ID:{bot.user.id}) connected to {str(len(servers))} servers")
await auto_backup()
await check_age()
@bot.event
async def on_message(msg):
try:
await bot.process_commands(msg)
ctx = await bot.get_context(msg)
if ctx.author.bot or ctx.channel.id == ignored_channel:
return
nospace = re.sub("[^a-zA-Z0-9]", "", msg.content).lower()
name = re.sub("[^a-zA-Z0-9]", "", ctx.me.display_name).lower()
top_role = str(ctx.guild.get_member(bot.user.id).top_role.id) if ctx.guild else None
if (
name in nospace
or top_role is not None
and top_role in nospace
or bot.user in msg.mentions
or (
msg.reference is not None
and msg.reference.resolved is not None
and msg.reference.resolved.author == bot.user
)
):
async with ctx.typing():
if random() < 0.005:
await ctx.reply(ctx.author.display_avatar, mention_author=False)
return
responses = []
with open(f"{here}\\bruhbot\\responses.txt", "r", encoding="utf-8") as f:
for line in f:
current = line[:-1]
responses.append(current)
response = choice(responses).replace(r"\n", "\n")
if response.endswith("- image"):
await send_image(ctx, response)
return
await ctx.reply(response, mention_author=False)
return
if "69" in nospace:
await ctx.reply("nice", mention_author=False) # nice
return
if "420" in nospace:
await ctx.reply("weed", mention_author=False) # weed
return
except Exception:
await ctx.reply("Error logged in on_message.")
ErrorLogger.run(traceback.format_exc())
@bot.command()
async def addr(ctx, *, arg: str = None):
try:
if ctx.message.attachments or ctx.message.reference: # image support
if ctx.message.reference:
attachments = ctx.message.reference.resolved.attachments
else:
attachments = ctx.message.attachments
invalid_counter = 0
size_counter = 0
dupe_counter = 0
filesize_limit = 26214400
for attachment in attachments:
dupe = False
if "image" in attachment.content_type:
if attachment.size > filesize_limit:
if len(attachments) == 1:
await ctx.reply("Error: File too large. (Max 25MB)")
return
else:
size_counter += 1
ext = os.path.splitext(attachment.filename)[-1]
with open(f"{here}\\bruhbot\\responses.txt", "r", encoding="utf-8") as f:
if arg and len(attachments) == 1:
pre = os.path.splitext("".join(arg[:]))[0] + ext
else:
if (
attachment.filename == f"image0{ext}"
or attachment.filename == f"unknown{ext}"
or attachment.filename == f"image{ext}"
):
new = "".join(choice(string.ascii_letters + string.digits) for i in range(6))
while os.path.exists(f".\\images\\{new}"):
new = new + "".join(choice(string.digits))
pre = new + ext
else:
pre = attachment.filename
file = f.readlines()
for line in file:
if pre.lower() == line.lower().replace(" - image\n", ""):
if len(attachments) == 1:
await ctx.reply(f"Error: **'{pre}'** already exists:")
return
else:
dupe_counter += 1
dupe = True
break
if dupe is False:
async with ctx.typing():
await attachment.save(f"{here}\\images\\{pre}")
else:
continue
with open(f"{here}\\bruhbot\\responses.txt", "a", encoding="utf-8") as f:
f.write(f"{pre} - image\n")
else:
if len(attachments) == 1:
await ctx.reply("Error: Invalid file type.")
return
else:
invalid_counter += 1
if (
len(attachments) - invalid_counter == 1
or len(attachments) - dupe_counter == 1
or len(attachments) - size_counter == 1
):
added = "Image was added."
elif (
len(attachments) - invalid_counter == 0
or len(attachments) - dupe_counter == 0
or len(attachments) - size_counter == 0
):
added = "No images were added."
else:
added = "Images were added."
if not invalid_counter == len(attachments):
await ctx.reply(f"{added} :thumbsup:", mention_author=False)
if dupe_counter > 0:
await ctx.send(f"Blocked {str(dupe_counter)} duplicate files.")
if invalid_counter > 0:
await ctx.send(f"Blocked {str(invalid_counter)} invalid files.")
if size_counter > 0:
await ctx.send(f"Blocked {str(size_counter)} large files. (Max 25MB)")
return
if not arg and not ctx.message.reference:
await help(ctx, "addr")
return
with open(f"{here}\\bruhbot\\responses.txt", "r", encoding="utf-8") as f:
pre1 = "".join(arg[:])
pre = pre1.replace("\n", r"\n")
file = f.readlines()
for line in file:
if pre.lower() == line.lower().strip("\n"):
await ctx.reply("Error: Response already exists.")
return
with open(f"{here}\\bruhbot\\responses.txt", "a", encoding="utf-8") as f:
f.write(pre + "\n")
await ctx.reply(f"**'{pre1}'** was added. :thumbsup:", mention_author=False)
except Exception:
await ctx.reply("Error logged in addr.")
ErrorLogger.run(traceback.format_exc())
@bot.command()
async def delr(ctx, *arg: str):
try:
def get_list(responses: list, start: int, end: int, curPage: int):
numbered = []
for i, j in enumerate(responses[start:end], 1):
if len(j) > 400:
j = j[:404] + "..."
j = escape_markdown(str(i) + ": " + j)
numbered.append(j)
content = "\n".join(numbered)
emb = discord.Embed(
title="Delete response",
description=f"**Page {curPage}/{pages}:**\n{content}",
color=color,
)
return emb
class pageModal(discord.ui.Modal, title="Enter a page number"):
page = discord.ui.TextInput(
style=discord.TextStyle.short,
label="Page",
required=True,
placeholder="Enter a page number",
)
async def on_submit(self, interaction: discord.Interaction):
await interaction.response.defer()
if not int(self.page.value):
raise Exception
async def on_error(self, interaction: discord.Interaction):
await interaction.followup.send("Error: Page must be a number.", ephemeral=True)
class menuView(discord.ui.View):
async def on_timeout(self) -> None:
emb = discord.Embed(title="Timeout", description="", color=color)
await self.msg.edit(embed=emb, view=None)
self.timeout = True
self.stop()
async def update(self):
if self.curPage == 1:
self.buttonBack10.disabled = True
self.buttonBack.disabled = True
else:
self.buttonBack10.disabled = False
self.buttonBack.disabled = False
if self.curPage == self.pages:
self.buttonNext.disabled = True
self.buttonNext10.disabled = True
else:
self.buttonNext.disabled = False
self.buttonNext10.disabled = False
if len(responses[self.start : self.end]) < 5:
self.button5.disabled = True
if len(responses[self.start : self.end]) < 4:
self.button4.disabled = True
if len(responses[self.start : self.end]) < 3:
self.button3.disabled = True
if len(responses[self.start : self.end]) < 2:
self.button2.disabled = True
else:
self.button1.disabled = False
self.button2.disabled = False
self.button3.disabled = False
self.button4.disabled = False
self.button5.disabled = False
async def check(self, interaction: discord.Interaction):
if interaction.user.id == ctx.author.id:
return True
else:
if self.counter < 5:
await interaction.followup.send("That's not your button.", ephemeral=True)
self.counter += 1
elif self.counter >= 5 and self.counter < 8:
await interaction.followup.send("Dude stop.", ephemeral=True)
self.counter += 1
else:
await interaction.followup.send("Seriously dude, enough.", ephemeral=True)
counter: int = 0
choice: int = None
cancel: bool = None
@discord.ui.button(style=discord.ButtonStyle.danger, label="1")
async def button1(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.defer()
if await self.check(interaction) is True:
self.choice = 0
await self.msg.delete()
self.stop()
@discord.ui.button(style=discord.ButtonStyle.danger, label="2")
async def button2(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.defer()
if await self.check(interaction) is True:
self.choice = 1
await self.msg.delete()
self.stop()
@discord.ui.button(style=discord.ButtonStyle.danger, label="3")
async def button3(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.defer()
if await self.check(interaction) is True:
self.choice = 2
await self.msg.delete()
self.stop()
@discord.ui.button(style=discord.ButtonStyle.danger, label="4")
async def button4(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.defer()
if await self.check(interaction) is True:
self.choice = 3
await self.msg.delete()
self.stop()
@discord.ui.button(style=discord.ButtonStyle.danger, label="5")
async def button5(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.defer()
if await self.check(interaction) is True:
self.choice = 4
await self.msg.delete()
self.stop()
@discord.ui.button(
style=discord.ButtonStyle.blurple,
emoji=discord.PartialEmoji.from_str("⏪"),
)
async def buttonBack10(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.defer()
if await self.check(interaction) is True:
self.curPage = max(1, self.curPage - 10)
self.start = max(0, self.start - 50)
self.end = max(5, self.end - 50)
emb = get_list(responses, self.start, self.end, self.curPage)
await self.update()
await self.msg.edit(embed=emb, view=self)
@discord.ui.button(
style=discord.ButtonStyle.blurple,
emoji=discord.PartialEmoji.from_str("◀️"),
)
async def buttonBack(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.defer()
if await self.check(interaction) is True:
self.curPage -= 1
self.start -= 5
self.end -= 5
emb = get_list(responses, self.start, self.end, self.curPage)
await self.update()
await self.msg.edit(embed=emb, view=self)
@discord.ui.button(
style=discord.ButtonStyle.blurple,
emoji=discord.PartialEmoji.from_str("▶️"),
)
async def buttonNext(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.defer()
if await self.check(interaction) is True:
self.curPage += 1
self.start += 5
self.end += 5
emb = get_list(responses, self.start, self.end, self.curPage)
await self.update()
await self.msg.edit(embed=emb, view=self)
@discord.ui.button(
style=discord.ButtonStyle.blurple,
emoji=discord.PartialEmoji.from_str("⏩"),
)
async def buttonNext10(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.defer()
if await self.check(interaction) is True:
self.curPage = min(self.pages, self.curPage + 10)
self.start = min(self.pages * 5 - 5, self.start + 50)
self.end = min(self.pages * 5, self.end + 50)
emb = get_list(responses, self.start, self.end, self.curPage)
await self.update()
await self.msg.edit(embed=emb, view=self)
@discord.ui.button(
style=discord.ButtonStyle.secondary,
emoji=discord.PartialEmoji.from_str("📖"),
)
async def buttonPage(self, interaction: discord.Interaction, button: discord.ui.Button):
if await self.check(interaction) is True:
pageM = pageModal()
await interaction.response.send_modal(pageM)
await pageM.wait()
page = int(pageM.page.value)
if page > self.pages:
self.curPage = min(self.pages, page)
self.start = min(self.pages, page) * 5 - 5
self.end = min(self.pages, page) * 5
elif page < 1:
self.curPage = 1
self.start = 0
self.end = 5
else:
self.curPage = page
self.start = page * 5 - 5
self.end = page * 5
emb = get_list(responses, self.start, self.end, self.curPage)
await self.update()
await self.msg.edit(embed=emb, view=self)
else:
await interaction.response.defer()
@discord.ui.button(
style=discord.ButtonStyle.secondary,
emoji=discord.PartialEmoji.from_str("❌"),
)
async def buttonCancel(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.defer()
if await self.check(interaction) is True:
self.cancel = True
emb = discord.Embed(title="Canceled", description="", color=color)
await self.msg.edit(embed=emb, view=None)
self.stop()
class confirmView(discord.ui.View):
async def on_timeout(self) -> None:
emb = discord.Embed(title="Timeout", description="", color=color)
await self.msg.edit(embed=emb, view=None)
self.timeout = True
self.stop()
async def check(self, interaction: discord.Interaction):
if interaction.user.id == ctx.author.id:
return True
else:
await interaction.followup.send("That's not your button.", ephemeral=True)
confirm: bool = None
cancel: bool = None
@discord.ui.button(style=discord.ButtonStyle.success, label="Confirm")
async def buttonConfirm(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.defer()
if await self.check(interaction) is True:
self.confirm = True
await self.msg.delete()
self.stop()
@discord.ui.button(style=discord.ButtonStyle.danger, label="Cancel")
async def buttonCancel(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.defer()
if await self.check(interaction) is True:
self.cancel = True
emb = discord.Embed(title="Canceled", description="", color=color)
await self.msg.edit(embed=emb, view=None)
self.stop()
start = 0
end = 5
pages = 0
curPage = 1
responses = []
color = await get_color(ctx)
with open(f"{here}\\bruhbot\\responses.txt", "r", encoding="utf-8") as f:
file = f.readlines()
for line in file:
current = line[:-1]
responses.append(current)
if arg: # direct
try:
choice = int("".join(arg[:])) - 1
if choice + 1 <= 0 or choice + 1 > len(responses):
raise IndexError
except ValueError:
await ctx.reply("Selection must be a number.")
return
except IndexError:
await ctx.reply("Error: Selection out of range.")
return
emb = discord.Embed(
title="Delete response",
description=f"Are you sure you want to delete this response?: \n**{responses[choice]}**",
color=color,
)
cview = confirmView(timeout=10)
msg = await ctx.send(embed=emb, view=cview)
cview.msg = msg
await cview.wait()
if cview.confirm is True:
with open(f"{here}\\bruhbot\\responses.txt", "w", encoding="utf-8") as f:
for line in file:
if line.strip("\n").lower() != responses[choice].lower():
f.write(line)
if " - image" in responses[choice]:
with suppress(FileNotFoundError):
image = responses[choice].replace(" - image", "")
os.remove(f"{here}\\images\\{image}")
await ctx.reply(f"**'{responses[choice]}'** was deleted. :thumbsup:", mention_author=False)
return
if cview.cancel is True or cview.timeout is True:
return
if not arg: # menu
mview = menuView(timeout=30)
while pages < len(responses) / 5:
pages += 1
emb = get_list(responses, start, end, curPage)
mview.start = start
mview.end = end
mview.pages = pages
mview.curPage = curPage
mview.responses = responses
await mview.update()
msg = await ctx.send(embed=emb, view=mview)
mview.msg = msg
await mview.wait()
if mview.cancel is True or mview.timeout is True:
return
if mview.choice is not None:
if mview.choice + 1 > len(responses[mview.start : mview.end]):
await ctx.reply("Invalid selection. That shouldn't happen...")
raise Exception
emb = discord.Embed(
title="Delete response",
description=f"Are you sure you want to delete this response?: \n**{responses[mview.start:mview.end][mview.choice]}**",
color=color,
)
cview = confirmView(timeout=10)
msg = await ctx.send(embed=emb, view=cview)
cview.msg = msg
await cview.wait()
if cview.confirm is True:
with open(f"{here}\\bruhbot\\responses.txt", "w", encoding="utf-8") as f:
for line in file:
if line.strip("\n").lower() != responses[mview.start : mview.end][mview.choice].lower():
f.write(line)
if " - image" in responses[mview.start : mview.end][mview.choice]:
with suppress(FileNotFoundError):
image = responses[mview.start : mview.end][mview.choice].replace(" - image", "")
os.remove(f"{here}\\images\\{image}")
await ctx.reply(
f"**'{responses[mview.start:mview.end][mview.choice]}'** was deleted. :thumbsup:",
mention_author=False,
)
if cview.cancel is True or cview.timeout is True:
return
except Exception:
await ctx.reply("Error logged in delr.")
ErrorLogger.run(traceback.format_exc())
@bot.command()
async def rlist(ctx):
try:
def get_list(numbered: list, start: int, end: int, curPage: int):
content = "\n".join(numbered[start:end])
emb = discord.Embed(
title="Response list",
description=f"**Page {curPage}/{pages}:**\n{content}",
color=color,
)
return emb
class pageModal(discord.ui.Modal, title="Enter a page number"):
page = discord.ui.TextInput(
style=discord.TextStyle.short,
label="Page",
required=True,
placeholder="Enter a page number",
)
async def on_submit(self, interaction: discord.Interaction):
await interaction.response.defer()
if not int(self.page.value):
raise Exception
async def on_error(self, interaction: discord.Interaction):
await interaction.followup.send("Error: Page must be a number.", ephemeral=True)
class menuView(discord.ui.View):
async def on_timeout(self) -> None:
await self.msg.delete()
self.timeout = True
self.stop()
async def update(self):
if self.curPage == 1:
self.buttonBack10.disabled = True
self.buttonBack.disabled = True
else:
self.buttonBack10.disabled = False
self.buttonBack.disabled = False
if self.curPage == self.pages:
self.buttonNext.disabled = True
self.buttonNext10.disabled = True
else:
self.buttonNext.disabled = False
self.buttonNext10.disabled = False
async def check(self, interaction: discord.Interaction):
if interaction.user.id == ctx.author.id:
return True
else:
if self.counter < 5:
await interaction.followup.send("That's not you button.", ephemeral=True)
self.counter += 1
elif self.counter >= 5 and self.counter < 8:
await interaction.followup.send("Dude stop.", ephemeral=True)
self.counter += 1
else:
await interaction.followup.send("Seriously dude, enough.", ephemeral=True)
counter: int = 0
@discord.ui.button(
style=discord.ButtonStyle.blurple,
emoji=discord.PartialEmoji.from_str("⏪"),
)
async def buttonBack10(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.defer()
if await self.check(interaction) is True:
self.curPage = max(1, self.curPage - 10)
self.start = max(0, self.start - 250)
self.end = max(25, self.end - 250)
emb = get_list(numbered, self.start, self.end, self.curPage)
await self.update()
await self.msg.edit(embed=emb, view=self)
@discord.ui.button(
style=discord.ButtonStyle.blurple,
emoji=discord.PartialEmoji.from_str("◀️"),
)
async def buttonBack(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.defer()
if await self.check(interaction) is True:
self.curPage -= 1
self.start -= 25
self.end -= 25
emb = get_list(numbered, self.start, self.end, self.curPage)
await self.update()
await self.msg.edit(embed=emb, view=self)
@discord.ui.button(
style=discord.ButtonStyle.blurple,
emoji=discord.PartialEmoji.from_str("▶️"),
)
async def buttonNext(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.defer()
if await self.check(interaction) is True:
self.curPage += 1
self.start += 25
self.end += 25
emb = get_list(numbered, self.start, self.end, self.curPage)
await self.update()
await self.msg.edit(embed=emb, view=self)
@discord.ui.button(
style=discord.ButtonStyle.blurple,
emoji=discord.PartialEmoji.from_str("⏩"),
)
async def buttonNext10(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.defer()
if await self.check(interaction) is True:
self.curPage = min(self.pages, self.curPage + 10)
self.start = min(self.pages * 25 - 25, self.start + 250)
self.end = min(self.pages * 25, self.end + 250)
emb = get_list(numbered, self.start, self.end, self.curPage)
await self.update()
await self.msg.edit(embed=emb, view=self)
@discord.ui.button(
style=discord.ButtonStyle.secondary,
emoji=discord.PartialEmoji.from_str("📖"),
)
async def buttonPage(self, interaction: discord.Interaction, button: discord.ui.Button):
if await self.check(interaction) is True:
pageM = pageModal()
await interaction.response.send_modal(pageM)
await pageM.wait()
page = int(pageM.page.value)
if page > self.pages:
self.curPage = min(self.pages, page)
self.start = min(self.pages, page) * 25 - 25
self.end = min(self.pages, page) * 25
elif page < 1:
self.curPage = 1
self.start = 0
self.end = 25
else:
self.curPage = page
self.start = page * 25 - 25
self.end = page * 25
emb = get_list(numbered, self.start, self.end, self.curPage)
await self.update()
await self.msg.edit(embed=emb, view=self)
else:
interaction.response.defer()
start = 0
end = 25
pages = 0
curPage = 1
responses = []
numbered = []
color = await get_color(ctx)
with open(f"{here}\\bruhbot\\responses.txt", "r", encoding="utf-8") as f:
file = f.readlines()
for line in file:
current = line[:-1]
responses.append(current)
for i, j in enumerate(responses, 1): # character limit fix
if len(j) > 81:
j = j[:73] + "..."
j = escape_markdown(str(i) + ": " + j)
numbered.append(j)
while pages < len(responses) / 25:
pages += 1
emb = get_list(numbered, start, end, curPage)
mview = menuView(timeout=30)
mview.start = start
mview.end = end
mview.pages = pages
mview.curPage = curPage
await mview.update()
msg = await ctx.reply(embed=emb, view=mview, mention_author=False)
mview.msg = msg
await mview.wait()
except Exception:
await ctx.reply("Error logged rlist.")
ErrorLogger.run(traceback.format_exc())
@bot.command()
@commands.is_owner()
async def rtest(ctx, arg: str): # for testing responses
try:
choice = int(arg) - 1
except ValueError:
return
responses = []
with open(f"{here}\\bruhbot\\responses.txt", "r", encoding="utf-8") as f:
for line in f:
current = line[:-1]
responses.append(current)
if choice + 1 > len(responses) or choice < 0:
await ctx.reply("Error: Selection out of range.")
return
response = responses[choice].replace(r"\n", "\n")
if response.endswith("- image"):
await send_image(ctx, response)
return
await ctx.reply(response, mention_author=False)
@bot.command()
async def help(ctx, *arg: str):
try:
def embed(ctx, name: str, description: str, color: int, pages: int):
embed = discord.Embed(color=color)
if pages > 1:
embed.title = "*Bruhbot*"
for page in range(pages):
embed.add_field(name=name[page], value=description[page], inline=False)
else:
embed.description = description
embed.set_author(
name=f"{ctx.me.display_name} Help Menu",
icon_url=bot.user.display_avatar,
)
embed.set_footer(
text="Type $help <command> for more info on a command. You can also type $help <category> for more info on a category."
)
return embed
pre = " ".join(arg[:]).lower()
args = ["addr", "delr", "rlist", "age", "forza", "response", "extra"]
helpColor = await get_color(ctx)
if pre in args:
with open(f"{here}\\bruhbot\\data.json") as f:
data = json.load(f)
helpDes = data[pre]["description"]
msg = embed(ctx, None, helpDes, helpColor, 1)
await ctx.send(embed=msg)
elif not arg:
categories = ["response", "extra"]
helpName = []
helpDes = []
with open(f"{here}\\bruhbot\\data.json") as f:
data = json.load(f)
for cat in categories:
helpDes.append(data[cat]["description"])
helpName.append(data[cat]["name"])
msg = embed(
ctx,
helpName,
helpDes,
helpColor,
len(categories),
)
await ctx.send(embed=msg)
else:
return
except Exception:
await ctx.reply("Error logged in help.")
ErrorLogger.run(traceback.format_exc())
@bot.command()
async def age(ctx):
def plural(num: int):
return 0 if num == 1 else 1
now = date.today()
bday = date(2020, 6, 20)
rdelta = relativedelta(now, bday)
plurals = [["year", "years"], ["month", "months"], ["day", "days"]]
await ctx.reply(
f"I am {rdelta.years} {plurals[0][plural(rdelta.years)]}, {rdelta.months} {plurals[1][plural(rdelta.months)]}, {rdelta.days} {plurals[2][plural(rdelta.days)]} old.",
mention_author=False,
)
@bot.group(invoke_without_command=True, ignore_extra=False)
async def forza(ctx):
if ctx.invoked_subcommand is None:
season = ForzaSeason.season()
await ctx.reply(
f"Horizon 4: {season[0].capitalize()}\nHorizon 5: {season[1].capitalize()}",
mention_author=False,
)
@forza.error
async def forza_error(ctx, error):
if isinstance(error, commands.errors.TooManyArguments):
await help(ctx, "forza")
else:
await ctx.reply("Error logged in forza.")
ErrorLogger.run(traceback.format_exc())
@forza.command()
async def time(ctx):
time = ForzaSeason.time()
await ctx.reply(f"Next seasons start in {time}.", mention_author=False)
@bot.command()
@commands.is_owner()
async def backup(ctx):
ResponseBackup.run()
with open(f"{here}\\bruhbot\\data.json", "r+") as f:
data = json.load(f)
now = date.today()
data["lastbackup"] = now.strftime("%Y-%m-%d")
f.seek(0)
json.dump(data, f, indent=4)
await ctx.reply("Response list saved to backup.", mention_author=False)
@bot.group(invoke_without_command=True, ignore_extra=False)
async def logs(ctx):
if ctx.invoked_subcommand is None:
class clearView(discord.ui.View):
async def on_timeout(self) -> None:
await self.msg.edit(view=None)
self.stop()
@discord.ui.button(style=discord.ButtonStyle.secondary, label="Clear Logs")
async def buttonClear(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.defer()
if interaction.user.id == 108105758578577408:
self.buttonClear.disabled = True
await self.msg.edit(view=self)
await clear(ctx)
self.stop()
else:
await interaction.followup.send("You dare try to hide your crimes?", ephemeral=True)
last = ErrorLogger.last()
if last == "No logs found.":
await ctx.reply(last, mention_author=False)
return
if len(last["error"]) >= 2000:
ab = map(str.encode, last["error"])
content = b"".join(ab)
view = clearView(timeout=10)
msg = await ctx.send(file=discord.File(BytesIO(content), "traceback.py"), view=view)
view.msg = msg
return
else:
view = clearView(timeout=10)
msg = await ctx.send(f"{last['log']}, {last['time']}\n```py\n{last['error']}```", view=view)
view.msg = msg
@logs.error
async def logs_error(ctx, error):
if isinstance(error, commands.errors.TooManyArguments):
await ctx.reply("Invalid Subcommand")
else:
await ctx.reply("Error logged in logs. Uh oh.")
ErrorLogger.run(traceback.format_exc())
@logs.command()
@commands.is_owner()
async def clear(ctx):
ErrorLogger.clear()
await ctx.send("Error logs cleared.")
@clear.error
async def clear_error(ctx, error):
if isinstance(error, commands.errors.NotOwner):
await ctx.send("You dare try to hide your crimes?")
else:
await ctx.send("Error logged in logs clear.")
ErrorLogger.run(traceback.format_exc())
@bot.command()
@commands.is_owner()
async def shutdown(ctx):
await ctx.message.add_reaction("👋🏼")
await bot.close()
bot.run(token)