-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheventmsg.py
550 lines (498 loc) · 18.1 KB
/
eventmsg.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
"""
EventMsg - Automated messages triggered by events
Generalized version of welcome and farewell
Dependancies: Auth
"""
from __future__ import annotations
import hashlib, enum
from datetime import datetime
from typing import Optional, Union, TYPE_CHECKING
import discord
from discord import app_commands
from discord.ext import commands
if TYPE_CHECKING:
from main import MerelyBot
from babel import Resolvable
from configparser import SectionProxy
getdatecomponent = [
{
'label': "Edit date",
'custom_id': 'edit_date',
'style': discord.ButtonStyle.secondary,
'emoji': discord.PartialEmoji(name='📅')
}
]
class Event():
"""
The conditions of an event which the bot should watch for
Event parameter cheat sheet;
member
mention
name
discriminator
guild
name
role
name
channel
name
mention
emoji
name
ban
reason
date
date
week
month
quarter
year
shortyear
xp
level
"""
def __init__(
self,
name:str,
usage:str,
variables:tuple[str],
components:Optional[list[dict[str]]] = None
) -> None:
self.name = name
self.example = usage
self.variables = variables
self.components = components # Additional custom components for this event
def __str__(self) -> str:
return self.name
def __hash__(self) -> int:
return int(hashlib.md5(self.name, usedforsecurity=False), 16)
@enum.unique
class Events(enum.Enum):
# Built-in discord events, most require members scope
WELCOME = Event(
'on_member_join',
"{member.mention} has joined {guild.name}!",
('member.mention', 'member.name', 'guild.name')
),
FAREWELL = Event(
'on_member_leave',
"{member.name}#{member.discriminator} has left {guild.name}.",
('member.name', 'member.discriminator', 'guild.name')
),
ROLE_GAIN = Event(
'on_member_update role add',
"{member.mention} has been given the role {role.name}!",
('member.mention', 'member.name', 'role.name')
),
ROLE_LOSE = Event(
'on_member_update role remove',
"{member.mention} has lost the role {role.name}!",
('member.mention', 'member.name', 'role.name')
),
MESSAGE = Event(
'on_message',
"{member.name} just posted in {channel.mention}",
('member.name', 'channel.mention')
), # Requires message content scope
NEW_EMOJI = Event(
'on_guild_emojis_update',
"A new emoji has just been added; {emoji} - use it with :{emoji.name}:!",
('emoji', 'emoji.name')
),
BAN = Event(
'on_member_ban',
"{user.name}#{user.discriminator} has been banned! Reason: {ban.reason}.",
('user.name', 'user.discriminator', 'ban.reason')
),
UNBAN = Event(
'on_member_unban',
"{user.name}#{user.disciminator}'s ban has been lifted!",
('user.name', 'user.discriminator')
),
# Time system
DAILY = Event(
'on_day',
"Daily post - {date.date}",
('date.date',),
getdatecomponent
),
WEEKLY = Event(
'on_week',
"Weekly post - {date.week}",
('date.date', 'date.week'),
getdatecomponent
),
MONTHLY = Event(
'on_month',
"Monthly post - {date.month}",
('date.date', 'date.month'),
getdatecomponent
),
QUARTERLY = Event(
'on_quarter',
"Quarterly post - Q{date.quarter}{date.shortyear}",
('date.date', 'date.quarter', 'date.year', 'date.shortyear'),
getdatecomponent
),
YEARLY = Event(
'on_year',
"Yearly post - {date.year}",
('date.date', 'date.year', 'date.shortyear'),
getdatecomponent
),
# XP system, does nothing unless XP module is enabled
XP_UP = Event(
'on_xp_up',
"{member.mention} has gained XP; level {xp.level}, {xp} XP.",
('member.mention', 'member.name', 'xp.level', 'xp')
),
LEVEL_UP = Event(
'on_level_up',
"{member.mention} is now level {xp.level}!",
('member.mention', 'member.name', 'xp.level', 'xp')
)
class Action(enum.Enum):
""" Actions that can be performed on an event """
NOTHING = 0
SEND_MESSAGE = 1
EDIT_MESSAGE = 2
GRANT_XP = 3
class Date(datetime):
"""
This is a modified datetime class which adds some easy formatting properties for strings
"""
@property
def date(self) -> str:
""" Override for date, returns D/MM/YYYY """
return f"<t:{round(self.timestamp())}:d>"
@property
def week(self) -> int:
""" Week number (0-52) """
return int(self.strftime('%W'))
@property
def quarter(self) -> int:
""" Quarter number (1-4) as in Q422 """
return {1:1, 2:1, 3:1, 4:2, 5:2, 6:2, 7:3, 8:3, 9:3, 10:4, 11:4, 12:4}[self.month]
@property
def shortyear(self) -> int:
""" 2 digit representaion of the current year (0-99) """
return self.year % 100
class EventMsg(commands.Cog):
""" Setup custom messages to send on an event """
SCOPE = 'eventmsg'
@property
def config(self) -> SectionProxy:
""" Shorthand for self.bot.config[scope] """
return self.bot.config[self.SCOPE]
def babel(self, target:Resolvable, key:str, **values: dict[str, str | bool]) -> str:
""" Shorthand for self.bot.babel(scope, key, **values) """
return self.bot.babel(target, self.SCOPE, key, **values)
def __init__(self, bot:MerelyBot):
self.bot = bot
# ensure config file has required data
if not bot.config.has_section(self.SCOPE):
bot.config.add_section(self.SCOPE)
def pop_msg_var(self, event:Event, message:str, **kwargs) -> str:
""" Goes through allowed variables for this event and fills the string """
newmessage = message
for evar in event.variables:
currentval: Optional[Union[
discord.Member,discord.User,discord.Guild,discord.Role,discord.Emoji,Date
]] = None
evarparts = evar.split('.')
if evarparts[0] in kwargs and kwargs[evarparts[0]] is not None:
currentvar = kwargs[evarparts[0]]
if len(evarparts) > 1:
if evarparts[1] in dir(currentvar):
currentval = getattr(currentvar, evarparts[1])
else:
raise AssertionError(f".{evarparts[1]} was not found in {evarparts[0]}!")
else:
currentval = currentvar
if currentval is not None:
newmessage = newmessage.replace("{"+evar+"}", str(currentval))
else:
print(f"WARN: Missing variable {evar} for event (value is None)")
else:
print(f"WARN: Missing variable {evar} for event (was not in kwargs)")
return newmessage
@commands.Cog.listener("on_member_join")
async def on_welcome(self, member:discord.Member):
"""welcome service, shows a custom welcome message to new users"""
if f"{member.guild.id}_welcome" in self.config:
data = self.config[f"{member.guild.id}_welcome"].split(', ')
channel = member.guild.get_channel(int(data[0]))
await channel.send(
', '.join(data[1:]).format(member.mention, member.guild.name)
)
@commands.Cog.listener("on_raw_member_remove")
async def on_farewell(self, payload:discord.RawMemberRemoveEvent):
"""farewell service, shows a custom farewell message whenever someone leaves"""
if f"{payload.guild_id}_farewell" in self.config:
data = self.config[f"{payload.guild_id}_farewell"].split(', ')
guild = self.bot.get_guild(payload.guild_id)
channel = guild.get_channel(int(data[0]))
await channel.send(', '.join(data[1:])
.format(f"{payload.user.name}#{payload.user.discriminator}", guild.name))
class EventMessageEditor(discord.ui.Modal):
""" Modal simply provides a text box to change the event message """
def __init__(self, eventview:"EventMsg.EventEditView"):
""" Create modal with the current message content """
super().__init__(
title="Edit event message",
custom_id=f'{eventview.eid}_editor',
components=[discord.ui.TextInput(
label="Message",
custom_id='message',
placeholder=eventview.message,
default=eventview.message,
style=discord.TextStyle.paragraph,
min_length=1
)]
)
self.eventview = eventview
async def on_submit(self, inter:discord.Interaction, /):
""" Handle the new message content """
self.eventview.message = inter.data.get('values')['message']
await self.eventview.update(inter)
class EventEditView(discord.ui.View):
""" Controls and state for an EventMessage """
def __init__(
self,
parent:"EventMsg",
inter:discord.Interaction,
event:Event,
action:Action,
message:str,
xp:int,
channel:discord.TextChannel,
usage:str
):
""" Create all buttons """
super().__init__(timeout=60)
self.parent = parent
self.inter = inter
self.event = event
self.action = action
self.channel = channel
self.message = message
self.xp = xp
self.usage = usage
self.eid = f"{channel.guild.id}_{channel.id}_{event.name}_{action.name}"
# Build view
match action:
case Action.SEND_MESSAGE | Action.EDIT_MESSAGE:
self.add_item(parent.bot.utilities.CallbackButton(
callback=self.edit_click,
label="Edit",
style=discord.ButtonStyle.secondary,
custom_id=f'{self.eid}_edit',
emoji='✏️'
))
if event.components:
for comp in event.components:
self.add_item(parent.bot.utilities.CallbackButton(
callback=self.custom_click,
**comp
))
case Action.GRANT_XP:
self.add_item(parent.bot.utilities.CallbackSelect(
callback=self.custom_click,
custom_id=f"{self.eid}_xpmult",
placeholder="XP Points",
options=range(1,25)
))
if event.name == 'on_message':
self.add_item(parent.bot.utilities.CallbackSelect(
callback=self.custom_click,
custom_id=f"{self.eid}_xpmode",
placeholder="Mode",
options={'Number (simple mode)': 0, 'Message length': 1}
))
case _:
raise AssertionError("An event was specified which was not handled in /eventmessage")
self.add_item(parent.bot.utilities.CallbackButton(
callback=self.submit_click,
label="Submit",
custom_id=f"{self.eid}_submit",
style=discord.ButtonStyle.primary,
emoji='✅',
disabled=True
))
async def update(self, inter:discord.Interaction):
""" Refresh the view, reflecting any changes made to variables """
state = self.parent.babel(inter, 'event_controlpanel',
message=self.message,
channel=self.channel.mention,
xp=self.xp,
usage=self.usage)
submitbtn:discord.Button = [
child for child in self.children if child.custom_id == f'{self.eid}_submit'
][0]
if self.message:
submitbtn.disabled = False
else:
submitbtn.disabled = True
await inter.response.edit_message(content=state, components=self.children)
async def edit_click(self, inter:discord.Interaction):
""" Opens the event message editor """
await inter.response.send_modal(EventMsg.EventMessageEditor(self))
async def submit_click(self, inter:discord.Interaction):
""" Saves event to storage and finishes the interaction """
pass
async def custom_click(self, inter:discord.Interaction):
""" Code to handle any other user input (Button or Select) """
if inter.component.custom_id == 'edit_date':
pass
elif inter.component.custom_id.endswith('_xpmult'):
pass
elif inter.component.custom_id.endswith('_xpmode'):
pass
else:
raise AssertionError(f"Unhandled click event '{inter.component.custom_id}'")
async def on_timeout(self):
for item in self.children:
if 'disabled' in item:
item.disabled = True
#await self.msg.edit(content=self.parent.bot.babel(self.msg.guild, 'error', 'timeoutview'))
@app_commands.command()
@app_commands.describe(
channel="The target channel where the event message will be sent",
event="The event for the bot to watch for",
action="The action the bot will take in response"
)
@app_commands.default_permissions(moderate_members=True)
@commands.bot_has_permissions(read_messages=True, manage_messages=True)
async def eventmessage(
self,
inter:discord.Interaction,
channel:discord.TextChannel,
event:Events,
action:Action
):
"""
Set up a message/action to take whenever something happens on the server.
"""
# Default state
usage = ''
message = ''
xp = 0
if action in (Action.SEND_MESSAGE, Action.EDIT_MESSAGE):
message = event.example
usage = self.pop_msg_var(
event=event,
message='\n'.join(["`[["+evar+"]]` = {"+evar+"}" for evar in event.variables]),
guild=channel.guild,
member=inter.user,
channel=channel,
emoji=channel.guild.emojis[-1],
role=channel.guild.roles[-1],
date=Date(2023, 10, 28) # example date shows month and day numbers clearly
).replace('[[', '{').replace(']]', '}')
elif action is Action.GRANT_XP:
xp = 0
elif action is Action.NOTHING:
#TODO: remove saved eventmessage
pass
else:
raise AssertionError(f"Unhandled action '{action.name}'!")
state = self.babel(inter, 'event_controlpanel',
message=message,
channel=channel.mention,
xp=xp,
usage=usage)
await inter.response.send_message(
state,
view=self.EventEditView(self, inter, event, action, message, xp, channel, usage),
ephemeral=True,
allowed_mentions=[]
)
welcome = app_commands.Group(
name='welcome',
description="An automation that posts a message whenever a user joins",
guild_only=True,
default_permissions=discord.Permissions(administrator=True)
)
@welcome.command(name='get')
async def welcome_get(self, inter:discord.Interaction):
""" Gets the current welcome message. Otherwise, gives instructions on how to set one """
if f'{inter.guild.id}_welcome' in self.config:
data = self.config[f"{inter.guild.id}_welcome"].split(', ')
await inter.response.send_message(
self.bot.babel(inter, 'greeter', 'greeting_preview',
channel=inter.guild.get_channel(int(data[0])).mention,
message=', '.join(data[1:]).format('@USER', inter.guild.name)),
ephemeral=True
)
else:
await inter.response.send_message(
self.bot.babel(inter, 'greeter', 'welcome_set_instructions'),
ephemeral=True
)
@welcome.command(name='set')
@app_commands.describe(message="The message that will be sent when a member joins.")
async def welcome_set(self, inter:discord.Interaction, message:str):
"""
Sets the welcome message based on your input.
"""
self.config[f'{inter.guild.id}_welcome'] = f"{inter.channel.id}, {message}"
self.bot.config.save()
await inter.response.send_message(
self.bot.babel(inter, 'greeter', 'welcome_set_success')
)
@welcome.command(name='clear')
async def welcome_clear(self, inter:discord.Interaction):
""" Clears the welcome message """
if f'{inter.guild.id}_welcome' in self.config:
self.config.pop(f'{inter.guild.id}_welcome')
self.bot.config.save()
await inter.response.send_message(
self.bot.babel(inter, 'greeter', 'welcome_clear_success')
)
else:
await inter.response.send_message(
self.bot.babel(inter, 'greeter', 'welcome_clear_failed')
)
farewell = app_commands.Group(
name='farewell',
description="An automation that posts a message whenever a user leaves",
guild_only=True,
default_permissions=discord.Permissions(administrator=True)
)
@farewell.command(name='get')
async def farewell_get(self, inter:discord.Interaction):
""" Gets the current farewell message. Otherwise, gives instructions on how to set one """
if f'{inter.guild.id}_farewell' in self.config:
data = self.config[f"{inter.guild.id}_farewell"].split(', ')
await inter.response.send_message(
self.bot.babel(inter, 'greeter', 'greeting_preview',
channel=inter.guild.get_channel(int(data[0])).mention,
message=', '.join(data[1:]).format('USER#1234', inter.guild.name)),
ephemeral=True
)
else:
await inter.response.send_message(
self.bot.babel(inter, 'greeter', 'farewell_set_instructions'),
ephemeral=True
)
@farewell.command(name='set')
@app_commands.describe(message="The message that will be sent when a member joins.")
async def farewell_set(self, inter:discord.Interaction, message:str):
"""
Sets the welcome message based on your input.
"""
self.config[f'{inter.guild.id}_farewell'] = f"{inter.channel.id}, {message}"
self.bot.config.save()
await inter.response.send_message(self.bot.babel(inter, 'greeter', 'farewell_set_success'))
@farewell.command(name='clear')
async def farewell_clear(self, inter:discord.Interaction):
""" Clears the farewell message """
if f'{inter.guild.id}_farewell' in self.config:
self.config.pop(f'{inter.guild.id}_farewell')
self.bot.config.save()
await inter.response.send_message(self.bot.babel(inter, 'greeter', 'farewell_clear_success'))
else:
await inter.response.send_message(self.bot.babel(inter, 'greeter', 'farewell_clear_failure'))
async def setup(bot:MerelyBot):
""" Bind this cog to the bot """
await bot.add_cog(EventMsg(bot))