diff --git a/audiotrivia/audiosession.py b/audiotrivia/audiosession.py index 73f3e45..20ad3ae 100644 --- a/audiotrivia/audiosession.py +++ b/audiotrivia/audiosession.py @@ -1,21 +1,21 @@ """Module to manage audio trivia sessions.""" import asyncio -from redbot.cogs.audio import Audio +import lavalink from redbot.cogs.trivia import TriviaSession class AudioSession(TriviaSession): """Class to run a session of audio trivia""" - def __init__(self, ctx, question_list: dict, settings: dict, audio_cog: Audio): + def __init__(self, ctx, question_list: dict, settings: dict, player: lavalink.Player): super().__init__(ctx, question_list, settings) - self.audio = audio_cog + self.player = player @classmethod - def start(cls, ctx, question_list, settings, audio_cog: Audio = None): - session = cls(ctx, question_list, settings, audio_cog) + def start(cls, ctx, question_list, settings, player: lavalink.Player = None): + session = cls(ctx, question_list, settings, player) loop = ctx.bot.loop session._task = loop.create_task(session.run()) return session @@ -36,12 +36,17 @@ class AudioSession(TriviaSession): self.count += 1 msg = "**Question number {}!**\n\nName this audio!".format(self.count) await self.ctx.send(msg) - print(question) + # print("Audio question: {}".format(question)) # await self.ctx.invoke(self.audio.play(ctx=self.ctx, query=question)) - await self.ctx.invoke(self.audio.play, query=question) + # ctx_copy = copy(self.ctx) - print("after audio.play") + # await self.ctx.invoke(self.player.play, query=question) + query = question.strip("<>") + tracks = await self.player.get_tracks(query) + self.player.add(self.ctx.author, tracks[0]) + if not self.player.current: + await self.player.play() continue_ = await self.wait_for_answer(answers, delay, timeout) if continue_ is False: @@ -55,4 +60,4 @@ class AudioSession(TriviaSession): async def end_game(self): await super().end_game() - await self.ctx.invoke(self.audio.disconnect) \ No newline at end of file + await self.ctx.invoke(self.player.disconnect) diff --git a/audiotrivia/audiotrivia.py b/audiotrivia/audiotrivia.py index 250f15a..c170efa 100644 --- a/audiotrivia/audiotrivia.py +++ b/audiotrivia/audiotrivia.py @@ -1,6 +1,8 @@ +import datetime import pathlib from typing import List +import lavalink import yaml from redbot.cogs.audio import Audio from redbot.cogs.trivia import LOG @@ -24,12 +26,12 @@ class AudioTrivia(Trivia): self.bot = bot self.audio = None - # @commands.command() - # @commands.is_owner() - # async def testit(self, ctx: commands.Context): - # self.audio: Audio = self.bot.get_cog("Audio") - # await ctx.invoke(self.audio.play, query="https://www.youtube.com/watch?v=FrceWR4XnVU") - # print("done") + @commands.command() + @commands.is_owner() + async def testit(self, ctx: commands.Context): + self.audio: Audio = self.bot.get_cog("Audio") + await ctx.invoke(self.audio.play, query="https://www.youtube.com/watch?v=FrceWR4XnVU") + print("done") @commands.group(invoke_without_command=True) @commands.guild_only() @@ -55,6 +57,29 @@ class AudioTrivia(Trivia): if session is not None: await ctx.send("There is already an ongoing trivia session in this channel.") return + + if not Audio._player_check(ctx): + try: + if not ctx.author.voice.channel.permissions_for(ctx.me).connect or Audio._userlimit( + ctx.author.voice.channel + ): + return await ctx.send("I don't have permission to connect to your channel." + ) + await lavalink.connect(ctx.author.voice.channel) + lavaplayer = lavalink.get_player(ctx.guild.id) + lavaplayer.store("connect", datetime.datetime.utcnow()) + except AttributeError: + return await ctx.send("Connect to a voice channel first.") + + lavaplayer = lavalink.get_player(ctx.guild.id) + lavaplayer.store("channel", ctx.channel.id) # What's this for? I dunno + lavaplayer.store("guild", ctx.guild.id) + + if ( + not ctx.author.voice or ctx.author.voice.channel != lavaplayer.channel + ): + return await ctx.send("You must be in the voice channel to use the audiotrivia command.") + trivia_dict = {} authors = [] for category in reversed(categories): @@ -89,7 +114,7 @@ class AudioTrivia(Trivia): if config and settings["allow_override"]: settings.update(config) settings["lists"] = dict(zip(categories, reversed(authors))) - session = AudioSession.start(ctx, trivia_dict, settings, self.audio) + session = AudioSession.start(ctx=ctx, question_list=trivia_dict, settings=settings, player=lavaplayer) self.trivia_sessions.append(session) LOG.debug("New audio trivia session; #%s in %d", ctx.channel, ctx.guild.id)