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)

View File

@@ -1,34 +0,0 @@
from abc import ABC, abstractmethod
from Handlers.HandlerResponse import HandlerResponse
from discord.ext.commands import Context
from discord import Message
from discord.ui import View
from Music.VulkanBot import VulkanBot
class AbstractCommandResponse(ABC):
def __init__(self, response: HandlerResponse) -> None:
self.__response: HandlerResponse = response
self.__context: Context = response.ctx
self.__message: Message = response.ctx.message
self.__bot: VulkanBot = response.ctx.bot
@property
def response(self) -> HandlerResponse:
return self.__response
@property
def bot(self) -> VulkanBot:
return self.__bot
@property
def message(self) -> Message:
return self.__message
@property
def context(self) -> Context:
return self.__context
@abstractmethod
async def run(self) -> None:
pass

View File

@@ -1,11 +0,0 @@
from UI.Responses.AbstractCogResponse import AbstractCommandResponse
from Handlers.HandlerResponse import HandlerResponse
class EmbedCommandResponse(AbstractCommandResponse):
def __init__(self, response: HandlerResponse) -> None:
super().__init__(response)
async def run(self) -> None:
if self.response.embed:
await self.context.send(embed=self.response.embed, view=self.response.view)

View File

@@ -1,16 +0,0 @@
from Config.Emojis import VEmojis
from UI.Responses.AbstractCogResponse import AbstractCommandResponse
from Handlers.HandlerResponse import HandlerResponse
class EmoteCommandResponse(AbstractCommandResponse):
def __init__(self, response: HandlerResponse) -> None:
super().__init__(response)
self.__emojis = VEmojis()
async def run(self) -> None:
if self.response.success:
await self.message.add_reaction(self.__emojis.SUCCESS)
else:
await self.message.add_reaction(self.__emojis.ERROR)

View File

@@ -7,7 +7,9 @@ from Music.VulkanBot import VulkanBot
emojis = VEmojis()
class EmptyView(View):
class BasicView(View):
"""View that receives buttons to hold, in timeout disable buttons"""
def __init__(self, bot: VulkanBot, buttons: List[Button], timeout: float = 6000):
super().__init__(timeout=timeout)
self.__bot = bot