Skip to content

Commit eeafc93

Browse files
authored
[commands] Add PartialMessageConverter
1 parent ed2650f commit eeafc93

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

discord/ext/commands/converter.py

+35-13
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
'MemberConverter',
3838
'UserConverter',
3939
'MessageConverter',
40+
'PartialMessageConverter',
4041
'TextChannelConverter',
4142
'InviteConverter',
4243
'RoleConverter',
@@ -251,21 +252,18 @@ async def convert(self, ctx, argument):
251252

252253
return result
253254

254-
class MessageConverter(Converter):
255-
"""Converts to a :class:`discord.Message`.
256-
257-
.. versionadded:: 1.1
255+
class PartialMessageConverter(Converter):
256+
"""Converts to a :class:`discord.PartialMessage`.
258257
259-
The lookup strategy is as follows (in order):
258+
.. versionadded:: 1.7
260259
261-
1. Lookup by "{channel ID}-{message ID}" (retrieved by shift-clicking on "Copy ID")
262-
2. Lookup by message ID (the message **must** be in the context channel)
263-
3. Lookup by message URL
260+
The creation strategy is as follows (in order):
264261
265-
.. versionchanged:: 1.5
266-
Raise :exc:`.ChannelNotFound`, :exc:`.MessageNotFound` or :exc:`.ChannelNotReadable` instead of generic :exc:`.BadArgument`
262+
1. By "{channel ID}-{message ID}" (retrieved by shift-clicking on "Copy ID")
263+
2. By message ID (The message is assumed to be in the context channel.)
264+
3. By message URL
267265
"""
268-
async def convert(self, ctx, argument):
266+
def _get_id_matches(self, argument):
269267
id_regex = re.compile(r'(?:(?P<channel_id>[0-9]{15,21})-)?(?P<message_id>[0-9]{15,21})$')
270268
link_regex = re.compile(
271269
r'https?://(?:(ptb|canary|www)\.)?discord(?:app)?\.com/channels/'
@@ -275,12 +273,36 @@ async def convert(self, ctx, argument):
275273
match = id_regex.match(argument) or link_regex.match(argument)
276274
if not match:
277275
raise MessageNotFound(argument)
278-
message_id = int(match.group("message_id"))
279276
channel_id = match.group("channel_id")
277+
return int(match.group("message_id")), int(channel_id) if channel_id else None
278+
279+
async def convert(self, ctx, argument):
280+
message_id, channel_id = self._get_id_matches(argument)
281+
channel = ctx.bot.get_channel(channel_id) if channel_id else ctx.channel
282+
if not channel:
283+
raise ChannelNotFound(channel_id)
284+
return discord.PartialMessage(channel=channel, id=message_id)
285+
286+
class MessageConverter(PartialMessageConverter):
287+
"""Converts to a :class:`discord.Message`.
288+
289+
.. versionadded:: 1.1
290+
291+
The lookup strategy is as follows (in order):
292+
293+
1. Lookup by "{channel ID}-{message ID}" (retrieved by shift-clicking on "Copy ID")
294+
2. Lookup by message ID (the message **must** be in the context channel)
295+
3. Lookup by message URL
296+
297+
.. versionchanged:: 1.5
298+
Raise :exc:`.ChannelNotFound`, :exc:`.MessageNotFound` or :exc:`.ChannelNotReadable` instead of generic :exc:`.BadArgument`
299+
"""
300+
async def convert(self, ctx, argument):
301+
message_id, channel_id = self._get_id_matches(argument)
280302
message = ctx.bot._connection._get_message(message_id)
281303
if message:
282304
return message
283-
channel = ctx.bot.get_channel(int(channel_id)) if channel_id else ctx.channel
305+
channel = ctx.bot.get_channel(channel_id) if channel_id else ctx.channel
284306
if not channel:
285307
raise ChannelNotFound(channel_id)
286308
try:

0 commit comments

Comments
 (0)