37
37
'MemberConverter' ,
38
38
'UserConverter' ,
39
39
'MessageConverter' ,
40
+ 'PartialMessageConverter' ,
40
41
'TextChannelConverter' ,
41
42
'InviteConverter' ,
42
43
'RoleConverter' ,
@@ -251,21 +252,18 @@ async def convert(self, ctx, argument):
251
252
252
253
return result
253
254
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`.
258
257
259
- The lookup strategy is as follows (in order):
258
+ .. versionadded:: 1.7
260
259
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):
264
261
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
267
265
"""
268
- async def convert (self , ctx , argument ):
266
+ def _get_id_matches (self , argument ):
269
267
id_regex = re .compile (r'(?:(?P<channel_id>[0-9]{15,21})-)?(?P<message_id>[0-9]{15,21})$' )
270
268
link_regex = re .compile (
271
269
r'https?://(?:(ptb|canary|www)\.)?discord(?:app)?\.com/channels/'
@@ -275,12 +273,36 @@ async def convert(self, ctx, argument):
275
273
match = id_regex .match (argument ) or link_regex .match (argument )
276
274
if not match :
277
275
raise MessageNotFound (argument )
278
- message_id = int (match .group ("message_id" ))
279
276
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 )
280
302
message = ctx .bot ._connection ._get_message (message_id )
281
303
if message :
282
304
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
284
306
if not channel :
285
307
raise ChannelNotFound (channel_id )
286
308
try :
0 commit comments