/
triggerhandler.py
1185 lines (1116 loc) · 51.7 KB
/
triggerhandler.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 asyncio
import functools
import multiprocessing as mp
import os
import random
import string
from copy import copy
from datetime import datetime, timezone
from io import BytesIO
from typing import Any, Dict, List, Literal, Optional, Pattern, Tuple, Union, cast
import aiohttp
import discord
from red_commons.logging import getLogger
from redbot.core import commands, modlog
from redbot.core.data_manager import cog_data_path
from redbot.core.i18n import Translator
from redbot.core.utils.chat_formatting import escape, humanize_list
from .abc import ReTriggerMixin
from .converters import Trigger, TriggerResponse
from .message import ReTriggerMessage
try:
import pytesseract
ALLOW_OCR = True
except ImportError:
ALLOW_OCR = False
try:
from PIL import Image, ImageSequence
ALLOW_RESIZE = True
except ImportError:
ALLOW_RESIZE = False
ALLOW_OCR = False
try:
import regex as re
except ImportError:
import re
log = getLogger("red.trusty-cogs.ReTrigger")
_ = Translator("ReTrigger", __file__)
RE_CTX: Pattern = re.compile(r"{([^}]+)\}")
RE_POS: Pattern = re.compile(r"{((\d+)[^.}]*(\.[^:}]+)?[^}]*)\}")
LINK_REGEX: Pattern = re.compile(
r"(http[s]?:\/\/[^\"\']*\.(?:png|jpg|jpeg|gif|mp3|mp4))", flags=re.I
)
IMAGE_REGEX: Pattern = re.compile(
r"(?:(?:https?):\/\/)?[\w\/\-?=%.]+\.(?:png|jpg|jpeg)+", flags=re.I
)
class TriggerHandler(ReTriggerMixin):
"""
Handles all processing of triggers
"""
async def remove_trigger_from_cache(self, guild_id: int, trigger: Trigger) -> None:
try:
del self.triggers[guild_id][trigger.name]
except KeyError:
# it will get removed on the next reload of the cog
log.info("Trigger can't be removed :blobthinking:")
async def can_edit(self, author: discord.Member, trigger: Trigger) -> bool:
"""Chekcs to see if the member is allowed to edit the trigger"""
if trigger.author == author.id:
return True
if await self.bot.is_owner(author):
return True
if author is author.guild.owner and TriggerResponse.mock not in trigger.response_type:
return True
return False
async def is_mod_or_admin(self, member: discord.Member) -> bool:
guild = member.guild
if member == guild.owner:
return True
if await self.bot.is_owner(member):
return True
if await self.bot.is_admin(member):
return True
if await self.bot.is_mod(member):
return True
return False
async def make_guild_folder(self, directory) -> None:
if not directory.is_dir():
log.info("Creating guild folder")
directory.mkdir(exist_ok=True, parents=True)
async def save_image_location(self, image_url: str, guild: discord.Guild) -> Optional[str]:
good_image_url = LINK_REGEX.search(image_url)
if not good_image_url:
return None
seed = "".join(random.sample(string.ascii_uppercase + string.digits, k=5))
filename = good_image_url.group(1).split("/")[-1]
filename = "{}-{}".format(seed, filename)
directory = cog_data_path(self) / str(guild.id)
file_path = str(cog_data_path(self)) + f"/{guild.id}/{filename}"
await self.make_guild_folder(directory)
async with aiohttp.ClientSession() as session:
async with session.get(good_image_url.group(1)) as resp:
test = await resp.read()
with open(file_path, "wb") as f:
f.write(test)
return filename
async def wait_for_image(self, ctx: commands.Context) -> Optional[discord.Message]:
await ctx.send(_("Upload an image for me to use! Type `exit` to cancel."))
msg = None
while msg is None:
def check(m):
return m.author == ctx.author and (m.attachments or "exit" in m.content)
try:
msg = await self.bot.wait_for("message", check=check, timeout=60)
except asyncio.TimeoutError:
await ctx.send(_("Image adding timed out."))
break
if "exit" in msg.content.lower():
await ctx.send(_("Image adding cancelled."))
break
return msg
async def wait_for_multiple_images(self, ctx: commands.Context) -> List[str]:
await ctx.send(_("Upload an image for me to use! Type `exit` to cancel."))
files: list = []
while True:
def check(m):
return m.author == ctx.author
try:
msg = await self.bot.wait_for("message", check=check, timeout=60)
except asyncio.TimeoutError:
break
if "exit" in msg.content.lower():
break
else:
link = LINK_REGEX.search(msg.content)
for a in msg.attachments:
if a.size > 8 * 1000 * 1000:
continue
try:
files.append(await self.save_image_location(a.url, ctx.guild))
await msg.add_reaction("✅")
except Exception:
pass
if link:
try:
files.append(await self.save_image_location(link.group(0), ctx.guild))
await msg.add_reaction("✅")
except Exception:
pass
return files
async def wait_for_multiple_responses(self, ctx: commands.Context) -> List[discord.Message]:
msg_text = _(
"Please enter your desired phrase to be used for this trigger."
"Type `exit` to stop adding responses."
)
await ctx.send(msg_text)
responses: list = []
while True:
def check(m):
return m.author == ctx.author
try:
message = await self.bot.wait_for("message", check=check, timeout=60)
await message.add_reaction("✅")
except asyncio.TimeoutError:
return responses
if message.content == "exit":
return responses
else:
responses.append(message.content)
def resize_image(self, size: int, image: str) -> discord.File:
length, width = (16, 16) # Start with the smallest size we want to upload
with Image.open(image) as im:
if size <= 0:
size = 1
im.thumbnail((length * size, width * size), Image.ANTIALIAS)
byte_array = BytesIO()
im.save(byte_array, format="PNG")
byte_array.seek(0)
return discord.File(byte_array, filename="resize.png")
def resize_gif(self, size: int, image: str) -> discord.File:
img_list = []
with Image.open(image) as im:
if size <= 0:
size = 1
length, width = (16 * size, 16 * size)
start_list = [frame.copy() for frame in ImageSequence.Iterator(im)]
for frame in start_list:
frame.thumbnail((length, width), Image.ANTIALIAS)
img_list.append(frame)
byte_array = BytesIO()
img_list[0].save(
byte_array, format="GIF", save_all=True, append_images=img_list, duration=0, loop=0
)
byte_array.seek(0)
return discord.File(byte_array, filename="resize.gif")
async def check_is_command(self, message: discord.Message) -> bool:
"""Checks if the message is a bot command"""
prefix_list = await self.bot.get_valid_prefixes(message.guild)
msg = message.content
is_command = False
for prefix in prefix_list:
if msg.startswith(prefix):
# Don't run a trigger if it's the name of a command
command_text = msg.replace(prefix, "").split(" ")[0]
if not command_text:
continue
command = self.bot.get_command(command_text)
if command:
is_command = True
return is_command
@commands.Cog.listener()
async def on_message(self, message: discord.Message) -> None:
if message.guild is None:
return
if message.author.bot:
return
if await self.bot.cog_disabled_in_guild(self, message.guild):
return
if getattr(message, "retrigger", False):
log.trace("A ReTrigger dispatched message, ignoring.")
return
await self.check_triggers(message, False)
@commands.Cog.listener()
async def on_raw_message_edit(self, payload: discord.RawMessageUpdateEvent) -> None:
if "content" not in payload.data and "embeds" not in payload.data:
return
if "guild_id" not in payload.data:
return
guild = self.bot.get_guild(int(payload.data["guild_id"]))
if not guild:
return
if guild.id not in self.triggers:
return
if await self.bot.cog_disabled_in_guild(self, guild):
return
if not any(t.check_edits for t in self.triggers[guild.id].values()):
# log.debug(f"No triggers in {guild=} have check_edits enabled")
return
if "bot" in payload.data.get("author", []):
return
channel = guild.get_channel(int(payload.data["channel_id"]))
try:
message = await channel.fetch_message(int(payload.data["id"]))
except (discord.errors.Forbidden, discord.errors.NotFound):
log.debug(
"I don't have permission to read channel history or cannot find the message."
)
return
except Exception:
log.info("Could not find channel or message")
# If we can't find the channel ignore it
return
if message.author.bot:
# somehow we got a bot through the previous check :thonk:
return
await self.check_triggers(message, True)
@commands.Cog.listener()
async def on_thread_create(self, thread: discord.Thread):
if await self.bot.cog_disabled_in_guild(self, thread.guild):
return
if not thread.permissions_for(thread.guild.me).manage_threads:
return
try:
await self.check_triggers_thread(thread)
except Exception:
log.exception("Error checking thread title")
@commands.Cog.listener()
async def on_thread_update(self, before: discord.Thread, after: discord.Thread):
if await self.bot.cog_disabled_in_guild(self, before.guild):
return
if not before.permissions_for(before.guild.me).manage_threads:
return
if before.name != after.name:
try:
await self.check_triggers_thread(after, edit=True)
except Exception:
log.exception("Error checking thread title change")
async def check_triggers_thread(self, thread: discord.Thread, *, edit: bool = False):
guild = thread.guild
for trigger in self.triggers[guild.id].values():
if not trigger.enabled:
continue
if TriggerResponse.delete not in trigger.response_type:
continue
if not trigger.read_thread_title:
continue
if edit and not trigger.check_edits:
continue
allowed_trigger = await trigger.check_bw_list(author=thread.owner, channel=thread)
is_auto_mod = any(r.is_automod for r in trigger.response_type)
is_mod = False
if thread.owner is not None:
is_mod = await self.is_mod_or_admin(thread.owner)
if not allowed_trigger:
log.debug(
"ReTrigger: %r is immune from allowlist/blocklist %r", thread.owner, trigger
)
continue
if allowed_trigger and (is_auto_mod and is_mod):
log.debug(
"ReTrigger: %r is immune from automated actions %r", thread.owner, trigger
)
continue
search = await self.safe_regex_search(guild, trigger, thread.name)
if not search[0]:
trigger.enabled = False
return
elif search[0] and search[1] != []:
trigger.count += 1
log.debug(
"ReTrigger: thread from %r triggered for deletion with %r",
thread.owner,
trigger,
)
try:
log.debug("Deleting thread %r", thread)
await thread.delete()
if await self.config.guild(guild).filter_logs():
await self.modlog_action(thread, trigger, search[1], _("Deleted Thread"))
except discord.errors.NotFound:
log.debug(
"Retrigger encountered an error in %r with trigger %r", guild, trigger
)
except discord.errors.Forbidden:
log.debug(
"Retrigger encountered an error in %r with trigger %r", guild, trigger
)
except Exception:
log.exception(
"Retrigger encountered an error in %r with trigger %r", guild, trigger
)
return
async def check_triggers(self, message: discord.Message, edit: bool) -> None:
"""
This is where we iterate through the triggers and perform the
search. This does all the permission checks and cooldown checks
before actually running the regex to avoid possibly long regex
operations.
"""
guild: discord.Guild = cast(discord.Guild, message.guild)
if guild.id not in self.triggers:
return
channel: discord.TextChannel = cast(discord.TextChannel, message.channel)
author: Optional[discord.Member] = guild.get_member(message.author.id)
if not author:
return
blocked = not await self.bot.allowed_by_whitelist_blacklist(author)
channel_perms = channel.permissions_for(author)
is_command = await self.check_is_command(message)
is_mod = await self.is_mod_or_admin(author)
for trigger in self.triggers[guild.id].values():
if not trigger.enabled:
continue
if edit and not trigger.check_edits:
continue
if trigger.chance:
if random.randint(0, trigger.chance) != 0:
continue
if trigger.nsfw and not channel.is_nsfw():
continue
allowed_trigger = await trigger.check_bw_list(author=author, channel=channel)
is_auto_mod = any(r.is_automod for r in trigger.response_type)
if not allowed_trigger:
log.debug("ReTrigger: %r is immune from allowlist/blocklist %r", author, trigger)
continue
if allowed_trigger and (is_auto_mod and is_mod):
log.debug("ReTrigger: %r is immune from automated actions %r", author, trigger)
continue
# log.debug(f"Checking trigger {trigger.name}")
if is_command and not trigger.ignore_commands:
log.debug(
"ReTrigger: %r is ignored because they used a command %r", author, trigger
)
continue
if any(r.is_automod for r in trigger.response_type):
if await self.bot.is_automod_immune(message):
log.debug("ReTrigger: %r is immune from automated actions %r", author, trigger)
continue
if TriggerResponse.delete in trigger.response_type:
if channel_perms.manage_messages or is_mod:
log.debug(
"ReTrigger: Delete is ignored because %r has manage messages permission %r",
author,
trigger,
)
continue
elif TriggerResponse.kick in trigger.response_type:
if channel_perms.kick_members or is_mod:
log.debug(
"ReTrigger: Kick is ignored because %r has kick permissions %r",
author,
trigger,
)
continue
elif TriggerResponse.ban in trigger.response_type:
if channel_perms.ban_members or is_mod:
log.debug(
"ReTrigger: Ban is ignored because %r has ban permissions %r",
author,
trigger,
)
continue
elif any(r.is_role_change for r in trigger.response_type):
if channel_perms.manage_roles or is_mod:
log.debug(
"ReTrigger: role change is ignored because %r has mange roles permissions %r",
author,
trigger,
)
else:
if blocked:
log.debug(
"ReTrigger: Channel is ignored or %r is blacklisted %r",
author,
trigger,
)
continue
content = ""
content += message.content
if trigger.read_filenames and message.attachments:
content += " " + " ".join(f.filename for f in message.attachments)
if trigger.ocr_search and ALLOW_OCR:
content += await self.get_image_text(message)
if trigger.read_embeds and len(message.embeds) > 0:
content += "\n".join(
self.convert_embed_to_string(embed, index)
for index, embed in enumerate(message.embeds)
)
if trigger.regex is None:
log.debug(
"ReTrigger: Trigger %r must have invalid regex.",
trigger,
)
trigger.disable()
continue
# log.debug("content = %s message.content = %s", content, message.content)
search = await self.safe_regex_search(guild, trigger, content)
if not search[0]:
trigger.enabled = False
return
elif search[0] and search[1] != []:
if await trigger.check_cooldown(message):
continue
trigger.count += 1
log.debug("ReTrigger: message from %r triggered %r", author, trigger)
await self.perform_trigger(message, trigger, search[1])
return
@staticmethod
def convert_embed_to_string(embed: discord.Embed, embed_index: int = 0) -> str:
embed_dict = embed.to_dict()
flattened_embed_dict = {}
field_blacklist = ["type", "color", "proxy_url", "height", "width", "proxy_icon_url"]
for field, value in embed_dict.items():
if field in field_blacklist:
continue
if isinstance(value, dict):
for subfield in value:
if subfield in field_blacklist:
continue
flattened_embed_dict[f"{field.lower()}-{subfield.lower()}"] = value[subfield]
elif isinstance(value, list):
for field_index, embedfields in enumerate(value):
emfield_name = embedfields["name"].lower()
flattened_embed_dict[
f"{field.lower()}-{field_index}-{emfield_name}"
] = embedfields["value"]
else:
flattened_embed_dict[field.lower()] = value
return "\n".join(
f"embed-{embed_index}-{field}: {value}"
for field, value in flattened_embed_dict.items()
)
async def get_image_text(self, message: discord.Message) -> str:
"""
This function is built to asynchronously search images for text using pytesseract
It takes a discord message and searches for valid
image links and all attachments on the message
then runs them through pytesseract. All contents
from pytesseract are returned as a string.
"""
content = " "
for attachment in message.attachments:
temp = BytesIO()
await attachment.save(temp)
task = functools.partial(pytesseract.image_to_string, Image.open(temp))
loop = asyncio.get_running_loop()
new_task = loop.run_in_executor(None, task)
try:
content += await asyncio.wait_for(new_task, timeout=5)
except asyncio.TimeoutError:
pass
good_image_url = IMAGE_REGEX.findall(message.content)
for link in good_image_url:
temp = BytesIO()
async with aiohttp.ClientSession() as session:
async with session.get(link) as resp:
data = await resp.read()
temp.write(data)
temp.seek(0)
task = functools.partial(pytesseract.image_to_string, Image.open(temp))
loop = asyncio.get_running_loop()
new_task = loop.run_in_executor(None, task)
try:
content += await asyncio.wait_for(new_task, timeout=5)
except asyncio.TimeoutError:
pass
return content
async def safe_regex_search(
self, guild: discord.Guild, trigger: Trigger, content: str
) -> Tuple[bool, list]:
"""
Mostly safe regex search to prevent reDOS from user defined regex patterns
This works by running the regex pattern inside a process pool defined at the
cog level and then checking that process in the default executor to keep
things asynchronous. If the process takes too long to complete we log a
warning and remove the trigger from trying to run again.
"""
if await self.config.guild(guild).bypass():
# log.debug(f"Bypassing safe regex in guild {guild.name} ({guild.id})")
return (True, trigger.regex.findall(content))
try:
process = self.re_pool.apply_async(trigger.regex.findall, (content,))
task = functools.partial(process.get, timeout=self.trigger_timeout)
loop = asyncio.get_running_loop()
new_task = loop.run_in_executor(None, task)
search = await asyncio.wait_for(new_task, timeout=self.trigger_timeout + 5)
except mp.TimeoutError:
error_msg = (
"ReTrigger: regex process took too long. Removing from memory "
"%s (%s) Author %s "
"Offending regex `%s` Name: %s"
)
log.warning(
error_msg,
guild.name,
guild.id,
trigger.author,
trigger.regex.pattern,
trigger.name,
)
return (False, [])
# we certainly don't want to be performing multiple triggers if this happens
except asyncio.TimeoutError:
error_msg = (
"ReTrigger: regex asyncio timed out."
"%s (%s) Author %s "
"Offending regex `%s` Name: %s"
)
log.warning(
error_msg,
guild.name,
guild.id,
trigger.author,
trigger.regex.pattern,
trigger.name,
)
return (False, [])
except ValueError:
return (False, [])
except Exception:
log.error(
"ReTrigger encountered an error %s %s in %s %s",
trigger.name,
trigger.regex,
guild.name,
guild.id,
exc_info=True,
)
return (True, [])
else:
return (True, search)
async def perform_trigger(
self, message: discord.Message, trigger: Trigger, find: List[str]
) -> None:
guild: discord.Guild = cast(discord.Guild, message.guild)
channel: discord.abc.GuildChannel = message.channel
author: discord.Member = cast(discord.Member, message.author)
reason = _("Trigger response: {trigger}").format(trigger=trigger.name)
own_permissions = channel.permissions_for(guild.me)
is_thread_message = getattr(message, "is_thread", False)
if not isinstance(channel, discord.Thread):
if (
trigger.thread.public is not None
and own_permissions.send_messages_in_threads
and trigger.thread.name
):
thread_name = await self.convert_parms(message, trigger.thread.name, trigger, find)
if trigger.thread.public is True and own_permissions.create_public_threads:
channel = await channel.create_thread(
name=thread_name[:100], message=message, reason=reason
)
elif trigger.thread.public is False and own_permissions.create_private_threads:
channel = await channel.create_thread(
name=thread_name[:100], invitable=trigger.thread.invitable, reason=reason
)
if trigger.thread.invitable or (
trigger.thread.invitable is False and own_permissions.manage_messages
):
try:
await channel.add_user(author)
except Exception:
log.exception(
"ReTrigger encountered an error adding a user to a private thread."
)
if (
TriggerResponse.resize in trigger.response_type
and own_permissions.attach_files
and ALLOW_RESIZE
):
await channel.typing()
path = str(cog_data_path(self)) + f"/{guild.id}/{trigger.image}"
if path.lower().endswith(".gif"):
task = functools.partial(self.resize_gif, size=len(find[0]) - 3, image=path)
else:
task = functools.partial(self.resize_image, size=len(find[0]) - 3, image=path)
loop = asyncio.get_running_loop()
new_task = loop.run_in_executor(None, task)
try:
file: discord.File = await asyncio.wait_for(new_task, timeout=60)
except asyncio.TimeoutError:
pass
try:
await channel.send(file=file)
except discord.errors.Forbidden:
log.debug("Retrigger encountered an error in %r with trigger %r", guild, trigger)
except Exception:
log.exception(
"Retrigger encountered an error in %r with trigger %r", guild, trigger
)
if TriggerResponse.rename in trigger.response_type and own_permissions.manage_nicknames:
# rename above text so the mention shows the renamed user name
if author == guild.owner:
# Don't want to accidentally kick the bot owner
# or try to kick the guild owner
return
if guild.me.top_role > author.top_role:
if trigger.multi_payload:
text_response = "\n".join(
t[1] for t in trigger.multi_payload if t[0] == "rename"
)
else:
text_response = str(trigger.text)
response = await self.convert_parms(message, text_response, trigger, find)
if response and not channel.permissions_for(author).mention_everyone:
response = escape(response, mass_mentions=True)
try:
await author.edit(nick=response[:32], reason=reason)
except discord.errors.Forbidden:
log.debug(
"Retrigger encountered an error in %r with trigger %r", guild, trigger
)
except Exception:
log.exception(
"Retrigger encountered an error in %r with trigger %r", guild, trigger
)
if TriggerResponse.publish in trigger.response_type and own_permissions.manage_messages:
if channel.is_news():
try:
await message.publish()
except Exception:
log.exception(
"Retrigger encountered an error in %r with trigger %r", guild, trigger
)
if TriggerResponse.text in trigger.response_type and own_permissions.send_messages:
await channel.typing()
if trigger.multi_payload:
text_response = "\n".join(t[1] for t in trigger.multi_payload if t[0] == "text")
else:
text_response = str(trigger.text)
response = await self.convert_parms(message, text_response, trigger, find)
if response and not channel.permissions_for(author).mention_everyone:
response = escape(response, mass_mentions=True)
kwargs = {}
if trigger.reply:
kwargs["reference"] = message
try:
await channel.send(
response,
tts=trigger.tts,
delete_after=trigger.delete_after,
allowed_mentions=trigger.allowed_mentions(),
**kwargs,
)
except discord.errors.Forbidden:
log.debug("Retrigger encountered an error in %r with trigger %r", guild, trigger)
except Exception:
log.exception(
"Retrigger encountered an error in %r with trigger %r", guild, trigger
)
if TriggerResponse.randtext in trigger.response_type and own_permissions.send_messages:
await channel.typing()
rand_text_response: str = random.choice(trigger.text)
crand_text_response = await self.convert_parms(
message, rand_text_response, trigger, find
)
if crand_text_response and not channel.permissions_for(author).mention_everyone:
crand_text_response = escape(crand_text_response, mass_mentions=True)
kwargs = {}
if trigger.reply:
kwargs["reference"] = message
try:
await channel.send(
crand_text_response,
tts=trigger.tts,
delete_after=trigger.delete_after,
allowed_mentions=trigger.allowed_mentions(),
**kwargs,
)
except discord.errors.Forbidden:
log.debug("Retrigger encountered an error in %r with trigger %r", guild, trigger)
except Exception:
log.exception(
"Retrigger encountered an error in %r with trigger %r", guild, trigger
)
if TriggerResponse.image in trigger.response_type and own_permissions.attach_files:
await channel.typing()
path = str(cog_data_path(self)) + f"/{guild.id}/{trigger.image}"
file = discord.File(path)
image_text_response = trigger.text
if image_text_response:
image_text_response = await self.convert_parms(
message, image_text_response, trigger, find
)
if image_text_response and not channel.permissions_for(author).mention_everyone:
image_text_response = escape(image_text_response, mass_mentions=True)
kwargs = {}
if trigger.reply:
kwargs["reference"] = message
try:
await channel.send(
image_text_response,
tts=trigger.tts,
file=file,
delete_after=trigger.delete_after,
allowed_mentions=trigger.allowed_mentions(),
**kwargs,
)
except discord.errors.Forbidden:
log.debug("Retrigger encountered an error in %r with trigger %r", guild, trigger)
except Exception:
log.exception(
"Retrigger encountered an error in %r with trigger %r", guild, trigger
)
if TriggerResponse.randimage in trigger.response_type and own_permissions.attach_files:
await channel.typing()
image = random.choice(trigger.image)
path = str(cog_data_path(self)) + f"/{guild.id}/{image}"
file = discord.File(path)
rimage_text_response = trigger.text
if rimage_text_response:
rimage_text_response = await self.convert_parms(
message, rimage_text_response, trigger, find
)
if rimage_text_response and not channel.permissions_for(author).mention_everyone:
rimage_text_response = escape(rimage_text_response, mass_mentions=True)
kwargs = {}
if trigger.reply:
kwargs["reference"] = message
try:
await channel.send(
rimage_text_response,
tts=trigger.tts,
file=file,
delete_after=trigger.delete_after,
allowed_mentions=trigger.allowed_mentions(),
**kwargs,
)
except discord.errors.Forbidden:
log.debug("Retrigger encountered an error in %r with trigger %r", guild, trigger)
except Exception:
log.exception(
"Retrigger encountered an error in %r with trigger %r", guild, trigger
)
if TriggerResponse.dm in trigger.response_type:
if trigger.multi_payload:
dm_response = "\n".join(t[1] for t in trigger.multi_payload if t[0] == "dm")
else:
dm_response = str(trigger.text)
response = await self.convert_parms(message, dm_response, trigger, find)
try:
await author.send(response, allowed_mentions=trigger.allowed_mentions())
except discord.errors.Forbidden:
log.debug("Retrigger encountered an error in %r with trigger %r", guild, trigger)
except Exception:
log.exception(
"Retrigger encountered an error in %r with trigger %r", guild, trigger
)
if TriggerResponse.dmme in trigger.response_type:
if trigger.multi_payload:
dm_response = "\n".join(t[1] for t in trigger.multi_payload if t[0] == "dmme")
else:
dm_response = str(trigger.text)
response = await self.convert_parms(message, dm_response, trigger, find)
trigger_author = self.bot.get_user(trigger.author)
if not trigger_author:
try:
trigger_author = await self.bot.fetch_user(trigger.author)
except Exception:
log.exception(
"Retrigger encountered an error in %r with trigger %r", guild, trigger
)
try:
await trigger_author.send(response, allowed_mentions=trigger.allowed_mentions())
except discord.errors.Forbidden:
trigger.enabled = False
log.debug("Retrigger encountered an error in %r with trigger %r", guild, trigger)
except Exception:
log.exception(
"Retrigger encountered an error in %r with trigger %r", guild, trigger
)
if TriggerResponse.react in trigger.response_type and own_permissions.add_reactions:
for emoji in trigger.reactions:
try:
await message.add_reaction(emoji)
except (discord.errors.Forbidden, discord.errors.NotFound):
log.debug(
"Retrigger encountered an error in %r with trigger %r", guild, trigger
)
except Exception:
log.exception(
"Retrigger encountered an error in %r with trigger %r", guild, trigger
)
if TriggerResponse.add_role in trigger.response_type and own_permissions.manage_roles:
for roles in trigger.add_roles:
add_role: discord.Role = cast(discord.Role, guild.get_role(roles))
if not add_role:
continue
try:
await author.add_roles(add_role, reason=reason)
if await self.config.guild(guild).add_role_logs():
await self.modlog_action(message, trigger, find, _("Added Role"))
except discord.errors.Forbidden:
log.debug(
"Retrigger encountered an error in %r with trigger %r", guild, trigger
)
except Exception:
log.exception(
"Retrigger encountered an error in %r with trigger %r", guild, trigger
)
if TriggerResponse.remove_role in trigger.response_type and own_permissions.manage_roles:
for roles in trigger.remove_roles:
rem_role: discord.Role = cast(discord.Role, guild.get_role(roles))
if not rem_role:
continue
try:
await author.remove_roles(rem_role, reason=reason)
if await self.config.guild(guild).remove_role_logs():
await self.modlog_action(message, trigger, find, _("Removed Role"))
except discord.errors.Forbidden:
log.debug(
"Retrigger encountered an error in %r with trigger %r", guild, trigger
)
except Exception:
log.exception(
"Retrigger encountered an error in %r with trigger %r", guild, trigger
)
if TriggerResponse.kick in trigger.response_type and own_permissions.kick_members:
if await self.bot.is_owner(author) or author == guild.owner:
# Don't want to accidentally kick the bot owner
# or try to kick the guild owner
return
if guild.me.top_role > author.top_role:
try:
await author.kick(reason=reason)
if await self.config.guild(guild).kick_logs():
await self.modlog_action(message, trigger, find, _("Kicked"))
except discord.errors.Forbidden:
log.debug(
"Retrigger encountered an error in %r with trigger %r", guild, trigger
)
except Exception:
log.exception(
"Retrigger encountered an error in %r with trigger %r", guild, trigger
)
if TriggerResponse.ban in trigger.response_type and own_permissions.ban_members:
if await self.bot.is_owner(author) or author == guild.owner:
# Don't want to accidentally ban the bot owner
# or try to ban the guild owner
return
if guild.me.top_role > author.top_role:
try:
await author.ban(reason=reason, delete_message_days=0)
if await self.config.guild(guild).ban_logs():
await self.modlog_action(message, trigger, find, _("Banned"))
except discord.errors.Forbidden:
log.debug("Discord forbidden error when banning %s", author, exc_info=True)
except Exception:
log.error("Exception when banning %s", author, exc_info=True)
if TriggerResponse.command in trigger.response_type:
if trigger.multi_payload:
command_response = [t[1] for t in trigger.multi_payload if t[0] == "command"]
for command in command_response:
command = await self.convert_parms(message, command, trigger, find)
msg = copy(message)
prefix_list = await self.bot.get_valid_prefixes(message.guild)
msg.content = prefix_list[0] + command
msg = ReTriggerMessage(message=msg)
self.bot.dispatch("message", msg)
else:
msg = copy(message)
command = await self.convert_parms(message, str(trigger.text), trigger, find)
prefix_list = await self.bot.get_valid_prefixes(message.guild)
msg.content = prefix_list[0] + command
msg = ReTriggerMessage(message=msg)
self.bot.dispatch("message", msg)
if TriggerResponse.mock in trigger.response_type:
if trigger.multi_payload:
mock_response = [t[1] for t in trigger.multi_payload if t[0] == "mock"]
for command in mock_response:
command = await self.convert_parms(message, command, trigger, find)
msg = copy(message)
mocker = guild.get_member(trigger.author)
if not mocker:
return
msg.author = mocker
prefix_list = await self.bot.get_valid_prefixes(message.guild)
msg.content = prefix_list[0] + command
msg = ReTriggerMessage(message=msg)
self.bot.dispatch("message", msg)
else:
msg = copy(message)
mocker = guild.get_member(trigger.author)
command = await self.convert_parms(message, str(trigger.text), trigger, find)
if not mocker:
return # We'll exit early if the author isn't on the server anymore
msg.author = mocker
prefix_list = await self.bot.get_valid_prefixes(message.guild)
msg.content = prefix_list[0] + command
msg = ReTriggerMessage(message=msg)
self.bot.dispatch("message", msg)
if TriggerResponse.delete in trigger.response_type and own_permissions.manage_messages:
# this should be last since we can accidentally delete the context when needed
log.debug("Performing delete trigger")
try:
await message.delete()
if await self.config.guild(guild).filter_logs():
await self.modlog_action(message, trigger, find, _("Deleted Message"))
except discord.errors.NotFound:
log.debug("Retrigger encountered an error in %r with trigger %r", guild, trigger)
except discord.errors.Forbidden:
log.debug("Retrigger encountered an error in %r with trigger %r", guild, trigger)
except Exception:
log.exception(
"Retrigger encountered an error in %r with trigger %r", guild, trigger
)
async def convert_parms(
self, message: discord.Message, raw_response: str, trigger: Trigger, find: List[str]