22import re
33import textwrap
44from datetime import timedelta
5- from typing import TYPE_CHECKING , Any
5+ from typing import TYPE_CHECKING , Any , overload
66from urllib .parse import quote_plus
77from urllib .parse import unquote as urlunquote
88
1414from disnake .ext import commands
1515
1616from monty .bot import Monty
17+ from monty .events import MessageContext , MontyEvent
1718from monty .log import get_logger
1819from monty .utils import scheduling
1920from monty .utils .helpers import EXPAND_BUTTON_PREFIX , block_url_traversal , decode_github_link
@@ -301,11 +302,26 @@ def _snippet_to_codeblock(
301302 # Returns an empty codeblock if the snippet is empty
302303 return f"{ ret } ```\n ```"
303304
304- async def _parse_snippets (self , content : str ) -> str :
305+ @overload
306+ async def _parse_snippets (self , * , content : str ) -> str : ...
307+ @overload
308+ async def _parse_snippets (self , * , context : MessageContext ) -> str : ...
309+
310+ async def _parse_snippets (self , * , content : str | None = None , context : MessageContext | None = None ) -> str :
305311 """Parse message content and return a string with a code block for each URL found."""
306- all_snippets = []
312+ if context and content :
313+ msg = "Provide either content or context, not both."
314+ raise ValueError (msg )
315+
316+ if context :
317+ content = "\n " .join (context .urls )
318+ elif content :
319+ content = remove_codeblocks (content )
320+ else :
321+ msg = "Either content or context must be provided."
322+ raise ValueError (msg )
307323
308- content = remove_codeblocks ( content )
324+ all_snippets : list [ tuple [ int , str ]] = []
309325
310326 for pattern , handler in self .pattern_handlers :
311327 for match in pattern .finditer (content ):
@@ -335,12 +351,9 @@ async def _parse_snippets(self, content: str) -> str:
335351 # Sorts the list of snippets by their match index and joins them into a single message
336352 return "\n " .join (m [1 ] for m in sorted (all_snippets ))
337353
338- @commands .Cog .listener ()
339- async def on_message (self , message : disnake .Message ) -> None :
354+ @commands .Cog .listener (MontyEvent . monty_message_processed . value )
355+ async def on_message (self , message : disnake .Message , context : MessageContext ) -> None :
340356 """Checks if the message has a snippet link, removes the embed, then sends the snippet contents."""
341- if message .author .bot :
342- return
343-
344357 if not message .guild :
345358 return
346359
@@ -354,7 +367,7 @@ async def on_message(self, message: disnake.Message) -> None:
354367 if not my_perms .send_messages :
355368 return
356369
357- message_to_send = await self ._parse_snippets (message . content )
370+ message_to_send = await self ._parse_snippets (content = context . text )
358371 destination = message .channel
359372
360373 if 0 < len (message_to_send ) <= 2000 and message_to_send .count ("\n " ) <= 27 :
@@ -380,7 +393,7 @@ async def send_expanded_links(self, inter: disnake.MessageInteraction) -> None:
380393
381394 custom_id = inter .data .custom_id [len (EXPAND_BUTTON_PREFIX ) :]
382395 link = decode_github_link (custom_id )
383- snippet = await self ._parse_snippets (link )
396+ snippet = await self ._parse_snippets (content = link )
384397
385398 def disable_button () -> disnake .ui .View :
386399 view = disnake .ui .View .from_message (inter .message )
0 commit comments