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_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_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()
    if target != 'all' and target not in msg.channel.chatters:
        raise InvalidArgumentsError(f'cannot find any viewer named "{target}"', cmd=cmd_sub_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_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_mine(msg: Message, *args):
    key = (msg.author, msg.channel_name)
    diff = (datetime.now() - last_mine_time.get(key, datetime.now())).total_seconds()

    if key not in last_mine_time or diff >= 0:
        bal = get_balance_from_msg(msg)
        bal.balance += mine_gain
        session.commit()
        last_mine_time[key] = datetime.now() + timedelta(minutes=5)

        await msg.reply(
            f'@{msg.author} you went to work at the mines and came out with '
            f'{mine_gain} {get_currency_name(msg.channel_name).name} worth of gold',
            whisper=True)
    else:
        await msg.reply(f'you cannot mine again for {int(abs(diff))} seconds', whisper=True)
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_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()