Improve controller message handling and dispatch logic

Refactored Player.invoke_controller to combine controller setting and update checks, and added error handling for message deletion. Updated dispatch_message to use Optional types and MISSING for default values, improving clarity and handling of delete_after logic.
This commit is contained in:
Choco
2025-10-09 11:12:17 +08:00
parent df5373353c
commit 87f22b5589
2 changed files with 13 additions and 13 deletions

View File

@@ -448,10 +448,7 @@ class Player(VoiceProtocol):
async def invoke_controller(self):
"""Sends or updates the music controller message in the designated channel."""
if not self.settings.get('controller', True):
return
if self._updating or not self.channel:
if not self.settings.get('controller', True) or self._updating or not self.channel:
return
self._updating = True
@@ -460,8 +457,7 @@ class Player(VoiceProtocol):
embed, view = self.build_embed(self.current), InteractiveController(self)
if not self.controller:
if request_channel_data := self.settings.get("music_request_channel"):
channel = self.bot.get_channel(request_channel_data.get("text_channel_id"))
if channel:
if channel := self.bot.get_channel(request_channel_data.get("text_channel_id")):
try:
self.controller = await channel.fetch_message(request_channel_data.get("controller_msg_id"))
await self.controller.edit(embed=embed, view=view)
@@ -473,7 +469,11 @@ class Player(VoiceProtocol):
self.controller = await dispatch_message(self.context, content=embed, view=view, delete_after=None, requires_fetch=True)
elif not await self.is_position_fresh():
await self.controller.delete()
try:
await self.controller.delete()
except errors.NotFound:
self.controller = None
self.controller = await dispatch_message(self.context, content=embed, view=view, delete_after=None, requires_fetch=True)
else:

View File

@@ -27,10 +27,10 @@ import socket
import discord
from itertools import zip_longest
from typing import Dict, Optional
from typing import Dict, Optional, Union
from timeit import default_timer as timer
from discord.ext import commands
from typing import Union
from discord.utils import MISSING
from .mongodb import MongoDBHandler
from .language import LangHandler
@@ -301,9 +301,9 @@ async def dispatch_message(
ctx: Union[commands.Context, discord.Interaction, TempCtx],
content: Union[str, discord.Embed] = None,
*params,
view: discord.ui.View = None,
file: discord.File = None,
delete_after: float = None,
view: Optional[discord.ui.View] = None,
file: Optional[discord.File] = None,
delete_after: Optional[float] = MISSING,
ephemeral: bool = False,
requires_fetch: bool = False
) -> Optional[discord.Message]:
@@ -354,7 +354,7 @@ async def dispatch_message(
send_kwargs["view"] = view
if "delete_after" in send_func.__code__.co_varnames:
if not delete_after and settings and ctx.channel.id == settings.get("music_request_channel", {}).get("text_channel_id"):
if delete_after is MISSING and settings and ctx.channel.id == settings.get("music_request_channel", {}).get("text_channel_id"):
delete_after = 10
send_kwargs["delete_after"] = delete_after