Chaging the folders and creating a separeted class for messages

This commit is contained in:
Rafael Vargas
2022-03-22 17:53:21 -04:00
parent 2240c7535a
commit a828350201
27 changed files with 145 additions and 85 deletions

140
Commands/Control.py Normal file
View File

@@ -0,0 +1,140 @@
import discord
from discord import Client
from discord.ext.commands.errors import CommandNotFound, MissingRequiredArgument, UserInputError
from discord.ext import commands
from Config.Config import Configs
from Config.Helper import Helper
helper = Helper()
class Control(commands.Cog):
"""Control the flow of the Bot"""
def __init__(self, bot: Client):
self.__bot = bot
self.__config = Configs()
self.__comandos = {
'MUSIC': ['resume', 'pause', 'loop', 'stop',
'skip', 'play', 'queue', 'clear',
'np', 'shuffle', 'move', 'remove',
'reset', 'prev', 'history'],
'RANDOM': ['choose', 'cara', 'random']
}
@commands.Cog.listener()
async def on_ready(self):
print(self.__config.STARTUP_MESSAGE)
await self.__bot.change_presence(status=discord.Status.online, activity=discord.Game(name=f"Vulkan | {self.__config.BOT_PREFIX}help"))
print(self.__config.STARTUP_COMPLETE_MESSAGE)
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
if isinstance(error, MissingRequiredArgument):
embed = discord.Embed(
title=self.__config.ERROR_TITLE,
description=self.__config.ERROR_MISSING_ARGUMENTS,
colour=self.__config.COLOURS['black']
)
await ctx.send(embed=embed)
elif isinstance(error, CommandNotFound):
embed = discord.Embed(
title=self.__config.ERROR_TITLE,
description=self.__config.COMMAND_NOT_FOUND,
colour=self.__config.COLOURS['black']
)
await ctx.send(embed=embed)
elif isinstance(error, UserInputError):
my_error = False
if len(error.args) > 0:
for arg in error.args:
if arg == self.__config.MY_ERROR_BAD_COMMAND:
embed = discord.Embed(
title=self.__config.BAD_COMMAND_TITLE,
description=self.__config.BAD_COMMAND,
colour=self.__config.COLOURS['black']
)
await ctx.send(embed=embed)
my_error = True
break
if not my_error:
raise error
else:
print(f'DEVELOPER NOTE -> Comand Error: {error}')
embed = discord.Embed(
title=self.__config.ERROR_TITLE,
description=self.__config.UNKNOWN_ERROR,
colour=self.__config.COLOURS['red']
)
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 != '':
for command in self.__bot.commands:
if command.name == command_help:
txt = command.description if command.description else command.help
embedhelp = discord.Embed(
title=f'**Description of {command_help}** command',
description=txt,
colour=self.__config.COLOURS['blue']
)
await ctx.send(embed=embedhelp)
return
embedhelp = discord.Embed(
title='Command Help',
description=f'Command {command_help} Not Found',
colour=self.__config.COLOURS['red']
)
await ctx.send(embed=embedhelp)
else:
helptxt = ''
help_music = '🎧 `MUSIC`\n'
help_random = '🎲 `RANDOM`\n'
help_help = '👾 `HELP`\n'
for command in self.__bot.commands:
if command.name in self.__comandos['MUSIC']:
help_music += f'**{command}** - {command.help}\n'
elif command.name in self.__comandos['RANDOM']:
help_random += f'**{command}** - {command.help}\n'
else:
help_help += f'**{command}** - {command.help}\n'
helptxt = f'\n{help_music}\n{help_help}\n{help_random}'
helptxt += f'\n\nType {self.__config.BOT_PREFIX}help "command" for more information about the command chosen'
embedhelp = discord.Embed(
title=f'**Available Commands of {self.__bot.user.name}**',
description=helptxt,
colour=self.__config.COLOURS['blue']
)
embedhelp.set_thumbnail(url=self.__bot.user.avatar_url)
await ctx.send(embed=embedhelp)
@commands.command(name='invite', help=helper.HELP_INVITE, description=helper.HELP_INVITE_LONG)
async def invite_bot(self, ctx):
invite_url = 'https://discordapp.com/oauth2/authorize?client_id={}&scope=bot>'.format(
self.__bot.user.id)
txt = self.__config.INVITE_MESSAGE.format(invite_url)
embed = discord.Embed(
title="Invite Vulkan",
description=txt,
colour=self.__config.COLOURS['blue']
)
await ctx.send(embed=embed)
def setup(bot):
bot.add_cog(Control(bot))

242
Commands/Music.py Normal file
View File

@@ -0,0 +1,242 @@
from typing import Dict
from discord import Guild, Client, Embed
from discord.ext import commands
from discord.ext.commands import Context
from Config.Config import Configs
from Config.Helper import Helper
from Music.Player import Player
from Music.utils import is_connected
from Controllers.SkipController import SkipController
from Views.EmoteView import EmoteView
from Views.EmbedView import EmbedView
helper = Helper()
class Music(commands.Cog):
def __init__(self, bot) -> None:
self.__guilds: Dict[Guild, Player] = {}
self.__bot: Client = bot
self.__config = Configs()
@commands.Cog.listener()
async def on_ready(self) -> None:
"""Load a player for each guild that the Bot are"""
for guild in self.__bot.guilds:
player = Player(self.__bot, guild)
await player.force_stop()
self.__guilds[guild] = player
print(f'Player for guild {guild.name} created')
@commands.Cog.listener()
async def on_guild_join(self, guild: Guild) -> None:
"""Load a player when joining a guild"""
self.__guilds[guild] = Player(self.__bot, guild)
print(f'Player for guild {guild.name} created')
@commands.Cog.listener()
async def on_guild_remove(self, guild: Guild) -> None:
"""Removes the player of the guild if banned"""
if guild in self.__guilds.keys():
self.__guilds.pop(guild, None)
print(f'Player for guild {guild.name} destroyed')
@commands.command(name="play", help=helper.HELP_PLAY, description=helper.HELP_PLAY_LONG, aliases=['p', 'tocar'])
async def play(self, ctx: Context, *args) -> None:
track = " ".join(args)
requester = ctx.author.name
player = self.__get_player(ctx)
if player is None:
await self.__send_embed(ctx, self.__config.ERROR_TITLE, self.__config.NO_GUILD, 'red')
return
if is_connected(ctx) is None:
success = await player.connect(ctx)
if success == False:
await self.__send_embed(ctx, self.__config.IMPOSSIBLE_MOVE, self.__config.NO_CHANNEL, 'red')
return
await player.play(ctx, track, requester)
@commands.command(name="queue", help=helper.HELP_QUEUE, description=helper.HELP_QUEUE_LONG, aliases=['q', 'fila'])
async def queue(self, ctx: Context) -> None:
player = self.__get_player(ctx)
if player is None:
return
embed = await player.queue()
await ctx.send(embed=embed)
@commands.command(name="skip", help=helper.HELP_SKIP, description=helper.HELP_SKIP_LONG, aliases=['s', 'pular'])
async def skip(self, ctx: Context) -> None:
controller = SkipController(ctx, self.__bot)
response = await controller.run()
if response.success:
view = EmoteView(response)
else:
view = EmbedView(response)
await view.run()
@commands.command(name='stop', help=helper.HELP_STOP, description=helper.HELP_STOP_LONG, aliases=['parar'])
async def stop(self, ctx: Context) -> None:
player = self.__get_player(ctx)
if player is None:
return
else:
await player.stop()
@commands.command(name='pause', help=helper.HELP_PAUSE, description=helper.HELP_PAUSE_LONG, aliases=['pausar'])
async def pause(self, ctx: Context) -> None:
player = self.__get_player(ctx)
if player is None:
return
else:
success = await player.pause()
if success:
await self.__send_embed(ctx, self.__config.SONG_PLAYER, self.__config.SONG_PAUSED, 'blue')
@commands.command(name='resume', help=helper.HELP_RESUME, description=helper.HELP_RESUME_LONG, aliases=['soltar'])
async def resume(self, ctx: Context) -> None:
player = self.__get_player(ctx)
if player is None:
return
else:
success = await player.resume()
if success:
await self.__send_embed(ctx, self.__config.SONG_PLAYER, self.__config.SONG_RESUMED, 'blue')
@commands.command(name='prev', help=helper.HELP_PREV, description=helper.HELP_PREV_LONG, aliases=['anterior'])
async def prev(self, ctx: Context) -> None:
player = self.__get_player(ctx)
if player is None:
return
if is_connected(ctx) is None:
success = await player.connect(ctx)
if success == False:
await self.__send_embed(ctx, self.__config.IMPOSSIBLE_MOVE, self.__config.NO_CHANNEL, 'red')
return
await player.play_prev(ctx)
@commands.command(name='history', help=helper.HELP_HISTORY, description=helper.HELP_HISTORY_LONG, aliases=['historico'])
async def history(self, ctx: Context) -> None:
player = self.__get_player(ctx)
if player is None:
return
else:
embed = player.history()
await ctx.send(embed=embed)
@commands.command(name='loop', help=helper.HELP_LOOP, description=helper.HELP_LOOP_LONG, aliases=['l', 'repeat'])
async def loop(self, ctx: Context, args: str) -> None:
player = self.__get_player(ctx)
if player is None:
return
else:
description = await player.loop(args)
await self.__send_embed(ctx, self.__config.SONG_PLAYER, description, 'blue')
@commands.command(name='clear', help=helper.HELP_CLEAR, description=helper.HELP_CLEAR_LONG, aliases=['c', 'limpar'])
async def clear(self, ctx: Context) -> None:
player = self.__get_player(ctx)
if player is None:
return
else:
await player.clear()
@commands.command(name='np', help=helper.HELP_NP, description=helper.HELP_NP_LONG, aliases=['playing', 'now'])
async def now_playing(self, ctx: Context) -> None:
player = self.__get_player(ctx)
if player is None:
return
else:
embed = await player.now_playing()
await self.__clean_messages(ctx)
await ctx.send(embed=embed)
@commands.command(name='shuffle', help=helper.HELP_SHUFFLE, description=helper.HELP_SHUFFLE_LONG, aliases=['aleatorio'])
async def shuffle(self, ctx: Context) -> None:
player = self.__get_player(ctx)
if player is None:
return
else:
description = await player.shuffle()
await self.__send_embed(ctx, self.__config.SONG_PLAYER, description, 'blue')
@commands.command(name='move', help=helper.HELP_MOVE, description=helper.HELP_MOVE_LONG, aliases=['m', 'mover'])
async def move(self, ctx: Context, pos1, pos2='1') -> None:
player = self.__get_player(ctx)
if player is None:
return
else:
description = await player.move(pos1, pos2)
await self.__send_embed(ctx, self.__config.SONG_PLAYER, description, 'blue')
@commands.command(name='remove', help=helper.HELP_REMOVE, description=helper.HELP_REMOVE_LONG, aliases=['remover'])
async def remove(self, ctx: Context, position) -> None:
player = self.__get_player(ctx)
if player is None:
return
else:
description = await player.remove(position)
await self.__send_embed(ctx, self.__config.SONG_PLAYER, description, 'blue')
@commands.command(name='reset', help=helper.HELP_RESET, description=helper.HELP_RESET_LONG, aliases=['resetar'])
async def reset(self, ctx: Context) -> None:
player = self.__get_player(ctx)
try:
await player.force_stop()
await player.stop()
self.__guilds[ctx.guild] = Player(self.__bot, ctx.guild)
player = self.__get_player(ctx)
await player.force_stop()
except Exception as e:
print(f'DEVELOPER NOTE -> Reset Error: {e}')
self.__guilds[ctx.guild] = Player(self.__bot, ctx.guild)
player = self.__get_player(ctx)
print(f'Player for guild {ctx.guild} created')
async def __send_embed(self, ctx: Context, title='', description='', colour='grey') -> None:
try:
colour = self.__config.COLOURS[colour]
except:
colour = self.__config.COLOURS['grey']
embedvc = Embed(
title=title,
description=description,
colour=colour
)
await ctx.send(embed=embedvc)
async def __clean_messages(self, ctx: Context) -> None:
last_messages = await ctx.channel.history(limit=5).flatten()
for message in last_messages:
try:
if message.author == self.__bot.user:
if len(message.embeds) > 0:
embed = message.embeds[0]
if len(embed.fields) > 0:
if embed.fields[0].name == 'Uploader:':
await message.delete()
except:
continue
def __get_player(self, ctx: Context) -> Player:
try:
return self.__guilds[ctx.guild]
except:
return None
def setup(bot):
bot.add_cog(Music(bot))

84
Commands/Random.py Normal file
View File

@@ -0,0 +1,84 @@
from random import randint, random
import discord
from discord.ext import commands
from Config.Config import Configs
from Config.Helper import Helper
helper = Helper()
class Random(commands.Cog):
"""Deal with returning random things"""
def __init__(self, bot):
self.__bot = bot
self.__config = Configs()
@commands.command(name='random', help=helper.HELP_RANDOM, description=helper.HELP_RANDOM_LONG)
async def random(self, ctx, arg: str) -> None:
try:
arg = int(arg)
except:
embed = discord.Embed(
description=self.__config.ERROR_NUMBER,
colour=self.__config.COLOURS['red']
)
await ctx.send(embed=embed)
return
if arg < 1:
a = arg
b = 1
else:
a = 1
b = arg
x = randint(a, b)
embed = discord.Embed(
title=f'Random number between [{a, b}]',
description=x,
colour=self.__config.COLOURS['green']
)
await ctx.send(embed=embed)
@commands.command(name='cara', help=helper.HELP_CARA, description=helper.HELP_CARA_LONG)
async def cara(self, ctx) -> None:
x = random()
if x < 0.5:
result = 'cara'
else:
result = 'coroa'
embed = discord.Embed(
title='Cara Cora',
description=f'Result: {result}',
colour=self.__config.COLOURS['green']
)
await ctx.send(embed=embed)
@commands.command(name='choose', help=helper.HELP_CHOOSE, description=helper.HELP_CHOOSE_LONG)
async def choose(self, ctx, *args: str) -> None:
try:
user_input = " ".join(args)
itens = user_input.split(sep=',')
index = randint(0, len(itens)-1)
embed = discord.Embed(
title='Choose something',
description=itens[index],
colour=self.__config.COLOURS['green']
)
await ctx.send(embed=embed)
except:
embed = discord.Embed(
title='Choose something.',
description=f'Error: Use {self.__config.BOT_PREFIX}help choose to understand this command.',
colour=self.__config.COLOURS['red']
)
await ctx.send(embed=embed)
def setup(bot):
bot.add_cog(Random(bot))