Exemple #1
0
 async def tea(self, ctx, user: discord.User = None):
     if user is None:
         self.check_existing_user(ctx.message.author)
         user_data = query.get_user(ctx.message.author.id)
         if user_data[ctx.message.author.id]['tea'] == 1:
             await ctx.send(ctx.message.author.display_name +
                            ', You have 1 cup of :tea:.')
         else:
             await ctx.send(ctx.message.author.display_name +
                            ', You have ' +
                            str(user_data[ctx.message.author.id]['tea']) +
                            ' cups of :tea:.')
         return
     if user.id == ctx.message.author.id:
         await ctx.send(ctx.message.author.display_name +
                        ', Sorry, cannot send :tea: to yourself!')
         return
     elif user.id == self.bot.user.id:
         await ctx.send(ctx.message.author.display_name +
                        ', Thanks for the :tea:!')
         return
     self.check_existing_user(user)
     user_data = query.get_user(user.id)
     query.update_user(user.id, 'tea', user_data[user.id]['tea'] + 1)
     await ctx.send(ctx.message.author.display_name +
                    ', sent a cup of :tea: to ' + user.mention)
Exemple #2
0
 async def submit(self, ctx, problem, lang, *, source=None):
     if ctx.message.author.id not in self.dmoj_sessions.keys():
         prefix = await self.bot.command_prefix(self.bot, ctx.message)
         await ctx.send(ctx.message.author.display_name + ', You are not logged in to a DMOJ account with submission permissions (this could happen if you last logged in a long time ago or have recently gone offline). Please use command `%sconnect dmoj <token>` (your DMOJ API token can be found by going to https://dmoj.ca/edit/profile/ and selecting the __Generate__ or __Regenerate__ option next to API Token). Note: The connect command will ONLY WORK IN DIRECT MESSAGE. Please do not share this token with anyone else.' % prefix)
         return
     user_session = self.dmoj_sessions[ctx.message.author.id]
     if not self.language.languageExists(lang):
         await ctx.send(ctx.message.author.display_name + ', That language is not available. The available languages are as followed: ```%s```' % ', '.join(self.language.getLanguages()))
         return
     try:
         if source is None and len(ctx.message.attachments) > 0:
             f = requests.get(ctx.message.attachments[0].url)
             source = f.content
         self.check_existing_user(ctx.message.author)
         user_data = query.get_user(ctx.message.author.id)
         if problem == '^' and user_data[ctx.message.author.id]['last_dmoj_problem'] is not None:
             problem = user_data[ctx.message.author.id]['last_dmoj_problem']
         id = user_session.submit(problem, self.language.getId(lang), source)
         response = user_session.getTestcaseStatus(id)
         responseText = str(response)
         if len(responseText) > 1950:
             responseText = responseText[1950:] + '\n(Result cut off to fit message length limit)'
         await ctx.send(ctx.message.author.display_name + ', ' + responseText + '\nTrack your submission here: https://dmoj.ca/submission/' + str(id))
     except InvalidSessionException:
         await ctx.send(ctx.message.author.display_name + ', Failed to connect, or problem not available. Make sure you are submitting to a valid problem, check your authentication, and try again.')
     except:
         await ctx.send(ctx.message.author.display_name + ', Error submitting to the problem. Report this using command `$suggest Submission to DMOJ failed`.')
Exemple #3
0
 async def user(self, ctx, user: discord.User = None):
     if user is None:
         user = ctx.message.author
     self.check_existing_user(user)
     user_data = query.get_user(user.id)
     embed = discord.Embed(title=user.display_name)
     embed.timestamp = datetime.utcnow()
     empty = True
     if user_data[user.id]['dmoj'] is not None:
         embed.add_field(name='DMOJ',
                         value='https://dmoj.ca/user/%s' %
                         user_data[user.id]['dmoj'],
                         inline=False)
         empty = False
     if user_data[user.id]['codeforces'] is not None:
         embed.add_field(name='Codeforces',
                         value='https://codeforces.com/profile/%s' %
                         user_data[user.id]['codeforces'],
                         inline=False)
         empty = False
     if user_data[user.id]['country'] is not None:
         embed.add_field(name='Country',
                         value=str(Country(user_data[user.id]['country'])),
                         inline=False)
         empty = False
     if empty:
         embed.description = 'No accounts linked...'
     await ctx.send('Requested profile by ' +
                    ctx.message.author.display_name,
                    embed=embed)
Exemple #4
0
 async def togglecountry(self, ctx, code=''):
     try:
         country_object = Country(code)
         self.check_existing_user(ctx.message.author)
         user_data = query.get_user(ctx.message.author.id)
         prev_country = user_data[ctx.message.author.id]['country']
         user_data[
             ctx.message.author.id]['country'] = country_object.country
         query.update_user(ctx.message.author.id, 'country',
                           user_data[ctx.message.author.id]['country'])
         if prev_country is not None and prev_country != country_object.country:
             await ctx.send(
                 ctx.message.author.display_name +
                 ', Changed your country from %s to %s.' %
                 (str(Country(prev_country)), str(country_object)))
         else:
             await ctx.send(ctx.message.author.display_name +
                            ', Set your country to %s.' %
                            str(country_object))
     except InvalidCountryException:
         prefix = await self.bot.command_prefix(self.bot, ctx.message)
         await ctx.send(
             ctx.message.author.display_name +
             ', Sorry, could not find that country. Search for a country using the name (e.g. `%stogglecountry Finland`, `%stogglecountry "United States"`) or the 2 character ISO code (e.g. `%stogglecountry FI`))'
             % (prefix, prefix, prefix))
Exemple #5
0
 async def togglesuggest(self, ctx):
     self.check_existing_user(ctx.message.author)
     user_data = query.get_user(ctx.message.author.id)
     for account in self.onlineJudges.accounts:
         if user_data[ctx.message.author.id][account] is not None:
             user_data[
                 ctx.message.author.id]['can_suggest'] = not user_data[
                     ctx.message.author.id]['can_suggest']
             query.update_user(
                 ctx.message.author.id, 'can_suggest',
                 user_data[ctx.message.author.id]['can_suggest'])
             prefix = await self.bot.command_prefix(self.bot, ctx.message)
             if user_data[ctx.message.author.id]['can_suggest']:
                 await ctx.send(
                     ctx.message.author.display_name +
                     ', random problems will now be suggested based on your existing solves'
                 )
             else:
                 await ctx.send(
                     ctx.message.author.display_name +
                     ', random problems will no longer be suggested based on your existing solves'
                 )
             return
     await ctx.send(ctx.message.author.display_name +
                    ', You are not linked to any accounts')
Exemple #6
0
async def on_member_join(member):
    global prefix
    join_message = query.get_join_message(member.guild.id)
    if not join_message:
        return
    this_user = query.get_user(member.id)
    if this_user == {}:
        query.insert_ignore_user(member.id)
        server_prefix = await prefix_from_guild(member.guild)
        await member.send('Hello, %s, and welcome to %s! The default prefix for this server is `%s`, but in direct messaging, use the prefix `%s`. It would seem that you have yet to join a server that has Practice Bot! Using Practice Bot, you can link your DMOJ or Codeforces account to your Discord account to perform different commands. You may use one of the following formats:\n\n*Please use connect commands in this direct message chat only!*\n\n`%sconnect dmoj <dmoj-api-token>` (your DMOJ API token can be found by going to https://dmoj.ca/edit/profile/ and selecting the __Generate__ or __Regenerate__ option next to API Token)\n\n`%sconnect cf <codeforces-handle>`\n\nUse `%shelp` to see a full list of commands and more details.' % (member.display_name, member.guild.name, server_prefix, prefix, prefix, prefix, prefix))
Exemple #7
0
    async def user(self, ctx, user: discord.User = None):
        if user is None:
            user = ctx.message.author

        self.check_existing_user(user)
        user_data = query.get_user(user.id)

        embed = discord.Embed(title=user.display_name)
        embed.timestamp = datetime.utcnow()
        embed.colour = discord.Colour(int('0000ff', 16))

        empty = True
        if user_data[user.id]['dmoj'] is not None:
            embed.add_field(name='DMOJ',
                            value='https://dmoj.ca/user/%s' %
                            user_data[user.id]['dmoj'],
                            inline=False)
            empty = False
        if user_data[user.id]['codeforces'] is not None:
            embed.add_field(name='Codeforces',
                            value='https://codeforces.com/profile/%s' %
                            user_data[user.id]['codeforces'],
                            inline=False)
            empty = False
        if user_data[user.id]['country'] is not None:
            embed.add_field(name='Country',
                            value=str(Country(user_data[user.id]['country'])),
                            inline=False)
        if empty:
            embed.description = 'No accounts linked...'
        elif user.id == ctx.message.author.id:
            embed.add_field(
                name='Can repeat',
                value=str(user_data[user.id]['can_repeat'] == 1) +
                ' (If true, problems you have already solved on sites where your account is linked will show up when you request for random problems)',
                inline=False)
            embed.add_field(
                name='Can suggest',
                value=str(user_data[user.id]['can_suggest'] == 1) +
                ' (If true, suggested problems based on your points on sites where your account is linked will show up when you request for random problems)',
                inline=False)
        await ctx.send('Requested profile by ' +
                       ctx.message.author.display_name,
                       embed=embed)
Exemple #8
0
    def get_random_problem(self, oj=None, points=None, maximum=None, iden=None):
        if oj is None:
            oj = rand.choice(self.onlineJudges.judges)

        oj = self.onlineJudges.get_oj(oj)
        
        if oj == 'cses' and points is not None:
            raise InvalidParametersException(cses=True)

        temp_dmoj_problems = {}
        temp_cf_problems = []
        user_data = query.get_user(iden)
        suggestions_on = False

        if iden is not None:
            suggestions_on = user_data[iden]['can_suggest'] and points is None and ((
                    oj == 'dmoj' and user_data[iden]['dmoj'] is not None
                ) or (
                    oj == 'codeforces' and user_data[iden]['codeforces'] is not None
                ))
            if suggestions_on:
                if oj == 'dmoj':
                    if iden not in self.dmoj_user_suggests.keys():
                        self.dmoj_user_suggests[iden] = DMOJUserSuggester(user_data[iden]['dmoj'])
                    points, maximum = self.dmoj_user_suggests[iden].get_pp_range()
                elif oj == 'codeforces':
                    if iden not in self.cf_user_suggests.keys():
                        self.cf_user_suggests[iden] = CodeforcesUserSuggester(user_data[iden]['codeforces'])
                    points, maximum = self.cf_user_suggests[iden].get_pp_range()
                
            if not user_data[iden]['can_repeat']:
                if oj == 'dmoj' and user_data[iden]['dmoj'] is not None:
                    user_response = json_get('https://dmoj.ca/api/user/info/%s' % user_data[iden]['dmoj'])
                    if user_response is not None:
                        if points is None:
                            for name, prob in list(self.dmoj_problems.items()):
                                if name not in user_response['solved_problems']:
                                    temp_dmoj_problems[name] = prob
                        else:
                            temp_dmoj_problems['dmoj'] = {}
                            for point in list(self.problems_by_points['dmoj']):
                                temp_dmoj_problems['dmoj'][point] = {}
                                for name, prob in list(self.problems_by_points['dmoj'][point].items()):
                                    if name not in user_response['solved_problems']:
                                        temp_dmoj_problems['dmoj'][point][name] = prob
                        if temp_dmoj_problems == {}:
                            raise InvalidParametersException()
                elif oj == 'codeforces' and user_data[iden]['codeforces'] is not None:
                    response = requests.get('https://codeforces.com/api/user.status?handle=' + user_data[iden]['codeforces'])
                    if response.status_code != 200 or response.json()['status'] != 'OK':
                        return None
                    solved = []
                    for sub in response.json()['result']:
                        if sub['verdict'] == 'OK':
                            if 'contestId' in sub['problem']:
                                solved.append((sub['problem']['contestId'], sub['problem']['index']))
                            elif 'problemsetName' in sub['problem']:
                                solved.append((sub['problem']['problemsetName'], sub['problem']['index']))
                    if points is None:
                        temp_cf_problems = list(filter(lambda prob: (prob.get('contestId', prob.get('problemsetName')), prob['index']) not in solved, self.cf_problems))
                    else:
                        temp_cf_problems = {'codeforces': {}}
                        for point in list(self.problems_by_points['codeforces']):
                            temp_cf_problems['codeforces'][point] = list(filter(lambda prob: (prob['contestId'], prob['index']) not in solved, self.problems_by_points['codeforces'][point]))
                    if temp_cf_problems == [] or (type(temp_cf_problems) is dict and temp_cf_problems['codeforces'] == {}):
                        raise InvalidParametersException()

        if temp_dmoj_problems != {}:
            problem_list = temp_dmoj_problems
        elif temp_cf_problems != []:
            problem_list = temp_cf_problems
        elif points is None:
            if oj == 'dmoj':
                problem_list = self.dmoj_problems
            elif oj == 'codeforces':
                problem_list = self.cf_problems
        else:
            problem_list = self.problems_by_points
                                    
        if points is not None:
            if not points.isdigit():
                raise InvalidQueryException()
            points = int(points)

        if maximum is not None:
            if not maximum.isdigit():
                raise InvalidQueryException()
            maximum = int(maximum)
            possibilities = []
            for point in list(problem_list[oj].keys()):
                if point >= points and point <= maximum:
                    possibilities.append(point)
            if len(possibilities) == 0:
                if suggestions_on and oj == 'dmoj':
                    while len(possibilities) == 0:
                        self.dmoj_user_suggests[iden].expand_pp_range()
                        points, maximum = map(int, self.dmoj_user_suggests[iden].get_pp_range())
                        for point in list(problem_list[oj].keys()):
                            if point >= points and point <= maximum:
                                possibilities.append(point)
                        if points <= 1 and maximum >= 50 and len(possibilities) == 0:
                            raise InvalidParametersException()
                elif suggestions_on and oj == 'codeforces':
                    while len(possibilities) == 0:
                        self.cf_user_suggests[iden].expand_pp_range()
                        points, maximum = map(int, self.cf_user_suggests[iden].get_pp_range())
                        for point in list(problem_list[oj].keys()):
                            if point >= points and point <= maximum:
                                possibilities.append(point)
                        if points <= 1 and maximum >= 50 and len(possibilities) == 0:
                            raise InvalidParametersException()
                else:
                    raise InvalidParametersException()
            points = rand.choice(possibilities)
            
        if oj == 'dmoj':
            if not self.dmoj_problems:
                raise OnlineJudgeHTTPException('DMOJ')
                
            if points is None:
                name, prob = rand.choice(list(problem_list.items()))
            elif points in problem_list['dmoj'] and len(problem_list['dmoj'][points]) > 0:
                name, prob = rand.choice(list(problem_list['dmoj'][points].items()))
            else:
                raise InvalidParametersException()
            if iden is not None:
                user_data[iden]['last_dmoj_problem'] = name
                query.update_user(iden, 'last_dmoj_problem', name)
            return self.embed_dmoj_problem(name, prob, suggestions_on)
            
        elif oj == 'codeforces':
            if not self.cf_problems:
                raise OnlineJudgeHTTPException('Codeforces')
                return
            if points is None:
                prob = rand.choice(problem_list)
            elif points in problem_list['codeforces']:
                prob = rand.choice(problem_list['codeforces'][points])
            else:
                raise InvalidParametersException()
            return self.embed_cf_problem(prob, suggestions_on)

        elif oj == 'atcoder':
            if not self.atcoder_problems:
                raise OnlineJudgeHTTPException('AtCoder')

            if points is None:
                prob = rand.choice(self.atcoder_problems)
            elif points in self.problems_by_points['atcoder']:
                prob = rand.choice(self.problems_by_points['atcoder'][points])
            else:
                raise InvalidParametersException()
            return self.embed_atcoder_problem(prob)

        # elif oj == 'peg':
        #     if not self.peg_problems:
        #         raise OnlineJudgeHTTPException('WCIPEG')
        #     if points is None:
        #         prob = rand.choice(list(self.peg_problems.values()))
        #     elif points in self.problems_by_points['peg']:
        #         prob = rand.choice(list(self.problems_by_points['peg'][points]))
        #     else:
        #         raise InvalidParametersException()
        #     return self.embed_peg_problem(prob)

        elif oj == 'cses':
            prob = rand.choice(list(self.cses_problems.values()))
            return self.embed_cses_problem(prob)

        elif oj == 'szkopul':
            prob = rand.choice(list(self.szkopul_problems.values()))
            return self.embed_szkopul_problem(prob)
        
        else:
            raise NoSuchOJException(oj)