mirror of
https://github.com/RafaelSolVargas/Vulkan.git
synced 2025-10-29 16:57:23 +00:00
Upgrading views manager in messages timeout
This commit is contained in:
parent
2d27a2f080
commit
2794f1a6d0
@ -11,7 +11,7 @@ from Music.Song import Song
|
|||||||
from Music.Playlist import Playlist
|
from Music.Playlist import Playlist
|
||||||
from typing import List, Union
|
from typing import List, Union
|
||||||
from discord import Button, Interaction
|
from discord import Button, Interaction
|
||||||
from UI.Buttons.EmptyButton import CallbackButton
|
from UI.Buttons.CallbackButton import CallbackButton
|
||||||
from UI.Buttons.PlaylistDropdown import PlaylistDropdown
|
from UI.Buttons.PlaylistDropdown import PlaylistDropdown
|
||||||
from Config.Emojis import VEmojis
|
from Config.Emojis import VEmojis
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
from typing import Dict, List
|
from typing import Dict, List
|
||||||
from discord import Message
|
from discord import Message
|
||||||
from Config.Singleton import Singleton
|
from Config.Singleton import Singleton
|
||||||
|
from UI.Views.AbstractView import AbstractView
|
||||||
from Messages.MessagesCategory import MessagesCategory
|
from Messages.MessagesCategory import MessagesCategory
|
||||||
|
|
||||||
|
|
||||||
@ -9,8 +10,10 @@ class MessagesManager(Singleton):
|
|||||||
if not super().created:
|
if not super().created:
|
||||||
# For each guild, and for each category, there will be a list of messages
|
# For each guild, and for each category, there will be a list of messages
|
||||||
self.__guildsMessages: Dict[int, Dict[MessagesCategory, List[Message]]] = {}
|
self.__guildsMessages: Dict[int, Dict[MessagesCategory, List[Message]]] = {}
|
||||||
|
# Will, for each message, store the AbstractView that controls it
|
||||||
|
self.__messagesViews: Dict[Message, AbstractView] = {}
|
||||||
|
|
||||||
def addMessage(self, guildID: int, category: MessagesCategory, message: Message) -> None:
|
def addMessage(self, guildID: int, category: MessagesCategory, message: Message, view: AbstractView = None) -> None:
|
||||||
if message is None:
|
if message is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -22,9 +25,11 @@ class MessagesManager(Singleton):
|
|||||||
self.__guildsMessages[guildID][category] = []
|
self.__guildsMessages[guildID][category] = []
|
||||||
|
|
||||||
sendedMessages = self.__guildsMessages[guildID][category]
|
sendedMessages = self.__guildsMessages[guildID][category]
|
||||||
|
if view is not None and isinstance(view, AbstractView):
|
||||||
|
self.__messagesViews[message] = view
|
||||||
sendedMessages.append(message)
|
sendedMessages.append(message)
|
||||||
|
|
||||||
async def addMessageAndClearPrevious(self, guildID: int, category: MessagesCategory, message: Message) -> None:
|
async def addMessageAndClearPrevious(self, guildID: int, category: MessagesCategory, message: Message, view: AbstractView = None) -> None:
|
||||||
if message is None:
|
if message is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -44,6 +49,10 @@ class MessagesManager(Singleton):
|
|||||||
# Create a new list with only the new message
|
# Create a new list with only the new message
|
||||||
self.__guildsMessages[guildID][category] = [message]
|
self.__guildsMessages[guildID][category] = [message]
|
||||||
|
|
||||||
|
# Store the view of this message
|
||||||
|
if view is not None and isinstance(view, AbstractView):
|
||||||
|
self.__messagesViews[message] = view
|
||||||
|
|
||||||
async def clearMessagesOfCategory(self, guildID: int, category: MessagesCategory) -> None:
|
async def clearMessagesOfCategory(self, guildID: int, category: MessagesCategory) -> None:
|
||||||
sendedMessages = self.__guildsMessages[guildID][category]
|
sendedMessages = self.__guildsMessages[guildID][category]
|
||||||
|
|
||||||
@ -59,6 +68,12 @@ class MessagesManager(Singleton):
|
|||||||
|
|
||||||
async def __deleteMessage(self, message: Message) -> None:
|
async def __deleteMessage(self, message: Message) -> None:
|
||||||
try:
|
try:
|
||||||
|
# If there is a view for this message delete the key
|
||||||
|
if message in self.__messagesViews.keys():
|
||||||
|
messageView = self.__messagesViews.pop(message)
|
||||||
|
messageView.stopView()
|
||||||
|
del messageView
|
||||||
|
|
||||||
await message.delete()
|
await message.delete()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f'[ERROR DELETING MESSAGE] -> {e}')
|
print(f'[ERROR DELETING MESSAGE] -> {e}')
|
||||||
|
|||||||
@ -19,6 +19,6 @@ class EmbedCommandResponse(AbstractCommandResponse):
|
|||||||
if message:
|
if message:
|
||||||
# Only delete the previous message if this is not error and not forbidden by method caller
|
# Only delete the previous message if this is not error and not forbidden by method caller
|
||||||
if deleteLast and self.response.success:
|
if deleteLast and self.response.success:
|
||||||
await self.manager.addMessageAndClearPrevious(self.context.guild.id, self.category, message)
|
await self.manager.addMessageAndClearPrevious(self.context.guild.id, self.category, message, self.response.view)
|
||||||
else:
|
else:
|
||||||
self.manager.addMessage(self.context.guild.id, self.category, message)
|
self.manager.addMessage(self.context.guild.id, self.category, message)
|
||||||
|
|||||||
@ -43,7 +43,7 @@ class ProcessCommandsExecutor:
|
|||||||
view = self.__getPlayerView(channel)
|
view = self.__getPlayerView(channel)
|
||||||
# Send Message and add to the MessagesManager
|
# Send Message and add to the MessagesManager
|
||||||
message = await channel.send(embed=embed, view=view)
|
message = await channel.send(embed=embed, view=view)
|
||||||
await self.__messagesManager.addMessageAndClearPrevious(self.__guildID, MessagesCategory.NOW_PLAYING, message)
|
await self.__messagesManager.addMessageAndClearPrevious(self.__guildID, MessagesCategory.NOW_PLAYING, message, view)
|
||||||
|
|
||||||
# Set in the view the message witch contains the view
|
# Set in the view the message witch contains the view
|
||||||
view.set_message(message=message)
|
view.set_message(message=message)
|
||||||
|
|||||||
@ -39,7 +39,7 @@ class CallbackButton(Button):
|
|||||||
|
|
||||||
# Clear the last sended message in this category and add the new one
|
# Clear the last sended message in this category and add the new one
|
||||||
if message:
|
if message:
|
||||||
await self.__messagesManager.addMessageAndClearPrevious(self.__guildID, self.__category, message)
|
await self.__messagesManager.addMessageAndClearPrevious(self.__guildID, self.__category, message, response.view)
|
||||||
|
|
||||||
def set_view(self, view: View):
|
def set_view(self, view: View):
|
||||||
self.__view = view
|
self.__view = view
|
||||||
@ -41,7 +41,7 @@ class HandlerButton(Button):
|
|||||||
|
|
||||||
# Clear the last category sended message and add the new one
|
# Clear the last category sended message and add the new one
|
||||||
if message:
|
if message:
|
||||||
await self.__messagesManager.addMessageAndClearPrevious(self.__guildID, self.__category, message)
|
await self.__messagesManager.addMessageAndClearPrevious(self.__guildID, self.__category, message, response.view)
|
||||||
|
|
||||||
def set_view(self, view: View):
|
def set_view(self, view: View):
|
||||||
self.__view = view
|
self.__view = view
|
||||||
|
|||||||
@ -19,15 +19,17 @@ class PlaylistDropdown(Select, AbstractItem):
|
|||||||
songs = list(playlist.getSongs())
|
songs = list(playlist.getSongs())
|
||||||
|
|
||||||
values = [str(x) for x in range(1, len(songs) + 1)]
|
values = [str(x) for x in range(1, len(songs) + 1)]
|
||||||
# Get the title of each of the 20 first songs, library doesn't accept more
|
# Get the title of each of the 20 first songs, the pycord library doesn't accept more
|
||||||
songsNames = [song.title[:80] for song in songs[:20]]
|
songsNames: List[str] = []
|
||||||
|
for x in range(20):
|
||||||
|
songsNames.append(f'{x + 1} - {songs[x].title[:80]}')
|
||||||
|
|
||||||
selectOptions: List[SelectOption] = []
|
selectOptions: List[SelectOption] = []
|
||||||
|
|
||||||
for x in range(len(songsNames)):
|
for x in range(len(songsNames)):
|
||||||
selectOptions.append(SelectOption(label=songsNames[x], value=values[x]))
|
selectOptions.append(SelectOption(label=songsNames[x], value=values[x]))
|
||||||
|
|
||||||
super().__init__(placeholder="Select one music to play now, may be overdue",
|
super().__init__(placeholder="Select one music to play now, may be outdated",
|
||||||
min_values=1, max_values=1, options=selectOptions)
|
min_values=1, max_values=1, options=selectOptions)
|
||||||
|
|
||||||
self.__playlist = playlist
|
self.__playlist = playlist
|
||||||
@ -55,7 +57,7 @@ class PlaylistDropdown(Select, AbstractItem):
|
|||||||
|
|
||||||
# Clear the last sended message in this category and add the new one
|
# Clear the last sended message in this category and add the new one
|
||||||
if message:
|
if message:
|
||||||
await self.__messagesManager.addMessageAndClearPrevious(self.__guildID, self.__category, message)
|
await self.__messagesManager.addMessageAndClearPrevious(self.__guildID, self.__category, message, response.view)
|
||||||
|
|
||||||
# Extreme ugly way to wait for the player process to actually retrieve the next song
|
# Extreme ugly way to wait for the player process to actually retrieve the next song
|
||||||
await asyncio.sleep(2)
|
await asyncio.sleep(2)
|
||||||
|
|||||||
@ -8,3 +8,7 @@ class AbstractView(ABC):
|
|||||||
|
|
||||||
def set_message(self, message) -> None:
|
def set_message(self, message) -> None:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def stopView(self) -> None:
|
||||||
|
pass
|
||||||
|
|||||||
@ -16,18 +16,25 @@ class BasicView(View, AbstractView):
|
|||||||
super().__init__(timeout=timeout)
|
super().__init__(timeout=timeout)
|
||||||
self.__bot = bot
|
self.__bot = bot
|
||||||
self.__message: Message = None
|
self.__message: Message = None
|
||||||
|
self.__working = True
|
||||||
|
|
||||||
for button in buttons:
|
for button in buttons:
|
||||||
# Set the buttons to have a instance of the view that contains them
|
# Set the buttons to have a instance of the view that contains them
|
||||||
button.set_view(self)
|
button.set_view(self)
|
||||||
self.add_item(button)
|
self.add_item(button)
|
||||||
|
|
||||||
|
def stopView(self):
|
||||||
|
self.__working = False
|
||||||
|
|
||||||
async def on_timeout(self) -> None:
|
async def on_timeout(self) -> None:
|
||||||
# Disable all itens and, if has the message, edit it
|
# Disable all itens and, if has the message, edit it
|
||||||
try:
|
try:
|
||||||
|
if not self.__working:
|
||||||
|
return
|
||||||
|
|
||||||
self.disable_all_items()
|
self.disable_all_items()
|
||||||
if self.__message is not None and isinstance(self.__message, Message):
|
if self.__message is not None and isinstance(self.__message, Message):
|
||||||
await self.__message.edit(view=self)
|
await self.__message.edit(f"{emojis.MUSIC} - The buttons in this message have been disabled due timeout", view=self)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f'[ERROR EDITING MESSAGE] -> {e}')
|
print(f'[ERROR EDITING MESSAGE] -> {e}')
|
||||||
|
|
||||||
@ -35,5 +42,11 @@ class BasicView(View, AbstractView):
|
|||||||
self.__message = message
|
self.__message = message
|
||||||
|
|
||||||
async def update(self):
|
async def update(self):
|
||||||
if self.__message is not None:
|
try:
|
||||||
await self.__message.edit(view=self)
|
if not self.__working:
|
||||||
|
return
|
||||||
|
|
||||||
|
if self.__message is not None:
|
||||||
|
await self.__message.edit(view=self)
|
||||||
|
except Exception as e:
|
||||||
|
print(f'[ERROR UPDATING MESSAGE] -> {e}')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user