async def on_reaction_add(self, reaction, user): log.debug("Got reaction {0} by {1} on {2}".format( str(reaction), user, str(reaction.message.guild))) message = reaction.message mid = message.id if mid not in message_dict: return ctx, _, expected_all, _, drink = message_dict[mid] if user not in expected_all or message.id not in message_dict: return if str(reaction) == conf.lang("ru_RU", "ok-emoji"): expected_all.remove(user) log.debug("Parsed as OK") ctx.author = user await self.drink(ctx, drink_name=drink.name) if not len(expected_all): try: await message.delete() except Forbidden or NotFound or HTTPException: pass finally: del message_dict[message.id] log.info("Removed message {0}".format(message.id, )) elif str(reaction) == conf.lang("ru_RU", "no-emoji"): log.debug("Parsed as NO") expected_all.remove(user) if not len(expected_all): try: await message.delete() except Forbidden or NotFound or HTTPException: pass finally: del message_dict[message.id] log.info("Removed message {0}".format(message.id, ))
async def restock(self, ctx: Context, *, drink_name: Optional[str]): """ Restock the bar or only one drink. Barman role required. Parameters: drink_name: name of the drink """ if drink_name is not None and len(drink_name) > DRINK_NAME_LENGTH: await ctx.send( conf.lang("ru_RU", "wrong_drink_name").format(DRINK_NAME_LENGTH)) return if drink_name is None: (Drink.update(portions_left=Drink.portions_per_day).where( Drink.server == ctx.guild.id)).execute() await ctx.send(conf.lang("ru_RU", "restocked_all")) log.info("Restocked all drinks on {1}".format( drink_name, ctx.guild.id)) else: (Drink.update(portions_left=Drink.portions_per_day).where( Drink.server == ctx.guild.id and Drink.server == drink_name)).execute() await ctx.send( conf.lang("ru_RU", "restocked_single").format(drink_name)) log.info("Restocked drink \"{0}\" on {1}".format( drink_name, ctx.guild.id))
async def lang(self, ctx: Context, lang_code: str = None): """ If lang is provided, sets the language. Requires Manage Guild permission. Return the list of available languages and quick reminder how to set it. """ if lang_code: if lang_code not in get_langs(): await ctx.send(conf.international("incorrect_language")) else: logger.info("Updated lang on {0} to {1}".format( ctx.guild.id, lang_code)) with db.atomic(): server = get_server_or_create(ctx.guild.id, ctx.guild.preferred_locale) Server.update(lang=lang_code).where( Server.sid == ctx.guild.id).execute() Drink.delete().where(Drink.server == server.id).execute() await add_default_drinks(ctx.guild) await ctx.send(conf.lang(lang_code, "lang_selected")) else: langs = "\n".join( "{0}, lang_code \"{1}\"".format(conf.lang(lang, "name"), lang) for lang in conf.get_langs()) await ctx.send( conf.international("lang_list").format(self.bot.command_prefix) + '\n' + langs)
async def list(self, ctx: Context): """ Returns the list with available drinks """ drinks = Drink.select().where(Drink.server == ctx.guild.id) # out = "" # for drink in drinks: # out += conf.lang("ru_RU", "drink_info").format(str(drink.name), str(drink.portion_size)) # out += '\n' out = "\n".join([ conf.lang("ru_RU", "drink_info").format(drink.name, drink.portion_size, drink.portions_left, drink.portions_per_day) for drink in drinks ]) if out != "": await ctx.send(out) else: await ctx.send(conf.lang("ru_RU", "no_drinks"))
async def serve(self, ctx: Context, drink_name: str, to: commands.Greedy[Member]): """ Trying to give a drink to the member. This command will send a message, where the member can choose to drink or not. Parameters: drink_name: name of the drink to: member (can be mention) """ gid = ctx.guild.id try: drink = Drink.get(Drink.server == gid and Drink.name == drink_name) except DoesNotExist: if not check_guild_drink_count(gid): await ctx.send( conf.lang("ru_RU", "too_many_drinks").format(DRINKS_PER_SERVER)) return drink = Drink.create(server=gid, name=drink_name, intoxication=DEFAULT_INTOXICATION, portion_size=DEFAULT_PORTION_SIZE, portions_per_day=DEFAULT_PORTIONS_PER_DAY, portions_left=DEFAULT_PORTIONS_PER_DAY) msg = await ctx.send( conf.lang("ru_RU", "serve_message").format(author=ctx.author.mention, drink=drink_name, portion_size=drink.portion_size)) await asyncio.wait({ msg.add_reaction(conf.lang("ru_RU", "ok-emoji")), msg.add_reaction(conf.lang("ru_RU", "no-emoji")) }) message_dict[msg.id] = (ctx, msg, set(to), datetime.today() + timedelta(0, 60 * 10), drink)
async def consume_drink(ctx: Context, person: Person, drink: Drink): if person.intoxication > 100 or person.intoxication < 0: person.intoxication = 0 guild: Guild = ctx.guild member: Member = guild.get_member(person.uid) if drink.portions_left <= 0: await ctx.send( conf.lang("ru_RU", "no_portions_left").format(drink.name)) else: if drink.portions_left == 1: await ctx.send( conf.lang("ru_RU", "last_portion").format(drink.name)) person.intoxication += drink.intoxication drink.portions_left -= 1 if person.intoxication >= 100: try: await member.move_to(None, reason="Drank too much") await ctx.send( conf.lang("ru_RU", "overdrink_kick_message").format(member.mention)) except Forbidden: log.info("Can't kick an alcoholic: no permissions in " + guild.id) await ctx.send( conf.lang("ru_RU", "overdrink_no_kick_message").format( member.mention)) finally: person.intoxication = 0 elif person.intoxication > 80: await ctx.send( conf.lang("ru_RU", "pre_overdrink").format(member.display_name)) log.info("{0} consumed drink \"{1}\" on {2}".format( member.display_name, drink.name, guild.id)) with db.atomic(): person.save() drink.save()
async def remove(self, ctx: Context, *, drink_name: str): """ Remove the drink from the bar. Barman role required. Parameters: drink_name: name of the drink, not empty """ if drink_name is None or len(drink_name) > DRINK_NAME_LENGTH: await ctx.send( conf.lang("ru_RU", "wrong_drink_name").format(DRINK_NAME_LENGTH)) return try: drink = Drink.get(Drink.server == ctx.guild.id and Drink.name == drink_name) drink.delete_instance() await ctx.send( conf.lang("ru_RU", "drink_deleted").format(drink_name)) log.info("Removed drink \"{0}\" from {1}".format( drink_name, ctx.guild.id)) except DoesNotExist: await ctx.send( conf.lang("ru_RU", "drink_not_found").format(drink_name))
async def drink(self, ctx: Context, *, drink_name: str): """ Drinks a drink and tails some random joke. Parameters: drink_name: name of the drink, not empty """ if drink_name is None or len(drink_name) > DRINK_NAME_LENGTH: await ctx.send( conf.lang("ru_RU", "wrong_drink_name").format(DRINK_NAME_LENGTH)) return try: drink = Drink.get(Drink.server == ctx.guild.id and Drink.name == drink_name) person = get_person_or_create(ctx.guild.id, ctx.author.id) await consume_drink(ctx, person, drink) async with ctx.typing(): joke = get_joke("ru_RU") await ctx.send(joke or conf.lang("ru_RU", "joke_not_loaded")) except DoesNotExist: await ctx.send( conf.lang("ru_RU", "drink_not_found").format(drink_name))
async def add(self, ctx: Context, drink_name: str, intoxication: int = DEFAULT_INTOXICATION, portion_size: int = DEFAULT_PORTION_SIZE, portions_per_day: int = DEFAULT_PORTIONS_PER_DAY): """ Add a new drink to the bar. Barman role required. Parameters: drink_name: name of the drink, not empty intoxication: percent of intoxication (0-100). When man drinks a drink, this value will be appended to the level of intoxication. portion_size: size of portion, in milliliters, greater than 0 and less than 10000 (10l). portions_per_day: portions of this drink available for one day, greater than 0 and less than 10000. """ if not 0 <= intoxication <= 100: await ctx.send(conf.lang("ru_RU", "wrong_intoxication")) elif not 0 < portion_size <= PORTION_MAX_SIZE: await ctx.send( conf.lang("ru_RU", "wrong_portion_size").format(PORTION_MAX_SIZE)) elif not 0 < portions_per_day <= PORTIONS_PER_DAY: await ctx.send( conf.lang("ru_RU", "wrong_portions_per_day").format(PORTIONS_PER_DAY)) elif drink_name is None or len(drink_name) > DRINK_NAME_LENGTH: await ctx.send( conf.lang("ru_RU", "wrong_drink_name").format(DRINK_NAME_LENGTH)) elif not check_guild_drink_count(ctx.guild.id): await ctx.send( conf.lang("ru_RU", "too_many_drinks").format(DRINKS_PER_SERVER)) elif Drink.select().where(Drink.server == ctx.guild.id and Drink.name == drink_name).count() > 0: await ctx.send( conf.lang("ru_RU", "duplicate_drink").format(drink_name)) else: Drink.create(server=ctx.guild.id, name=drink_name, intoxication=intoxication, portion_size=portion_size, portions_per_day=portions_per_day, portions_left=portions_per_day) await ctx.send( conf.lang("ru_RU", "drink_added").format(drink_name)) log.info("Added drink \"{0}\" on {1}".format( drink_name, ctx.guild.id))
async def on_command_error(self, ctx: Context, error): if not hasattr(error, "bcdb_checked") or not error.bcdb_checked: log.error("Error on command {0}".format(ctx.command), exc_info=error) await ctx.send(conf.lang(get_lang_from_context(ctx), "on_error")) return True