diff --git a/audiotrivia/audiosession.py b/audiotrivia/audiosession.py index 081c984..946965e 100644 --- a/audiotrivia/audiosession.py +++ b/audiotrivia/audiosession.py @@ -13,6 +13,13 @@ class AudioSession(TriviaSession): self.audio = audio_cog + @classmethod + def start(cls, ctx, question_list, settings, audio_cog: Audio = None): + session = cls(ctx, question_list, settings, audio_cog) + loop = ctx.bot.loop + session._task = loop.create_task(session.run()) + return session + async def run(self): """Run the audio trivia session. @@ -29,7 +36,9 @@ class AudioSession(TriviaSession): self.count += 1 msg = "**Question number {}!**\n\nName this audio!".format(self.count) await self.ctx.send(msg) - await self.audio.play(self.ctx, question) + print(question) + + await self.audio.play(ctx=self.ctx, query=question) continue_ = await self.wait_for_answer(answers, delay, timeout) if continue_ is False: @@ -40,7 +49,3 @@ class AudioSession(TriviaSession): else: await self.ctx.send("There are no more questions!") await self.end_game() - - - - diff --git a/audiotrivia/audiotrivia.py b/audiotrivia/audiotrivia.py index f1cce57..9b861e8 100644 --- a/audiotrivia/audiotrivia.py +++ b/audiotrivia/audiotrivia.py @@ -1,9 +1,9 @@ import pathlib from typing import List +import yaml from redbot.cogs.trivia import LOG from redbot.cogs.trivia.trivia import InvalidListError, Trivia -from redbot.core import Config, checks from redbot.core import commands from redbot.core.bot import Red from redbot.core.data_manager import cog_data_path @@ -42,7 +42,6 @@ class AudioTrivia(Trivia): await ctx.send("Audio is not loaded. Load it and try again") return - categories = [c.lower() for c in categories] session = self._get_trivia_session(ctx.channel) if session is not None: @@ -54,7 +53,7 @@ class AudioTrivia(Trivia): # We reverse the categories so that the first list's config takes # priority over the others. try: - dict_ = self.get_trivia_list(category) + dict_ = self.get_audio_list(category) except FileNotFoundError: await ctx.send( "Invalid category `{0}`. See `{1}audiotrivia list`" @@ -82,7 +81,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) + session = AudioSession.start(ctx, trivia_dict, settings, self.audio) self.trivia_sessions.append(session) LOG.debug("New audio trivia session; #%s in %d", ctx.channel, ctx.guild.id) @@ -98,8 +97,40 @@ class AudioTrivia(Trivia): return await ctx.send(msg) + def get_audio_list(self, category: str) -> dict: + """Get the audiotrivia list corresponding to the given category. + + Parameters + ---------- + category : str + The desired category. Case sensitive. + + Returns + ------- + `dict` + A dict mapping questions (`str`) to answers (`list` of `str`). + + """ + try: + path = next(p for p in self._audio_lists() if p.stem == category) + except StopIteration: + raise FileNotFoundError("Could not find the `{}` category.".format(category)) + + with path.open(encoding="utf-8") as file: + try: + dict_ = yaml.load(file) + except yaml.error.YAMLError as exc: + raise InvalidListError("YAML parsing failed.") from exc + else: + return dict_ + def _audio_lists(self) -> List[pathlib.Path]: - print(cog_data_path(self)) personal_lists = [p.resolve() for p in cog_data_path(self).glob("*.yaml")] - return personal_lists \ No newline at end of file + return personal_lists + get_core_lists() + + +def get_core_lists() -> List[pathlib.Path]: + """Return a list of paths for all trivia lists packaged with the bot.""" + core_lists_path = pathlib.Path(__file__).parent.resolve() / "data/lists" + return list(core_lists_path.glob("*.yaml"))