add the ability to select an element to hamstudy (#344)

fixes #208
This commit is contained in:
classabbyamp 2021-01-25 17:24:45 -05:00 committed by GitHub
parent b091b22c17
commit 6dfc05217f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View File

@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Added ### Added
- MUF and foF2 maps from [prop.kc2g.com](https://prop.kc2g.com/). - MUF and foF2 maps from [prop.kc2g.com](https://prop.kc2g.com/).
- Commands to show METAR (`?metar`) and TAF (`?taf`) (aeronautical weather conditions). - Commands to show METAR (`?metar`) and TAF (`?taf`) (aeronautical weather conditions).
- The ability to select an element of a pool in `?hamstudy`.
### Changed ### Changed
- New colour theme for `?greyline`. - New colour theme for `?greyline`.
- Moved great circle distance and bearing calculation from `?ungrid` to `?griddistance`. - Moved great circle distance and bearing calculation from `?ungrid` to `?griddistance`.

View File

@ -31,13 +31,14 @@ class StudyCog(commands.Cog):
self.session = aiohttp.ClientSession(connector=bot.qrm.connector) self.session = aiohttp.ClientSession(connector=bot.qrm.connector)
@commands.command(name="hamstudy", aliases=["rq", "randomquestion", "randomq"], category=cmn.cat.study) @commands.command(name="hamstudy", aliases=["rq", "randomquestion", "randomq"], category=cmn.cat.study)
async def _random_question(self, ctx: commands.Context, country: str = "", level: str = ""): async def _random_question(self, ctx: commands.Context, country: str = "", level: str = "", element: str = ""):
"""Gets a random question from [HamStudy's](https://hamstudy.org) question pools.""" """Gets a random question from [HamStudy's](https://hamstudy.org) question pools."""
with ctx.typing(): with ctx.typing():
embed = cmn.embed_factory(ctx) embed = cmn.embed_factory(ctx)
country = country.lower() country = country.lower()
level = level.lower() level = level.lower()
element = element.upper()
if country in study.pool_names.keys(): if country in study.pool_names.keys():
if level in study.pool_names[country].keys(): if level in study.pool_names[country].keys():
@ -115,7 +116,19 @@ class StudyCog(commands.Cog):
pool = json.loads(await resp.read())["pool"] pool = json.loads(await resp.read())["pool"]
# Select a question # Select a question
pool_section = random.choice(pool)["sections"] 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"]
pool_questions = random.choice(pool_section)["questions"] pool_questions = random.choice(pool_section)["questions"]
question = random.choice(pool_questions) question = random.choice(pool_questions)