def command_part(msg: twitchirc.ChannelMessage): p = twitchirc.ArgumentParser(prog='!part', add_help=False) p.add_argument('channel', metavar='CHANNEL', nargs='?', const=msg.channel, default=msg.channel) p.add_argument('-h', '--help', action='store_true', dest='help') args = p.parse_args(msg.text.split(' ')[1:]) if args is None or args.help: usage = msg.reply(f'@{msg.user} {p.format_usage()}') bot.send(usage) return if args.channel == '': args.channel = msg.channel channel = args.channel.lower() if channel != msg.channel.lower() and bot.check_permissions( msg, [twitchirc.PERMISSION_COMMAND_PART_OTHER], enable_local_bypass=False): bot.send( msg.reply( f"Cannot part from channel {channel}: your permissions are not valid in that channel." )) return if channel not in bot.channels_connected: bot.send(msg.reply(f'Not in {channel}')) return else: bot.send(msg.reply(f'Parting from {channel}')) m = twitchirc.ChannelMessage('Bye.', 'OUTGOING', channel) m.outgoing = True bot.send(m) bot.flush_single_queue(msg.channel) bot.part(channel)
async def command_perm(msg: StandardizedMessage): p = twitchirc.ArgumentParser(prog='!perm', add_help=False) g = p.add_mutually_exclusive_group(required=True) g.add_argument('-a', '--add', metavar=('USER', 'PERMISSION'), nargs=2, dest='add') g.add_argument('-r', '--remove', metavar=('USER', 'PERMISSION'), nargs=2, dest='remove') g.add_argument('-l', '--list', metavar='USER', const=msg.user, default=None, nargs='?', dest='list') g.add_argument('-f', '--flush', action='store_true', dest='flush') g.add_argument('-h', '--help', action='store_true', dest='help') p.add_argument('-c', '--command', action='store_true', dest='update_for_command') args = p.parse_args(args=msg.text.split(' ')[1:]) if args is None or args.help: usage = f'@{msg.user} {p.format_usage()}' return usage if args.update_for_command: cmd_name = (args.add and args.add[1]) or (args.remove and args.remove[1]) cmd: typing.Optional[util_bot.Command] = None for command in util_bot.bot.commands: if command.chat_command == cmd_name or cmd_name in command.aliases: cmd = command break if cmd: args.add = args.add and (args.add[0], ','.join(cmd.permissions_required)) args.remove = args.remove and (args.remove[0], ','.join(cmd.permissions_required)) else: return f'@{msg.user}, {cmd_name!r}: command not found.' if args.add: return await _perm_add(args, msg) elif args.remove: return await _perm_remove(args, msg) elif args.list: return await _perm_list(args, msg) elif args.flush: return await _perm_flush(args, msg)
async def command_part(msg: StandardizedMessage): if msg.platform != Platform.TWITCH: return f'@{msg.user}, This command only works on Twitch.' p = twitchirc.ArgumentParser(prog=msg.text.split(' ', 1)[0], add_help=False) p.add_argument('channel', metavar='CHANNEL', nargs='?', const=msg.channel, default=msg.channel) p.add_argument('-h', '--help', action='store_true', dest='help') args = p.parse_args(msg.text.split(' ')[1:]) if args is None or args.help: usage = f'@{msg.user} {p.format_usage()}' return usage if args.channel == '': args.channel = msg.channel channel = args.channel.lower() missing_perms = await util_bot.bot.acheck_permissions(msg, [twitchirc.PERMISSION_COMMAND_PART_OTHER], enable_local_bypass=False) if channel != msg.channel.lower() and missing_perms: return (f'Cannot part from channel #{channel}: you don\'t have permissions to run this command from anywhere, ' f'run it in that channel to prove that you have at least moderator permissions there.') if channel not in util_bot.bot.channels_connected: return f'Not in #{channel}' else: await util_bot.bot.part(channel) li = util_bot.bot.storage['channels'] while channel in li: li.remove(channel) util_bot.bot.storage.save() return f'Parted from #{channel}'
import twitchirc try: import plugin_plugin_manager as plugin_manager except ImportError: import plugins.plugin_manager as plugin_manager try: import plugin_plugin_prefixes as plugin_prefixes except ImportError: import plugins.plugin_prefixes as plugin_prefixes __meta_data__ = {'name': 'plugin_whois', 'commands': ['whois']} log = main.make_log_function('whois') whois_parser = twitchirc.ArgumentParser(prog='whois') g = whois_parser.add_mutually_exclusive_group(required=True) g.add_argument('-i', '--id', help='Use the ID instead of using a username', dest='id', type=int) g.add_argument('-n', '--name', help='Use the username', dest='name', type=str) @plugin_manager.add_conditional_alias('whois', plugin_prefixes.condition_prefix_exists) @main.bot.add_command('mb.whois') async def command_whois(msg: twitchirc.ChannelMessage): if not bots or bots_invalidates <= time.time(): await _load_bots()
def command_perm(msg: twitchirc.ChannelMessage): p = twitchirc.ArgumentParser(prog='!perm', add_help=False) g = p.add_mutually_exclusive_group(required=True) g.add_argument('-a', '--add', metavar=('USER', 'PERMISSION'), nargs=2, dest='add') g.add_argument('-r', '--remove', metavar=('USER', 'PERMISSION'), nargs=2, dest='remove') g.add_argument('-l', '--list', metavar='USER', const=msg.user, default=None, nargs='?', dest='list') g.add_argument('-h', '--help', action='store_true', dest='help') args = p.parse_args(args=msg.text.split(' ')[1:]) if args is None or args.help: usage = msg.reply(f'@{msg.user} {p.format_usage()}') bot.send(usage) return if args.add: if bot.check_permissions(msg, [twitchirc.PERMISSION_COMMAND_PERM_ADD], enable_local_bypass=False): bot.send( msg.reply( f"@{msg.user} You cannot use !perm -a, since you don't have" f"the {twitchirc.PERMISSION_COMMAND_PERM_ADD} permission" )) return if args.add[0] not in bot.permissions: bot.permissions[args.add[0]] = [] if args.add[1] not in bot.permissions[args.add[0]]: bot.permissions[args.add[0]].append(args.add[1]) bot.send( msg.reply( f'@{msg.user} Given permission {args.add[1]} to user {args.add[0]}.' )) else: bot.send( msg.reply( f'@{msg.user} User {args.add[0]} already has permission {args.add[1]}.' )) return elif args.remove: if bot.check_permissions( msg, [twitchirc.PERMISSION_COMMAND_PERM_REMOVE], enable_local_bypass=False): bot.send( msg.reply( f"@{msg.user} You cannot use !perm -r, since you don't have" f"the {twitchirc.PERMISSION_COMMAND_PERM_REMOVE} permission" )) return if args.remove[0] not in bot.permissions: bot.permissions[args.remove[0]] = [] if args.remove[1] not in bot.permissions[args.remove[0]]: bot.send( msg.reply(f"@{msg.user} User {args.remove[0]} already " f"doesn't have permission {args.remove[1]}.")) return else: bot.permissions[args.remove[0]].remove(args.remove[1]) bot.send( msg.reply( f'@{msg.user} Removed permission {args.remove[1]} from user {args.remove[0]}.' )) return elif args.list: if bot.check_permissions(msg, [twitchirc.PERMISSION_COMMAND_PERM_LIST]): bot.send( msg.reply( f"@{msg.user} You cannot use !perm -l, since you don't have" f"the {twitchirc.PERMISSION_COMMAND_PERM_LIST} permission" )) return args.list = args.list.lower() if args.list not in bot.permissions: bot.permissions[args.list] = [] if args.list == msg.user: output = ', '.join(bot.permissions.get_permission_state(msg)) bot.send(msg.reply(f'You have permissions: {output}')) else: output = ', '.join(bot.permissions[args.list]) bot.send( msg.reply(f'User {args.list} has permissions: {output}')) return
output = [] all_votes = sum(results.values()) if all_votes == 0: return '[no votes]' winners = [(0, '')] for key, value in results.items(): output.append( f'{value} ({value / all_votes * 100:.2f}%) votes for {key!r}') if value > winners[0][0]: winners = [(value, key)] elif value == winners[0][0]: winners.append((value, key)) return ", ".join(output) + f'. {_nice_winners(winners)}' vote_admin_parser = twitchirc.ArgumentParser(prog='!poll', add_help=False) vap_gr1 = vote_admin_parser.add_mutually_exclusive_group() # name,COMMA-SEPARATED-VOTES vap_gr1.add_argument('-n', '--new', help='Create a poll.', dest='new', metavar=('NAME', 'COMMA_SEPARATED_OPTIONS'), nargs=2) vap_gr1.add_argument('-e', '--end', help='Stop the poll and calculate the results.', dest='end', action='store_true') vote_admin_parser.add_argument('-m', '--allow-multiple',
# ] } TIME_STR_PATTERN = r'(?:([0-9]+)h)?([0-9]+)m(?:([0-9]+)s)?' def process_time(time_string: str) -> int: m = re.match(TIME_STR_PATTERN, time_string) if m: print(m, m[1], m[2], m[3], m[0]) seconds = (((int(m[1]) * 3600) if m[1] is not None else 0) + (int(m[2]) * 60) + ((int(m[3])) if m[3] is not None else 0)) return seconds return -1 c_rem_parser = twitchirc.ArgumentParser(prog='!reminder', add_help=False) c_rem_action = c_rem_parser.add_mutually_exclusive_group(required=True) # { c_rem_action.add_argument('-a', '--add', dest='add', metavar='TEXT', nargs='+') c_rem_action.add_argument('-h', '--help', dest='help', action='store_true') c_rem_action.add_argument('-l', '--list', dest='list', action='store_true') c_rem_action.add_argument('-r', '--remove', dest='remove', metavar='USER') # } c_rem_parser.add_argument('-t', '--time', dest='time', metavar='[0h]10m[0s]', default='0h10m0s') c_rem_parser.add_argument('-nr', '--no-remove', dest='nr',
return True def _command_prefix_get_channel(msg: StandardizedMessage, args: Any): if main.bot.check_permissions(msg, ['util.prefix.set_other'], enable_local_bypass=False): # user doesn't have the permissions needed to the a prefix for another channel, ignore --channel return msg.channel else: if args.channel is not None: args.channel = ''.join(args.channel) return args.channel else: return msg.channel prefix_parser = twitchirc.ArgumentParser(prog='mb.prefix') g = prefix_parser.add_mutually_exclusive_group(required=True) g.add_argument('--query', metavar='CHANNEL', nargs=1, help='Check the prefix set on channel CHANNEL.', dest='query') g.add_argument('--set', metavar='NEW_PREFIX', nargs=1, help='Set the prefix on the current channel ' '(can be overridden by --channel) to ' 'NEW_PREFIX. This action cannot be undone ' 'using the old prefix, be careful', dest='set') g.add_argument('--set-platform', metavar=('NEW_PREFIX', 'PLATFORM'), nargs=2, help='Set the prefix on the current platform ' 'to NEW_PREFIX. This action cannot be undone ' 'using the old prefix, be careful', dest='set_platform')
TIME_DELETION = 60 try: # noinspection PyPackageRequirements import main except ImportError: import util_bot as main exit() import twitchirc __meta_data__ = {'name': 'plugin_simple_command_manager', 'commands': []} log = main.make_log_function('simple_command_manager') add_echo_command_parser = twitchirc.ArgumentParser(prog='mb.add_echo_command') add_echo_command_parser.add_argument('name', metavar='NAME', help='Name of the command') add_echo_command_parser.add_argument( '-s', '--scope', metavar='SCOPE', help='Channels this commands will run in. ' 'You need special permissions to have it ' 'run in channels you don\'t own', default=LOCAL_COMMAND) add_echo_command_parser.add_argument('data', metavar='TEXT', nargs=argparse.REMAINDER, help='Text to be returned.')
lambda inp: inp / 36, UNITS['YARD'] + UNITS['INCH']: lambda inp: inp * 36, # meters to millimeters to inches to feet UNITS['METER'] + UNITS["FOOT"]: lambda inp: inp * 1_000 / 25.4 / 12, UNITS['FOOT'] + UNITS['METER']: lambda inp: inp / 1_000 * 25.4 * 12, UNITS['CENTIMETER'] + UNITS['FOOT']: lambda inp: convert_unit( convert_unit(inp, UNITS['CENTIMETER'], UNITS['METER']), UNITS['METER'], UNITS['FOOT']) } p_conv = twitchirc.ArgumentParser(prog='!convert', add_help=False) p_conv.add_argument('data') p_conv.add_argument('unit_to') @main.bot.add_command('convert') def command_convert(msg: twitchirc.ChannelMessage): argv = shlex.split(main.delete_spammer_chrs(msg.text)) if len(argv) == 1: return f"@{msg.user} " + p_conv.format_usage() args = p_conv.parse_args(argv[1:]) if args is None: return f"@{msg.user} " + p_conv.format_usage() number, unit_from = find_unit_with_data(args.data) unit_to = find_unit(args.unit_to) converted = convert_unit(number, unit_from, unit_to)