Changing to pycord

This commit is contained in:
Rafael Vargas
2022-07-27 01:36:55 -03:00
parent 4a22b43ce9
commit fededdbb8c
27 changed files with 217 additions and 118 deletions

73
Music/MusicBot.py Normal file
View File

@@ -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

View File

@@ -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)