2019-10-05 18:53:23 -04:00
|
|
|
"""
|
2019-12-07 17:26:55 -05:00
|
|
|
Study extension for qrm
|
2019-10-05 18:53:23 -04:00
|
|
|
---
|
|
|
|
Copyright (C) 2019 Abigail Gold, 0x5c
|
|
|
|
|
2019-12-08 15:35:58 -05:00
|
|
|
This file is part of discord-qrm2 and is released under the terms of the GNU
|
2019-10-05 18:53:23 -04:00
|
|
|
General Public License, version 2.
|
|
|
|
"""
|
|
|
|
|
2019-10-18 08:27:05 -04:00
|
|
|
import random
|
|
|
|
import json
|
|
|
|
|
2019-10-05 18:53:23 -04:00
|
|
|
import discord.ext.commands as commands
|
|
|
|
|
2019-10-05 19:59:19 -04:00
|
|
|
import aiohttp
|
2019-10-09 01:25:33 -04:00
|
|
|
|
2019-12-06 01:19:42 -05:00
|
|
|
import common as cmn
|
|
|
|
|
2019-10-05 18:53:23 -04:00
|
|
|
|
|
|
|
class StudyCog(commands.Cog):
|
2019-10-18 08:27:05 -04:00
|
|
|
def __init__(self, bot: commands.Bot):
|
2019-10-05 18:53:23 -04:00
|
|
|
self.bot = bot
|
|
|
|
self.lastq = dict()
|
2019-12-06 01:19:42 -05:00
|
|
|
self.source = 'Data courtesy of [HamStudy.org](https://hamstudy.org/)'
|
2019-10-05 18:53:23 -04:00
|
|
|
|
2019-12-06 01:19:42 -05:00
|
|
|
@commands.command(name="hamstudy", aliases=['rq', 'randomquestion', 'randomq'], category=cmn.cat.study)
|
2019-10-18 08:27:05 -04:00
|
|
|
async def _random_question(self, ctx: commands.Context, level: str = None):
|
2019-10-05 18:53:23 -04:00
|
|
|
'''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'
|
|
|
|
|
2019-12-23 10:32:24 -05:00
|
|
|
embed = cmn.embed_factory(ctx)
|
2019-10-05 18:53:23 -04:00
|
|
|
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
|
|
|
|
|
2019-10-18 08:27:05 -04:00
|
|
|
if (level is None) or (level == 'all'): # no pool given or user wants all, so pick a random pool
|
2019-10-05 18:53:23 -04:00
|
|
|
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
|
2019-12-23 10:32:24 -05:00
|
|
|
embed.title = 'Error in HamStudy command'
|
|
|
|
embed.description = ('The question pool you gave was unrecognized. '
|
|
|
|
'There are many ways to call up certain question pools - try ?rq t, g, or e. '
|
|
|
|
'\n\nNote that currently only the US question pools are available.')
|
|
|
|
embed.colour = cmn.colours.bad
|
|
|
|
await ctx.send(embed=embed)
|
2019-10-05 18:53:23 -04:00
|
|
|
return
|
|
|
|
|
2019-10-05 19:59:19 -04:00
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
async with session.get(f'https://hamstudy.org/pools/{selected_pool}') as resp:
|
|
|
|
if resp.status != 200:
|
2019-12-23 10:32:24 -05:00
|
|
|
embed.title = 'Error in HamStudy command'
|
|
|
|
embed.description = 'Could not load questions'
|
|
|
|
embed.colour = cmn.colours.bad
|
|
|
|
await ctx.send(embed=embed)
|
|
|
|
return
|
2019-10-05 19:59:19 -04:00
|
|
|
pool = json.loads(await resp.read())['pool']
|
2019-10-05 18:53:23 -04:00
|
|
|
|
|
|
|
# Select a question
|
|
|
|
pool_section = random.choice(pool)['sections']
|
|
|
|
pool_questions = random.choice(pool_section)['questions']
|
|
|
|
question = random.choice(pool_questions)
|
|
|
|
|
2019-12-16 03:49:34 -05:00
|
|
|
embed.title = question['id']
|
|
|
|
embed.description = self.source
|
|
|
|
embed.colour = cmn.colours.good
|
2019-12-23 09:53:49 -05:00
|
|
|
embed.add_field(name='Question:', value=question['text'], inline=False)
|
|
|
|
embed.add_field(name='Answers:', value='**A:** ' + question['answers']['A'] +
|
2019-12-23 09:57:05 -05:00
|
|
|
'\n**B:** ' + question['answers']['B'] +
|
|
|
|
'\n**C:** ' + question['answers']['C'] +
|
|
|
|
'\n**D:** ' + question['answers']['D'],
|
|
|
|
inline=False)
|
2019-12-23 09:53:49 -05:00
|
|
|
embed.add_field(name='Answer:', value='Type _?rqa_ for answer', inline=False)
|
2019-10-05 18:53:23 -04:00
|
|
|
if 'image' in question:
|
|
|
|
image_url = f'https://hamstudy.org/_1330011/images/{selected_pool.split("_",1)[1]}/{question["image"]}'
|
2019-12-23 09:53:49 -05:00
|
|
|
embed.set_image(url=image_url)
|
2019-10-05 18:53:23 -04:00
|
|
|
self.lastq[ctx.message.channel.id] = (question['id'], question['answer'])
|
|
|
|
await ctx.send(embed=embed)
|
|
|
|
|
2019-12-06 01:19:42 -05:00
|
|
|
@commands.command(name="hamstudyanswer", aliases=['rqa', 'randomquestionanswer', 'randomqa', 'hamstudya'],
|
|
|
|
category=cmn.cat.study)
|
|
|
|
async def _q_answer(self, ctx: commands.Context, answer: str = None):
|
2019-10-05 18:53:23 -04:00
|
|
|
'''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]
|
2019-12-16 03:49:34 -05:00
|
|
|
embed = cmn.embed_factory(ctx)
|
2019-12-06 01:19:42 -05:00
|
|
|
if answer is not None:
|
|
|
|
answer = answer.upper()
|
|
|
|
if answer == correct_ans:
|
2019-10-05 18:53:23 -04:00
|
|
|
result = f'Correct! The answer to {q_num} was **{correct_ans}**.'
|
2019-12-16 03:49:34 -05:00
|
|
|
embed.title = f'{q_num} Answer'
|
|
|
|
embed.description = f'{self.source}\n\n{result}'
|
|
|
|
embed.colour = cmn.colours.good
|
2019-10-05 18:53:23 -04:00
|
|
|
else:
|
2019-12-06 01:19:42 -05:00
|
|
|
result = f'Incorrect. The answer to {q_num} was **{correct_ans}**, not **{answer}**.'
|
2019-12-16 03:49:34 -05:00
|
|
|
embed.title = f'{q_num} Answer'
|
|
|
|
embed.description = f'{self.source}\n\n{result}'
|
|
|
|
embed.colour = cmn.colours.bad
|
2019-10-05 18:53:23 -04:00
|
|
|
else:
|
|
|
|
result = f'The correct answer to {q_num} was **{correct_ans}**.'
|
2019-12-16 03:49:34 -05:00
|
|
|
embed.title = f'{q_num} Answer'
|
|
|
|
embed.description = f'{self.source}\n\n{result}'
|
|
|
|
embed.colour = cmn.colours.neutral
|
2019-10-05 18:53:23 -04:00
|
|
|
await ctx.send(embed=embed)
|
|
|
|
|
|
|
|
|
2019-10-18 08:27:05 -04:00
|
|
|
def setup(bot: commands.Bot):
|
2019-10-05 18:53:23 -04:00
|
|
|
bot.add_cog(StudyCog(bot))
|