Upgrading messages sended mananger and refactoring Buttons logic

This commit is contained in:
Rafael Vargas
2022-08-04 18:25:06 -04:00
parent 2627f95a6d
commit 0c20f68c2b
21 changed files with 384 additions and 173 deletions

View File

@@ -1,13 +1,22 @@
from typing import Awaitable
from discord import ButtonStyle, Interaction
from Config.Emojis import VEmojis
from discord import ButtonStyle, Interaction, Message, TextChannel
from discord.ui import Button
from Handlers.HandlerResponse import HandlerResponse
from Messages.MessagesCategory import MessagesCategory
from Messages.MessagesManager import MessagesManager
from Music.VulkanBot import VulkanBot
class EmptyButton(Button):
def __init__(self, bot: VulkanBot, cb: Awaitable, emoji, label=None, *args, **kwargs):
class CallbackButton(Button):
"""When clicked execute an callback passing the args and kwargs"""
def __init__(self, bot: VulkanBot, cb: Awaitable, emoji: VEmojis, textChannel: TextChannel, guildID: int, category: MessagesCategory, label=None, *args, **kwargs):
super().__init__(label=label, style=ButtonStyle.secondary, emoji=emoji)
self.__channel = textChannel
self.__guildID = guildID
self.__category = category
self.__messagesManager = MessagesManager()
self.__bot = bot
self.__args = args
self.__kwargs = kwargs
@@ -21,6 +30,9 @@ class EmptyButton(Button):
response: HandlerResponse = await self.__callback(*self.__args, **self.__kwargs)
if response and response.view is not None:
await interaction.followup.send(embed=response.embed, view=response.view)
elif response:
await interaction.followup.send(embed=response.embed)
message: Message = await self.__channel.send(embed=response.embed, view=response.view)
else:
message: Message = await self.__channel.send(embed=response.embed)
# Clear the last sended message in this category and add the new one
await self.__messagesManager.addMessageAndClearPrevious(self.__guildID, self.__category, message)

View File

@@ -0,0 +1,40 @@
from Config.Emojis import VEmojis
from discord import ButtonStyle, Interaction, Message, TextChannel
from discord.ui import Button
from Handlers.HandlerResponse import HandlerResponse
from Messages.MessagesCategory import MessagesCategory
from Music.VulkanBot import VulkanBot
from Handlers.AbstractHandler import AbstractHandler
from Messages.MessagesManager import MessagesManager
class HandlerButton(Button):
"""Button that will create and execute a Handler Object when clicked"""
def __init__(self, bot: VulkanBot, handler: type[AbstractHandler], emoji: VEmojis, textChannel: TextChannel, guildID: int, category: MessagesCategory, label=None, *args, **kwargs):
super().__init__(label=label, style=ButtonStyle.secondary, emoji=emoji)
self.__messagesManager = MessagesManager()
self.__category = category
self.__guildID = guildID
self.__channel = textChannel
self.__bot = bot
self.__args = args
self.__kwargs = kwargs
self.__handlerClass = handler
async def callback(self, interaction: Interaction) -> None:
"""Callback to when Button is clicked"""
# Return to Discord that this command is being processed
await interaction.response.defer()
# Create the handler object
handler = self.__handlerClass(interaction, self.__bot)
response: HandlerResponse = await handler.run(*self.__args, **self.__kwargs)
if response and response.view is not None:
message: Message = await self.__channel.send(embed=response.embed, view=response.view)
else:
message: Message = await self.__channel.send(embed=response.embed)
# Clear the last category sended message and add the new one
await self.__messagesManager.addMessageAndClearPrevious(self.__guildID, self.__category, message)