From 640ad9f8e331b47f8f31cebc4210b9656faed2ef Mon Sep 17 00:00:00 2001 From: Abigail Gold Date: Sat, 5 Oct 2019 18:53:23 -0400 Subject: [PATCH 1/2] but Maaaaa, I don't wanna study! --- cogs/studycog.py | 97 ++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 1 + 2 files changed, 98 insertions(+) create mode 100644 cogs/studycog.py diff --git a/cogs/studycog.py b/cogs/studycog.py new file mode 100644 index 0000000..6516efb --- /dev/null +++ b/cogs/studycog.py @@ -0,0 +1,97 @@ +""" +Study cog for qrm +--- +Copyright (C) 2019 Abigail Gold, 0x5c + +This file is part of discord-qrmbot and is released under the terms of the GNU +General Public License, version 2. +""" + +import discord +import discord.ext.commands as commands + +import random +import json +import urllib + +class StudyCog(commands.Cog): + def __init__(self, bot): + self.bot = bot + self.gs = bot.get_cog("GlobalSettings") + self.lastq = dict() + + @commands.command(name="rq", aliases=['randomq']) + async def _random_question(self, ctx, level: str = None): + '''Gets a random question from the Technician, General, and/or Extra question pools.''' + tech_pool = 'E2_2018' + gen_pool = 'E3_2019' + extra_pool = 'E4_2016' + + with ctx.typing(): + selected_pool = None + try: + level = level.lower() + except AttributeError: # no level given (it's None) + pass + + if level in ['t', 'technician', 'tech']: + selected_pool = tech_pool + + if level in ['g', 'gen', 'general']: + selected_pool = gen_pool + + if level in ['e', 'ae', 'extra']: + selected_pool = extra_pool + + if (level is None) or (level == 'all'): # no pool given or user wants all, so pick a random pool and use that + selected_pool = random.choice([tech_pool, gen_pool, extra_pool]) + if (level is not None) and (selected_pool is None): # unrecognized pool given by user + await ctx.send('The question pool you gave was unrecognized. ' + + 'There are many ways to call up certain question pools - try ?rq t, g, or e. ' + + '(Note that only the US question pools are available).') + return + + with urllib.request.urlopen(f'https://hamstudy.org/pools/{selected_pool}') as url: + pool = json.loads(url.read().decode())['pool'] + + # Select a question + pool_section = random.choice(pool)['sections'] + pool_questions = random.choice(pool_section)['questions'] + question = random.choice(pool_questions) + + embed = discord.Embed(title=question['id'], colour=self.gs.colours.good) + embed = embed.add_field(name='Question:', value=question['text'], inline=False) + embed = embed.add_field(name='Answers:', value= + '**A:** ' + question['answers']['A'] + + '\n**B:** ' + question['answers']['B'] + + '\n**C:** ' + question['answers']['C'] + + '\n**D:** ' + question['answers']['D'], inline=False) + embed = embed.add_field(name='Answer:', value='Type _?rqa_ for answer', inline=False) + if 'image' in question: + image_url = f'https://hamstudy.org/_1330011/images/{selected_pool.split("_",1)[1]}/{question["image"]}' + embed = embed.set_image(url=image_url) + self.lastq[ctx.message.channel.id] = (question['id'], question['answer']) + await ctx.send(embed=embed) + + @commands.command(name="rqa") + async def _q_answer(self, ctx, ans: str = None): + '''Returns the answer to question last asked (Optional argument: your answer).''' + with ctx.typing(): + correct_ans = self.lastq[ctx.message.channel.id][1] + q_num = self.lastq[ctx.message.channel.id][0] + if ans is not None: + ans = ans.upper() + if ans == correct_ans: + result = f'Correct! The answer to {q_num} was **{correct_ans}**.' + embed = discord.Embed(title=f'{q_num} Answer', description=result, colour=self.gs.colours.good) + else: + result = f'Incorrect. The answer to {q_num} was **{correct_ans}**, not **{ans}**.' + embed = discord.Embed(title=f'{q_num} Answer', description=result, colour=self.gs.colours.bad) + else: + result = f'The correct answer to {q_num} was **{correct_ans}**.' + embed = discord.Embed(title=f'{q_num} Answer', description=result, colour=self.gs.colours.neutral) + await ctx.send(embed=embed) + + +def setup(bot): + bot.add_cog(StudyCog(bot)) diff --git a/main.py b/main.py index 401d19b..071849b 100644 --- a/main.py +++ b/main.py @@ -70,6 +70,7 @@ bot.add_cog(GlobalSettings(bot)) bot.load_extension("cogs.basecog") bot.load_extension("cogs.morsecog") bot.load_extension("cogs.funcog") +bot.load_extension("cogs.studycog") _ensure_activity.start() From 96d251fe47514f776171680ff598be948bfd09be Mon Sep 17 00:00:00 2001 From: Abigail Gold Date: Sat, 5 Oct 2019 19:59:19 -0400 Subject: [PATCH 2/2] convert urllib code to aiohttp --- cogs/studycog.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cogs/studycog.py b/cogs/studycog.py index 6516efb..530b3c9 100644 --- a/cogs/studycog.py +++ b/cogs/studycog.py @@ -12,7 +12,7 @@ import discord.ext.commands as commands import random import json -import urllib +import aiohttp class StudyCog(commands.Cog): def __init__(self, bot): @@ -51,8 +51,11 @@ class StudyCog(commands.Cog): '(Note that only the US question pools are available).') return - with urllib.request.urlopen(f'https://hamstudy.org/pools/{selected_pool}') as url: - pool = json.loads(url.read().decode())['pool'] + async with aiohttp.ClientSession() as session: + async with session.get(f'https://hamstudy.org/pools/{selected_pool}') as resp: + if resp.status != 200: + return await ctx.send('Could not load questions...') + pool = json.loads(await resp.read())['pool'] # Select a question pool_section = random.choice(pool)['sections']