mirror of
https://github.com/miaowware/qrm2.git
synced 2024-10-31 14:27:11 -04:00
removed hamstudyanswer command and replaced it with using reactions to
answer questions. Fixes #169
This commit is contained in:
parent
b6f6d0408c
commit
09c58f9ba2
133
exts/study.py
133
exts/study.py
@ -10,15 +10,19 @@ General Public License, version 2.
|
|||||||
import random
|
import random
|
||||||
import json
|
import json
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
import asyncio
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
|
||||||
import discord.ext.commands as commands
|
import discord.ext.commands as commands
|
||||||
|
|
||||||
import common as cmn
|
import common as cmn
|
||||||
|
from resources import study
|
||||||
|
|
||||||
|
|
||||||
class StudyCog(commands.Cog):
|
class StudyCog(commands.Cog):
|
||||||
|
choices = {cmn.emojis.a: 'A', cmn.emojis.b: 'B', cmn.emojis.c: 'C', cmn.emojis.d: 'D'}
|
||||||
|
|
||||||
def __init__(self, bot: commands.Bot):
|
def __init__(self, bot: commands.Bot):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.lastq = dict()
|
self.lastq = dict()
|
||||||
@ -34,81 +38,46 @@ class StudyCog(commands.Cog):
|
|||||||
country = country.lower()
|
country = country.lower()
|
||||||
level = level.lower()
|
level = level.lower()
|
||||||
|
|
||||||
pool_names = {'us': {'technician': 'E2',
|
if country in study.pool_names.keys():
|
||||||
'tech': 'E2',
|
if level in study.pool_names[country].keys():
|
||||||
't': 'E2',
|
pool_name = study.pool_names[country][level]
|
||||||
'general': 'E3',
|
|
||||||
'gen': 'E3',
|
|
||||||
'g': 'E3',
|
|
||||||
'extra': 'E4',
|
|
||||||
'e': 'E4'},
|
|
||||||
'ca': {'basic': 'CA_B',
|
|
||||||
'b': 'CA_B',
|
|
||||||
'advanced': 'CA_A',
|
|
||||||
'adv': 'CA_A',
|
|
||||||
'a': 'CA_A',
|
|
||||||
'basic_fr': 'CA_FB',
|
|
||||||
'b_fr': 'CA_FB',
|
|
||||||
'base': 'CA_FB',
|
|
||||||
'advanced_fr': 'CA_FS',
|
|
||||||
'adv_fr': 'CA_FS',
|
|
||||||
'a_fr': 'CA_FS',
|
|
||||||
'supérieure': 'CA_FS',
|
|
||||||
'superieure': 'CA_FS',
|
|
||||||
's': 'CA_FS'},
|
|
||||||
'us_c': {'c1': 'C1',
|
|
||||||
'comm1': 'C1',
|
|
||||||
'c3': 'C3',
|
|
||||||
'comm3': 'C3',
|
|
||||||
'c6': 'C6',
|
|
||||||
'comm6': 'C6',
|
|
||||||
'c7': 'C7',
|
|
||||||
'comm7': 'C7',
|
|
||||||
'c7r': 'C7R',
|
|
||||||
'comm7r': 'C7R',
|
|
||||||
'c8': 'C8',
|
|
||||||
'comm8': 'C8',
|
|
||||||
'c9': 'C9',
|
|
||||||
'comm9': 'C9'}}
|
|
||||||
|
|
||||||
if country in pool_names.keys():
|
|
||||||
if level in pool_names[country].keys():
|
|
||||||
pool_name = pool_names[country][level]
|
|
||||||
|
|
||||||
elif level in ("random", "r"):
|
elif level in ("random", "r"):
|
||||||
# select a random level in that country
|
# select a random level in that country
|
||||||
pool_name = random.choice(list(pool_names[country].values()))
|
pool_name = random.choice(list(study.pool_names[country].values()))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# show list of possible pools
|
# show list of possible pools
|
||||||
embed.title = "Pool Not Found!"
|
embed.title = "Pool Not Found!"
|
||||||
embed.description = "Possible arguments are:"
|
embed.description = "Possible arguments are:"
|
||||||
embed.colour = cmn.colours.bad
|
embed.colour = cmn.colours.bad
|
||||||
for cty in pool_names:
|
for cty in study.pool_names:
|
||||||
levels = '`, `'.join(pool_names[cty].keys())
|
levels = '`, `'.join(study.pool_names[cty].keys())
|
||||||
embed.add_field(name=f"**Country: `{cty}`**", value=f"Levels: `{levels}`", inline=False)
|
embed.add_field(name=f"**Country: `{cty}` {study.pool_emojis[cty]}**", value=f"Levels: `{levels}`", inline=False)
|
||||||
|
embed.add_field(name="**Random**", value="To select a random pool or country, use `random` or `r`")
|
||||||
await ctx.send(embed=embed)
|
await ctx.send(embed=embed)
|
||||||
return
|
return
|
||||||
|
|
||||||
elif country in ("random", "r"):
|
elif country in ("random", "r"):
|
||||||
# select a random country and level
|
# select a random country and level
|
||||||
country = random.choice(list(pool_names.keys()))
|
country = random.choice(list(study.pool_names.keys()))
|
||||||
pool_name = random.choice(list(pool_names[country].values()))
|
pool_name = random.choice(list(study.pool_names[country].values()))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# show list of possible pools
|
# show list of possible pools
|
||||||
embed.title = "Pool Not Found!"
|
embed.title = "Pool Not Found!"
|
||||||
embed.description = "Possible arguments are:"
|
embed.description = "Possible arguments are:"
|
||||||
embed.colour = cmn.colours.bad
|
embed.colour = cmn.colours.bad
|
||||||
for cty in pool_names:
|
for cty in study.pool_names:
|
||||||
levels = '`, `'.join(pool_names[cty].keys())
|
levels = '`, `'.join(study.pool_names[cty].keys())
|
||||||
embed.add_field(name=f"**Country: `{cty}`**", value=f"Levels: `{levels}`", inline=False)
|
embed.add_field(name=f"**Country: `{cty}` {study.pool_emojis[cty]}**", value=f"Levels: `{levels}`", inline=False)
|
||||||
|
embed.add_field(name="**Random**", value="To select a random pool or country, use `random` or `r`")
|
||||||
await ctx.send(embed=embed)
|
await ctx.send(embed=embed)
|
||||||
return
|
return
|
||||||
|
|
||||||
pools = await self.hamstudy_get_pools()
|
pools = await self.hamstudy_get_pools()
|
||||||
|
|
||||||
pool_matches = [p for p in pools.keys() if p.startswith(pool_name)]
|
pool_matches = [p for p in pools.keys() if "_".join(p.split("_")[:-1]) == pool_name]
|
||||||
|
|
||||||
if len(pool_matches) > 0:
|
if len(pool_matches) > 0:
|
||||||
if len(pool_matches) == 1:
|
if len(pool_matches) == 1:
|
||||||
@ -127,12 +96,14 @@ class StudyCog(commands.Cog):
|
|||||||
embed.title = "Pool Not Found!"
|
embed.title = "Pool Not Found!"
|
||||||
embed.description = "Possible arguments are:"
|
embed.description = "Possible arguments are:"
|
||||||
embed.colour = cmn.colours.bad
|
embed.colour = cmn.colours.bad
|
||||||
for cty in pool_names:
|
for cty in study.pool_names:
|
||||||
levels = '`, `'.join(pool_names[cty].keys())
|
levels = '`, `'.join(study.pool_names[cty].keys())
|
||||||
embed.add_field(name=f"**Country: `{cty}`**", value=f"Levels: `{levels}`", inline=False)
|
embed.add_field(name=f"**Country: `{cty}` {study.pool_emojis[cty]}**", value=f"Levels: `{levels}`", inline=False)
|
||||||
|
embed.add_field(name="**Random**", value="To select a random pool or country, use `random` or `r`")
|
||||||
await ctx.send(embed=embed)
|
await ctx.send(embed=embed)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
pool_meta = pools[pool]
|
||||||
|
|
||||||
async with self.session.get(f'https://hamstudy.org/pools/{pool}') as resp:
|
async with self.session.get(f'https://hamstudy.org/pools/{pool}') as resp:
|
||||||
if resp.status != 200:
|
if resp.status != 200:
|
||||||
@ -148,9 +119,8 @@ class StudyCog(commands.Cog):
|
|||||||
pool_questions = random.choice(pool_section)['questions']
|
pool_questions = random.choice(pool_section)['questions']
|
||||||
question = random.choice(pool_questions)
|
question = random.choice(pool_questions)
|
||||||
|
|
||||||
embed.title = question['id']
|
embed.title = f"{study.pool_emojis[country]} {pool_meta['class']} {question['id']}"
|
||||||
embed.description = self.source
|
embed.description = self.source
|
||||||
embed.colour = cmn.colours.good
|
|
||||||
embed.add_field(name='Question:', value=question['text'], inline=False)
|
embed.add_field(name='Question:', value=question['text'], inline=False)
|
||||||
embed.add_field(name='Answers:',
|
embed.add_field(name='Answers:',
|
||||||
value=(f"**{cmn.emojis.a}** {question['answers']['A']}"
|
value=(f"**{cmn.emojis.a}** {question['answers']['A']}"
|
||||||
@ -158,11 +128,13 @@ class StudyCog(commands.Cog):
|
|||||||
f"\n**{cmn.emojis.c}** {question['answers']['C']}"
|
f"\n**{cmn.emojis.c}** {question['answers']['C']}"
|
||||||
f"\n**{cmn.emojis.d}** {question['answers']['D']}"),
|
f"\n**{cmn.emojis.d}** {question['answers']['D']}"),
|
||||||
inline=False)
|
inline=False)
|
||||||
embed.add_field(name='Answer:', value='Type _?rqa_ for answer', inline=False)
|
embed.add_field(name='To Answer:',
|
||||||
|
value='Answer with reactions below. If not answered within 10 minutes, the answer will be revealed.',
|
||||||
|
inline=False)
|
||||||
if 'image' in question:
|
if 'image' in question:
|
||||||
image_url = f'https://hamstudy.org/_1330011/images/{pool.split("_",1)[1]}/{question["image"]}'
|
image_url = f'https://hamstudy.org/_1330011/images/{pool.split("_",1)[1]}/{question["image"]}'
|
||||||
embed.set_image(url=image_url)
|
embed.set_image(url=image_url)
|
||||||
self.lastq[ctx.message.channel.id] = (question['id'], question['answer'])
|
|
||||||
q_msg = await ctx.send(embed=embed)
|
q_msg = await ctx.send(embed=embed)
|
||||||
|
|
||||||
await cmn.add_react(q_msg, cmn.emojis.a)
|
await cmn.add_react(q_msg, cmn.emojis.a)
|
||||||
@ -170,32 +142,26 @@ class StudyCog(commands.Cog):
|
|||||||
await cmn.add_react(q_msg, cmn.emojis.c)
|
await cmn.add_react(q_msg, cmn.emojis.c)
|
||||||
await cmn.add_react(q_msg, cmn.emojis.d)
|
await cmn.add_react(q_msg, cmn.emojis.d)
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
@commands.command(name="hamstudyanswer", aliases=['rqa', 'randomquestionanswer', 'randomqa', 'hamstudya'], category=cmn.cat.study)
|
try:
|
||||||
async def _q_answer(self, ctx: commands.Context, answer: str = None):
|
reaction, user = await self.bot.wait_for('reaction_add', timeout=600.0, check=check)
|
||||||
'''Returns the answer to question last asked (Optional argument: your answer).'''
|
except asyncio.TimeoutError:
|
||||||
with ctx.typing():
|
embed.remove_field(2)
|
||||||
correct_ans = self.lastq[ctx.message.channel.id][1]
|
embed.add_field(name="Answer:", value=f"Timed out! The correct answer was **{question['answer']}**.")
|
||||||
q_num = self.lastq[ctx.message.channel.id][0]
|
await q_msg.edit(embed=embed)
|
||||||
embed = cmn.embed_factory(ctx)
|
else:
|
||||||
if answer is not None:
|
if self.choices[str(reaction.emoji)] == question['answer']:
|
||||||
answer = answer.upper()
|
embed.remove_field(2)
|
||||||
if answer == correct_ans:
|
embed.add_field(name="Answer:", value=f"Correct! The answer was **{question['answer']}**.")
|
||||||
result = f'Correct! The answer to {q_num} was **{correct_ans}**.'
|
|
||||||
embed.title = f'{q_num} Answer'
|
|
||||||
embed.description = f'{self.source}\n\n{result}'
|
|
||||||
embed.colour = cmn.colours.good
|
embed.colour = cmn.colours.good
|
||||||
|
await q_msg.edit(embed=embed)
|
||||||
else:
|
else:
|
||||||
result = f'Incorrect. The answer to {q_num} was **{correct_ans}**, not **{answer}**.'
|
embed.remove_field(2)
|
||||||
embed.title = f'{q_num} Answer'
|
embed.add_field(name="Answer:", value=f"Incorrect! The correct answer was **{question['answer']}**.")
|
||||||
embed.description = f'{self.source}\n\n{result}'
|
|
||||||
embed.colour = cmn.colours.bad
|
embed.colour = cmn.colours.bad
|
||||||
else:
|
await q_msg.edit(embed=embed)
|
||||||
result = f'The correct answer to {q_num} was **{correct_ans}**.'
|
|
||||||
embed.title = f'{q_num} Answer'
|
|
||||||
embed.description = f'{self.source}\n\n{result}'
|
|
||||||
embed.colour = cmn.colours.neutral
|
|
||||||
await ctx.send(embed=embed)
|
|
||||||
|
|
||||||
async def hamstudy_get_pools(self):
|
async def hamstudy_get_pools(self):
|
||||||
async with self.session.get('https://hamstudy.org/pools/') as resp:
|
async with self.session.get('https://hamstudy.org/pools/') as resp:
|
||||||
@ -204,11 +170,10 @@ class StudyCog(commands.Cog):
|
|||||||
else:
|
else:
|
||||||
pools_dict = json.loads(await resp.read())
|
pools_dict = json.loads(await resp.read())
|
||||||
|
|
||||||
pools_list = []
|
pools = dict()
|
||||||
for l in pools_dict.values():
|
for ls in pools_dict.values():
|
||||||
pools_list += l
|
for pool in ls:
|
||||||
|
pools[pool["id"]] = pool
|
||||||
pools = {p["id"]: p for p in pools_list}
|
|
||||||
|
|
||||||
return pools
|
return pools
|
||||||
|
|
||||||
|
49
resources/study.py
Normal file
49
resources/study.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
"""
|
||||||
|
A listing of hamstudy command resources
|
||||||
|
---
|
||||||
|
Copyright (C) 2019-2020 Abigail Gold, 0x5c
|
||||||
|
|
||||||
|
This file is part of discord-qrmbot and is released under the terms of the GNU
|
||||||
|
General Public License, version 2.
|
||||||
|
"""
|
||||||
|
|
||||||
|
pool_names = {'us': {'technician': 'E2',
|
||||||
|
'tech': 'E2',
|
||||||
|
't': 'E2',
|
||||||
|
'general': 'E3',
|
||||||
|
'gen': 'E3',
|
||||||
|
'g': 'E3',
|
||||||
|
'extra': 'E4',
|
||||||
|
'e': 'E4'},
|
||||||
|
'ca': {'basic': 'CA_B',
|
||||||
|
'b': 'CA_B',
|
||||||
|
'advanced': 'CA_A',
|
||||||
|
'adv': 'CA_A',
|
||||||
|
'a': 'CA_A',
|
||||||
|
'basic_fr': 'CA_FB',
|
||||||
|
'b_fr': 'CA_FB',
|
||||||
|
'base': 'CA_FB',
|
||||||
|
'advanced_fr': 'CA_FS',
|
||||||
|
'adv_fr': 'CA_FS',
|
||||||
|
'a_fr': 'CA_FS',
|
||||||
|
'supérieure': 'CA_FS',
|
||||||
|
'superieure': 'CA_FS',
|
||||||
|
's': 'CA_FS'},
|
||||||
|
'us_c': {'c1': 'C1',
|
||||||
|
'comm1': 'C1',
|
||||||
|
'c3': 'C3',
|
||||||
|
'comm3': 'C3',
|
||||||
|
'c6': 'C6',
|
||||||
|
'comm6': 'C6',
|
||||||
|
'c7': 'C7',
|
||||||
|
'comm7': 'C7',
|
||||||
|
'c7r': 'C7R',
|
||||||
|
'comm7r': 'C7R',
|
||||||
|
'c8': 'C8',
|
||||||
|
'comm8': 'C8',
|
||||||
|
'c9': 'C9',
|
||||||
|
'comm9': 'C9'}}
|
||||||
|
|
||||||
|
pool_emojis = {'us': '🇺🇸',
|
||||||
|
'ca': '🇨🇦',
|
||||||
|
'us_c': '🇺🇸 🏢'}
|
Loading…
Reference in New Issue
Block a user