From ae09dec4d93382378ec686ba8fd7d78b824ead40 Mon Sep 17 00:00:00 2001 From: Endkind Date: Tue, 8 Jul 2025 00:36:53 +0200 Subject: [PATCH] feat: return sent Message from send_message and apply guard clause style - send_message now returns the discord.Message instance that was sent - returns InteractionMessage if it was the initial response - returns WebhookMessage if it was a followup message - refactored send_message using guard clauses for improved readability --- ModuBotDiscord/commands/__init__.py | 78 ++++++++++++++++------------- pyproject.toml | 5 +- 2 files changed, 46 insertions(+), 37 deletions(-) diff --git a/ModuBotDiscord/commands/__init__.py b/ModuBotDiscord/commands/__init__.py index b3415cc..4940078 100644 --- a/ModuBotDiscord/commands/__init__.py +++ b/ModuBotDiscord/commands/__init__.py @@ -2,7 +2,7 @@ import logging import warnings from abc import ABC, abstractmethod -from typing import Awaitable, Callable, List, Optional, TypeVar, Union +from typing import Awaitable, Callable, Dict, List, Optional, Sequence, TypeVar, Union import discord from discord import Embed, Interaction @@ -33,7 +33,11 @@ async def send_message( silent: bool = False, delete_after: Optional[float] = None, poll: Union[discord.Poll, _MissingSentinel] = MISSING, -) -> None: +) -> Optional[ + Union[ + discord.interactions.InteractionMessage, discord.webhook.async_.WebhookMessage + ] +]: if msg is not None: warnings.warn( "`msg` is deprecated, use `content` instead", @@ -44,41 +48,43 @@ async def send_message( if content is None: content = msg - if not interaction.is_expired(): - if not interaction.response.is_done(): - await interaction.response.send_message( - content=content, - embed=embed, - embeds=embeds, - file=file, - files=files, - view=view, - tts=tts, - ephemeral=ephemeral, - allowed_mentions=allowed_mentions, - suppress_embeds=suppress_embeds, - silent=silent, - delete_after=delete_after, - poll=poll, - ) - else: - await interaction.followup.send( - content=content, - embed=embed, - embeds=embeds, - file=file, - files=files, - view=view, - tts=tts, - ephemeral=ephemeral, - allowed_mentions=allowed_mentions, - suppress_embeds=suppress_embeds, - silent=silent, - delete_after=delete_after, - poll=poll, - ) - else: + if interaction.is_expired(): logger.warning("Interaction is expired. Skipping send_message().") + return None + + if interaction.response.is_done(): + return await interaction.followup.send( + content=content, + embed=embed, + embeds=embeds, + file=file, + files=files, + view=view, + tts=tts, + ephemeral=ephemeral, + allowed_mentions=allowed_mentions, + suppress_embeds=suppress_embeds, + silent=silent, + poll=poll, + ) + + await interaction.response.send_message( + content=content, + embed=embed, + embeds=embeds, + file=file, + files=files, + view=view, + tts=tts, + ephemeral=ephemeral, + allowed_mentions=allowed_mentions, + suppress_embeds=suppress_embeds, + silent=silent, + delete_after=delete_after, + poll=poll, + ) + + return await interaction.original_response() async def send_error( diff --git a/pyproject.toml b/pyproject.toml index 86779d1..ee24c03 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "ModuBotDiscord" -version = "0.3.2" +version = "0.4.0" description = "Modular Discord bot framework built on top of ModuBotCore" authors = [{ name = "Endkind", email = "endkind.ender@endkind.net" }] readme = "README.md" @@ -16,3 +16,6 @@ include = ["ModuBotDiscord*"] [project.urls] "Source" = "https://github.com/EnderModuBot/discord" + +[tool.isort] +profile = "black"