async def cmd_set_bal(msg: Message, *args): if not len(args): raise InvalidArgumentsError(reason='missing required arguments', cmd=cmd_set_bal) elif len(args) == 2: target = args[1].lstrip('@') if target not in msg.channel.chatters: raise InvalidArgumentsError( reason=f'no viewer found by the name of "{args[1]}"', cmd=cmd_set_bal) else: target = msg.author try: new_balance = int(args[0]) if new_balance < 0: raise InvalidArgumentsError( reason='new balance cannot be negative', cmd=cmd_set_bal) set_balance(msg.channel_name, target, new_balance) except ValueError: raise InvalidArgumentsError( reason=f'target balance must be a integer. example: 100') await msg.reply(f'@{target} now has {args[0]} ' f'{get_currency_name(msg.channel_name).name}')
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_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_edit_timer(msg: Message, *args): if len(args) < 3: raise InvalidArgumentsError(reason='missing required arguments', cmd=cmd_edit_timer) name = args[0].lower() timer = get_message_timer(msg.channel_name, name) if not timer: raise InvalidArgumentsError(reason=f'no timer was found by "{name}"', cmd=cmd_edit_timer) mode = args[1].lower() if mode not in ('msg', 'interval'): raise InvalidArgumentsError(reason='invalid option, must be `msg` or `interval`', cmd=cmd_edit_timer) if mode == 'interval': try: interval = int(args[2]) except ValueError: raise InvalidArgumentsError(reason='interval must be a valid integer, ex: 10', cmd=cmd_edit_timer) set_message_timer_interval(msg.channel_name, name, interval) restart_message_timer(msg.channel_name, name) return await msg.reply(f'updated timer interval for "{name}"') elif mode == 'msg': value = ' '.join(args[2:]) set_message_timer_message(msg.channel_name, name, value) restart_message_timer(msg.channel_name, name) return await msg.reply(f'updated timer message for "{name}"')
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_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_help(msg: Message, *args): if not args: raise InvalidArgumentsError(reason='missing required argument', cmd=cmd_help) cmd = get_command(args[0]) if not cmd: raise InvalidArgumentsError(reason=f'command not found', cmd=cmd_help) await msg.reply(msg=f'help for {cmd.fullname} - syntax: {cmd.syntax} - help: {cmd.help}')
async def cmd_get_quote(msg: Message, *args): if not args: raise InvalidArgumentsError(reason='missing required argument', cmd=cmd_get_quote) quote = get_quote(msg.channel_name, args[0]) if quote is None: raise InvalidArgumentsError(reason='no quote found', cmd=cmd_get_quote) await msg.reply(f'"{quote.value}" user: {quote.user} alias: {quote.alias}')
async def cmd_get_custom_command(msg: Message, *args): if not args: raise InvalidArgumentsError(reason='missing required arguments', cmd=cmd_get_custom_command) cmd = get_custom_command(msg.channel_name, args[0].lower()) if cmd is None: raise InvalidArgumentsError(reason=f'no command found for "{args[0]}"', cmd=cmd_get_custom_command) await msg.reply(f'the response for "{cmd.name}" is "{cmd.response}"')
async def cmd_del_quote(msg: Message, *args): if not args: raise InvalidArgumentsError(reason='missing required argument', cmd=cmd_del_quote) quote = get_quote(msg.channel_name, args[0]) if quote is None: raise InvalidArgumentsError(reason='no quote found', cmd=cmd_del_quote) delete_quote_by_id(msg.channel_name, quote.id) await msg.reply( f'successfully deleted quote, id: {quote.id}, alias: {quote.alias}')
async def cmd_del_sound(msg: Message, *args): if not args: raise InvalidArgumentsError(reason='missing required argument', cmd=cmd_del_sound) snd = get_sound(msg.channel_name, args[0]) if snd is None: raise InvalidArgumentsError(reason='no such sound found', cmd=cmd_del_sound) delete_sound(msg.channel_name, snd.sndid) await msg.reply(f'successfully deleted sound "{snd.sndid}"')
async def cmd_del_custom_command(msg: Message, *args): if not args: raise InvalidArgumentsError(reason='missing required arguments', cmd=cmd_del_custom_command) cmd = get_custom_command(msg.channel_name, args[0].lower()) if cmd is None: raise InvalidArgumentsError(reason=f'no command found for "{args[0]}"', cmd=cmd_del_custom_command) if delete_custom_command(msg.channel_name, cmd.name): await msg.reply(f'successfully deleted command {cmd.name}') else: await msg.reply(f'failed to delete command {cmd.name}')
async def cmd_del_timer(msg: Message, *args): if not args: raise InvalidArgumentsError(reason='missing required argument', cmd=cmd_del_timer) name = args[0].lower() timer = get_message_timer(msg.channel_name, name) if not timer: raise InvalidArgumentsError(reason=f'no timer was found by "{name}"', cmd=cmd_del_timer) if delete_message_timer(msg.channel_name, name): await msg.reply(f'successfully deleted timer "{name}"') else: await msg.reply(f'failed to delete timer "{name}"')
async def cmd_disable_mod(msg: Message, *args): if len(args) != 1: raise InvalidArgumentsError(reason=f'expected one argument, got {len(args)}', cmd=cmd_disable_mod) mod = args[0] if not mod_exists(mod): raise InvalidArgumentsError(reason=f'could not find mod "{mod}"', cmd=cmd_disable_mod) if is_mod_disabled(msg.channel_name, mod): await msg.reply(f'mod "{mod}" is already disabled') return disable_mod(msg.channel_name, mod) await msg.reply(f'mod "{mod}" has been disabled for this channel')
async def cmd_color(msg: Message, *args): if not args: raise InvalidArgumentsError(reason='missing required arguments', cmd=cmd_color) await msg.channel.color(args[0]) await msg.reply(f'set color to {args[0]}')
async def cmd_set_currency_name(msg: Message, *args): if len(args) != 1: raise InvalidArgumentsError(reason='missing required arguments', cmd=cmd_set_currency_name) set_currency_name(msg.channel_name, args[0]) await msg.reply( f"this channel's currency name is now \"{get_currency_name(msg.channel_name).name}\"")
async def cmd_add_timer(msg: Message, *args): if len(args) < 3: raise InvalidArgumentsError(reason='missing required arguments', cmd=cmd_add_timer) valid, interval = await _parse_interval(msg, args[1]) if not valid: return timer_msg = ' '.join(args[2:]) timer_name = args[0].lower() if message_timer_exist(msg.channel_name, timer_name): raise InvalidArgumentsError(reason=f'a timer already exist by the name of "{timer_name}"', cmd=cmd_add_timer) set_message_timer(msg.channel_name, timer_name, timer_msg, interval) await msg.reply(f'created timer successfully')
async def cmd_stop_timer(msg: Message, *args): if not args: raise InvalidArgumentsError(reason='missing required argument', cmd=cmd_stop_timer) name = args[0].lower() timer = get_message_timer(msg.channel_name, name) if not timer: raise InvalidArgumentsError(reason=f'no timer was found by "{name}"', cmd=cmd_stop_timer) if not timer.running: await msg.reply(f'that timer is not running') return if set_message_timer_active(msg.channel_name, name, False): await msg.reply(f'successfully stopped the timer "{name}"') else: await msg.reply(f'failed to stop the timer "{name}"')
async def cmd_del_perm(msg: Message, *args): if len(args) != 2: raise InvalidArgumentsError(reason='missing required arguments', cmd=cmd_del_perm) group, perm = args if perms.delete_permission(msg.channel_name, group, perm): await msg.reply(whisper=WHISPER, msg=f'deleted permission "{perm}" from "{group}"') else: await msg.reply(whisper=WHISPER, msg=f'no group found by the "{group}"')
async def cmd_add_group(msg: Message, *args): if not args: raise InvalidArgumentsError(reason='missing required argument', cmd=cmd_add_group) group = args[0] if perms.add_group(msg.channel_name, group): await msg.reply(whisper=WHISPER, msg=f'added permission group "{group}"') else: await msg.reply(whisper=WHISPER, msg=f'permission group "{group}" already exist')
async def cmd_del_group(msg: Message, *args): if not args: raise InvalidArgumentsError(reason='missing required argument', cmd=cmd_del_group) group = args[0] if perms.delete_group(msg.channel_name, group): await msg.reply(whisper=WHISPER, msg=f'deleted permission group "{group}"') else: await msg.reply(whisper=WHISPER, msg=f'no group found by the "{group}"')
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_roll(msg: Message, *args): try: sides = int(args[0]) if args else 6 except ValueError: raise InvalidArgumentsError(reason='invalid value for sides', cmd=cmd_roll) num = randint(1, sides) user = msg.mention if msg.is_privmsg else '' await msg.reply(f'{user} you rolled a {num}')
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_add_quote(msg: Message, *args): if not args: raise InvalidArgumentsError(reason='missing required arguments', cmd=cmd_add_quote) optionals = ' '.join(args[1:]) user = alias = None if 'user='******'user=([\w\d]+)', msg.content) if not m: raise InvalidArgumentsError( reason= 'invalid user for user=, must be a combination of digits and letters, ex: john_doe17', cmd=cmd_add_quote) user = m.group(1) if 'alias=' in optionals: m = re.search(r'alias=([\d\w]+)', msg.content) if not m: raise InvalidArgumentsError( reason= 'invalid alias for alias=, must be a combination of digits and letters, ex: my_quote1', cmd=cmd_add_quote) alias = m.group(1) if get_quote_by_alias(msg.channel_name, alias) is not None: raise InvalidArgumentsError( reason='there is already a quote with that alias', cmd=cmd_add_quote) quote = Quote.create(channel=msg.channel_name, value=args[0], user=user, alias=alias) if add_quote(quote): resp = f'successfully added quote #{quote.id}' else: resp = 'failed to add quote, already exist' await msg.reply(resp)
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_add_member(msg: Message, *args): if len(args) != 2: raise InvalidArgumentsError(reason='missing required arguments', cmd=cmd_add_member) group, member = args member = member.lstrip('@') if perms.add_member(msg.channel_name, group, member): await msg.reply(whisper=WHISPER, msg=f'added "{member}" to "{group}"') else: await msg.reply(whisper=WHISPER, msg=f'failed to add member, group does not exist')
async def cmd_reload_mod(msg: Message, *args): if len(args) != 1: raise InvalidArgumentsError(f'missing required argument: modname', cmd=cmd_reload_mod) mod = args[0] if mod != 'all' and not mod_exists(mod): raise InvalidArgumentsError(f'could not find any mod by name: {mod}', cmd=cmd_reload_mod) if mod == 'all': for mod in tuple(mods): if not reload_mod(mod): await msg.reply(f'failed to reload mod "{mod}"') return await msg.reply('reloaded all mods') return if reload_mod(mod): await msg.reply(f'reloaded mod "{mod}"') else: await msg.reply(f'failed to reload mod "{mod}"')
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_add_custom_command(msg: Message, *args): if len(args) < 2: raise InvalidArgumentsError(reason='missing required arguments', cmd=cmd_add_custom_command) name, resp = args[0], ' '.join(args[1:]) name = name.lower() if not _verify_resp_is_valid(resp): raise InvalidArgumentsError( reason='response cannot have . or / as the starting character', cmd=cmd_add_custom_command) if custom_command_exist(msg.channel_name, name): raise InvalidArgumentsError( reason='custom command already exist by that name', cmd=cmd_add_custom_command) if add_custom_command(CustomCommand.create(msg.channel_name, name, resp)): await msg.reply('successfully added command') else: await msg.reply('failed to add command')