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
|
|
|
---
|
2021-04-03 18:13:42 -04:00
|
|
|
Copyright (C) 2019-2021 classabbyamp, 0x5c
|
2019-10-05 18:53:23 -04:00
|
|
|
|
2021-06-26 20:23:55 -04:00
|
|
|
SPDX-License-Identifier: LiLiQ-Rplus-1.1
|
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):
|
2021-02-28 01:51:19 -05:00
|
|
|
choices = {"A": cmn.emojis.a, "B": cmn.emojis.b, "C": cmn.emojis.c, "D": cmn.emojis.d, "E": cmn.emojis.e}
|
2021-03-06 21:16:00 -05:00
|
|
|
choices_inv = {y: x for x, y in choices.items()}
|
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
|
|
|
|
2021-03-28 02:38:36 -04:00
|
|
|
@commands.command(name="hamstudy", aliases=["rq", "randomquestion", "randomq"], category=cmn.Cats.STUDY)
|
2021-01-25 17:24:45 -05:00
|
|
|
async def _random_question(self, ctx: commands.Context, country: str = "", level: str = "", element: str = ""):
|
2020-01-30 06:15:42 -05:00
|
|
|
"""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()
|
2021-01-25 17:24:45 -05:00
|
|
|
element = element.upper()
|
2020-01-13 14:06:26 -05:00
|
|
|
|
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:
|
2020-02-20 01:32:17 -05:00
|
|
|
valid_from = datetime.fromisoformat(pools[p]["valid_from"][:-1])
|
|
|
|
expires = datetime.fromisoformat(pools[p]["expires"][:-1])
|
2020-01-13 14:06:26 -05: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
|
2021-01-25 17:24:45 -05:00
|
|
|
if element:
|
|
|
|
els = [el["id"] for el in pool]
|
|
|
|
if element in els:
|
|
|
|
pool_section = pool[els.index(element)]["sections"]
|
|
|
|
else:
|
|
|
|
embed.title = "Element Not Found!"
|
|
|
|
embed.description = f"Possible Elements for Country `{country}` and Level `{level}` are:"
|
|
|
|
embed.colour = cmn.colours.bad
|
|
|
|
embed.description += "\n\n" + "`" + "`, `".join(els) + "`"
|
|
|
|
await ctx.send(embed=embed)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
pool_section = random.choice(pool)["sections"]
|
2020-01-30 06:15:42 -05:00
|
|
|
pool_questions = random.choice(pool_section)["questions"]
|
2019-10-05 18:53:23 -04:00
|
|
|
question = random.choice(pool_questions)
|
2021-02-28 01:51:19 -05:00
|
|
|
answers = question['answers']
|
|
|
|
answers_str = ""
|
|
|
|
answers_str_bolded = ""
|
|
|
|
for letter, ans in answers.items():
|
|
|
|
answers_str += f"{self.choices[letter]} {ans}\n"
|
|
|
|
if letter == question["answer"]:
|
|
|
|
answers_str_bolded += f"{self.choices[letter]} **{ans}**\n"
|
|
|
|
else:
|
|
|
|
answers_str_bolded += f"{self.choices[letter]} {ans}\n"
|
2019-10-05 18:53:23 -04:00
|
|
|
|
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
|
2021-03-06 21:16:00 -05:00
|
|
|
embed.add_field(name="Question", value=question["text"], inline=False)
|
|
|
|
embed.add_field(name="Answers", value=answers_str, inline=False)
|
|
|
|
embed.add_field(name="To Answer",
|
2021-02-28 01:51:19 -05:00
|
|
|
value=("Answer with reactions below. If not answered within 5 minutes,"
|
2020-01-30 06:15:42 -05:00
|
|
|
" 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:
|
2020-02-25 19:45:32 -05:00
|
|
|
image_url = f"https://hamstudy.org/images/{pool_meta['year']}/{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)
|
|
|
|
|
2021-02-28 01:51:19 -05:00
|
|
|
for i in range(len(answers)):
|
|
|
|
await cmn.add_react(q_msg, list(self.choices.values())[i])
|
2021-03-06 21:16:00 -05:00
|
|
|
await cmn.add_react(q_msg, cmn.emojis.question)
|
2020-01-20 20:50:55 -05:00
|
|
|
|
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
|
2021-03-06 21:16:00 -05:00
|
|
|
and (str(reaction.emoji) in self.choices.values() or str(reaction.emoji) == cmn.emojis.question))
|
2020-01-21 19:48:19 -05:00
|
|
|
|
|
|
|
try:
|
2021-03-06 21:16:00 -05:00
|
|
|
reaction, user = await self.bot.wait_for("reaction_add", timeout=300.0, check=check)
|
2020-01-21 19:48:19 -05:00
|
|
|
except asyncio.TimeoutError:
|
2021-03-06 21:16:00 -05:00
|
|
|
embed.set_field_at(1, name="Answers", value=answers_str_bolded, inline=False)
|
|
|
|
embed.set_field_at(2, name="Answer",
|
2021-04-03 18:06:14 -04:00
|
|
|
value=(f"{cmn.emojis.stopwatch} "
|
2021-02-28 01:51:19 -05:00
|
|
|
f"**Timed out!** The correct answer was {self.choices[question['answer']]}"))
|
|
|
|
embed.colour = cmn.colours.timeout
|
2020-01-21 19:48:19 -05:00
|
|
|
await q_msg.edit(embed=embed)
|
|
|
|
else:
|
2021-03-06 21:16:00 -05:00
|
|
|
if str(reaction.emoji) == cmn.emojis.question:
|
|
|
|
embed.set_field_at(1, name="Answers", value=answers_str_bolded, inline=False)
|
|
|
|
embed.set_field_at(2, name="Answer",
|
|
|
|
value=f"The correct answer was {self.choices[question['answer']]}", inline=False)
|
|
|
|
embed.add_field(name="Answer Requested By", value=str(user), inline=False)
|
|
|
|
embed.colour = cmn.colours.timeout
|
2020-01-21 19:48:19 -05:00
|
|
|
await q_msg.edit(embed=embed)
|
2019-10-05 18:53:23 -04:00
|
|
|
else:
|
2021-03-06 21:16:00 -05:00
|
|
|
answers_str_checked = ""
|
|
|
|
chosen_ans = self.choices_inv[str(reaction.emoji)]
|
|
|
|
for letter, ans in answers.items():
|
|
|
|
answers_str_checked += f"{self.choices[letter]}"
|
|
|
|
if letter == question["answer"] == chosen_ans:
|
|
|
|
answers_str_checked += f"{cmn.emojis.check_mark} **{ans}**\n"
|
|
|
|
elif letter == question["answer"]:
|
|
|
|
answers_str_checked += f" **{ans}**\n"
|
|
|
|
elif letter == chosen_ans:
|
|
|
|
answers_str_checked += f"{cmn.emojis.x} {ans}\n"
|
|
|
|
else:
|
|
|
|
answers_str_checked += f" {ans}\n"
|
|
|
|
|
|
|
|
if self.choices[question["answer"]] == str(reaction.emoji):
|
|
|
|
embed.set_field_at(1, name="Answers", value=answers_str_checked, inline=False)
|
|
|
|
embed.set_field_at(2, name="Answer", value=(f"{cmn.emojis.check_mark} "
|
|
|
|
f"**Correct!** The answer was {reaction.emoji}"))
|
|
|
|
embed.add_field(name="Answered By", value=str(user), inline=False)
|
|
|
|
embed.colour = cmn.colours.good
|
|
|
|
await q_msg.edit(embed=embed)
|
|
|
|
else:
|
|
|
|
embed.set_field_at(1, name="Answers", value=answers_str_checked, inline=False)
|
|
|
|
embed.set_field_at(2, name="Answer",
|
|
|
|
value=(f"{cmn.emojis.x} **Incorrect!** The correct answer was "
|
|
|
|
f"{self.choices[question['answer']]}, not {reaction.emoji}"))
|
|
|
|
embed.add_field(name="Answered By", value=str(user), inline=False)
|
|
|
|
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))
|