From da5db1e73abbf377f378a268f89a58c6cba5a983 Mon Sep 17 00:00:00 2001 From: ErrorNoInternet Date: Mon, 6 Jan 2025 14:31:08 -0500 Subject: [PATCH] feat(utils): use improved disnake_paginator interaction wrapper Adds edit support (via utils.reply) --- commands/voice.py | 6 ++---- core.py | 4 +--- utils.py | 31 ++++++++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/commands/voice.py b/commands/voice.py index 5682a2d..463de75 100644 --- a/commands/voice.py +++ b/commands/voice.py @@ -219,7 +219,7 @@ async def queue_or_play(message, edited=False): ], ) ), - ).start(disnake_paginator.wrappers.MessageInteractionWrapper(message)) + ).start(utils.MessageInteractionWrapper(message)) else: await utils.reply( message, @@ -255,9 +255,7 @@ async def playing(message): ) for embed in paginator.embeds: embed.url = source.original_url - await paginator.start( - disnake_paginator.wrappers.MessageInteractionWrapper(message) - ) + await paginator.start(utils.MessageInteractionWrapper(message)) else: await utils.reply( message, diff --git a/core.py b/core.py index 83442c6..a66082b 100644 --- a/core.py +++ b/core.py @@ -93,9 +93,7 @@ async def on_message(message, edited=False): invalid_user_function=utils.invalid_user_handler, color=constants.EMBED_COLOR, segments=disnake_paginator.split(output), - ).start( - disnake_paginator.wrappers.MessageInteractionWrapper(message) - ) + ).start(utils.MessageInteractionWrapper(message)) elif len(output.strip()) == 0: await utils.add_check_reaction(message) else: diff --git a/utils.py b/utils.py index b451748..3e9ca82 100644 --- a/utils.py +++ b/utils.py @@ -4,6 +4,34 @@ import constants from state import message_responses +class ChannelResponseWrapper: + def __init__(self, message): + self.message = message + self.sent_message = None + + async def send_message(self, **kwargs): + if "ephemeral" in kwargs: + del kwargs["ephemeral"] + self.sent_message = await reply(self.message, **kwargs) + + async def edit_message(self, content=None, embed=None, view=None): + if self.sent_message: + content = content or self.sent_message.content + if not embed and len(self.sent_message.embeds) > 0: + embed = self.sent_message.embeds[0] + await self.sent_message.edit(content=content, embed=embed, view=view) + + +class MessageInteractionWrapper: + def __init__(self, message): + self.message = message + self.author = message.author + self.response = ChannelResponseWrapper(message) + + async def edit_original_message(self, content=None, embed=None, view=None): + await self.response.edit_message(content=content, embed=embed, view=view) + + def format_duration(duration: int, natural: bool = False): def format_plural(noun, count): return noun if count == 1 else noun + "s" @@ -49,6 +77,7 @@ async def reply(message, *args, **kwargs): *args, **kwargs, allowed_mentions=disnake.AllowedMentions.none() ) message_responses[message.id] = response + return message_responses[message.id] async def channel_send(message, *args, **kwargs): @@ -59,7 +88,7 @@ async def channel_send(message, *args, **kwargs): async def invalid_user_handler(interaction): await interaction.response.send_message( - "You are not the intended receiver of this message!", ephemeral=True + "you are not the intended receiver of this message!", ephemeral=True )