async def cmd_add_bal(msg: Message, *args): if len(args) != 2: raise InvalidArgumentsError('must supply both <user or all> and <amount>', cmd=cmd_add_bal) target = args[0].lower() if target != 'all' and target not in msg.channel.chatters: raise InvalidArgumentsError(f'cannot find any viewer named "{target}"', cmd=cmd_add_bal) try: amount = int(args[1]) except ValueError: raise InvalidArgumentsError(f'cannot parse "{args[1]}" to a int, must be a valid int, ex: 100', cmd=cmd_add_bal) if amount <= 0: raise InvalidArgumentsError(f'amount must be positive and not zero, ex: 1, 2, 100', cmd=cmd_add_bal) currency = get_currency_name(msg.channel_name).name if target == 'all': add_balance_to_all(msg.channel_name, amount) await msg.reply(f"added {amount} {currency} to everyone's balance") else: add_balance(msg.channel_name, target, amount) await msg.reply( f'gave {target} {amount} {currency}, ' f'their total is now {get_balance(msg.channel_name, target).balance} {currency}')
async def cmd_arena(msg: Message, *args): def _can_pay_entry_fee(fee): return get_balance(msg.channel_name, msg.author).balance >= fee arena = running_arenas.get(msg.channel_name) curname = get_currency_name(msg.channel_name).name # arena is already running for this channel if arena: if msg.author in arena.users: return await msg.reply( whisper=True, msg='you are already entered the in the arena') elif not _can_pay_entry_fee(arena.entry_fee): await msg.reply( whisper=True, msg=f'{msg.mention} you do not have enough {curname} ' f'to join the arena, entry_fee is {arena.entry_fee} {curname}') return arena.add_user(msg.author) add_balance(msg.channel_name, msg.author, -arena.entry_fee) await msg.reply( whisper=True, msg= f'{msg.mention} you have been added to the arena, you were charged {arena.entry_fee} {curname} upon entry' ) # start a new arena as one is not already running for this channel else: if args: try: entry_fee = int(args[0]) except ValueError: await msg.reply(msg='invalid value for entry fee') return else: entry_fee = ARENA_DEFAULT_ENTRY_FEE if entry_fee and entry_fee < ARENA_DEFAULT_ENTRY_FEE: await msg.reply( msg=f'entry fee cannot be less than {ARENA_DEFAULT_ENTRY_FEE}') return if not _can_pay_entry_fee(entry_fee): await msg.reply( whisper=True, msg=f'{msg.mention} you do not have {entry_fee} {curname}') return arena = Arena(msg.channel, entry_fee, on_arena_ended_func=_remove_running_arena_entry) arena.start() arena.add_user(msg.author) add_balance(msg.channel_name, msg.author, -arena.entry_fee) running_arenas[msg.channel_name] = arena
async def cmd_accept(msg: Message, *args): if len(args) != 1: raise InvalidArgumentsError('missing required arguments') challenger = args[0].lstrip('@') winner, bet = accept_duel(msg.channel_name, challenger, msg.author) if not winner: raise InvalidArgumentsError( reason=f'{msg.mention}, you have not been challenged by {challenger}, or the duel might have expired', cmd=cmd_accept) loser = msg.author if winner == msg.author else challenger add_balance(msg.channel_name, winner, bet) subtract_balance(msg.channel_name, loser, bet) currency_name = get_currency_name(msg.channel_name).name await msg.reply(f'@{winner} has won the duel, {bet} {currency_name} went to the winner')