def parse_answerline(answerline): formatted_answer = answerline.lower() main_answer = html_to_discord(re.split(r'[\[<]', formatted_answer)[0]) unformatted_main_answer = main_answer.replace('*', '').replace('_', '') correct_answers = re.findall(r'\*+.+?\*+', formatted_answer) correct_answers = [ a.lstrip().rstrip().lstrip('*').rstrip('*').strip('_') for a in correct_answers ] return formatted_answer, main_answer, unformatted_main_answer, correct_answers
async def read_tossup(self, ctx, session, delay=2): self.read_loop.cancel() session.session_state.position = 0 if delay != 0: await asyncio.sleep(delay) if await self.load_tossup(session): await self.start_read( ctx, html_to_discord( session.session_state.current_tossup[0].formatted_text), session)
async def send_question(self, channel, session): session_state = session.session_state if len(session_state.bonuses) <= 1: try: session_state.bonuses.extend( self.get_bonus_batch(session.command)) except Exception as e: await channel.send('Error: ' + str(e)) await channel.send('Stacktrace' + str(traceback.print_tb(e.__traceback__))) if session_state.current_bonus is None or session_state.bonus_parts_answered == len( session_state.current_bonus[1]): try: session_state.current_bonus = session_state.bonuses.pop() except IndexError as e: await channel.send('Search returned 0 results -- ending pk') await end(session.context) return session_state.bonuses_answered += 1 leadin_msg = discord.Embed(color=0x00ff00) name = session_state.current_bonus[2].name if name is None: name = 'unknown tournament' leadin_msg.add_field( name=f'{name}', value= f'{html_to_discord(session_state.current_bonus[0].leadin)}') leadin_msg.set_author( name=f' for {session.context.author.display_name}', icon_url=session.context.author.avatar_url) await channel.send(embed=leadin_msg) session_state.bonus_parts_answered = 0 bonus_part = discord.Embed(color=0x0000ff) bonus_part.add_field( name=str(session_state.bonus_parts_answered + 1), value=html_to_discord(session_state.current_bonus[1][ session_state.bonus_parts_answered].formatted_text)) bonus_part.set_author( name=f' for {session.context.author.display_name}', icon_url=session.context.author.avatar_url) await channel.send(embed=bonus_part)
def check_power(session): tossup_markdown = html_to_discord( session.session_state.current_tossup[0].formatted_text) return self.fix_markdown(' '.join( tossup_markdown.split(' ') [:session.session_state.position])).endswith('**')
async def on_message(self, message): if get_global_state( ).skip_message is not None and message.id == get_global_state( ).skip_message.id: # skip it, it was used for a bot command return if message.content.startswith('_'): # skip skip return def check_power(session): tossup_markdown = html_to_discord( session.session_state.current_tossup[0].formatted_text) return self.fix_markdown(' '.join( tossup_markdown.split(' ') [:session.session_state.position])).endswith('**') session = get_session(message.author, TkSessionState) if session is not None: if session.session_state.prompting: session.session_state.prompting = False if message.content.lower().startswith('y'): if check_power(session): session.session_state.user_stats[ message.author.id]['points'] += 15 session.session_state.user_stats[ message.author.id]['powers'] += 1 else: session.session_state.user_stats[ message.author.id]['points'] += 10 session.session_state.user_stats[ message.author.id]['tens'] += 1 else: session.session_state.user_stats[ message.author.id]['incorrect'] += 1 session.session_state.user_stats[ message.author.id]['tossups_heard'] += 1 await self.read_tossup(session.context, session, delay=2) else: if session.session_state.lockout is not None: session.session_state.lockout = None else: session.session_state.lockout = message self.read_loop.cancel() buzz_notification = discord.Embed(color=0xff00ff) buzz_notification.set_author( name=f'{session.context.author.display_name}', icon_url=session.context.author.avatar_url) buzz_notification.add_field( name='Buzz', value=f'goes to {session.context.author.display_name}') await session.context.send(embed=buzz_notification) return given_answer = message.content answerline = session.session_state.current_tossup[ 0].formatted_answer correctness, formatted_answer, main_answer, unformatted_main_answer, correct_answers = validate( given_answer, answerline) if correctness == Correctness.INCORRECT: session.session_state.user_stats[ message.author.id]['incorrect'] += 1 session.session_state.user_stats[ message.author.id]['tossups_heard'] += 1 # break out early - definitely not the answer incorrect_msg = discord.Embed(color=0xff0000) wiki_search = hyperlink( html_to_discord(formatted_answer), get_wikipedia_search(unformatted_main_answer)) incorrect_msg.add_field(name='Sorry, incorrect', value=wiki_search) incorrect_msg.set_author( name=f' for {session.context.author.display_name}', icon_url=session.context.author.avatar_url) await message.channel.send(embed=incorrect_msg) await self.read_tossup(session.context, session, delay=2) elif correctness == Correctness.CORRECT: if check_power(session): session.session_state.user_stats[ message.author.id]['points'] += 15 session.session_state.user_stats[ message.author.id]['powers'] += 1 else: session.session_state.user_stats[ message.author.id]['points'] += 10 session.session_state.user_stats[ message.author.id]['tens'] += 1 session.session_state.user_stats[ message.author.id]['tossups_heard'] += 1 correct_msg = discord.Embed(color=0x0000ff) wiki_search = hyperlink( html_to_discord(formatted_answer), get_wikipedia_search(unformatted_main_answer)) correct_msg.add_field(name='Correct', value=wiki_search) correct_msg.set_author( name=f' for {session.context.author.display_name}', icon_url=session.context.author.avatar_url) await message.channel.send(embed=correct_msg) await self.read_tossup(session.context, session, delay=2) else: session.session_state.prompting = True # await message.channel.send( # f'DEBUG: correct_answers = {correct_answers}, similarities = {[fuzz.ratio(ans.lower(), given_answer.lower()) for ans in correct_answers]}') prompt_msg = discord.Embed(color=0xff0000) wiki_search = hyperlink( html_to_discord(formatted_answer), get_wikipedia_search(unformatted_main_answer)) prompt_msg.add_field(name='Were you correct? [y/n]', value=wiki_search) prompt_msg.set_author( name=f' for {session.context.author.display_name}', icon_url=session.context.author.avatar_url) await message.channel.send(embed=prompt_msg)
async def on_message(self, message): if get_global_state( ).skip_message is not None and message.id == get_global_state( ).skip_message.id: # skip it, it was used for a bot command return if message.content.startswith('_'): # skip skip return session = get_session(message.author, PkSessionState) if session is not None: if session.session_state.prompting: session.session_state.prompting = False if message.content.lower().startswith('y'): session.session_state.points += 10 if COLLECTING_TRAINING_DATA: answerline = html_to_discord( session.session_state.current_bonus[1][ session.session_state.bonus_parts_answered]. formatted_answer) correctness = CORRECTNESS_MAP[message.content.lower()] data = TrainingData(given_answer=self.given_answer, correctness=correctness, formatted_answer=answerline) if not hasattr(self, 'pkbot_session'): # initialize ourselves a session into db self.pkbot_session = get_pkbot_session() self.pkbot_session.add(data) self.pkbot_session.commit() message.content.lower() session.session_state.bonus_parts_answered += 1 await self.send_question(message.channel, session) else: self.given_answer = message.content answerline = html_to_discord(session.session_state.current_bonus[1]\ [session.session_state.bonus_parts_answered].formatted_answer) correctness, formatted_answer, main_answer, unformatted_main_answer, correct_answers = validate( self.given_answer, answerline) if COLLECTING_TRAINING_DATA: correctness = Correctness.PROMPT if correctness == Correctness.INCORRECT: session.session_state.bonus_parts_answered += 1 # break out early - definitely not the answer incorrect_msg = discord.Embed(color=0xff0000) wiki_search = hyperlink( html_to_discord(formatted_answer), get_wikipedia_search(unformatted_main_answer)) incorrect_msg.add_field(name='Sorry, incorrect', value=wiki_search) incorrect_msg.set_author( name=f' for {session.context.author.display_name}', icon_url=session.context.author.avatar_url) await message.channel.send(embed=incorrect_msg) await self.send_question(message.channel, session) return elif correctness == Correctness.CORRECT: session.session_state.points += 10 session.session_state.bonus_parts_answered += 1 correct_msg = discord.Embed(color=0x0000ff) wiki_search = hyperlink( html_to_discord(formatted_answer), get_wikipedia_search(unformatted_main_answer)) correct_msg.add_field(name='Correct', value=wiki_search) correct_msg.set_author( name=f' for {session.context.author.display_name}', icon_url=session.context.author.avatar_url) await message.channel.send(embed=correct_msg) await self.send_question(message.channel, session) else: session.session_state.prompting = True # await message.channel.send( # f'DEBUG: correct_answers = {correct_answers}, similarities = {[fuzz.ratio(ans.lower(), given_answer.lower()) for ans in correct_answers]}') prompt_msg = discord.Embed(color=0xff0000) wiki_search = hyperlink( html_to_discord(formatted_answer), get_wikipedia_search(unformatted_main_answer)) prompt_msg.add_field(name='Were you correct? [y/n/p]', value=wiki_search) if COLLECTING_TRAINING_DATA: prompt_msg.add_field(name='Bonus part id', value=session.session_state.current_bonus[1]\ [session.session_state.bonus_parts_answered].id) prompt_msg.set_author( name=f' for {session.context.author.display_name}', icon_url=session.context.author.avatar_url) await message.channel.send(embed=prompt_msg)