async def update_web_user(request: Request, user_data: dict): logger.info("updating user data") session_id = get_session_id(request) user_id = str(user_data["id"]) database.hset(f"web.session:{session_id}", "user_id", user_id) database.expire(f"web.session:{session_id}", DATABASE_SESSION_USER_EXPIRE) database.hset( f"web.user:{user_id}", mapping={ "avatar_hash": str(user_data["avatar"]), "avatar_url": f"https://cdn.discordapp.com/avatars/{user_id}/{user_data['avatar']}.png", "username": str(user_data["username"]), "discriminator": str(user_data["discriminator"]), }, ) await user_setup(user_id) tempScore = int(database.hget(f"web.session:{session_id}", "tempScore")) if tempScore not in (0, -1): score_increment(user_id, tempScore) database.zincrby( f"daily.webscore:{str(datetime.datetime.now(datetime.timezone.utc).date())}", 1, user_id, ) database.hset(f"web.session:{session_id}", "tempScore", -1) logger.info("updated user data")
async def check(self, ctx, *, arg): logger.info("command: check") currentBird = database.hget(f"channel:{ctx.channel.id}", "bird").decode("utf-8") if currentBird == "": # no bird await ctx.send("You must ask for a bird first!") return # if there is a bird, it checks answer sciBird = (await get_sciname(currentBird)).lower().replace("-", " ") arg = arg.lower().replace("-", " ") currentBird = currentBird.lower().replace("-", " ") alpha_code = alpha_codes.get(string.capwords(currentBird)) logger.info("currentBird: " + currentBird) logger.info("arg: " + arg) bird_setup(ctx, currentBird) race_in_session = bool(database.exists(f"race.data:{ctx.channel.id}")) if race_in_session: logger.info("race in session") if database.hget(f"race.data:{ctx.channel.id}", "strict"): logger.info("strict spelling") correct = arg in (currentBird, sciBird) else: logger.info("spelling leniency") correct = spellcheck(arg, currentBird) or spellcheck( arg, sciBird) if not correct and database.hget(f"race.data:{ctx.channel.id}", "alpha"): logger.info("checking alpha codes") correct = arg.upper() == alpha_code else: logger.info("no race") if database.hget(f"session.data:{ctx.author.id}", "strict"): logger.info("strict spelling") correct = arg in (currentBird, sciBird) else: logger.info("spelling leniency") correct = (spellcheck(arg, currentBird) or spellcheck(arg, sciBird) or arg.upper() == alpha_code) if correct: logger.info("correct") database.hset(f"channel:{ctx.channel.id}", "bird", "") database.hset(f"channel:{ctx.channel.id}", "answered", "1") session_increment(ctx, "correct", 1) streak_increment(ctx, 1) database.zincrby(f"correct.user:{ctx.author.id}", 1, string.capwords(str(currentBird))) if (race_in_session and Filter.from_int( int(database.hget(f"race.data:{ctx.channel.id}", "filter"))).vc): await voice_functions.stop(ctx, silent=True) await ctx.send( f"Correct! Good job! The bird was **{currentBird}**." if not race_in_session else f"**{ctx.author.mention}**, you are correct! The bird was **{currentBird}**." ) url = get_wiki_url(ctx, currentBird) await ctx.send(url) score_increment(ctx, 1) if int(database.zscore("users:global", str(ctx.author.id))) in achievement: number = str( int(database.zscore("users:global", str(ctx.author.id)))) await ctx.send( f"Wow! You have answered {number} birds correctly!") filename = f"bot/media/achievements/{number}.PNG" with open(filename, "rb") as img: await ctx.send(file=discord.File(img, filename="award.png") ) if race_in_session: media = database.hget(f"race.data:{ctx.channel.id}", "media").decode("utf-8") limit = int( database.hget(f"race.data:{ctx.channel.id}", "limit")) first = database.zrevrange(f"race.scores:{ctx.channel.id}", 0, 0, True)[0] if int(first[1]) >= limit: logger.info("race ending") race = self.bot.get_cog("Race") await race.stop_race_(ctx) else: logger.info(f"auto sending next bird {media}") filter_int, taxon, state = database.hmget( f"race.data:{ctx.channel.id}", ["filter", "taxon", "state"]) birds = self.bot.get_cog("Birds") await birds.send_bird_( ctx, media, Filter.from_int(int(filter_int)), taxon.decode("utf-8"), state.decode("utf-8"), ) else: logger.info("incorrect") streak_increment(ctx, None) # reset streak session_increment(ctx, "incorrect", 1) incorrect_increment(ctx, str(currentBird), 1) if race_in_session: await ctx.send("Sorry, that wasn't the right answer.") else: database.hset(f"channel:{ctx.channel.id}", "bird", "") database.hset(f"channel:{ctx.channel.id}", "answered", "1") await ctx.send("Sorry, the bird was actually **" + currentBird + "**.") url = get_wiki_url(ctx, currentBird) await ctx.send(url)
async def check_bird(request: Request, guess: str): logger.info("endpoint: check bird") session_id = get_session_id(request) user_id = int(database.hget(f"web.session:{session_id}", "user_id")) currentBird = database.hget(f"web.session:{session_id}", "bird").decode("utf-8") if currentBird == "": # no bird logger.info("bird is blank") raise HTTPException(status_code=404, detail="Bird is blank") if guess == "": logger.info("empty guess") raise HTTPException(status_code=422, detail="empty guess") # if there is a bird, it checks answer sciBird = (await get_sciname(currentBird)).lower().replace("-", " ") guess = guess.lower().replace("-", " ") currentBird = currentBird.lower().replace("-", " ") alpha_code = alpha_codes.get(string.capwords(currentBird), "") logger.info("currentBird: " + currentBird) logger.info("args: " + guess) database.zincrby(f"daily.web:{date()}", 1, "check") if user_id != 0: bird_setup(user_id, currentBird) accepted_answers = [currentBird, sciBird] if currentBird == "screech owl": accepted_answers += screech_owls accepted_answers += sci_screech_owls if (better_spellcheck(guess, accepted_answers, birdListMaster + sciListMaster) or guess.upper() == alpha_code): logger.info("correct") database.hset(f"web.session:{session_id}", "bird", "") database.hset(f"web.session:{session_id}", "answered", "1") tempScore = int(database.hget(f"web.session:{session_id}", "tempScore")) if user_id != 0: database.zincrby(f"daily.webscore:{date()}", 1, user_id) score_increment(user_id, 1) streak_increment(user_id, 1) # elif tempScore >= 10: # logger.info("trial maxed") # raise HTTPException(status_code=403, detail="Sign in to continue") else: database.hset(f"web.session:{session_id}", "tempScore", str(tempScore + 1)) url = get_wiki_url(currentBird) return { "guess": guess, "answer": currentBird, "sciname": sciBird, "status": "correct", "wiki": url, } logger.info("incorrect") database.hset(f"web.session:{session_id}", "bird", "") database.hset(f"web.session:{session_id}", "answered", "1") database.zincrby("incorrect:global", 1, currentBird) if user_id != 0: incorrect_increment(user_id, currentBird, 1) streak_increment(user_id, None) # reset streak url = get_wiki_url(currentBird) return { "guess": guess, "answer": currentBird, "sciname": sciBird, "status": "incorrect", "wiki": url, }