hamstudy improvements (#348)

- added orange colour for timeouts
- added emojis in the last field
- made the correct answer bold after answering/timeout
- minor code optimisations
- added ability to react ? to get the answer

fixes #347
This commit is contained in:
classabbyamp 2021-03-06 23:39:50 -05:00 committed by GitHub
commit 351b1e2d21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 26 deletions

View File

@ -9,11 +9,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- MUF and foF2 maps from [prop.kc2g.com](https://prop.kc2g.com/).
- Commands to show METAR (`?metar`) and TAF (`?taf`) (aeronautical weather conditions).
- The ability to select an element of a pool in `?hamstudy`.
- The ability to answer ❓ to a HamStudy question to get the answer.
### Changed
- New colour theme for `?greyline`.
- Moved great circle distance and bearing calculation from `?ungrid` to `?griddistance`.
- `?ungrid` to `?latlong`.
- Renamed `?cond` to `?solar`.
- Reduced `?hamstudy` timeout to 5 minutes.
### Fixed
- Weird image caching situation for `?greyline` on Discord's side.
### Deprecated

View File

@ -36,6 +36,7 @@ colours = SimpleNamespace(
good=0x43B581,
neutral=0x7289DA,
bad=0xF04747,
timeout=0xF26522,
)
# meow
@ -56,10 +57,12 @@ emojis = SimpleNamespace(
question="",
no_entry="",
bangbang="‼️",
clock="",
a="🇦",
b="🇧",
c="🇨",
d="🇩",
e="🇪",
)
paths = SimpleNamespace(

View File

@ -22,7 +22,8 @@ from resources import study
class StudyCog(commands.Cog):
choices = {cmn.emojis.a: "A", cmn.emojis.b: "B", cmn.emojis.c: "C", cmn.emojis.d: "D"}
choices = {"A": cmn.emojis.a, "B": cmn.emojis.b, "C": cmn.emojis.c, "D": cmn.emojis.d, "E": cmn.emojis.e}
choices_inv = {y: x for x, y in choices.items()}
def __init__(self, bot: commands.Bot):
self.bot = bot
@ -131,18 +132,22 @@ class StudyCog(commands.Cog):
pool_section = random.choice(pool)["sections"]
pool_questions = random.choice(pool_section)["questions"]
question = random.choice(pool_questions)
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"
embed.title = f"{study.pool_emojis[country]} {pool_meta['class']} {question['id']}"
embed.description = self.source
embed.add_field(name="Question:", value=question["text"], inline=False)
embed.add_field(name="Answers:",
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)
embed.add_field(name="To Answer:",
value=("Answer with reactions below. If not answered within 10 minutes,"
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",
value=("Answer with reactions below. If not answered within 5 minutes,"
" the answer will be revealed."),
inline=False)
if "image" in question:
@ -151,33 +156,61 @@ class StudyCog(commands.Cog):
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)
for i in range(len(answers)):
await cmn.add_react(q_msg, list(self.choices.values())[i])
await cmn.add_react(q_msg, cmn.emojis.question)
def check(reaction, user):
return (user.id != self.bot.user.id
and reaction.message.id == q_msg.id
and str(reaction.emoji) in self.choices.keys())
and (str(reaction.emoji) in self.choices.values() or str(reaction.emoji) == cmn.emojis.question))
try:
reaction, user = await self.bot.wait_for("reaction_add", timeout=600.0, check=check)
reaction, user = await self.bot.wait_for("reaction_add", timeout=300.0, check=check)
except asyncio.TimeoutError:
embed.remove_field(2)
embed.add_field(name="Answer:", value=f"Timed out! The correct answer was **{question['answer']}**.")
embed.set_field_at(1, name="Answers", value=answers_str_bolded, inline=False)
embed.set_field_at(2, name="Answer",
value=(f"{cmn.emojis.clock} "
f"**Timed out!** The correct answer was {self.choices[question['answer']]}"))
embed.colour = cmn.colours.timeout
await q_msg.edit(embed=embed)
else:
if self.choices[str(reaction.emoji)] == question["answer"]:
embed.remove_field(2)
embed.add_field(name="Answer:", value=f"Correct! The answer was **{question['answer']}**.")
embed.colour = cmn.colours.good
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
await q_msg.edit(embed=embed)
else:
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)
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)
async def hamstudy_get_pools(self):
async with self.session.get("https://hamstudy.org/pools/") as resp: