From fededdbb8c5ad82e5f4d1b77b09f3103b68a5709 Mon Sep 17 00:00:00 2001 From: Rafael Vargas Date: Wed, 27 Jul 2022 01:36:55 -0300 Subject: [PATCH] Changing to pycord --- DiscordCogs/ControlCog.py | 37 +++++------------ DiscordCogs/MusicCog.py | 2 + DiscordCogs/RandomCog.py | 7 +++- Handlers/AbstractHandler.py | 5 ++- Handlers/ClearHandler.py | 4 +- Handlers/HistoryHandler.py | 4 +- Handlers/LoopHandler.py | 4 +- Handlers/MoveHandler.py | 4 +- Handlers/NowPlayingHandler.py | 4 +- Handlers/PauseHandler.py | 4 +- Handlers/PlayHandler.py | 4 +- Handlers/PrevHandler.py | 4 +- Handlers/QueueHandler.py | 4 +- Handlers/RemoveHandler.py | 4 +- Handlers/ResetHandler.py | 4 +- Handlers/ResumeHandler.py | 4 +- Handlers/ShuffleHandler.py | 4 +- Handlers/SkipHandler.py | 4 +- Handlers/StopHandler.py | 4 +- Music/MusicBot.py | 73 ++++++++++++++++++++++++++++++++++ Music/VulkanInitializer.py | 43 ++++++++++++++++++++ Parallelism/PlayerProcess.py | 37 +++++------------ Utils/Cleaner.py | 7 ++-- Views/AbstractView.py | 7 ++-- Views/Embeds.py | 2 +- main.py | 55 +++++++++++++++---------- requirements.txt | Bin 288 -> 244 bytes 27 files changed, 217 insertions(+), 118 deletions(-) create mode 100644 Music/MusicBot.py create mode 100644 Music/VulkanInitializer.py diff --git a/DiscordCogs/ControlCog.py b/DiscordCogs/ControlCog.py index e0e62da..ff6ef56 100644 --- a/DiscordCogs/ControlCog.py +++ b/DiscordCogs/ControlCog.py @@ -1,22 +1,24 @@ -from discord import Client, Game, Status, Embed -from discord.ext.commands.errors import CommandNotFound, MissingRequiredArgument +from discord import Embed from discord.ext import commands +from discord.ext.commands import Cog from Config.Configs import Configs from Config.Helper import Helper -from Config.Messages import Messages from Config.Colors import Colors +from Music.MusicBot import VulkanBot from Views.Embeds import Embeds helper = Helper() -class ControlCog(commands.Cog): +class ControlCog(Cog): """Class to handle discord events""" - def __init__(self, bot: Client): + def __init__(self, bot: VulkanBot): + print('Eae3') self.__bot = bot + print(self.__bot) + print(bot.extensions) self.__config = Configs() - self.__messages = Messages() self.__colors = Colors() self.__embeds = Embeds() self.__commands = { @@ -28,27 +30,6 @@ class ControlCog(commands.Cog): } - @commands.Cog.listener() - async def on_ready(self): - print(self.__messages.STARTUP_MESSAGE) - await self.__bot.change_presence(status=Status.online, activity=Game(name=f"Vulkan | {self.__config.BOT_PREFIX}help")) - print(self.__messages.STARTUP_COMPLETE_MESSAGE) - - @commands.Cog.listener() - async def on_command_error(self, ctx, error): - if isinstance(error, MissingRequiredArgument): - embed = self.__embeds.MISSING_ARGUMENTS() - await ctx.send(embed=embed) - - elif isinstance(error, CommandNotFound): - embed = self.__embeds.COMMAND_NOT_FOUND() - await ctx.send(embed=embed) - - else: - print(f'DEVELOPER NOTE -> Command Error: {error}') - embed = self.__embeds.UNKNOWN_ERROR() - await ctx.send(embed=embed) - @commands.command(name="help", help=helper.HELP_HELP, description=helper.HELP_HELP_LONG, aliases=['h', 'ajuda']) async def help_msg(self, ctx, command_help=''): if command_help != '': @@ -97,7 +78,7 @@ class ControlCog(commands.Cog): colour=self.__colors.BLUE ) - embedhelp.set_thumbnail(url=self.__bot.user.avatar_url) + embedhelp.set_thumbnail(url=self.__bot.user.avatar) await ctx.send(embed=embedhelp) @commands.command(name='invite', help=helper.HELP_INVITE, description=helper.HELP_INVITE_LONG, aliases=['convite', 'inv', 'convidar']) diff --git a/DiscordCogs/MusicCog.py b/DiscordCogs/MusicCog.py index 4f3b137..8c50f33 100644 --- a/DiscordCogs/MusicCog.py +++ b/DiscordCogs/MusicCog.py @@ -232,4 +232,6 @@ class MusicCog(commands.Cog): def setup(bot): + print('Loading Music') bot.add_cog(MusicCog(bot)) + print('Voltou') diff --git a/DiscordCogs/RandomCog.py b/DiscordCogs/RandomCog.py index b2ec3fc..02c00f3 100644 --- a/DiscordCogs/RandomCog.py +++ b/DiscordCogs/RandomCog.py @@ -1,5 +1,5 @@ from random import randint, random -from discord import Client +from Music.MusicBot import VulkanBot from discord.ext.commands import Context, command, Cog from Config.Helper import Helper from Views.Embeds import Embeds @@ -10,7 +10,10 @@ helper = Helper() class RandomCog(Cog): """Class to listen to commands of type Random""" - def __init__(self, bot: Client): + def __init__(self, bot: VulkanBot): + print('Eae2') + print(bot) + print(bot.extensions) self.__embeds = Embeds() @command(name='random', help=helper.HELP_RANDOM, description=helper.HELP_RANDOM_LONG, aliases=['rand']) diff --git a/Handlers/AbstractHandler.py b/Handlers/AbstractHandler.py index 93e7e58..19f81a6 100644 --- a/Handlers/AbstractHandler.py +++ b/Handlers/AbstractHandler.py @@ -3,6 +3,7 @@ from typing import List from discord.ext.commands import Context from discord import Client, Guild, ClientUser, Member from Config.Messages import Messages +from Music.MusicBot import VulkanBot from Handlers.HandlerResponse import HandlerResponse from Config.Configs import Configs from Config.Helper import Helper @@ -10,8 +11,8 @@ from Views.Embeds import Embeds class AbstractHandler(ABC): - def __init__(self, ctx: Context, bot: Client) -> None: - self.__bot: Client = bot + def __init__(self, ctx: Context, bot: VulkanBot) -> None: + self.__bot: VulkanBot = bot self.__guild: Guild = ctx.guild self.__ctx: Context = ctx self.__bot_user: ClientUser = self.__bot.user diff --git a/Handlers/ClearHandler.py b/Handlers/ClearHandler.py index df2d822..eb53a8a 100644 --- a/Handlers/ClearHandler.py +++ b/Handlers/ClearHandler.py @@ -1,12 +1,12 @@ from discord.ext.commands import Context -from discord import Client +from discord import VulkanBot from Handlers.AbstractHandler import AbstractHandler from Handlers.HandlerResponse import HandlerResponse from Parallelism.ProcessManager import ProcessManager class ClearHandler(AbstractHandler): - def __init__(self, ctx: Context, bot: Client) -> None: + def __init__(self, ctx: Context, bot: VulkanBot) -> None: super().__init__(ctx, bot) async def run(self) -> HandlerResponse: diff --git a/Handlers/HistoryHandler.py b/Handlers/HistoryHandler.py index b33c263..7869b6f 100644 --- a/Handlers/HistoryHandler.py +++ b/Handlers/HistoryHandler.py @@ -1,5 +1,5 @@ from discord.ext.commands import Context -from discord import Client +from Music.MusicBot import VulkanBot from Handlers.AbstractHandler import AbstractHandler from Handlers.HandlerResponse import HandlerResponse from Utils.Utils import Utils @@ -7,7 +7,7 @@ from Parallelism.ProcessManager import ProcessManager class HistoryHandler(AbstractHandler): - def __init__(self, ctx: Context, bot: Client) -> None: + def __init__(self, ctx: Context, bot: VulkanBot) -> None: super().__init__(ctx, bot) async def run(self) -> HandlerResponse: diff --git a/Handlers/LoopHandler.py b/Handlers/LoopHandler.py index b9b0bf4..c9e429f 100644 --- a/Handlers/LoopHandler.py +++ b/Handlers/LoopHandler.py @@ -1,5 +1,5 @@ from discord.ext.commands import Context -from discord import Client +from Music.MusicBot import VulkanBot from Handlers.AbstractHandler import AbstractHandler from Handlers.HandlerResponse import HandlerResponse from Config.Exceptions import BadCommandUsage @@ -7,7 +7,7 @@ from Parallelism.ProcessManager import ProcessManager class LoopHandler(AbstractHandler): - def __init__(self, ctx: Context, bot: Client) -> None: + def __init__(self, ctx: Context, bot: VulkanBot) -> None: super().__init__(ctx, bot) async def run(self, args: str) -> HandlerResponse: diff --git a/Handlers/MoveHandler.py b/Handlers/MoveHandler.py index 8ce3ce9..be0849c 100644 --- a/Handlers/MoveHandler.py +++ b/Handlers/MoveHandler.py @@ -1,6 +1,6 @@ from typing import Union from discord.ext.commands import Context -from discord import Client +from Music.MusicBot import VulkanBot from Handlers.AbstractHandler import AbstractHandler from Handlers.HandlerResponse import HandlerResponse from Config.Exceptions import BadCommandUsage, VulkanError, InvalidInput, NumberRequired, UnknownError @@ -9,7 +9,7 @@ from Parallelism.ProcessManager import ProcessManager class MoveHandler(AbstractHandler): - def __init__(self, ctx: Context, bot: Client) -> None: + def __init__(self, ctx: Context, bot: VulkanBot) -> None: super().__init__(ctx, bot) async def run(self, pos1: str, pos2: str) -> HandlerResponse: diff --git a/Handlers/NowPlayingHandler.py b/Handlers/NowPlayingHandler.py index 93095c9..500ba4d 100644 --- a/Handlers/NowPlayingHandler.py +++ b/Handlers/NowPlayingHandler.py @@ -1,13 +1,13 @@ from discord.ext.commands import Context -from discord import Client from Handlers.AbstractHandler import AbstractHandler from Handlers.HandlerResponse import HandlerResponse +from Music.MusicBot import VulkanBot from Utils.Cleaner import Cleaner from Parallelism.ProcessManager import ProcessManager class NowPlayingHandler(AbstractHandler): - def __init__(self, ctx: Context, bot: Client) -> None: + def __init__(self, ctx: Context, bot: VulkanBot) -> None: super().__init__(ctx, bot) self.__cleaner = Cleaner() diff --git a/Handlers/PauseHandler.py b/Handlers/PauseHandler.py index 20afe7b..61a0c9d 100644 --- a/Handlers/PauseHandler.py +++ b/Handlers/PauseHandler.py @@ -1,13 +1,13 @@ from discord.ext.commands import Context -from discord import Client from Handlers.AbstractHandler import AbstractHandler from Handlers.HandlerResponse import HandlerResponse from Parallelism.ProcessManager import ProcessManager from Parallelism.Commands import VCommands, VCommandsType +from Music.MusicBot import VulkanBot class PauseHandler(AbstractHandler): - def __init__(self, ctx: Context, bot: Client) -> None: + def __init__(self, ctx: Context, bot: VulkanBot) -> None: super().__init__(ctx, bot) async def run(self) -> HandlerResponse: diff --git a/Handlers/PlayHandler.py b/Handlers/PlayHandler.py index b9636c1..d1c4945 100644 --- a/Handlers/PlayHandler.py +++ b/Handlers/PlayHandler.py @@ -2,7 +2,6 @@ import asyncio from typing import List from Config.Exceptions import DownloadingError, InvalidInput, VulkanError from discord.ext.commands import Context -from discord import Client from Handlers.AbstractHandler import AbstractHandler from Config.Exceptions import ImpossibleMove, UnknownError from Handlers.HandlerResponse import HandlerResponse @@ -12,10 +11,11 @@ from Music.Song import Song from Parallelism.ProcessManager import ProcessManager from Parallelism.ProcessInfo import ProcessInfo from Parallelism.Commands import VCommands, VCommandsType +from Music.MusicBot import VulkanBot class PlayHandler(AbstractHandler): - def __init__(self, ctx: Context, bot: Client) -> None: + def __init__(self, ctx: Context, bot: VulkanBot) -> None: super().__init__(ctx, bot) self.__searcher = Searcher() self.__down = Downloader() diff --git a/Handlers/PrevHandler.py b/Handlers/PrevHandler.py index a1336ae..4ef209e 100644 --- a/Handlers/PrevHandler.py +++ b/Handlers/PrevHandler.py @@ -1,14 +1,14 @@ from discord.ext.commands import Context -from discord import Client from Handlers.AbstractHandler import AbstractHandler from Config.Exceptions import BadCommandUsage, ImpossibleMove from Handlers.HandlerResponse import HandlerResponse from Parallelism.ProcessManager import ProcessManager from Parallelism.Commands import VCommands, VCommandsType +from Music.MusicBot import VulkanBot class PrevHandler(AbstractHandler): - def __init__(self, ctx: Context, bot: Client) -> None: + def __init__(self, ctx: Context, bot: VulkanBot) -> None: super().__init__(ctx, bot) async def run(self) -> HandlerResponse: diff --git a/Handlers/QueueHandler.py b/Handlers/QueueHandler.py index 953287a..3f691bb 100644 --- a/Handlers/QueueHandler.py +++ b/Handlers/QueueHandler.py @@ -1,14 +1,14 @@ from discord.ext.commands import Context -from discord import Client from Handlers.AbstractHandler import AbstractHandler from Handlers.HandlerResponse import HandlerResponse from Music.Downloader import Downloader from Utils.Utils import Utils from Parallelism.ProcessManager import ProcessManager +from Music.MusicBot import VulkanBot class QueueHandler(AbstractHandler): - def __init__(self, ctx: Context, bot: Client) -> None: + def __init__(self, ctx: Context, bot: VulkanBot) -> None: super().__init__(ctx, bot) self.__down = Downloader() diff --git a/Handlers/RemoveHandler.py b/Handlers/RemoveHandler.py index c611f90..c3a8511 100644 --- a/Handlers/RemoveHandler.py +++ b/Handlers/RemoveHandler.py @@ -1,15 +1,15 @@ from typing import Union from discord.ext.commands import Context -from discord import Client from Handlers.AbstractHandler import AbstractHandler from Handlers.HandlerResponse import HandlerResponse from Config.Exceptions import BadCommandUsage, VulkanError, ErrorRemoving, InvalidInput, NumberRequired from Music.Playlist import Playlist from Parallelism.ProcessManager import ProcessManager +from Music.MusicBot import VulkanBot class RemoveHandler(AbstractHandler): - def __init__(self, ctx: Context, bot: Client) -> None: + def __init__(self, ctx: Context, bot: VulkanBot) -> None: super().__init__(ctx, bot) async def run(self, position: str) -> HandlerResponse: diff --git a/Handlers/ResetHandler.py b/Handlers/ResetHandler.py index c938d21..762b849 100644 --- a/Handlers/ResetHandler.py +++ b/Handlers/ResetHandler.py @@ -1,13 +1,13 @@ from discord.ext.commands import Context -from discord import Client from Handlers.AbstractHandler import AbstractHandler from Handlers.HandlerResponse import HandlerResponse from Parallelism.ProcessManager import ProcessManager from Parallelism.Commands import VCommands, VCommandsType +from Music.MusicBot import VulkanBot class ResetHandler(AbstractHandler): - def __init__(self, ctx: Context, bot: Client) -> None: + def __init__(self, ctx: Context, bot: VulkanBot) -> None: super().__init__(ctx, bot) async def run(self) -> HandlerResponse: diff --git a/Handlers/ResumeHandler.py b/Handlers/ResumeHandler.py index bd5c254..abda613 100644 --- a/Handlers/ResumeHandler.py +++ b/Handlers/ResumeHandler.py @@ -1,13 +1,13 @@ from discord.ext.commands import Context -from discord import Client from Handlers.AbstractHandler import AbstractHandler from Handlers.HandlerResponse import HandlerResponse from Parallelism.ProcessManager import ProcessManager from Parallelism.Commands import VCommands, VCommandsType +from Music.MusicBot import VulkanBot class ResumeHandler(AbstractHandler): - def __init__(self, ctx: Context, bot: Client) -> None: + def __init__(self, ctx: Context, bot: VulkanBot) -> None: super().__init__(ctx, bot) async def run(self) -> HandlerResponse: diff --git a/Handlers/ShuffleHandler.py b/Handlers/ShuffleHandler.py index 94999c8..392923a 100644 --- a/Handlers/ShuffleHandler.py +++ b/Handlers/ShuffleHandler.py @@ -1,13 +1,13 @@ from discord.ext.commands import Context -from discord import Client from Handlers.AbstractHandler import AbstractHandler from Handlers.HandlerResponse import HandlerResponse from Config.Exceptions import UnknownError from Parallelism.ProcessManager import ProcessManager +from Music.MusicBot import VulkanBot class ShuffleHandler(AbstractHandler): - def __init__(self, ctx: Context, bot: Client) -> None: + def __init__(self, ctx: Context, bot: VulkanBot) -> None: super().__init__(ctx, bot) async def run(self) -> HandlerResponse: diff --git a/Handlers/SkipHandler.py b/Handlers/SkipHandler.py index ab0355d..7603ee3 100644 --- a/Handlers/SkipHandler.py +++ b/Handlers/SkipHandler.py @@ -1,14 +1,14 @@ from discord.ext.commands import Context -from discord import Client from Handlers.AbstractHandler import AbstractHandler from Config.Exceptions import BadCommandUsage from Handlers.HandlerResponse import HandlerResponse +from Music.MusicBot import VulkanBot from Parallelism.ProcessManager import ProcessManager from Parallelism.Commands import VCommands, VCommandsType class SkipHandler(AbstractHandler): - def __init__(self, ctx: Context, bot: Client) -> None: + def __init__(self, ctx: Context, bot: VulkanBot) -> None: super().__init__(ctx, bot) async def run(self) -> HandlerResponse: diff --git a/Handlers/StopHandler.py b/Handlers/StopHandler.py index 669a953..0a59cae 100644 --- a/Handlers/StopHandler.py +++ b/Handlers/StopHandler.py @@ -1,13 +1,13 @@ from discord.ext.commands import Context -from discord import Client from Handlers.AbstractHandler import AbstractHandler from Handlers.HandlerResponse import HandlerResponse +from Music.MusicBot import VulkanBot from Parallelism.ProcessManager import ProcessManager from Parallelism.Commands import VCommands, VCommandsType class StopHandler(AbstractHandler): - def __init__(self, ctx: Context, bot: Client) -> None: + def __init__(self, ctx: Context, bot: VulkanBot) -> None: super().__init__(ctx, bot) async def run(self) -> HandlerResponse: diff --git a/Music/MusicBot.py b/Music/MusicBot.py new file mode 100644 index 0000000..9093883 --- /dev/null +++ b/Music/MusicBot.py @@ -0,0 +1,73 @@ +from asyncio import AbstractEventLoop +from discord import Guild, Status, Game, Message +from discord.ext.commands.errors import CommandNotFound, MissingRequiredArgument +from Config.Configs import Configs +from discord.ext import commands +from Config.Messages import Messages +from Views.Embeds import Embeds + + +class VulkanBot(commands.Bot): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.__configs = Configs() + self.__messages = Messages() + self.__embeds = Embeds() + self.remove_command("help") + + def startBot(self) -> None: + """Blocking function that will start the bot""" + if self.__configs.BOT_TOKEN == '': + print('DEVELOPER NOTE -> Token not found') + exit() + + super().run(self.__configs.BOT_TOKEN, reconnect=True) + + async def startBotCoro(self, loop: AbstractEventLoop) -> None: + """Start a bot coroutine, does not wait for connection to be established""" + task = loop.create_task(self.__login()) + await task + loop.create_task(self.__connect()) + + async def __login(self): + """Coroutine to login the Bot in discord""" + await self.login(token=self.__configs.BOT_TOKEN) + + async def __connect(self): + """Coroutine to connect the Bot in discord""" + await self.connect(reconnect=True) + + async def on_ready(self): + print(self.__messages.STARTUP_MESSAGE) + await self.change_presence(status=Status.online, activity=Game(name=f"Vulkan | {self.__configs.BOT_PREFIX}help")) + print(self.__messages.STARTUP_COMPLETE_MESSAGE) + + async def on_command_error(self, ctx, error): + if isinstance(error, MissingRequiredArgument): + embed = self.__embeds.MISSING_ARGUMENTS() + await ctx.send(embed=embed) + + elif isinstance(error, CommandNotFound): + embed = self.__embeds.COMMAND_NOT_FOUND() + await ctx.send(embed=embed) + + else: + print(f'DEVELOPER NOTE -> Command Error: {error}') + embed = self.__embeds.UNKNOWN_ERROR() + await ctx.send(embed=embed) + + async def process_commands(self, message: Message): + if message.author.bot: + return + + ctx = await self.get_context(message, cls=Context) + + if ctx.valid and not message.guild: + return + + await self.invoke(ctx) + + +class Context(commands.Context): + bot: VulkanBot + guild: Guild diff --git a/Music/VulkanInitializer.py b/Music/VulkanInitializer.py new file mode 100644 index 0000000..dcf1776 --- /dev/null +++ b/Music/VulkanInitializer.py @@ -0,0 +1,43 @@ +from random import choices +import string +from discord.bot import Bot +from discord import Intents +from Music.MusicBot import VulkanBot +from os import listdir +from Config.Configs import Configs + + +class VulkanInitializer: + def __init__(self, willListen: bool) -> None: + self.__config = Configs() + self.__intents = Intents.default() + self.__intents.message_content = True + self.__intents.members = True + self.__bot = self.__create_bot(willListen) + self.__add_cogs(self.__bot) + + def getBot(self) -> VulkanBot: + return self.__bot + + def __create_bot(self, willListen: bool) -> VulkanBot: + if willListen: + prefix = self.__config.BOT_PREFIX + else: + prefix = ''.join(choices(string.ascii_uppercase + string.digits, k=4)) + + bot = VulkanBot(command_prefix=prefix, + pm_help=True, + case_insensitive=True, + intents=self.__intents) + return bot + + def __add_cogs(self, bot: Bot) -> None: + try: + for filename in listdir(f'./{self.__config.COMMANDS_PATH}'): + if filename.endswith('.py'): + print(f'Loading {filename}') + bot.load_extension(f'{self.__config.COMMANDS_PATH}.{filename[:-3]}') + + bot.load_extension(f'DiscordCogs.MusicCog') + except Exception as e: + print(e) diff --git a/Parallelism/PlayerProcess.py b/Parallelism/PlayerProcess.py index 9e42284..ab2bd8d 100644 --- a/Parallelism/PlayerProcess.py +++ b/Parallelism/PlayerProcess.py @@ -1,6 +1,6 @@ import asyncio -from os import listdir -from discord import Intents, User, Member, Message, Embed +from Music.VulkanInitializer import VulkanInitializer +from discord import User, Member, Message, Embed from asyncio import AbstractEventLoop, Semaphore from multiprocessing import Process, Queue, RLock from threading import Lock, Thread @@ -10,7 +10,7 @@ from Music.Playlist import Playlist from Music.Song import Song from Config.Configs import Configs from Config.Messages import Messages -from discord.ext.commands import Bot +from Music.MusicBot import VulkanBot from Views.Embeds import Embeds from Parallelism.Commands import VCommands, VCommandsType @@ -50,7 +50,7 @@ class PlayerProcess(Process): self.__authorID = authorID # All information of discord context will be retrieved directly with discord API self.__guild: Guild = None - self.__bot: Client = None + self.__bot: VulkanBot = None self.__voiceChannel: VoiceChannel = None self.__textChannel: TextChannel = None self.__author: User = None @@ -270,30 +270,13 @@ class PlayerProcess(Process): self.__playlist.clear() self.__playlist.loop_off() - async def __createBotInstance(self) -> Client: - """Load a new bot instance that should not be directly called. - Get the guild, voice and text Channel in discord API using IDs passed in constructor - """ - intents = Intents.default() - intents.members = True - bot = Bot(command_prefix='Rafael', - pm_help=True, - case_insensitive=True, - intents=intents) - bot.remove_command('help') + async def __createBotInstance(self) -> VulkanBot: + """Load a new bot instance that should not be directly called.""" + initializer = VulkanInitializer(willListen=False) + bot = initializer.getBot() - # Add the Cogs for this bot too - for filename in listdir(f'./{self.__configs.COMMANDS_PATH}'): - if filename.endswith('.py'): - bot.load_extension(f'{self.__configs.COMMANDS_PATH}.{filename[:-3]}') - - # Login and connect the bot instance to discord API - task = self.__loop.create_task(bot.login(token=self.__configs.BOT_TOKEN, bot=True)) - await task - self.__loop.create_task(bot.connect(reconnect=True)) - # Sleep to wait connection to be established + await bot.startBotCoro(self.__loop) await self.__ensureDiscordConnection(bot) - return bot async def __timeoutHandler(self) -> None: @@ -316,7 +299,7 @@ class PlayerProcess(Process): except Exception as e: print(f'[Error in Timeout] -> {e}') - async def __ensureDiscordConnection(self, bot: Client) -> None: + async def __ensureDiscordConnection(self, bot: VulkanBot) -> None: """Await in this point until connection to discord is established""" guild = None while guild is None: diff --git a/Utils/Cleaner.py b/Utils/Cleaner.py index f944a84..d66a0f2 100644 --- a/Utils/Cleaner.py +++ b/Utils/Cleaner.py @@ -1,16 +1,17 @@ from typing import List from discord.ext.commands import Context -from discord import Client, Message, Embed +from discord import Message, Embed from Config.Singleton import Singleton +from Music.MusicBot import VulkanBot class Cleaner(Singleton): - def __init__(self, bot: Client = None) -> None: + def __init__(self, bot: VulkanBot = None) -> None: if not super().created: self.__bot = bot self.__clean_str = 'Uploader:' - def set_bot(self, bot: Client) -> None: + def set_bot(self, bot: VulkanBot) -> None: self.__bot = bot async def clean_messages(self, ctx: Context, quant: int) -> None: diff --git a/Views/AbstractView.py b/Views/AbstractView.py index 87b6d19..a8f848b 100644 --- a/Views/AbstractView.py +++ b/Views/AbstractView.py @@ -1,7 +1,8 @@ from abc import ABC, abstractmethod from Handlers.HandlerResponse import HandlerResponse from discord.ext.commands import Context -from discord import Client, Message +from discord import Message +from Music.MusicBot import VulkanBot class AbstractView(ABC): @@ -9,14 +10,14 @@ class AbstractView(ABC): self.__response: HandlerResponse = response self.__context: Context = response.ctx self.__message: Message = response.ctx.message - self.__bot: Client = response.ctx.bot + self.__bot: VulkanBot = response.ctx.bot @property def response(self) -> HandlerResponse: return self.__response @property - def bot(self) -> Client: + def bot(self) -> VulkanBot: return self.__bot @property diff --git a/Views/Embeds.py b/Views/Embeds.py index ef60595..894bf52 100644 --- a/Views/Embeds.py +++ b/Views/Embeds.py @@ -334,7 +334,7 @@ class Embeds: def CARA_COROA(self, result: str) -> Embed: embed = Embed( - title='Cara Cora', + title='Cara Coroa', description=f'Result: {result}', colour=self.__colors.GREEN ) diff --git a/main.py b/main.py index e4b6269..c800823 100644 --- a/main.py +++ b/main.py @@ -1,38 +1,49 @@ -from discord import Intents, Client +from random import choices +import string +from discord.bot import Bot +from discord import Intents +from Music.MusicBot import VulkanBot from os import listdir from Config.Configs import Configs -from discord.ext.commands import Bot class VulkanInitializer: - def __init__(self) -> None: + def __init__(self, willListen: bool) -> None: self.__config = Configs() self.__intents = Intents.default() + self.__intents.message_content = True self.__intents.members = True - self.__bot = self.__create_bot() + self.__bot = self.__create_bot(willListen) self.__add_cogs(self.__bot) - def __create_bot(self) -> Client: - bot = Bot(command_prefix=self.__config.BOT_PREFIX, - pm_help=True, - case_insensitive=True, - intents=self.__intents) - bot.remove_command('help') + def getBot(self) -> VulkanBot: + return self.__bot + + def __create_bot(self, willListen: bool) -> VulkanBot: + if willListen: + prefix = self.__config.BOT_PREFIX + else: + prefix = ''.join(choices(string.ascii_uppercase + string.digits, k=4)) + + bot = VulkanBot(command_prefix=prefix, + pm_help=True, + case_insensitive=True, + intents=self.__intents) return bot - def __add_cogs(self, bot: Client) -> None: - for filename in listdir(f'./{self.__config.COMMANDS_PATH}'): - if filename.endswith('.py'): - bot.load_extension(f'{self.__config.COMMANDS_PATH}.{filename[:-3]}') + def __add_cogs(self, bot: Bot) -> None: + try: + for filename in listdir(f'./{self.__config.COMMANDS_PATH}'): + if filename.endswith('.py'): + print(f'Loading {filename}') + bot.load_extension(f'{self.__config.COMMANDS_PATH}.{filename[:-3]}') - def run(self) -> None: - if self.__config.BOT_TOKEN == '': - print('DEVELOPER NOTE -> Token not found') - exit() - - self.__bot.run(self.__config.BOT_TOKEN, bot=True, reconnect=True) + bot.load_extension(f'DiscordCogs.MusicCog') + except Exception as e: + print(e) if __name__ == '__main__': - vulkan = VulkanInitializer() - vulkan.run() + initializer = VulkanInitializer(willListen=True) + vulkanBot = initializer.getBot() + vulkanBot.startBot() diff --git a/requirements.txt b/requirements.txt index 65cdaf22c17f9ce4d38bf9e470e8f215c8755c7d..b6d030b3e99f8a0229bc56ce6076336b2353ede2 100644 GIT binary patch delta 23 ecmZ3$^o5cC|Gxr;N(NnqWQKf(B8HTS)=vRs{s