async def skipsong(self, ctx): logger.info("command: skipsong") await channel_setup(ctx) await user_setup(ctx) database.zadd("streak:global", {str(ctx.author.id): 0}) currentSongBird = str(database.hget(f"channel:{str(ctx.channel.id)}", "sBird"))[2:-1] database.hset(f"channel:{str(ctx.channel.id)}", "sBird", "") database.hset(f"channel:{str(ctx.channel.id)}", "sAnswered", "1") if currentSongBird != "": # check if there is bird birdPage = wikipedia.page(f"{currentSongBird} (bird)") await ctx.send(f"Ok, skipping {currentSongBird.lower()}") await ctx.send(birdPage.url if not database.exists(f"race.data:{str(ctx.channel.id)}") else f"<{birdPage.url}>") # sends wiki page if database.exists(f"race.data:{str(ctx.channel.id)}") and str( database.hget(f"race.data:{str(ctx.channel.id)}", "media"))[2:-1] == "song": limit = int(database.hget(f"race.data:{str(ctx.channel.id)}", "limit")) first = database.zrevrange(f"race.scores:{str(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("auto sending next bird song") addon, bw = map(str, database.hmget(f"race.data:{str(ctx.channel.id)}", ["addon", "bw"])) birds = self.bot.get_cog("Birds") await birds.send_bird_(ctx, addon[2:-1], bw[2:-1]) else: await ctx.send("You need to ask for a bird first!")
async def stop_race_(self, ctx): first = database.zrevrange(f"race.scores:{str(ctx.channel.id)}", 0, 0, True)[0] if ctx.guild is not None: user = ctx.guild.get_member(int(first[0])) else: user = None if user is None: user = self.bot.get_user(int(first[0])) if user is None: user = "******" else: user = f"{user.name}#{user.discriminator}" else: user = f"{user.name}#{user.discriminator} ({str(user.mention)})" await ctx.send(f"**Congratulations, {user}!**\n" + f"You have won the race by correctly identifying `{str(int(first[1]))}` birds. " + "*Way to go!*") database.hset(f"race.data:{str(ctx.channel.id)}", "stop", round(time.time())) await self._send_stats(ctx, "**Race stopped.**") database.delete(f"race.data:{str(ctx.channel.id)}") database.delete(f"race.scores:{str(ctx.channel.id)}")
async def check(self, ctx, *, arg): logger.info("command: check") await channel_setup(ctx) await user_setup(ctx) currentBird = str( database.hget(f"channel:{str(ctx.channel.id)}", "bird"))[2:-1] if currentBird == "": # no bird await ctx.send("You must ask for a bird first!") else: # if there is a bird, it checks answer logger.info("currentBird: " + str(currentBird.lower().replace("-", " "))) logger.info("args: " + str(arg.lower().replace("-", " "))) await bird_setup(ctx, currentBird) sciBird = await get_sciname(currentBird) if spellcheck(arg, currentBird) is True or spellcheck( arg, sciBird) is True: logger.info("correct") database.zincrby("streak:global", 1, str(ctx.author.id)) database.hset(f"channel:{str(ctx.channel.id)}", "bird", "") database.hset(f"channel:{str(ctx.channel.id)}", "answered", "1") if database.exists(f"session.data:{ctx.author.id}"): logger.info("session active") session_increment(ctx, "correct", 1) # check if streak is greater than max, if so, increases max if database.zscore("streak:global", str( ctx.author.id)) > database.zscore( "streak.max:global", str(ctx.author.id)): database.zadd( "streak.max:global", { str(ctx.author.id): database.zscore("streak:global", str( ctx.author.id)) }) await ctx.send("Correct! Good job!" if not database.exists( f"race.data:{str(ctx.channel.id)}" ) else f"**{str(ctx.author.mention)}**, you are correct!") page = wikipedia.page(f"{currentBird} (bird)") await ctx.send(page.url if not database.exists( f"race.data:{str(ctx.channel.id)}") else f"<{page.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 = 'achievements/' + number + ".PNG" with open(filename, 'rb') as img: await ctx.send( file=discord.File(img, filename="award.png")) if database.exists(f"race.data:{str(ctx.channel.id)}") and str( database.hget(f"race.data:{str(ctx.channel.id)}", "media"))[2:-1] == "image": limit = int( database.hget(f"race.data:{str(ctx.channel.id)}", "limit")) first = database.zrevrange( f"race.scores:{str(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("auto sending next bird image") addon, bw = map( str, database.hmget(f"race.data:{str(ctx.channel.id)}", ["addon", "bw"])) birds = self.bot.get_cog("Birds") await birds.send_bird_(ctx, addon[2:-1], bw[2:-1]) else: logger.info("incorrect") database.zadd("streak:global", {str(ctx.author.id): 0}) if database.exists(f"session.data:{str(ctx.author.id)}"): logger.info("session active") session_increment(ctx, "incorrect", 1) incorrect_increment(ctx, str(currentBird), 1) if database.exists(f"race.data:{str(ctx.channel.id)}"): await ctx.send("Sorry, that wasn't the right answer.") else: database.hset(f"channel:{str(ctx.channel.id)}", "bird", "") database.hset(f"channel:{str(ctx.channel.id)}", "answered", "1") await ctx.send("Sorry, the bird was actually " + currentBird.lower() + ".") page = wikipedia.page(f"{currentBird} (bird)") await ctx.send(page.url)
async def leaderboard(self, ctx, scope="", page=1): logger.info("command: leaderboard") await channel_setup(ctx) await user_setup(ctx) try: page = int(scope) except ValueError: if scope is "": scope = "global" scope = scope.lower() else: scope = "global" logger.info(f"scope: {scope}") logger.info(f"page: {page}") if not scope in ("global", "server", "g", "s"): logger.info("invalid scope") await ctx.send(f"**{scope} is not a valid scope!**\n*Valid Scopes:* `global, server`") return if page < 1: logger.info("invalid page") await ctx.send("Not a valid number. Pick a positive integer!") return database_key = "" if scope in ("server", "s"): if ctx.guild is not None: database_key = f"users.server:{ctx.guild.id}" scope = "server" else: logger.info("dm context") await ctx.send("**Server scopes are not avaliable in DMs.**\n*Showing global leaderboard instead.*") scope = "global" database_key = "users:global" else: database_key = "users:global" scope = "global" user_amount = int(database.zcard(database_key)) page = (page * 10) - 10 if user_amount is 0: logger.info(f"no users in {database_key}") await ctx.send("There are no users in the database.") return if page > user_amount: page = user_amount - (user_amount % 10) leaderboard_list = database.zrevrangebyscore(database_key, "+inf", "-inf", page, 10, True) embed = discord.Embed(type="rich", colour=discord.Color.blurple()) embed.set_author(name="Bird ID - An Ornithology Bot") leaderboard = "" for i, stats in enumerate(leaderboard_list): if ctx.guild is not None: user = ctx.guild.get_member(int(stats[0])) else: user = None if user is None: user = self.bot.get_user(int(stats[0])) if user is None: user = "******" else: user = f"**{user.name}#{user.discriminator}**" else: user = f"**{user.name}#{user.discriminator}** ({str(user.mention)})" leaderboard += f"{str(i+1+page)}. {user} - {str(int(stats[1]))}\n" embed.add_field(name=f"Leaderboard ({scope})", value=leaderboard, inline=False) if database.zscore(database_key, str(ctx.author.id)) is not None: placement = int(database.zrevrank(database_key, str(ctx.author.id))) + 1 distance = int(database.zrevrange(database_key, placement-2, placement-2, True)[0 ][1]) - int(database.zscore(database_key, str(ctx.author.id))) if placement is 1: embed.add_field(name="You:", value=f"You are #{str(placement)} on the leaderboard.\n" + f"You are in first place.", inline=False) elif distance is 0: embed.add_field(name="You:", value=f"You are #{str(placement)} on the leaderboard.\n" + f"You are tied with #{str(placement-1)}", inline=False) else: embed.add_field(name="You:", value=f"You are #{str(placement)} on the leaderboard.\n" + f"You are {str(distance)} away from #{str(placement-1)}", inline=False) else: embed.add_field(name="You:", value="You haven't answered any correctly.") await ctx.send(embed=embed)