-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsupportutils.py
1425 lines (1263 loc) · 56.2 KB
/
supportutils.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
from __future__ import annotations
import asyncio
import json
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional, Union, TYPE_CHECKING
import discord
from discord.ext import commands
from discord.utils import MISSING
from core import checks
from core.models import getLogger, PermissionLevel
from core.paginator import EmbedPaginatorSession
from core.time import UserFriendlyTime
from core.utils import truncate
if TYPE_CHECKING:
from motor.motor_asyncio import AsyncIOMotorCollection
from bot import ModmailBot
from core.thread import Thread
info_json = Path(__file__).parent.resolve() / "info.json"
with open(info_json, encoding="utf-8") as f:
__plugin_info__ = json.loads(f.read())
__plugin_name__ = __plugin_info__["name"]
__version__ = __plugin_info__["version"]
__description__ = "\n".join(__plugin_info__["description"]).format(__version__)
logger = getLogger(__name__)
max_selectmenu_description = 100
# <!-- Developer -->
try:
from discord.ext.modmail_utils import ConfirmView, EmojiConverter, Limit
except ImportError as exc:
required = __plugin_info__["cogs_required"][0]
raise RuntimeError(
f"`modmail_utils` package is required for {__plugin_name__} plugin to function.\n"
f"Install {required} plugin to resolve this issue."
) from exc
from .core.config import SupportUtilityConfig
from .core.models import ContactManager, FeedbackManager, ThreadMoveManager
from .core.views import Button, ContactView, Modal, SupportUtilityView
# <!-- ----- -->
class SupportUtility(commands.Cog, name=__plugin_name__):
__doc__ = __description__
def __init__(self, bot: ModmailBot):
self.bot: ModmailBot = bot
self.db: AsyncIOMotorCollection = self.bot.api.get_plugin_partition(self)
self.config: SupportUtilityConfig = SupportUtilityConfig(self, self.db)
self.contact_manager: ContactManager = ContactManager(self)
self.feedback_manager: FeedbackManager = FeedbackManager(self)
self.move_manager: ThreadMoveManager = ThreadMoveManager(self)
async def cog_load(self) -> None:
self.bot.loop.create_task(self.initialize())
async def cog_unload(self) -> None:
view = self.contact_manager.view
if view is not MISSING:
view.stop()
for feedback in self.feedback_manager.active:
feedback.task.cancel()
self.move_manager.teardown()
async def initialize(self) -> None:
await self.bot.wait_for_connected()
await self.config.fetch()
await self.contact_manager.initialize()
await self.feedback_manager.populate()
await self.move_manager.initialize()
def _resolve_modal_payload(self, item: Button) -> Dict[str, Any]:
"""
Internal method to respectively resolve the required payload to initiate
the `Modal` view.
"""
view = item.view
keys = view.extras.get("keys", [])
if not 1 < len(keys) < 4:
raise ValueError("Unable to unpack. keys length must only be 2 or 3.")
if len(keys) == 2:
prefix, key, subkey = *keys, None
else:
prefix, key, subkey = keys
# confusing part
# valid_prefixes = ("contact", "feedback", "thread_move") # these three were root config
# valid_keys = ("button", "confirmation", "select", "embed", "rating", "response", "inactive", "responded")
# valid_subkeys = ("embed", "placeholder", "options")
options = {}
current = view.extras.get("current")
if key == "button":
elements = [("emoji", 256), ("label", Limit.button_label), ("style", 32)]
for name, length in elements:
options[name] = {
"label": name.title(),
"max_length": length,
"required": False,
"default": view.inputs.get(name) or current.get(name),
}
elif key in ("select", "rating"):
if subkey == "placeholder":
options[subkey] = {
"label": subkey.title(),
"max_length": Limit.select_placeholder,
"required": True,
"default": view.inputs.get(subkey) or current,
}
else:
# select options
elements = [
("emoji", 256),
("label", Limit.button_label),
("description", Limit.select_description),
("category", 256),
]
for name, length in elements:
options[name] = {
"label": name.title(),
"max_length": length,
"required": name in ("label", "category"),
"default": view.inputs.get(name),
}
elif "embed" in (key, subkey):
elements = [
("title", Limit.embed_title),
("description", Limit.text_input_max),
("footer", Limit.embed_footer),
]
for name, length in elements:
options[name] = {
"label": name.title(),
"max_length": length,
"style": discord.TextStyle.long if name == "description" else discord.TextStyle.short,
"required": name == "description",
"default": view.inputs.get(name) or current.get(name),
}
elif key == "response":
options[key] = {
"label": key.title(),
"max_length": Limit.text_input_max,
"style": discord.TextStyle.long,
"required": True,
"default": view.inputs.get(key) or current,
}
else:
raise ValueError(f"Invalid view input session. Got `{prefix}.{key}.{subkey}` keys.")
return options
async def _button_callback(self, interaction: discord.Interaction, item: Button) -> None:
if not isinstance(item, Button):
raise TypeError(
f"Invalid type of item received. Expected Button, got {type(item).__name__} instead."
)
options = self._resolve_modal_payload(item)
view = item.view
title = view.extras["title"] + " config"
modal = Modal(view, options, self._modal_callback, title=title)
await interaction.response.send_modal(modal)
await modal.wait()
if view.value:
view.disable_and_stop()
return
async def _modal_callback(self, interaction: discord.Interaction, modal: Modal) -> None:
"""
Resolve and convert inputs submitted from Modal view.
Things that need to be converted:
- Emoji
- Category channel
- Button style
Everything else is just a plain string.
"""
view = modal.view
converters = {
"emoji": EmojiConverter,
"category": commands.CategoryChannelConverter,
}
errors = []
if view.extras["keys"][1] in ("button", "select") and all(
(view.inputs.get(elem) is None for elem in ("emoji", "label"))
):
errors.append("ValueError: Emoji and Label cannot both be None.")
for key, value in view.inputs.items():
if value is None:
view.outputs[key] = value
continue
if key == "style":
try:
value = value.lower()
entity = discord.ButtonStyle[value]
if entity == discord.ButtonStyle.url:
errors.append("ValueError: ButtonStyle.url is not supported.")
continue
except (KeyError, TypeError, ValueError):
errors.append(f"ValueError: `{value}` is invalid for color style.")
continue
view.outputs[key] = value
continue
conv = converters.get(key)
if conv is None:
# mostly plain string
view.outputs[key] = value
continue
try:
entity = await conv().convert(view.ctx, value)
except Exception as exc:
errors.append(f"{type(exc).__name__}: {str(exc)}")
continue
if isinstance(entity, discord.CategoryChannel):
# check exists
for data in self.config.contact["select"]["options"]:
category_id = data["category"]
if category_id and str(entity.id) == category_id:
errors.append(
f"ValueError: Category {entity} is already linked to {data['label'] or data['emoji']}."
)
continue
value = str(entity.id)
elif isinstance(entity, (discord.PartialEmoji, discord.Emoji)):
value = str(entity)
else:
errors.append(f"TypeError: Invalid type of converted value, `{type(entity).__name__}`.")
continue
view.outputs[key] = value
if errors:
embed = discord.Embed(
description="\n".join(errors),
color=self.bot.error_color,
)
view.value = False
modal.stop()
return await interaction.response.send_message(embed=embed, ephemeral=True)
await interaction.response.defer()
view.value = True
modal.stop()
def get_config_view(self, ctx: commands.Context, **extras: Dict[str, Any]) -> SupportUtilityView:
view = SupportUtilityView(ctx, extras=extras)
set_label = "add" if extras["keys"][-1] == "options" else "set"
buttons = [
(set_label, discord.ButtonStyle.grey, self._button_callback),
("cancel", discord.ButtonStyle.red, view._action_cancel),
]
for label, style, callback in buttons:
button = Button(
label=label.title(),
style=style,
callback=callback,
)
view.add_item(button)
return view
async def _set_button_invoker(
self,
ctx: commands.Context,
name: str,
keys: List[str],
button_config: Dict[str, Any],
defaults: Dict[str, Any],
argument: Optional[str],
) -> None:
if argument and argument.lower() in ("clear", "reset"):
button_config.clear()
for key, value in defaults.items():
button_config[key] = value
await self.config.update()
embed = discord.Embed(
color=self.bot.main_color,
description=f"{name.capitalize()} configurations are now reset to defaults.",
)
await ctx.send(embed=embed)
elif argument is None:
description = ctx.command.help.split("\n\n")[:-1]
description = "\n\n".join(description) + "\n\n"
description += (
"__**Available fields:**__\n"
"- **Emoji** : Emoji shown on the button. May be a unicode emoji, "
"format of `:name:`, `<:name:id>` or `<a:name:id>` (animated emoji).\n"
f"- **Label** : Button label. Must not exceed {Limit.button_label} characters.\n"
"- **Style** : The color style for the button. Must be one of these (case insensitive):\n"
" - `Blurple`\n"
" - `Green`\n"
" - `Red`\n"
" - `Grey`\n\n"
)
embed = discord.Embed(
title=name.capitalize(),
color=self.bot.main_color,
description=description,
)
embed.set_footer(text="Press Set to set/edit the values")
embed.description += "### Current values"
for key in ("emoji", "label", "style"):
embed.add_field(name=key.title(), value=f"`{button_config.get(key)}`")
view = self.get_config_view(ctx, title=embed.title, keys=keys, current=button_config)
view.message = message = await ctx.send(embed=embed, view=view)
await view.wait()
await message.edit(view=view)
if view.value:
payload = view.outputs
embed = discord.Embed(
description=f"Successfully set the new configurations for {name}.\n\n",
color=self.bot.main_color,
)
embed.description += "### New values"
for key in list(payload):
embed.add_field(name=key.title(), value=f"`{payload[key]}`")
button_config[key] = payload.pop(key)
await self.config.update()
await view.interaction.followup.send(embed=embed)
else:
raise commands.BadArgument(f"{argument} is not a valid argument.")
async def _set_embed_invoker(
self,
ctx: commands.Context,
name: str,
keys: List[str],
embed_config: Dict[str, Any],
defaults: Dict[str, Any],
argument: Optional[str],
) -> None:
if argument and argument.lower() in ("clear", "reset"):
embed_config.clear()
for key, value in defaults.items():
embed_config[key] = value
await self.config.update()
embed = discord.Embed(
color=self.bot.main_color,
description=f"{name.capitalize()} embed configurations are now reset to defaults.",
)
await ctx.send(embed=embed)
elif argument is None:
# remove the last part where it shows the argument usage bit
description = ctx.command.help.format(prefix=self.bot.prefix).split("\n\n")[:-1]
description = "\n\n".join(description) + "\n\n"
description += (
"__**Available fields:**__\n"
f"- **Title** : Embed title. Max {Limit.embed_title} characters.\n"
f"- **Description** : Embed description. Max {Limit.text_input_max} characters.\n"
f"- **Footer** : Embed footer text. Max {Limit.embed_footer} characters.\n\n"
)
embed = discord.Embed(
title=name.capitalize(),
color=self.bot.main_color,
description=description,
)
embed.set_footer(text="Press Set to set/edit the values")
embed.description += "### Current values"
for key in ("title", "description", "footer"):
embed.add_field(name=key.title(), value=f"`{truncate(str(embed_config.get(key)), max=256)}`")
view = self.get_config_view(ctx, title=embed.title, keys=keys, current=embed_config)
view.message = message = await ctx.send(embed=embed, view=view)
await view.wait()
await message.edit(view=view)
if view.value:
payload = view.outputs
embed = discord.Embed(
description=f"Successfully set the new configurations for {name} embed.\n\n",
color=self.bot.main_color,
)
embed.description += "### New values"
for key in list(payload):
embed.add_field(name=key.title(), value=f"`{truncate(str(payload[key]), max=1024)}`")
embed_config[key] = payload.pop(key)
await self.config.update()
await view.interaction.followup.send(embed=embed)
else:
raise commands.BadArgument(f"{argument} is not a valid argument.")
async def _set_enable_invoker(
self,
ctx: commands.Context,
name: str,
parent_config: Dict[str, Any],
mode: Optional[bool],
) -> None:
enabled = parent_config.get("enable", False)
if mode is None:
embed = discord.Embed(
color=self.bot.main_color,
description=f"{name.capitalize()} feature is currently "
+ ("enabled." if enabled else "disabled."),
)
return await ctx.send(embed=embed)
if mode == enabled:
raise commands.BadArgument(
f"{name.capitalize()} feature is already " + ("enabled." if enabled else "disabled.")
)
parent_config["enable"] = mode
await self.config.update()
embed = discord.Embed(
color=self.bot.main_color,
description=f"{name.capitalize()} feature is now " + ("enabled." if mode else "disabled."),
)
await ctx.send(embed=embed)
async def _set_category_invoker(
self,
ctx: commands.Context,
key: str,
entity: Optional[Union[discord.CategoryChannel, str]],
) -> None:
embed = discord.Embed(color=self.bot.main_color)
if entity is None:
category = getattr(self.move_manager, f"{key}_category")
embed.description = f"{key.capitalize()} category is currently set to {category}."
await ctx.send(embed=embed)
elif isinstance(entity, discord.CategoryChannel):
if ctx.guild != self.bot.modmail_guild:
raise commands.BadArgument(
f"{key.capitalize()} category can only be set in modmail guild: {self.bot.modmail_guild}."
)
self.move_manager.config[key]["category"] = str(entity.id)
await self.config.update()
embed.description = f"{key.capitalize()} category is now set to {entity}."
await ctx.send(embed=embed)
elif entity in ("reset", "clear"):
default = self.config.copy(self.config.defaults["thread_move"][key]["category"])
self.move_manager.config[key]["category"] = default
await self.config.update()
embed.description = f"{key.capitalize()} category is now reset to default."
await ctx.send(embed=embed)
else:
raise commands.BadArgument(f"Category {entity} not found.")
@commands.group(aliases=["conmenu"], invoke_without_command=True)
@checks.has_permissions(PermissionLevel.ADMINISTRATOR)
async def contactmenu(self, ctx: commands.Context):
"""
Base command for contact menu.
Create and customise button, dropdown, and embed content for contact menu.
"""
await ctx.send_help(ctx.command)
@contactmenu.command(name="create")
@checks.has_permissions(PermissionLevel.ADMINISTRATOR)
async def cm_create(self, ctx: commands.Context, *, channel: Optional[discord.TextChannel] = None):
"""
Create a contact message and add contact components to it.
Button and dropdown settings will be retrieved from config. If you want to have custom settings, make sure to set those first with:
- `{prefix}contactmenu config [option] [value]`
Otherwise default settings will be used.
Or you can customise the settings later, then apply the new settings with command:
- `{prefix}contactmenu refresh`
`channel` if specified, may be a channel ID, mention, or name.
If not specified, fallbacks to current channel.
"""
manager = self.contact_manager
if manager.view is not MISSING:
message = manager.view.message
if message:
trail = f" on this [message]({message.jump_url})"
else:
trail = ""
raise commands.BadArgument(
f"There is already active contact menu{trail}. Please disable it first before creating a new one."
)
if channel is None:
channel = ctx.channel
embed_config = self.config.contact["embed"]
embed = discord.Embed(
title=embed_config["title"],
color=self.bot.main_color,
description=embed_config["description"],
timestamp=discord.utils.utcnow(),
)
embed.set_author(name=self.bot.user.name, icon_url=self.bot.user.display_avatar)
footer_text = embed_config.get("footer")
if not footer_text:
footer_text = f"{self.bot.guild.name}: Contact menu"
embed.set_footer(text=footer_text, icon_url=self.bot.guild.icon)
view = ContactView(self)
manager.message = view.message = message = await channel.send(embed=embed, view=view)
self.config.contact["message"] = str(message.id)
self.config.contact["channel"] = str(message.channel.id)
await self.config.update()
if channel != ctx.channel:
await ctx.message.add_reaction("\u2705")
@contactmenu.command(name="attach")
@checks.has_permissions(PermissionLevel.ADMINISTRATOR)
async def cm_attach(self, ctx: commands.Context, *, message: discord.Message):
"""
Attach contact components to the message specified.
Button and dropdown settings will be retrieved from config.
`message` may be a message ID, link, or format of `channelid-messageid`.
"""
manager = self.contact_manager
if manager.view is not MISSING:
message = manager.view.message
if message:
trail = f" on this [message]({message.jump_url})"
else:
trail = ""
raise commands.BadArgument(
f"There is already active contact menu{trail}. Please disable it first before creating a new one."
)
if message.author != self.bot.user:
raise commands.BadArgument("Cannot attach components to a message sent by others.")
view = ContactView(self, message)
await message.edit(view=view)
self.config.contact["message"] = str(message.id)
self.config.contact["channel"] = str(message.channel.id)
await self.config.update()
await ctx.message.add_reaction("\u2705")
@contactmenu.command(name="refresh")
@checks.has_permissions(PermissionLevel.ADMINISTRATOR)
async def cm_refresh(self, ctx: commands.Context):
"""
Refresh components on the contact menu message.
This should be run to update the button and dropdown to apply new settings.
"""
manager = self.contact_manager
if manager.view is MISSING:
raise commands.BadArgument("There is currently no active contact menu.")
manager.view.stop()
manager.view = MISSING
message = manager.message
view = ContactView(self, message)
try:
await message.edit(view=view)
except discord.HTTPException as exc:
logger.error(f"{type(exc).__name__}: {str(exc)}")
raise commands.BadArgument("Unable to refresh contact menu message.")
await ctx.message.add_reaction("\u2705")
@contactmenu.command(name="disable", aliases=["clear"])
@checks.has_permissions(PermissionLevel.ADMINISTRATOR)
async def cm_disable(self, ctx: commands.Context):
"""
Clear contact components attached to the contact menu message.
This will remove the button and dropdown, and stop listening to interactions made on the message.
"""
manager = self.contact_manager
if manager.view is MISSING:
raise commands.BadArgument("There is currently no active contact menu.")
await manager.view.force_stop()
manager.clear()
self.config.contact["message"] = None
self.config.contact["channel"] = None
await self.config.update()
embed = discord.Embed(
color=self.bot.main_color,
description="Contact menu is now cleared.",
)
await ctx.send(embed=embed)
@contactmenu.group(name="config", usage="<subcommand> [argument]", invoke_without_command=True)
@checks.has_permissions(PermissionLevel.ADMINISTRATOR)
async def cm_config(self, ctx: commands.Context):
"""
Contact menu configurations. Retrieve, set or update the values.
__**Customisable options:**__
- Button
- Dropdown
- Embed (title, description, footer)
- Confirmation embed (title, description, footer)
"""
await ctx.send_help(ctx.command)
@cm_config.command(
name="embed",
help=(
"Customise the embed title, description and footer text for contact menu message.\n"
"Please note that this embed will only be posted if the contact menu is initiated from "
"`{prefix}contactmenu create` command.\n\n"
"Leave `argument` empty to set the values.\n"
"Set `argument` to `clear` or `reset` to restore the default value."
),
)
@checks.has_permissions(PermissionLevel.ADMINISTRATOR)
async def cm_config_embed(self, ctx: commands.Context, *, argument: Optional[str] = None):
await self._set_embed_invoker(
ctx,
"contact menu",
["contact", "embed"],
self.config.contact["embed"],
self.config.deepcopy(self.config.defaults["contact"]["embed"]),
argument,
)
@cm_config.command(name="button")
@checks.has_permissions(PermissionLevel.ADMINISTRATOR)
async def cm_config_button(self, ctx: commands.Context, *, argument: Optional[str] = None):
"""
Customise the contact button using buttons and text input.
Leave `argument` empty to set the values.
Set `argument` to `clear` or `reset` to restore the default value.
"""
await self._set_button_invoker(
ctx,
"contact button",
["contact", "button"],
self.config.contact["button"],
self.config.deepcopy(self.config.defaults["contact"]["button"]),
argument,
)
@cm_config.group(name="dropdown", invoke_without_command=True)
@checks.has_permissions(PermissionLevel.ADMINISTRATOR)
async def cm_config_dropdown(self, ctx: commands.Context):
"""
Contact menu dropdown configurations.
"""
await ctx.send_help(ctx.command)
@cm_config_dropdown.command(
name="placeholder",
help=(
"Placeholder text shown on the dropdown menu if nothing is selected.\n"
f"Must not exceed {Limit.select_placeholder} characters."
),
)
@checks.has_permissions(PermissionLevel.ADMINISTRATOR)
async def cm_config_dropdown_placeholder(self, ctx: commands.Context):
embed = discord.Embed(
title="Contact dropdown placeholder",
color=self.bot.main_color,
description=ctx.command.help,
)
current = self.config.contact["select"]["placeholder"]
embed.add_field(name="Current value", value=f"`{current}`")
embed.set_footer(text="Press Set to set/edit the dropdown placeholder")
view = self.get_config_view(
ctx, title=embed.title, keys=["contact", "select", "placeholder"], current=current
)
view.message = message = await ctx.send(embed=embed, view=view)
await view.wait()
await message.edit(view=view)
if not view.value:
return
placeholder = view.outputs["placeholder"]
self.config.contact["select"]["placeholder"] = placeholder
await self.config.update()
embed = discord.Embed(
color=self.bot.main_color,
description=f"Placeholder is now set to:\n{placeholder}",
)
await view.interaction.followup.send(embed=embed)
@cm_config_dropdown.command(
name="add",
help=(
"Add and customise the dropdown for contact menu.\n\n"
"A select option can be linked to a custom category where the thread will be created.\n\n"
"__**Available fields:**__\n"
"- **Emoji** : Emoji for select option. May be a unicode emoji, format of `:name:`, `<:name:id>` "
"or `<a:name:id>` (animated emoji).\n"
f"- **Label** : Label for select option. Must be {Limit.select_label} or fewer in length. "
"This field is required.\n"
f"- **Description** : Short description for the option. Must not exceed {Limit.select_description} characters.\n"
"- **Category** : The discord category channel where the thread will be created if the user choose the option. "
"This field is required.\n"
),
)
@checks.has_permissions(PermissionLevel.ADMINISTRATOR)
async def cm_config_dropdown_add(self, ctx: commands.Context):
embed = discord.Embed(
title="Contact dropdown",
color=self.bot.main_color,
description=ctx.command.help,
)
embed.set_footer(text="Press Add to add new dropdown option")
view = self.get_config_view(ctx, title=embed.title, keys=["contact", "select", "options"])
view.message = message = await ctx.send(embed=embed, view=view)
await view.wait()
await message.edit(view=view)
if not view.value:
return
payload = {}
embed = discord.Embed(
color=self.bot.main_color,
description="Successfully added a dropdown option.\n\n",
)
embed.description += "### New option"
for key in list(view.outputs):
embed.add_field(name=key.title(), value=f"`{view.outputs[key]}`")
payload[key] = view.outputs.pop(key)
self.config.contact["select"]["options"].append(payload)
await self.config.update()
await view.interaction.followup.send(embed=embed)
@cm_config_dropdown.command(name="list")
@checks.has_permissions(PermissionLevel.ADMINISTRATOR)
async def cm_config_dropdown_list(self, ctx: commands.Context):
"""
Show the list of currently set dropdown options for contact menu.
"""
options = self.config.contact["select"]["options"]
if not options:
raise commands.BadArgument("There is no dropdown option set.")
embeds = []
for n, elem in enumerate(options, start=1):
embed = discord.Embed(
title=f"Option {n}",
color=self.bot.main_color,
)
embed.add_field(name="Label", value=f"{elem.get('label')}")
embed.add_field(name="Description", value=f"{elem.get('description')}")
category_id = elem.get("category")
if not category_id:
category = self.bot.main_category
else:
category = self.bot.get_channel(int(category_id))
embed.add_field(name="Category", value=category.mention if category else "Not found")
embeds.append(embed)
session = EmbedPaginatorSession(ctx, *embeds)
await session.run()
@cm_config_dropdown.command(name="clear")
@checks.has_permissions(PermissionLevel.ADMINISTRATOR)
async def cm_config_dropdown_clear(self, ctx: commands.Context):
"""
Clear the dropdown configurations. Please note that this operation cannot be undone.
"""
options = self.config.contact["select"]["options"]
if not options:
raise commands.BadArgument("There is no dropdown option set.")
view = ConfirmView(self.bot, ctx.author)
embed = discord.Embed(
color=self.bot.main_color,
description="Are you sure you want to clear all dropdown configurations?",
)
view.message = await ctx.send(embed=embed, view=view)
await view.wait()
if not view.value:
return
del embed
options.clear()
await self.config.update()
embed = discord.Embed(
color=self.bot.main_color, description="All dropdown configurations are now cleared."
)
await ctx.send(embed=embed)
@cm_config.command(name="confirmembed")
@checks.has_permissions(PermissionLevel.ADMINISTRATOR)
async def cm_config_confirmembed(self, ctx: commands.Context, *, argument: Optional[str] = None):
"""
Customise the embed title, description and footer text for thread creation confirmation embed.
This embed will be sent as ephemeral after a user presses the Contact button.
Leave `argument` empty to set the values.
Set `argument` to `clear` or `reset` to restore the default value.
"""
await self._set_embed_invoker(
ctx,
"thread creation confirmation",
["contact", "confirmation", "embed"],
self.config.contact["confirmation"]["embed"],
self.config.deepcopy(self.config.defaults["contact"]["confirmation"]["embed"]),
argument,
)
@cm_config.command(name="override_dmdisabled", aliases=["ignore_dmdisabled"])
@checks.has_permissions(PermissionLevel.ADMINISTRATOR)
async def cm_config_override_dmdisabled(self, ctx: commands.Context, *, mode: Optional[bool] = None):
"""
Enable or disable the override of DM disabled.
`mode` may be `True` or `False` (case insensitive).
Leave `mode` empty to retrieve the current set value.
__**Note:**__
- This can only override the disable new thread setting.
"""
config = self.config.contact
enabled = config.get("override_dmdisabled", False)
if mode is None:
embed = discord.Embed(
color=self.bot.main_color,
description="DM disabled override is currently " + ("enabled." if enabled else "disabled."),
)
return await ctx.send(embed=embed)
if mode == enabled:
raise commands.BadArgument(
"DM disabled override is already " + ("enabled." if enabled else "disabled.")
)
config["override_dmdisabled"] = mode
await self.config.update()
embed = discord.Embed(
color=self.bot.main_color,
description="DM disabled override is now " + ("enabled." if mode else "disabled."),
)
await ctx.send(embed=embed)
@cm_config.command(name="clear")
@checks.has_permissions(PermissionLevel.ADMINISTRATOR)
async def cm_config_clear(self, ctx: commands.Context):
"""
Clear the contact menu configurations.
This will reset all the contact menu settings (e.g. button, dropdown, embed etc) to defaults.
__**Note:**__
- This operation cannot be undone.
"""
view = ConfirmView(self.bot, ctx.author)
embed = discord.Embed(
color=self.bot.main_color, description="Are you sure you want to clear all contact menu settings?"
)
view.message = await ctx.send(embed=embed, view=view)
await view.wait()
if not view.value:
return
del embed
self.config.remove("contact", restore_default=True)
await self.config.update()
embed = discord.Embed(
color=self.bot.main_color,
description="All contact menu configurations have been reset to defaults.",
)
await ctx.send(embed=embed)
@commands.group(aliases=["fback"], invoke_without_command=True)
@checks.has_permissions(PermissionLevel.SUPPORTER)
async def feedback(self, ctx: commands.Context):
"""
Feedback prompt after the thread is closed.
This feature is disabled by default. To enable, use command:
`{prefix}feedback config enable true`
To see more customisable options, see:
`{prefix}feedback config`
__**Notes:**__
- The button on the feedback prompt message will only available for 24 hours.
- Each user can only have one active session at a time.
"""
await ctx.send_help(ctx.command)
@feedback.command(name="send")
@checks.has_permissions(PermissionLevel.SUPPORTER)
async def fb_send(self, ctx: commands.Context, *, user: Optional[discord.Member] = None):
"""
Manually send the feedback prompt message to user.
`user` if specified, may be a user ID, mention, or name.
Leave `user` parameter empty if this command is run in thread channel to send to the current recipient.
"""
embed = discord.Embed(color=self.bot.main_color)
manager = self.feedback_manager
if user:
if manager.is_active(user):
raise commands.BadArgument(f"There is already active feedback session for {user.mention}.")
await manager.send(user)
embed.description = f"Feedback prompt message has been sent to {user.mention}."
await ctx.send(embed=embed)
return
thread = ctx.thread
if not thread:
raise commands.BadArgument(
"This command can only be run in thread channel is `user` parameter is not specified."
)
for user in thread.recipients:
if user is None:
continue
if not isinstance(user, discord.Member):
entity = self.bot.guild.get_member(user.id)
if not entity:
continue
user = entity
try:
await manager.send(user, thread)
except RuntimeError as exc:
logger.error(f"{type(exc).__name__}: {str(exc)}")
logger.error(f"Skipping sending feedback prompt message to {user}.")
if len(thread.recipients) > 1:
recip = "all recipients"
else:
recip = "recipient"
embed.description = f"Successfully sent to {recip}."
await ctx.reply(embed=embed)
@feedback.command(name="cancel")
@checks.has_permissions(PermissionLevel.SUPPORTER)
async def fb_cancel(self, ctx: commands.Context, *, user: discord.Member):
"""
Manually cancel the feedback session sent to user.
`user` may be a user ID, mention, or name.
"""
feedback = self.feedback_manager.find_session(user)
if not feedback:
raise commands.BadArgument(f"There is no active feedback session for {user.mention}.")
feedback.stop()
embed = discord.Embed(
color=self.bot.main_color, description=f"Feedback session for {user.mention} is now stopped."
)
await ctx.send(embed=embed)
@feedback.command(name="list")
@checks.has_permissions(PermissionLevel.SUPPORTER)
async def fb_list(self, ctx: commands.Context):
"""
Show active feedback sessions sent to users.
"""
manager = self.feedback_manager
if not manager.active:
raise commands.BadArgument("There is currently no active feedback session.")
embeds = []
for feedback in manager.active:
user = feedback.user
embed = discord.Embed(
color=self.bot.main_color,
)
embed.set_author(name=str(user), icon_url=user.display_avatar)
embed.set_footer(text=f"User ID: {user.id}")
embed.add_field(
name="Sent", value=discord.utils.format_dt(datetime.fromtimestamp(feedback.started), "F")
)
embed.add_field(
name="Ends", value=discord.utils.format_dt(datetime.fromtimestamp(feedback.ends), "F")
)
embed.add_field(name="Message ID", value=f"`{feedback.message.id}`")
embed.add_field(name="Channel ID", value=f"`{feedback.message.channel.id}`")
embeds.append(embed)
session = EmbedPaginatorSession(ctx, *embeds)
await session.run()
@feedback.group(name="config", invoke_without_command=True)
@checks.has_permissions(PermissionLevel.ADMINISTRATOR)
async def fb_config(self, ctx: commands.Context):
"""
Feedback feature configurations.
Use the subcommands respectively to change the values.
"""
await ctx.send_help(ctx.command)
@fb_config.command(name="channel")
@checks.has_permissions(PermissionLevel.ADMINISTRATOR)
async def fb_config_channel(
self, ctx: commands.Context, *, channel: Optional[discord.TextChannel] = None
):
"""
Feedback log channel.