async def cmd_give(msg: Message, *args):
    if len(args) != 2:
        raise InvalidArgumentsException()

    if args[0] not in msg.channel.chatters:
        await msg.reply(msg=f'no viewer found by the name "{args[0]}"')
        return

    caller = get_balance_from_msg(msg)
    target = get_balance(msg.channel_name, args[0])

    try:
        give = int(args[1])
    except ValueError:
        await msg.reply('invalid give amount')
        return

    cur_name = get_currency_name(msg.channel_name).name

    if caller.balance < give:
        await msg.reply(f"@{msg.author} you don't have enough {cur_name}")
        return

    caller.balance -= give
    target.balance += give

    session.commit()

    await msg.reply(
        f"@{msg.author} you gave @{args[0]} {give} {cur_name}, @{args[0]}'s balance is now {target.balance}"
    )
async def cmd_duel(msg: Message, *args):
    if not args:
        raise InvalidArgumentsError(reason='missing required arguments', cmd=cmd_duel)

    target = args[0].lstrip('@')

    if target == msg.author:
        raise InvalidArgumentsError(reason='you cannot duel yourself', cmd=cmd_duel)

    if target not in msg.channel.chatters:
        raise InvalidArgumentsError(reason=f'{msg.mention} {target} is not in this channel', cmd=cmd_duel)

    duel = get_duel(msg.channel_name, msg.author, target)

    if duel and not duel_expired(duel):
        raise InvalidArgumentsError(reason=f'{msg.mention} you already have a pending duel with {target}', cmd=cmd_duel)

    try:
        bet = int(args[1])
    except ValueError:
        raise InvalidArgumentsError(reason=f'invalid bet: {args[1]}, bet must be a number with no decimals, ex: 12',
                                    cmd=cmd_duel)
    except IndexError:
        bet = 10

    add_duel(msg.channel_name, msg.author, target, bet)

    currency_name = get_currency_name(msg.channel_name).name
    await msg.reply(
        f'{msg.mention} has challenged @{target} to a duel for {bet} {currency_name}'
        f', do "{cfg.prefix}accept {msg.mention}" to accept the duel')
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_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_give(msg: Message, *args):
    if len(args) != 2:
        raise InvalidArgumentsError(reason='missing required arguments', cmd=cmd_give)

    if not msg.mentions or msg.mentions[0] not in msg.channel.chatters:
        raise InvalidArgumentsError(reason=f'no viewer found by the name "{(msg.mentions or args)[0]}"')

    caller = get_balance_from_msg(msg)
    target = get_balance(msg.channel_name, msg.mentions[0])

    try:
        give = int(args[1])
    except ValueError:
        raise InvalidArgumentsError(reason='give amount must be a integer, example: 100', cmd=cmd_give)

    if give <= 0:
        raise InvalidArgumentsError(reason='give amount must be 1 or higher', cmd=cmd_give)

    cur_name = get_currency_name(msg.channel_name).name

    if caller.balance < give:
        raise InvalidArgumentsError(reason=f"{msg.mention} you don't have enough {cur_name}", cmd=cmd_give)

    caller.balance -= give
    target.balance += give

    session.commit()

    await msg.reply(
        f"@{msg.author} you gave @{args[0]} {give} {cur_name}, @{args[0]}'s balance is now {target.balance}")
async def cmd_sub_bal(msg: Message, *args):
    if len(args) != 2:
        raise InvalidArgumentsError('must supply both <user or all> and <amount>', cmd=cmd_sub_bal)

    target = args[0].lower()
    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_sub_bal)

    if amount <= 0:
        raise InvalidArgumentsError(f'amount must be positive and not zero, ex: 1, 2, 100', cmd=cmd_sub_bal)

    currency = get_currency_name(msg.channel_name).name

    if get_balance_from_msg(msg).balance < amount:
        raise InvalidArgumentsError(f'{target} does not have {currency} to subtract {amount} {currency} from',
                                    cmd=cmd_sub_bal)

    if target == 'all':
        subtract_balance_from_all(msg.channel_name, amount)
        await msg.reply(f"subtracted {amount} {currency} from everyone's balance")
    else:
        subtract_balance(msg.channel_name, target, amount)
        await msg.reply(
            f'subtracted {amount} {currency} from {target}, '
            f'their total is now {get_balance(msg.channel_name, target).balance} {currency}')
async def cmd_get_bal(msg: Message, *args):
    if args:
        target = args[0].lstrip('@')
    else:
        target = msg.author

    currency_name = get_currency_name(msg.channel_name).name
    balance = get_balance(msg.channel_name, target).balance
    await msg.reply(whisper=True, msg=f'@{target} has {balance} {currency_name}')
async def cmd_get_bal(msg: Message, *args):
    if args:
        target = args[0].lstrip('@')

        if target not in msg.channel.chatters:
            raise InvalidArgumentsError(reason=f'no viewer found by the name of "{target}"', cmd=cmd_get_bal)
    else:
        target = msg.author

    currency_name = get_currency_name(msg.channel_name).name
    balance = get_balance(msg.channel_name, target).balance
    await msg.reply(whisper=True, msg=f'@{target} has {balance} {currency_name}')
async def cmd_gen_sb_list(msg: Message):
    channel = msg.channel_name
    with open(f"{SB_PATH}/sb_list_{channel}.txt", 'w') as f:
        f.write(f'# Soundbank [sub]commands list for channel {channel}\n\n')
        f.write('# AUTOMATICALLY GENERATED FILE\n')
        currency = get_currency_name(channel).name
        for snd in session.query(Sound).filter(Sound.channel == channel).all():
            if snd.price:
                price = snd.price
            elif snd.pricemult:
                price = snd.pricemult * SB_DEFPRICE
            else:
                price = SB_DEFPRICE
            f.write(f'{PREFIX}sb {snd.sndid}\t\t{price} {currency}\n')
    await msg.reply(f'sound list generated')
async def cmd_gamble(msg: Message, *args):
    if not len(args):
        raise InvalidArgumentsError(reason='missing required arguments', cmd=cmd_gamble)

    try:
        bet = int(args[0])
    except ValueError:
        raise InvalidArgumentsError(reason='invalid value for bet', cmd=cmd_gamble)

    sides = 6
    if len(args) == 2:
        if not args[1].isdigit():
            raise InvalidArgumentsError(reason='invalid value for dice_sides', cmd=cmd_gamble)
        sides = int(args[1])

    if bet < 10:
        raise InvalidArgumentsError(reason='bet cannot be less then 10', cmd=cmd_gamble)

    elif sides < 2:
        raise InvalidArgumentsError(reason='sides cannot be less than 2', cmd=cmd_gamble)

    bal = get_balance_from_msg(msg)

    cur_name = get_currency_name(msg.channel_name).name
    if bal.balance < bet:
        raise InvalidArgumentsError(reason=f"{msg.mention} you don't have enough {cur_name}", cmd=cmd_gamble)

    def _calc_winnings(sides, roll, bet):
        if roll == sides:
            return int(bet * ((sides / 6) + 1.5))
        return int(bet * (roll / sides))

    roll = randbelow(sides) + 1
    winnings = _calc_winnings(sides=sides, roll=roll, bet=bet)

    if winnings > bet:
        gain = winnings - bet
        bal.balance += gain
        await msg.reply(f'you rolled {roll} and won {gain} {cur_name}')
    else:
        loss = bet - winnings
        bal.balance -= loss
        await msg.reply(f'you rolled {roll} and lost {loss} {cur_name}')

    session.commit()
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')
async def cmd_gamble(msg: Message, *args):
    if len(args) != 2:
        raise InvalidArgumentsError(reason='missing required arguments',
                                    cmd=cmd_gamble)

    try:
        sides = int(args[0])
        bet = int(args[1])
    except ValueError:
        raise InvalidArgumentsError(reason='invalid value for sides or bet',
                                    cmd=cmd_gamble)

    if bet < 10:
        raise InvalidArgumentsError(reason='bet cannot be less then 10',
                                    cmd=cmd_gamble)

    elif sides < 2:
        raise InvalidArgumentsError(reason='sides cannot be less than 2',
                                    cmd=cmd_gamble)

    bal = get_balance_from_msg(msg)
    cur_name = get_currency_name(msg.channel_name).name

    if bal.balance < bet:
        raise InvalidArgumentsError(
            reason=f"{msg.mention} you don't have enough {cur_name}",
            cmd=cmd_gamble)

    n = randbelow(sides) + 1

    if n == 1:
        if sides >= 6:
            bet *= 2
        gain = bet + int(bet * (sides / 6))
        bal.balance += gain
        await msg.reply(f'you rolled {n} and won {gain} {cur_name}')

    else:
        bal.balance -= bet
        await msg.reply(f'you rolled {n} and lost your bet of {bet} {cur_name}'
                        )

    session.commit()
async def cmd_get_sound(msg: Message, *args):
    # sanity checks:
    if not args:
        #raise InvalidArgumentsError(reason='missing required argument', cmd=cmd_get_sound)
        await msg.reply(
            f'You can play sounds from the soundboard with "!sb <sndname>".')
        return

    snd = get_sound(msg.channel_name, args[0])
    if snd is None:
        raise InvalidArgumentsError(reason='no sound found with this name',
                                    cmd=cmd_get_sound)

    # calculate the sound price
    if snd.price:
        price = snd.price
    elif snd.pricemult:
        price = snd.pricemult * SB_DEFPRICE
    else:
        price = SB_DEFPRICE

    # make the author pay the price:
    currency = get_currency_name(msg.channel_name).name
    if get_balance_from_msg(msg).balance < price:
        raise InvalidArgumentsError(
            f'{msg.author} tried to play {snd.sndid} '
            f'for {price} {currency}, but they do not have enough {currency}!')
    subtract_balance(msg.channel_name, msg.author, price)

    # report success
    if cfg.soundbank_verbose:
        await msg.reply(
            f'{msg.author} played "{snd.sndid}" for {price} {currency}')

    # play the sound with PyDub; supports all formats supported by ffmpeg.
    # Tested with mp3, wav, ogg.
    if snd.gain:
        gain = snd.gain
    else:
        gain = 0
    sound = pd_audio.from_file(snd.filepath) + cfg.soundbank_gain + gain
    pd_play(sound)
async def cmd_gamble(msg: Message, *args):
    if len(args) != 2:
        raise InvalidArgumentsException()

    try:
        sides = int(args[0])
        bet = int(args[1])
    except ValueError:
        await msg.reply(f'invalid value for sides or bet')
        return

    if bet < 10:
        await msg.reply('bet cannot be less then 10')
        return

    elif sides < 2:
        await msg.reply('sides cannot be less than 2')
        return

    bal = get_balance_from_msg(msg)
    if bal.balance < bet:
        await msg.reply(
            f"you don't have enough {get_currency_name(msg.channel_name).name}"
        )
        return

    n = randbelow(sides) + 1
    cur_name = get_currency_name(msg.channel_name).name

    if n == 1:
        if sides >= 6:
            bet *= 2
        gain = bet + int(bet * (sides / 6))
        bal.balance += gain
        await msg.reply(f'you rolled {n} and won {gain} {cur_name}')

    else:
        bal.balance -= bet
        await msg.reply(f'you rolled {n} and lost your bet of {bet} {cur_name}'
                        )

    session.commit()