Add option to toggle auto-deletion of play command messages in ExtendedAudio cog
Some checks are pending
Run pre-commit / Run pre-commit (push) Waiting to run

This commit introduces a new configuration option, `delete_play_messages`, allowing users to enable or disable the automatic deletion of messages related to play commands. A new command, `deleteplay`, is added to toggle this setting, helping to maintain a cleaner chat environment during music playback. Additionally, the cog is updated to handle message deletion after play commands are invoked.
This commit is contained in:
Valerie 2025-05-28 02:24:07 -04:00
parent c517b95f7c
commit 640a9cf7af

View file

@ -35,6 +35,7 @@ class ExtendedAudio(commands.Cog):
"bot_managed_only": False, # Whether to only allow bot-created channels
"last_status_message": None, # ID of the last status message (for cleanup)
"clean_old_messages": True, # Whether to delete old status messages
"delete_play_messages": False, # Whether to delete play command messages
}
default_global = {
"disable_notify_command": True, # Whether to disable the audioset notify command
@ -172,6 +173,18 @@ class ExtendedAudio(commands.Cog):
state = "will" if enabled else "will not"
await ctx.send(f"Old status messages {state} be deleted when new songs play.")
@extendedaudioset.command(name="deleteplay")
async def toggle_delete_play(self, ctx: commands.Context):
"""Toggle whether play command messages should be automatically deleted.
When enabled, messages for play commands (like queue notifications) will be deleted after being sent.
This helps keep the chat clean when queuing multiple songs.
"""
current = await self.config.guild(ctx.guild).delete_play_messages()
await self.config.guild(ctx.guild).delete_play_messages.set(not current)
state = "enabled" if not current else "disabled"
await ctx.send(f"Auto-deletion of play command messages has been {state}.")
def format_duration(self, milliseconds: int) -> str:
"""Format milliseconds into a readable duration."""
seconds = milliseconds // 1000
@ -425,4 +438,30 @@ class ExtendedAudio(commands.Cog):
await ctx.send("I cannot play music in this channel.")
return False
return True
return True
async def maybe_delete_response(self, ctx: commands.Context, message: discord.Message):
"""Delete a message if delete_play_messages is enabled."""
try:
if await self.config.guild(ctx.guild).delete_play_messages():
await message.delete(delay=5) # Delete after 5 seconds
except (discord.Forbidden, discord.NotFound):
pass
async def cog_after_invoke(self, ctx: commands.Context):
"""Handle message deletion after command invocation."""
if ctx.guild and hasattr(ctx, 'command'):
# Check if it's an audio play command
audio_play_commands = [
"play", "bumpplay", "forceplay", "playnow", "playnext",
"playlist play", "playlist start"
]
if ctx.command.qualified_name.lower() in audio_play_commands:
# If there's a stored response message
if hasattr(ctx, 'message') and ctx.message:
await self.maybe_delete_response(ctx, ctx.message)
# If there's a command response
if hasattr(ctx, 'sent_messages'):
for message in ctx.sent_messages:
await self.maybe_delete_response(ctx, message)