-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathedit_message.py
More file actions
138 lines (117 loc) · 4.64 KB
/
edit_message.py
File metadata and controls
138 lines (117 loc) · 4.64 KB
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
"""Contains cog classes for any edit_message interactions."""
import re
from typing import TYPE_CHECKING, override
import discord
from exceptions import DiscordMemberNotInMainGuildError
from exceptions.base import BaseDoesNotExistError
from utils import CommandChecks, TeXBotBaseCog
if TYPE_CHECKING:
from collections.abc import Sequence
from collections.abc import Set as AbstractSet
from utils import TeXBotApplicationContext, TeXBotAutocompleteContext
__all__: "Sequence[str]" = ("EditMessageCommandCog",)
class EditMessageCommandCog(TeXBotBaseCog):
"""Cog class that defines the "/edit-message" command and its call-back method."""
@staticmethod
@override
async def autocomplete_get_text_channels(
ctx: "TeXBotAutocompleteContext",
) -> "AbstractSet[discord.OptionChoice] | AbstractSet[str]":
"""
Autocomplete callable that generates the set of available selectable channels.
The list of available selectable channels is unique to each member and is used in any
of the "edit-message" slash-command options that have a channel input-type.
"""
if not ctx.interaction.user:
return set()
try:
if not await ctx.bot.check_user_has_committee_role(ctx.interaction.user):
return set()
except (BaseDoesNotExistError, DiscordMemberNotInMainGuildError):
return set()
return await TeXBotBaseCog.autocomplete_get_text_channels(ctx)
@discord.slash_command(
name="edit-message",
description="Edits a message sent by TeX-Bot to the value supplied.",
)
@discord.option(
name="channel",
description="The channel that the message, you wish to edit, is in.",
input_type=str,
autocomplete=discord.utils.basic_autocomplete(autocomplete_get_text_channels),
required=True,
parameter_name="str_channel_id",
)
@discord.option(
name="message-id",
input_type=str,
description="The ID of the message you wish to edit.",
required=True,
max_length=20,
min_length=17,
parameter_name="str_message_id",
)
@discord.option(
name="text",
input_type=str,
description="The new text you want the message to say.",
required=True,
max_length=2000,
min_length=1,
parameter_name="new_message_content",
)
@CommandChecks.check_interaction_user_has_committee_role
@CommandChecks.check_interaction_user_in_main_guild
async def edit_message(
self,
ctx: "TeXBotApplicationContext",
str_channel_id: str,
str_message_id: str,
new_message_content: str,
) -> None:
"""
Definition & callback response of the "edit_message" command.
The "write_roles" command edits a message sent by TeX-Bot to the value supplied.
"""
# NOTE: Shortcut accessors are placed at the top of the function so that the exceptions they raise are displayed before any further errors may be sent
main_guild: discord.Guild = self.bot.main_guild
if not re.fullmatch(r"\A\d{17,20}\Z", str_channel_id):
await self.command_send_error(
ctx, message=f"{str_channel_id!r} is not a valid channel ID."
)
return
channel_id: int = int(str_channel_id)
if not re.fullmatch(r"\A\d{17,20}\Z", str_message_id):
await self.command_send_error(
ctx, message=f"{str_message_id!r} is not a valid message ID."
)
return
message_id: int = int(str_message_id)
channel: discord.TextChannel | None = discord.utils.get(
main_guild.text_channels, id=channel_id
)
if not channel:
await self.command_send_error(
ctx, message=f"Text channel with ID '{channel_id}' does not exist."
)
return
try:
message: discord.Message = await channel.fetch_message(message_id)
except discord.NotFound:
await self.command_send_error(
ctx, message=f"Message with ID '{message_id}' does not exist."
)
return
try:
await message.edit(content=new_message_content)
except discord.Forbidden:
await self.command_send_error(
ctx,
message=(
f"Message with ID {str(message_id)!r} cannot be edited "
"because it belongs to another user."
),
)
return
else:
await ctx.respond("Message edited successfully.", ephemeral=True)