|
|
@@ -2,6 +2,7 @@ from discord import Guild, Member, Message, PartialEmoji, RawReactionActionEvent
|
|
2
|
2
|
from discord.ext import commands
|
|
3
|
3
|
from datetime import datetime, timedelta
|
|
4
|
4
|
|
|
|
5
|
+from abc import ABC, abstractmethod
|
|
5
|
6
|
from config import CONFIG
|
|
6
|
7
|
from rscollections import AgeBoundDict
|
|
7
|
8
|
from storage import ConfigKey, Storage
|
|
|
@@ -28,6 +29,70 @@ class BotMessageReaction:
|
|
28
|
29
|
other.is_enabled == self.is_enabled and \
|
|
29
|
30
|
other.description == self.description
|
|
30
|
31
|
|
|
|
32
|
+ @classmethod
|
|
|
33
|
+ def standard_set(cls,
|
|
|
34
|
+ did_delete: bool = None,
|
|
|
35
|
+ message_count: int = 1,
|
|
|
36
|
+ did_kick: bool = None,
|
|
|
37
|
+ did_ban: bool = None,
|
|
|
38
|
+ user_count: int = 1) -> list:
|
|
|
39
|
+ """
|
|
|
40
|
+ Convenience factory for generating any of the three most common
|
|
|
41
|
+ commands: delete message(s), kick user(s), and ban user(s). All
|
|
|
42
|
+ arguments are optional. Resulting list can be passed directly to
|
|
|
43
|
+ `BotMessage.set_reactions()`.
|
|
|
44
|
+
|
|
|
45
|
+ Params
|
|
|
46
|
+ - did_delete Whether the message(s) have been deleted. Pass True or
|
|
|
47
|
+ False if this applies, omit to leave out delete action.
|
|
|
48
|
+ - message_count How many messages there are. Used for pluralizing
|
|
|
49
|
+ description. Defaults to 1. Omit if n/a.
|
|
|
50
|
+ - did_kick Whether the user(s) have been kicked. Pass True or
|
|
|
51
|
+ False if this applies, omit to leave out kick action.
|
|
|
52
|
+ - did_ban Whether the user(s) have been banned. Pass True or
|
|
|
53
|
+ False if this applies, omit to leave out ban action.
|
|
|
54
|
+ - user_count How many users there are. Used for pluralizing
|
|
|
55
|
+ description. Defaults to 1. Omit if n/a.
|
|
|
56
|
+ """
|
|
|
57
|
+ reactions = []
|
|
|
58
|
+ if did_delete is not None:
|
|
|
59
|
+ if did_delete:
|
|
|
60
|
+ reactions.append(BotMessageReaction(
|
|
|
61
|
+ CONFIG['trash_emoji'],
|
|
|
62
|
+ False,
|
|
|
63
|
+ 'Message deleted' if message_count == 1 else 'Messages deleted'))
|
|
|
64
|
+ else:
|
|
|
65
|
+ reactions.append(BotMessageReaction(
|
|
|
66
|
+ CONFIG['trash_emoji'],
|
|
|
67
|
+ True,
|
|
|
68
|
+ 'Delete message' if message_count == 1 else 'Delete messages'))
|
|
|
69
|
+ if did_kick is not None:
|
|
|
70
|
+ if did_ban is not None and did_ban:
|
|
|
71
|
+ # Don't show kick option at all if we also banned
|
|
|
72
|
+ pass
|
|
|
73
|
+ elif did_kick:
|
|
|
74
|
+ reactions.append(BotMessageReaction(
|
|
|
75
|
+ CONFIG['kick_emoji'],
|
|
|
76
|
+ False,
|
|
|
77
|
+ 'User kicked' if user_count == 1 else 'Users kicked'))
|
|
|
78
|
+ else:
|
|
|
79
|
+ reactions.append(BotMessageReaction(
|
|
|
80
|
+ CONFIG['kick_emoji'],
|
|
|
81
|
+ True,
|
|
|
82
|
+ 'Kick user' if user_count == 1 else 'Kick users'))
|
|
|
83
|
+ if did_ban is not None:
|
|
|
84
|
+ if did_ban:
|
|
|
85
|
+ reactions.append(BotMessageReaction(
|
|
|
86
|
+ CONFIG['ban_emoji'],
|
|
|
87
|
+ False,
|
|
|
88
|
+ 'User banned' if user_count == 1 else 'Users banned'))
|
|
|
89
|
+ else:
|
|
|
90
|
+ reactions.append(BotMessageReaction(
|
|
|
91
|
+ CONFIG['ban_emoji'],
|
|
|
92
|
+ True,
|
|
|
93
|
+ 'Ban user' if user_count == 1 else 'Ban users'))
|
|
|
94
|
+ return reactions
|
|
|
95
|
+
|
|
31
|
96
|
class BotMessage:
|
|
32
|
97
|
"""
|
|
33
|
98
|
Holds state for a bot-generated message. A message is composed, sent via
|
|
|
@@ -55,6 +120,7 @@ class BotMessage:
|
|
55
|
120
|
self.type = type
|
|
56
|
121
|
self.context = context
|
|
57
|
122
|
self.quote = None
|
|
|
123
|
+ self.source_cog = None # Set by `BaseCog.post_message()`
|
|
58
|
124
|
self.__posted_text = None # last text posted, to test for changes
|
|
59
|
125
|
self.__posted_emoji = set()
|
|
60
|
126
|
self.__message = None # Message
|
|
|
@@ -160,11 +226,11 @@ class BotMessage:
|
|
160
|
226
|
else:
|
|
161
|
227
|
channel_id = Storage.get_config_value(self.guild, ConfigKey.WARNING_CHANNEL_ID)
|
|
162
|
228
|
if channel_id is None:
|
|
163
|
|
- BaseCog.guild_trace(self.guild, 'No warning channel set! No warning issued.')
|
|
|
229
|
+ BaseCog.log(self.guild, '\u0007No warning channel set! No warning issued.')
|
|
164
|
230
|
return
|
|
165
|
231
|
channel: TextChannel = self.guild.get_channel(channel_id)
|
|
166
|
232
|
if channel is None:
|
|
167
|
|
- BaseCog.guild_trace(self.guild, 'Configured warning channel does not exist!')
|
|
|
233
|
+ BaseCog.log(self.guild, '\u0007Configured warning channel does not exist!')
|
|
168
|
234
|
return
|
|
169
|
235
|
self.__message = await channel.send(content=content)
|
|
170
|
236
|
self.__posted_text = content
|
|
|
@@ -205,9 +271,9 @@ class BotMessage:
|
|
205
|
271
|
s += '\n\nAvailable actions:'
|
|
206
|
272
|
for reaction in self.__reactions:
|
|
207
|
273
|
if reaction.is_enabled:
|
|
208
|
|
- s += f'\n {reaction.emoji} {reaction.description}'
|
|
|
274
|
+ s += f'\n {reaction.emoji} {reaction.description}'
|
|
209
|
275
|
else:
|
|
210
|
|
- s += f'\n {reaction.description}'
|
|
|
276
|
+ s += f'\n {reaction.description}'
|
|
211
|
277
|
|
|
212
|
278
|
return s
|
|
213
|
279
|
|
|
|
@@ -241,10 +307,10 @@ class BaseCog(commands.Cog):
|
|
241
|
307
|
Storage.set_state_value(guild, 'bot_messages', bm)
|
|
242
|
308
|
return bm
|
|
243
|
309
|
|
|
244
|
|
- @classmethod
|
|
245
|
|
- async def post_message(cls, message: BotMessage) -> bool:
|
|
|
310
|
+ async def post_message(self, message: BotMessage) -> bool:
|
|
|
311
|
+ message.source_cog = self
|
|
246
|
312
|
await message._update()
|
|
247
|
|
- guild_messages = cls.__bot_messages(message.guild)
|
|
|
313
|
+ guild_messages = self.__bot_messages(message.guild)
|
|
248
|
314
|
if message.is_sent():
|
|
249
|
315
|
guild_messages[message.message_id()] = message
|
|
250
|
316
|
return True
|
|
|
@@ -279,6 +345,9 @@ class BaseCog(commands.Cog):
|
|
279
|
345
|
if bot_message is None:
|
|
280
|
346
|
# Unknown message (expired or was never tracked)
|
|
281
|
347
|
return
|
|
|
348
|
+ if self is not bot_message.source_cog:
|
|
|
349
|
+ # Belongs to a different cog
|
|
|
350
|
+ return
|
|
282
|
351
|
reaction = bot_message.reaction_for_emoji(payload.emoji)
|
|
283
|
352
|
if reaction is None or not reaction.is_enabled:
|
|
284
|
353
|
# Can't use this reaction with this message
|
|
|
@@ -298,6 +367,8 @@ class BaseCog(commands.Cog):
|
|
298
|
367
|
"""
|
|
299
|
368
|
pass
|
|
300
|
369
|
|
|
|
370
|
+ # Helpers
|
|
|
371
|
+
|
|
301
|
372
|
@classmethod
|
|
302
|
373
|
async def validate_param(cls, context: commands.Context, param_name: str, value,
|
|
303
|
374
|
allowed_types: tuple = None,
|
|
|
@@ -330,17 +401,19 @@ class BaseCog(commands.Cog):
|
|
330
|
401
|
@classmethod
|
|
331
|
402
|
async def warn(cls, guild: Guild, message: str) -> Message:
|
|
332
|
403
|
"""
|
|
|
404
|
+ DEPRECATED. Use post_message.
|
|
|
405
|
+
|
|
333
|
406
|
Sends a warning message to the configured warning channel for the
|
|
334
|
407
|
given guild. If no warning channel is configured no action is taken.
|
|
335
|
408
|
Returns the Message if successful or None if not.
|
|
336
|
409
|
"""
|
|
337
|
410
|
channel_id = Storage.get_config_value(guild, ConfigKey.WARNING_CHANNEL_ID)
|
|
338
|
411
|
if channel_id is None:
|
|
339
|
|
- cls.guild_trace(guild, 'No warning channel set! No warning issued.')
|
|
|
412
|
+ cls.log(guild, '\u0007No warning channel set! No warning issued.')
|
|
340
|
413
|
return None
|
|
341
|
414
|
channel: TextChannel = guild.get_channel(channel_id)
|
|
342
|
415
|
if channel is None:
|
|
343
|
|
- cls.guild_trace(guild, 'Configured warning channel does not exist!')
|
|
|
416
|
+ cls.log(guild, '\u0007Configured warning channel does not exist!')
|
|
344
|
417
|
return None
|
|
345
|
418
|
mention: str = Storage.get_config_value(guild, ConfigKey.WARNING_MENTION)
|
|
346
|
419
|
text: str = message
|
|
|
@@ -352,6 +425,8 @@ class BaseCog(commands.Cog):
|
|
352
|
425
|
@classmethod
|
|
353
|
426
|
async def update_warn(cls, warn_message: Message, new_text: str) -> None:
|
|
354
|
427
|
"""
|
|
|
428
|
+ DEPRECATED. Use post_message.
|
|
|
429
|
+
|
|
355
|
430
|
Updates the text of a previously posted `warn`. Includes configured
|
|
356
|
431
|
mentions if necessary.
|
|
357
|
432
|
"""
|
|
|
@@ -364,5 +439,10 @@ class BaseCog(commands.Cog):
|
|
364
|
439
|
await warn_message.edit(content=text)
|
|
365
|
440
|
|
|
366
|
441
|
@classmethod
|
|
367
|
|
- def guild_trace(cls, guild: Guild, message: str) -> None:
|
|
|
442
|
+ def log(cls, guild: Guild, message) -> None:
|
|
|
443
|
+ now = datetime.now()
|
|
|
444
|
+ print(f'[{now.strftime("%Y-%m-%dT%H:%M:%S")}|{cls.__name__}|{guild.name}] {message}')
|
|
|
445
|
+
|
|
|
446
|
+ @classmethod
|
|
|
447
|
+ def deprecated_guild_trace(cls, guild: Guild, message: str) -> None:
|
|
368
|
448
|
print(f'[guild {guild.id}|{guild.name}] {message}')
|