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 _get_options(self, ctx): bw, addon, state, order = database.hmget(f"session.data:{str(ctx.author.id)}", ["bw", "addon", "state", "order"]) options = str( f"**Age/Sex:** {str(addon)[2:-1] if addon else 'default'}\n" + f"**Black & White:** {bw==b'bw'}\n" + f"**State bird list:** {str(state)[2:-1] if state else 'None'}\n" + f"**Bird Order:** {str(order)[2:-1] if order else 'None'}\n" ) return options
async def _get_options(self, ctx): bw, addon, state, media, limit = database.hmget(f"race.data:{str(ctx.channel.id)}", ["bw", "addon", "state", "media", "limit"]) options = str( f"**Age/Sex:** {str(addon)[2:-1] if addon else 'default'}\n" + f"**Black & White:** {bw==b'bw'}\n" + f"**Special bird list:** {str(state)[2:-1] if state else 'None'}\n" + f"**Media Type:** {str(media)[2:-1]}\n" + f"**Amount to Win:** {str(limit)[2:-1]}\n" ) return options
async def _get_stats(self, ctx): start, correct, incorrect, total = map( int, database.hmget(f"session.data:{str(ctx.author.id)}", ["start", "correct", "incorrect", "total"])) elapsed = str(datetime.timedelta(seconds=round(time.time()) - start)) try: accuracy = round(100 * (correct / (correct + incorrect)), 2) except ZeroDivisionError: accuracy = 0 stats = str( f"**Duration:** `{elapsed}`\n" + f"**# Correct:** {correct}\n" + f"**# Incorrect:** {incorrect}\n" + f"**Total Birds:** {total}\n" + f"**Accuracy:** {accuracy}%\n" ) return stats
async def _send_stats(self, ctx): start, correct, incorrect, total = map( int, database.hmget(f"session.data:{str(ctx.author.id)}", ["start", "correct", "incorrect", "total"]) ) elapsed = str(datetime.timedelta(seconds=round(time.time()) - start)) try: accuracy = round(100 * (correct / (correct + incorrect)), 2) except ZeroDivisionError: accuracy = 0 await ctx.send( f"""**Session Stats:** *Duration:* {elapsed} *# Correct:* {correct} *# Incorrect:* {incorrect} *Total Fossils:* {total} *Accuracy:* {accuracy}%""" )
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 start(self, ctx, *, args_str: str = ""): logger.info("command: start race") await channel_setup(ctx) await user_setup(ctx) if ctx.guild is None: logger.info("dm context") await ctx.send("**Sorry, racing is not avaliable in DMs.**") return if not str(ctx.channel.name).startswith("racing"): logger.info("not race channel") await ctx.send("**Sorry, racing is not availiable in this channel.**\n" + "*Set the channel name to start with `racing` to enable it.*") return if database.exists(f"race.data:{str(ctx.channel.id)}"): logger.info("already race") await ctx.send("**There is already a race in session.** *Change settings/view stats with `b!race view`*") return else: args = args_str.split(" ") logger.info(f"args: {args}") if "bw" in args: bw = "bw" else: bw = "" states_args = set(states.keys()).intersection( {arg.upper() for arg in args}) if states_args: state = " ".join(states_args).strip() else: state = " ".join(check_state_role(ctx)) female = "female" in args or "f" in args juvenile = "juvenile" in args or "j" in args if female and juvenile: await ctx.send("**Juvenile females are not yet supported.**\n*Please try again*") return elif female: addon = "female" elif juvenile: addon = "juvenile" else: addon = "" song = "song" in args or "s" in args image = "image" in args or "i" in args or "picture" in args or "p" in args if song and image: await ctx.send("**Songs and images are not yet supported.**\n*Please try again*") return elif song: media = "song" elif image: media = "image" else: media = "image" ints = [] for n in args: try: ints.append(int(n)) except ValueError: continue if len(ints) is not 0: limit = int(ints[0]) else: limit = 10 if limit > 1000000: await ctx.send("**Sorry, the maximum amount to win is 1 million.**") limit = 1000000 logger.info( f"adding bw: {bw}; addon: {addon}; state: {state}; media: {media}; limit: {limit}") database.hmset( f"race.data:{str(ctx.channel.id)}", { "start": round(time.time()), "stop": 0, "limit": limit, "bw": bw, "state": state, "addon": addon, "media": media } ) database.zadd(f"race.scores:{str(ctx.channel.id)}", { str(ctx.author.id): 0}) await ctx.send(f"**Race started with options:**\n{await self._get_options(ctx)}") if str(database.hget(f"race.data:{str(ctx.channel.id)}", "media"))[2:-1] == "image": 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]) if str(database.hget(f"race.data:{str(ctx.channel.id)}", "media"))[2:-1] == "song": logger.info("auto sending next bird song") birds = self.bot.get_cog("Birds") await birds.send_song_(ctx)