diff --git a/src/cogs/music.py b/src/cogs/music.py new file mode 100644 index 0000000..a7af432 --- /dev/null +++ b/src/cogs/music.py @@ -0,0 +1,60 @@ +import discord +from subprocess import Popen, DEVNULL +from wavelink import Client as wClient +from discord.ext import commands +from utils.core import load + +def setup(client): + client.add_cog(Music(client)) + +class Music(commands.Cog): + """Commandes relatives à la musique.""" + def __init__(self, client): + self.client = client + self.keys = load(["PASSWORD_LAVALINK"]) + print("Démarrage du serveur Lavalink...") + Popen(['java', '-jar', 'Lavalink.jar'], stdout = DEVNULL) + print("Lavalink prêt") + + if not hasattr(client, 'wavelink'): + self.client.wavelink = wClient(bot = self.client) + + self.client.loop.create_task(self.start_nodes()) + + async def start_nodes(self): + await self.client.wait_until_ready() + + # Initiate our nodes. For this example we will use one server. + # Region should be a discord.py guild.region e.g sydney or us_central (Though this is not technically required) + await self.client.wavelink.initiate_node(host = "127.0.0.1", + port = 2333, + rest_uri = "http://127.0.0.1:2333", + password = self.keys["TOKEN_REDDIT_CLIENT_ID"], + identifier = "TEST", + region = "us_central") + + @commands.command(name='connect') + async def connect_(self, ctx, *, channel: discord.VoiceChannel = None): + if not channel: + try: + channel = ctx.author.voice.channel + except AttributeError: + raise discord.DiscordException("Aucun channel à rejoindre. Veuillez soit spécifier un canal valide, soit en rejoindre un.") + + player = self.client.wavelink.get_player(ctx.guild.id) + await ctx.send(f"Connexion à **`{channel.name}`**") + await player.connect(channel.id) + + @commands.command() + async def play(self, ctx, *, query: str): + tracks = await self.client.wavelink.get_tracks(f"ytsearch:{query}") + + if not tracks: + return await ctx.send("Je n'ai pas trouvé la musique demandé.") + + player = self.client.wavelink.get_player(ctx.guild.id) + if not player.is_connected: + await ctx.invoke(self.connect_) + + await ctx.send(f"Ajout de {str(tracks[0])} à la file d'attente.") + await player.play(tracks[0])