async def send_song_(self, ctx): songAnswered = int( database.hget(f"channel:{ctx.channel.id}", "sAnswered")) # check to see if previous bird was answered if songAnswered: # if yes, give a new bird roles = check_state_role(ctx) if database.exists(f"session.data:{ctx.author.id}"): logger.info("session active") session_increment(ctx, "total", 1) roles = database.hget(f"session.data:{ctx.author.id}", "state").decode("utf-8").split(" ") if roles[0] == "": roles = [] if not roles: logger.info("no session lists") roles = check_state_role(ctx) logger.info(f"roles: {roles}") if roles: birds = list( itertools.chain.from_iterable(states[state]["songBirds"] for state in roles)) else: birds = songBirds logger.info(f"number of birds: {len(birds)}") currentSongBird = random.choice(birds) prevS = database.hget(f"channel:{ctx.channel.id}", "prevS").decode("utf-8") while currentSongBird == prevS and len(birds) > 1: currentSongBird = random.choice(birds) database.hset(f"channel:{ctx.channel.id}", "prevS", str(currentSongBird)) database.hset(f"channel:{ctx.channel.id}", "sBird", str(currentSongBird)) logger.info("currentSongBird: " + str(currentSongBird)) database.hset(f"channel:{ctx.channel.id}", "sAnswered", "0") await send_birdsong(ctx, currentSongBird, on_error=error_skip_song, message=SONG_MESSAGE) else: await send_birdsong(ctx, database.hget(f"channel:{ctx.channel.id}", "sBird").decode("utf-8"), on_error=error_skip_song, message=SONG_MESSAGE)
async def start(self, ctx, *, args_str: str = ""): logger.info("command: start session") await channel_setup(ctx) await user_setup(ctx) if database.exists(f"session.data:{ctx.author.id}"): logger.info("already session") await ctx.send("**There is already a session running.** *Change settings/view stats with `b!session edit`*") 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)) taxon_args = set(taxons.keys()).intersection({arg.lower() for arg in args}) if taxon_args: taxon = " ".join(taxon_args).strip() else: taxon = "" 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 = "" logger.info(f"adding bw: {bw}; addon: {addon}; state: {state}") database.hmset( f"session.data:{ctx.author.id}", { "start": round(time.time()), "stop": 0, "correct": 0, "incorrect": 0, "total": 0, "bw": bw, "state": state, "addon": addon, "taxon": taxon } ) await ctx.send(f"**Session started with options:**\n{await self._get_options(ctx)}")
async def parse(ctx, args_str: str): """Parse arguments for options.""" args = args_str.split(" ") logger.info(f"args: {args}") if not database.exists(f"race.data:{ctx.channel.id}"): roles = check_state_role(ctx) taxon_args = set(taxons.keys()).intersection( {arg.lower() for arg in args}) if taxon_args: taxon = " ".join(taxon_args).strip() else: taxon = "" state_args = set(states.keys()).intersection( {arg.upper() for arg in args}) if state_args: state = " ".join(state_args).strip() else: state = "" if database.exists(f"session.data:{ctx.author.id}"): logger.info("session parameters") if taxon_args: current_taxons = set( database.hget(f"session.data:{ctx.author.id}", "taxon").decode("utf-8").split(" ")) logger.info(f"toggle taxons: {taxon_args}") logger.info(f"current taxons: {current_taxons}") taxon_args.symmetric_difference_update(current_taxons) taxon_args.discard("") logger.info(f"new taxons: {taxon_args}") taxon = " ".join(taxon_args).strip() else: taxon = database.hget(f"session.data:{ctx.author.id}", "taxon").decode("utf-8") roles = (database.hget(f"session.data:{ctx.author.id}", "state").decode("utf-8").split(" ")) if roles[0] == "": roles = [] if not roles: logger.info("no session lists") roles = check_state_role(ctx) session_filter = int( database.hget(f"session.data:{ctx.author.id}", "filter")) filters = Filter.parse(args_str, defaults=False) if filters.vc: filters.vc = False await ctx.send("**The VC filter is not allowed inline!**") default_quality = Filter().quality if (Filter.from_int(session_filter).quality == default_quality and filters.quality and filters.quality != default_quality): filters ^= Filter() # clear defaults filters ^= session_filter else: filters = Filter.parse(args_str) if filters.vc: filters.vc = False await ctx.send("**The VC filter is not allowed inline!**") if state_args: logger.info(f"toggle states: {state_args}") logger.info(f"current states: {roles}") state_args.symmetric_difference_update(set(roles)) state_args.discard("") logger.info(f"new states: {state_args}") state = " ".join(state_args).strip() else: state = " ".join(roles).strip() if "CUSTOM" in state.upper().split(" "): if not database.exists(f"custom.list:{ctx.author.id}"): await ctx.send("**You don't have a custom list set!**") state_list = state.split(" ") state_list.remove("CUSTOM") state = " ".join(state_list) elif database.exists(f"custom.confirm:{ctx.author.id}"): await ctx.send( "**Please verify or confirm your custom list before using!**" ) state_list = state.split(" ") state_list.remove("CUSTOM") state = " ".join(state_list) else: logger.info("race parameters") race_filter = int( database.hget(f"race.data:{ctx.channel.id}", "filter")) filters = Filter.parse(args_str, defaults=False) if filters.vc: filters.vc = False await ctx.send("**The VC filter is not allowed inline!**") default_quality = Filter().quality if (Filter.from_int(race_filter).quality == default_quality and filters.quality and filters.quality != default_quality): filters ^= Filter() # clear defaults filters ^= race_filter taxon = database.hget(f"race.data:{ctx.channel.id}", "taxon").decode("utf-8") state = database.hget(f"race.data:{ctx.channel.id}", "state").decode("utf-8") logger.info( f"args: filters: {filters}; taxon: {taxon}; state: {state}") return (filters, taxon, state)
async def start(self, ctx, *, args_str: str = ""): logger.info("command: start session") if database.exists(f"session.data:{ctx.author.id}"): logger.info("already session") await ctx.send( "**There is already a session running.** *Change settings/view stats with `b!session edit`*" ) return filters = Filter.parse(args_str) args = args_str.lower().split(" ") logger.info(f"args: {args}") if "wiki" in args: wiki = "" else: wiki = "wiki" if "strict" in args: strict = "strict" else: strict = "" 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)) taxon_args = set(taxons.keys()).intersection( {arg.lower() for arg in args}) if taxon_args: taxon = " ".join(taxon_args).strip() else: taxon = "" logger.info( f"adding filters: {filters}; state: {state}; wiki: {wiki}; strict: {strict}" ) database.hset( f"session.data:{ctx.author.id}", mapping={ "start": round(time.time()), "stop": 0, "correct": 0, "incorrect": 0, "total": 0, "filter": str(filters.to_int()), "state": state, "taxon": taxon, "wiki": wiki, "strict": strict, }, ) await ctx.send( f"**Session started with options:**\n{await self._get_options(ctx)}" )
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:{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 = "" taxon_args = set(taxons.keys()).intersection( {arg.lower() for arg in args}) if taxon_args: taxon = " ".join(taxon_args).strip() else: taxon = "" 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 ints: 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:{ctx.channel.id}", { "start": round(time.time()), "stop": 0, "limit": limit, "bw": bw, "state": state, "addon": addon, "media": media, "taxon": taxon }) database.zadd(f"race.scores:{ctx.channel.id}", {str(ctx.author.id): 0}) await ctx.send( f"**Race started with options:**\n{await self._get_options(ctx)}" ) if database.hget(f"race.data:{ctx.channel.id}", "media").decode("utf-8") == "image": logger.info("clearing previous bird") database.hset(f"channel:{ctx.channel.id}", "bird", "") database.hset(f"channel:{ctx.channel.id}", "answered", "1") logger.info("auto sending next bird image") addon, bw, taxon = database.hmget( f"race.data:{ctx.channel.id}", ["addon", "bw", "taxon"]) birds = self.bot.get_cog("Birds") await birds.send_bird_( ctx, addon.decode("utf-8"), # type: ignore bw.decode("utf-8"), # type: ignore taxon.decode("utf-8") # type: ignore ) if database.hget(f"race.data:{ctx.channel.id}", "media").decode("utf-8") == "song": logger.info("clearing previous bird") database.hset(f"channel:{ctx.channel.id}", "sBird", "") database.hset(f"channel:{ctx.channel.id}", "sAnswered", "1") logger.info("auto sending next bird song") birds = self.bot.get_cog("Birds") await birds.send_song_(ctx)
def parse(ctx, args_str: str): """Parse arguments for options.""" args = args_str.split(" ") logger.info(f"args: {args}") if not database.exists(f"race.data:{ctx.channel.id}"): roles = check_state_role(ctx) taxon_args = set(taxons.keys()).intersection( {arg.lower() for arg in args}) if taxon_args: taxon = " ".join(taxon_args).strip() else: taxon = "" state_args = set(states.keys()).intersection( {arg.upper() for arg in args}) if state_args: state = " ".join(state_args).strip() else: state = "" if database.exists(f"session.data:{ctx.author.id}"): logger.info("session parameters") if taxon_args: current_taxons = set( database.hget(f"session.data:{ctx.author.id}", "taxon").decode("utf-8").split(" ")) logger.info(f"toggle taxons: {taxon_args}") logger.info(f"current taxons: {current_taxons}") taxon_args.symmetric_difference_update(current_taxons) logger.info(f"new taxons: {taxon_args}") taxon = " ".join(taxon_args).strip() else: taxon = database.hget(f"session.data:{ctx.author.id}", "taxon").decode("utf-8") roles = (database.hget(f"session.data:{ctx.author.id}", "state").decode("utf-8").split(" ")) if roles[0] == "": roles = [] if not roles: logger.info("no session lists") roles = check_state_role(ctx) session_filter = int( database.hget(f"session.data:{ctx.author.id}", "filter")) filters = Filter.parse(args_str, defaults=False) default_quality = Filter().quality if (Filter.from_int(session_filter).quality == default_quality and filters.quality and filters.quality != default_quality): filters ^= Filter() # clear defaults filters ^= session_filter else: filters = Filter.parse(args_str) if state_args: logger.info(f"toggle states: {state_args}") logger.info(f"current states: {roles}") state_args.symmetric_difference_update(set(roles)) logger.info(f"new states: {state_args}") state = " ".join(state_args).strip() else: state = " ".join(roles).strip() else: logger.info("race parameters") race_filter = int( database.hget(f"race.data:{ctx.channel.id}", "filter")) filters = Filter.parse(args_str, defaults=False) default_quality = Filter().quality if (Filter.from_int(race_filter).quality == default_quality and filters.quality and filters.quality != default_quality): filters ^= Filter() # clear defaults filters ^= race_filter taxon = database.hget(f"race.data:{ctx.channel.id}", "taxon").decode("utf-8") state = database.hget(f"race.data:{ctx.channel.id}", "state").decode("utf-8") logger.info( f"args: filters: {filters}; taxon: {taxon}; state: {state}") return (filters, taxon, state)
async def send_bird_(self, ctx, add_on: str = "", bw: bool = False, taxon_str: str = ""): if add_on == "": message = BIRD_MESSAGE.format(option="n image") else: message = BIRD_MESSAGE.format(option=f" {add_on}") if taxon_str: taxon = taxon_str.split(" ") else: taxon = [] logger.info( "bird: " + database.hget(f"channel:{ctx.channel.id}", "bird").decode("utf-8")) answered = int(database.hget(f"channel:{ctx.channel.id}", "answered")) logger.info(f"answered: {answered}") # check to see if previous bird was answered if answered: # if yes, give a new bird roles = check_state_role(ctx) if database.exists(f"session.data:{ctx.author.id}"): logger.info("session active") session_increment(ctx, "total", 1) roles = database.hget(f"session.data:{ctx.author.id}", "state").decode("utf-8").split(" ") if roles[0] == "": roles = [] if not roles: logger.info("no session lists") roles = check_state_role(ctx) logger.info(f"addon: {add_on}; bw: {bw}; roles: {roles}") if taxon: birds_in_taxon = set( itertools.chain.from_iterable(taxons[o] for o in taxon)) if roles: birds_in_state = set( itertools.chain.from_iterable(states[state]["birdList"] for state in roles)) birds = list(birds_in_taxon.intersection(birds_in_state)) else: birds = list(birds_in_taxon.intersection(set(birdList))) elif roles: birds = list( set( itertools.chain.from_iterable(states[state]["birdList"] for state in roles))) else: birds = birdList if not birds: logger.info("no birds for taxon/state") await ctx.send( f"**Sorry, no birds could be found for the taxon/state combo.**\n*Please try again*" ) return logger.info(f"number of birds: {len(birds)}") currentBird = random.choice(birds) prevB = database.hget(f"channel:{ctx.channel.id}", "prevB").decode("utf-8") while currentBird == prevB and len(birds) > 1: currentBird = random.choice(birds) database.hset(f"channel:{ctx.channel.id}", "prevB", str(currentBird)) database.hset(f"channel:{ctx.channel.id}", "bird", str(currentBird)) logger.info("currentBird: " + str(currentBird)) database.hset(f"channel:{ctx.channel.id}", "answered", "0") await send_bird(ctx, currentBird, on_error=error_skip, message=message, addOn=add_on, bw=bw) else: # if no, give the same bird await send_bird(ctx, database.hget(f"channel:{ctx.channel.id}", "bird").decode("utf-8"), on_error=error_skip, message=message, addOn=add_on, bw=bw)