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
|
|
|
---
|
2020-01-06 23:27:48 -05:00
|
|
|
Copyright (C) 2019-2020 Abigail Gold, 0x5c
|
2019-10-05 18:53:23 -04:00
|
|
|
|
2020-02-15 06:27:48 -05:00
|
|
|
This file is part of qrm2 and is released under the terms of
|
2020-01-31 06:50:50 -05:00
|
|
|
the GNU General Public License, version 2.
|
2019-10-05 18:53:23 -04:00
|
|
|
"""
|
|
|
|
|
2020-01-31 06:50:50 -05:00
|
|
|
|
2019-10-18 08:27:05 -04:00
|
|
|
import random
|
|
|
|
import json
|
2020-01-13 14:06:26 -05:00
|
|
|
from datetime import datetime
|
2020-01-21 19:48:19 -05:00
|
|
|
import asyncio
|
2019-10-18 08:27:05 -04:00
|
|
|
|
2020-01-07 05:36:09 -05:00
|
|
|
import aiohttp
|
|
|
|
|
2019-10-05 18:53:23 -04:00
|
|
|
import discord.ext.commands as commands
|
|
|
|
|
2019-12-06 01:19:42 -05:00
|
|
|
import common as cmn
|
2020-01-21 19:48:19 -05:00
|
|
|
from resources import study
|
2019-12-06 01:19:42 -05:00
|
|
|
|
2019-10-05 18:53:23 -04:00
|
|
|
|
|
|
|
class StudyCog(commands.Cog):
|
2020-01-30 06:15:42 -05:00
|
|
|
choices = {cmn.emojis.a: "A", cmn.emojis.b: "B", cmn.emojis.c: "C", cmn.emojis.d: "D"}
|
2020-01-21 19:48:19 -05:00
|
|
|
|
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()
|
2020-01-30 06:15:42 -05:00
|
|
|
self.source = "Data courtesy of [HamStudy.org](https://hamstudy.org/)"
|
2020-01-07 05:36:09 -05:00
|
|
|
self.session = aiohttp.ClientSession(connector=bot.qrm.connector)
|
2019-10-05 18:53:23 -04:00
|
|
|
|
2020-01-30 06:15:42 -05:00
|
|
|
@commands.command(name="hamstudy", aliases=["rq", "randomquestion", "randomq"], category=cmn.cat.study)
|
|
|
|
async def _random_question(self, ctx: commands.Context, country: str = "", level: str = ""):
|
|
|
|
"""Gets a random question from [HamStudy's](https://hamstudy.org) question pools."""
|
2019-10-05 18:53:23 -04:00
|
|
|
with ctx.typing():
|
2020-01-13 14:06:26 -05:00
|
|
|
embed = cmn.embed_factory(ctx)
|
|
|
|
|
|
|
|
country = country.lower()
|
|
|
|
level = level.lower()
|
|
|
|
|
2020-01-21 19:48:19 -05:00
|
|
|
if country in study.pool_names.keys():
|
|
|
|
if level in study.pool_names[country].keys():
|
|
|
|
pool_name = study.pool_names[country][level]
|
2020-01-13 14:06:26 -05:00
|
|
|
|
|
|
|
elif level in ("random", "r"):
|
|
|
|
# select a random level in that country
|
2020-01-21 19:48:19 -05:00
|
|
|
pool_name = random.choice(list(study.pool_names[country].values()))
|
2020-01-13 14:06:26 -05:00
|
|
|
|
|
|
|
else:
|
|
|
|
# show list of possible pools
|
|
|
|
embed.title = "Pool Not Found!"
|
|
|
|
embed.description = "Possible arguments are:"
|
|
|
|
embed.colour = cmn.colours.bad
|
2020-01-21 19:48:19 -05:00
|
|
|
for cty in study.pool_names:
|
2020-01-30 06:15:42 -05:00
|
|
|
levels = "`, `".join(study.pool_names[cty].keys())
|
2020-01-21 20:31:13 -05:00
|
|
|
embed.add_field(name=f"**Country: `{cty}` {study.pool_emojis[cty]}**",
|
|
|
|
value=f"Levels: `{levels}`", inline=False)
|
2020-01-21 19:48:19 -05:00
|
|
|
embed.add_field(name="**Random**", value="To select a random pool or country, use `random` or `r`")
|
2020-01-13 14:06:26 -05:00
|
|
|
await ctx.send(embed=embed)
|
|
|
|
return
|
|
|
|
|
|
|
|
elif country in ("random", "r"):
|
|
|
|
# select a random country and level
|
2020-01-21 19:48:19 -05:00
|
|
|
country = random.choice(list(study.pool_names.keys()))
|
|
|
|
pool_name = random.choice(list(study.pool_names[country].values()))
|
2020-01-13 14:06:26 -05:00
|
|
|
|
|
|
|
else:
|
|
|
|
# show list of possible pools
|
|
|
|
embed.title = "Pool Not Found!"
|
|
|
|
embed.description = "Possible arguments are:"
|
2019-12-23 10:32:24 -05:00
|
|
|
embed.colour = cmn.colours.bad
|
2020-01-21 19:48:19 -05:00
|
|
|
for cty in study.pool_names:
|
2020-01-30 06:15:42 -05:00
|
|
|
levels = "`, `".join(study.pool_names[cty].keys())
|
2020-01-21 20:31:13 -05:00
|
|
|
embed.add_field(name=f"**Country: `{cty}` {study.pool_emojis[cty]}**",
|
|
|
|
value=f"Levels: `{levels}`", inline=False)
|
2020-01-21 19:48:19 -05:00
|
|
|
embed.add_field(name="**Random**", value="To select a random pool or country, use `random` or `r`")
|
2019-12-23 10:32:24 -05:00
|
|
|
await ctx.send(embed=embed)
|
2019-10-05 18:53:23 -04:00
|
|
|
return
|
|
|
|
|
2020-01-13 14:06:26 -05:00
|
|
|
pools = await self.hamstudy_get_pools()
|
|
|
|
|
2020-01-21 19:48:19 -05:00
|
|
|
pool_matches = [p for p in pools.keys() if "_".join(p.split("_")[:-1]) == pool_name]
|
2020-01-13 14:06:26 -05:00
|
|
|
|
|
|
|
if len(pool_matches) > 0:
|
|
|
|
if len(pool_matches) == 1:
|
|
|
|
pool = pool_matches[0]
|
|
|
|
else:
|
|
|
|
# look at valid_from and expires dates to find the correct one
|
|
|
|
for p in pool_matches:
|
|
|
|
valid_from = datetime.fromisoformat(pools[p]["valid_from"][:-1] + "+00:00")
|
|
|
|
expires = datetime.fromisoformat(pools[p]["expires"][:-1] + "+00:00")
|
|
|
|
|
|
|
|
if valid_from < datetime.utcnow() < expires:
|
|
|
|
pool = p
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# show list of possible pools
|
|
|
|
embed.title = "Pool Not Found!"
|
|
|
|
embed.description = "Possible arguments are:"
|
|
|
|
embed.colour = cmn.colours.bad
|
2020-01-21 19:48:19 -05:00
|
|
|
for cty in study.pool_names:
|
2020-01-30 06:15:42 -05:00
|
|
|
levels = "`, `".join(study.pool_names[cty].keys())
|
2020-01-21 20:31:13 -05:00
|
|
|
embed.add_field(name=f"**Country: `{cty}` {study.pool_emojis[cty]}**",
|
|
|
|
value=f"Levels: `{levels}`", inline=False)
|
2020-01-21 19:48:19 -05:00
|
|
|
embed.add_field(name="**Random**", value="To select a random pool or country, use `random` or `r`")
|
2020-01-13 14:06:26 -05:00
|
|
|
await ctx.send(embed=embed)
|
|
|
|
return
|
|
|
|
|
2020-01-21 19:48:19 -05:00
|
|
|
pool_meta = pools[pool]
|
2020-01-13 14:06:26 -05:00
|
|
|
|
2020-01-30 06:15:42 -05:00
|
|
|
async with self.session.get(f"https://hamstudy.org/pools/{pool}") as resp:
|
2019-12-23 17:54:20 -05:00
|
|
|
if resp.status != 200:
|
2020-01-27 00:37:52 -05:00
|
|
|
raise cmn.BotHTTPError(resp)
|
2020-01-30 06:15:42 -05:00
|
|
|
pool = json.loads(await resp.read())["pool"]
|
2019-10-05 18:53:23 -04:00
|
|
|
|
|
|
|
# Select a question
|
2020-01-30 06:15:42 -05:00
|
|
|
pool_section = random.choice(pool)["sections"]
|
|
|
|
pool_questions = random.choice(pool_section)["questions"]
|
2019-10-05 18:53:23 -04:00
|
|
|
question = random.choice(pool_questions)
|
|
|
|
|
2020-01-21 19:48:19 -05:00
|
|
|
embed.title = f"{study.pool_emojis[country]} {pool_meta['class']} {question['id']}"
|
2019-12-16 03:49:34 -05:00
|
|
|
embed.description = self.source
|
2020-01-30 06:15:42 -05:00
|
|
|
embed.add_field(name="Question:", value=question["text"], inline=False)
|
|
|
|
embed.add_field(name="Answers:",
|
2020-01-20 20:50:55 -05:00
|
|
|
value=(f"**{cmn.emojis.a}** {question['answers']['A']}"
|
|
|
|
f"\n**{cmn.emojis.b}** {question['answers']['B']}"
|
|
|
|
f"\n**{cmn.emojis.c}** {question['answers']['C']}"
|
|
|
|
f"\n**{cmn.emojis.d}** {question['answers']['D']}"),
|
|
|
|
inline=False)
|
2020-01-30 06:15:42 -05:00
|
|
|
embed.add_field(name="To Answer:",
|
|
|
|
value=("Answer with reactions below. If not answered within 10 minutes,"
|
|
|
|
" the answer will be revealed."),
|
2020-01-21 19:48:19 -05:00
|
|
|
inline=False)
|
2020-01-30 06:15:42 -05:00
|
|
|
if "image" in question:
|
|
|
|
image_url = f"https://hamstudy.org/_1330011/images/{pool.split('_',1)[1]}/{question['image']}"
|
2019-12-23 09:53:49 -05:00
|
|
|
embed.set_image(url=image_url)
|
2020-01-21 19:48:19 -05:00
|
|
|
|
2020-01-20 20:50:55 -05:00
|
|
|
q_msg = await ctx.send(embed=embed)
|
|
|
|
|
|
|
|
await cmn.add_react(q_msg, cmn.emojis.a)
|
|
|
|
await cmn.add_react(q_msg, cmn.emojis.b)
|
|
|
|
await cmn.add_react(q_msg, cmn.emojis.c)
|
|
|
|
await cmn.add_react(q_msg, cmn.emojis.d)
|
|
|
|
|
2020-01-21 19:48:19 -05:00
|
|
|
def check(reaction, user):
|
2020-01-21 20:31:13 -05:00
|
|
|
return (user.id != self.bot.user.id
|
|
|
|
and reaction.message.id == q_msg.id
|
|
|
|
and str(reaction.emoji) in self.choices.keys())
|
2020-01-21 19:48:19 -05:00
|
|
|
|
|
|
|
try:
|
2020-01-30 06:15:42 -05:00
|
|
|
reaction, user = await self.bot.wait_for("reaction_add", timeout=600.0, check=check)
|
2020-01-21 19:48:19 -05:00
|
|
|
except asyncio.TimeoutError:
|
|
|
|
embed.remove_field(2)
|
|
|
|
embed.add_field(name="Answer:", value=f"Timed out! The correct answer was **{question['answer']}**.")
|
|
|
|
await q_msg.edit(embed=embed)
|
|
|
|
else:
|
2020-01-30 06:15:42 -05:00
|
|
|
if self.choices[str(reaction.emoji)] == question["answer"]:
|
2020-01-21 19:48:19 -05:00
|
|
|
embed.remove_field(2)
|
|
|
|
embed.add_field(name="Answer:", value=f"Correct! The answer was **{question['answer']}**.")
|
|
|
|
embed.colour = cmn.colours.good
|
|
|
|
await q_msg.edit(embed=embed)
|
2019-10-05 18:53:23 -04:00
|
|
|
else:
|
2020-01-21 19:48:19 -05:00
|
|
|
embed.remove_field(2)
|
|
|
|
embed.add_field(name="Answer:", value=f"Incorrect! The correct answer was **{question['answer']}**.")
|
|
|
|
embed.colour = cmn.colours.bad
|
|
|
|
await q_msg.edit(embed=embed)
|
2019-10-05 18:53:23 -04:00
|
|
|
|
2020-01-13 14:06:26 -05:00
|
|
|
async def hamstudy_get_pools(self):
|
2020-01-30 06:15:42 -05:00
|
|
|
async with self.session.get("https://hamstudy.org/pools/") as resp:
|
2020-01-13 14:06:26 -05:00
|
|
|
if resp.status != 200:
|
2020-01-27 00:37:52 -05:00
|
|
|
raise cmn.BotHTTPError(resp)
|
2020-01-13 14:06:26 -05:00
|
|
|
else:
|
|
|
|
pools_dict = json.loads(await resp.read())
|
|
|
|
|
2020-01-21 19:48:19 -05:00
|
|
|
pools = dict()
|
|
|
|
for ls in pools_dict.values():
|
|
|
|
for pool in ls:
|
|
|
|
pools[pool["id"]] = pool
|
2020-01-13 14:06:26 -05:00
|
|
|
|
|
|
|
return pools
|
|
|
|
|
2019-10-05 18:53:23 -04:00
|
|
|
|
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))
|