Esempio n. 1
0
 async def subcategories(self, ctx, *args):
     state = get_global_state()
     state.skip_message = ctx.message
     def chunks(lst, n):
         for i in range(0, len(lst), n):
             yield lst[i:i + n]
     # note: Embeds have a max of 25 fields, and there are far more than 25 subcategories
     for chunk in chunks(list(get_subcategories().items()), 25):
         await ctx.send(embed=inverse_dictionary_to_embed('Subcategories', dict(chunk), ctx))
Esempio n. 2
0
async def end(ctx):
    state = get_global_state()
    state.skip_message = ctx.message

    # Don't end what a different cog is doing
    session = get_session(ctx.author)
    if session is not None:
        await stats(ctx)
        state.sessions.remove(session)
Esempio n. 3
0
 async def tk(self, ctx, *args):
     state = get_global_state()
     state.skip_message = ctx.message
     if all([
             ctx.author != session.context.author
             for session in state.sessions
     ]):
         new_session = Session(ctx, args, TkSessionState())
         state.sessions.append(new_session)
         await self.read_tossup(ctx, new_session)
     else:
         await ctx.send(f'{ctx.author} is already in a session')
Esempio n. 4
0
async def stats(ctx):
    get_global_state().skip_message = ctx.message
    session = get_session(ctx.author)
    if session is not None:
        stats_dictionary, session_to_db_fn = session.session_state.stats()
        msg = dictionary_to_embed('Stats', stats_dictionary, ctx)
        msg.add_field(name='Settings', value=str(session.command))
        await ctx.send(embed=msg)
        if session_to_db_fn is not None:
            db_object = session_to_db_fn(stats_dictionary, session)
            pkbot_session.add(db_object)
            pkbot_session.commit()
Esempio n. 5
0
def get_session(author, session_type=None):
    state = get_global_state()
    session = [
        session for session in state.sessions
        if session.context.author == author
    ]
    if session_type is not None:
        session = list(
            filter(lambda x: isinstance(x.session_state, session_type),
                   session))
    assert len(session) < 2
    if len(session) == 0:
        return None
    return session[0]
Esempio n. 6
0
 async def tournaments(self, ctx, *args):
     state = get_global_state()
     state.skip_message = ctx.message
     await ctx.send(embed=dictionary_to_embed('Tournaments', get_tournaments(), ctx))
Esempio n. 7
0
 async def categories(self, ctx, *args):
     state = get_global_state()
     state.skip_message = ctx.message
     await ctx.send(embed=inverse_dictionary_to_embed('Categories', get_categories(), ctx))
Esempio n. 8
0
    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)
Esempio n. 9
0
    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)