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))
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)
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')
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)