class CharacterHistoryController: def __init__(self): pass def inject(self, registry): self.db: DB = registry.get_instance("db") self.text: Text = registry.get_instance("text") self.pork_manager = registry.get_instance("pork_manager") @command(command="history", params=[Any("character"), Int("server_num")], access_level="all", description="Get history of character for a specific server num") def handle_history_cmd1(self, channel, sender, reply, args): name = args[0].capitalize() server_num = args[1] reply(self.get_character_history(name, server_num)) @command(command="history", params=[Any("character")], access_level="all", description="Get history of character for the current server num", sub_command="list") def handle_history_cmd2(self, channel, sender, reply, args): name = args[0].capitalize() reply(self.get_character_history(name, 5)) def get_character_history(self, name, server_num): return "<header>" + name + " (" + str(server_num) + ")<end>"
class AliasController: def __init__(self): pass def inject(self, registry): self.command_alias_manager = registry.get_instance( "command_alias_manager") def start(self): pass @command(command="alias", params=[Const("list")], access_level="all", description="List command aliases") def alias_list_cmd(self, channel, sender, reply, args): blob = "" data = self.command_alias_manager.get_enabled_aliases() count = 0 for row in data: count += 1 blob += row['alias'] + " - " + row['command'] + "\n" reply(ChatBlob("Aliases (%d)" % count, blob)) @command(command="alias", params=[Const("add"), Any("alias"), Any("command")], access_level="superadmin", description="Add a command alias", sub_command="modify") def alias_add_cmd(self, channel, sender, reply, args): alias = args[1] command = args[2] if self.command_alias_manager.add_alias(alias, command): reply( "Alias <highlight>%s<end> for command <highlight>%s<end> added successfully." % (alias, command)) else: reply( "Cannot add alias <highlight>%s<end> since there is already an active alias with that name." % alias) @command(command="alias", params=[Options(["rem", "remove"]), Any("alias")], access_level="superadmin", description="Remove a command alias", sub_command="modify") def alias_remove_cmd(self, channel, sender, reply, args): alias = args[1] if self.command_alias_manager.remove_alias(alias): reply("Alias <highlight>%s<end> has been removed successfully." % alias) else: reply("Could not find alias <highlight>%s<end>." % alias)
class PrivateChannelController: def __init__(self): pass def inject(self, registry): self.bot = registry.get_instance("mangopie") self.private_channel_manager = registry.get_instance( "private_channel_manager") self.character_manager = registry.get_instance("character_manager") @command(command="join", params=[], access_level="member", description="Join the private channel") def join_cmd(self, channel, sender, reply, args): self.private_channel_manager.invite(sender.char_id) @command(command="leave", params=[], access_level="all", description="Leave the private channel") def leave_cmd(self, channel, sender, reply, args): self.private_channel_manager.kick(sender.char_id) @command(command="invite", params=[Any("character")], access_level="member", description="Invite a character to the private channel") def invite_cmd(self, channel, sender, reply, args): char = args[0].capitalize() char_id = self.character_manager.resolve_char_to_id(char) if sender.char_id == char_id: self.private_channel_manager.invite(sender.char_id) elif char_id: self.bot.send_private_message( char_id, "You have been invited to the private channel by <highlight>%s<end>." % sender.name) self.private_channel_manager.invite(char_id) reply( "You have invited <highlight>%s<end> to the private channel." % char) else: reply("Could not find character <highlight>%s<end>." % char) @event(PrivateChannelManager.JOINED_PRIVATE_CHANNEL_EVENT, "Notify private channel when someone joins") def private_channel_joined_event(self, event_type, event_data): char_name = self.character_manager.get_char_name(event_data.char_id) self.bot.send_private_channel_message( "<highlight>%s<end> has joined the private channel." % char_name) @event(PrivateChannelManager.LEFT_PRIVATE_CHANNEL_EVENT, "Notify private channel when someone leaves") def private_channel_left_event(self, event_type, event_data): char_name = self.character_manager.get_char_name(event_data.char_id) self.bot.send_private_channel_message( "<highlight>%s<end> has left the private channel." % char_name)
class CountdownController: def __init__(self): pass def inject(self, registry): self.job_scheduler = registry.get_instance("job_scheduler") @command(command="countdown", params=[Any("message", is_optional=True)], access_level="all", description="Start a 5-second countdown") def whois_cmd(self, channel, sender, reply, args): message = args[0] if args[0] else "GO GO GO" message_format = "%s-------> %s <-------<end>" self.job_scheduler.delayed_job(self.show_countdown, 1, reply, message_format, "<red>", "5") self.job_scheduler.delayed_job(self.show_countdown, 2, reply, message_format, "<red>", "4") self.job_scheduler.delayed_job(self.show_countdown, 3, reply, message_format, "<orange>", "3") self.job_scheduler.delayed_job(self.show_countdown, 4, reply, message_format, "<orange>", "2") self.job_scheduler.delayed_job(self.show_countdown, 5, reply, message_format, "<orange>", "1") self.job_scheduler.delayed_job(self.show_countdown, 6, reply, message_format, "<green>", message) def show_countdown(self, timestamp, reply, message_format, color, message): reply(message_format % (color, message))
class AltsController: def __init__(self): pass def inject(self, registry): self.alts_manager = registry.get_instance("alts_manager") self.character_manager = registry.get_instance("character_manager") def start(self): pass @command(command="alts", params=[], access_level="all", description="Show your alts") def alts_list_cmd(self, channel, sender, reply, args): count = 0 alts = self.alts_manager.get_alts(sender.char_id) blob = "" for alt in alts: count += 1 blob += "<highlight>%s<end> (%s/<green>%s<end>) %s %s%s\n" % ( alt['char'][0]['name'], alt['char'][0]['level'], alt['char'][0]['ai_level'], alt['char'][0]['faction'], alt['char'][0]['profession'], self.get_alt_status(alt["status"])) reply(ChatBlob("Alts for %s (%d)" % (sender.name, count), blob)) def get_alt_status(self, status): if status == AltsManager.MAIN: return " - [main]" elif status == AltsManager.VALIDATED: return "" else: return " - [unvalidated]" @command(command="alts", params=[Const("add"), Any("character")], access_level="all", description="Add an alt") def alts_add_cmd(self, channel, sender, reply, args): alt = args[1].capitalize() alt_char_id = self.character_manager.resolve_char_to_id(alt) if not alt_char_id: reply("Could not find character <highlight>%s<end>." % alt) elif self.alts_manager.add_alt(sender.char_id, alt_char_id): reply("<highlight>%s<end> added as alt successfully." % alt) else: reply("Could not add <highlight>%s<end> as alt." % alt) @command(command="alts", params=[Options(["rem", "remove"]), Any("character")], access_level="all", description="Remove an alt") def alts_remove_cmd(self, channel, sender, reply, args): alt = args[1].capitalize() alt_char_id = self.character_manager.resolve_char_to_id(alt) if not alt_char_id: reply("Could not find character <highlight>%s<end>." % alt) elif self.alts_manager.remove_alt(sender.char_id, alt_char_id): reply("<highlight>%s<end> removed as alt successfully." % alt) else: reply("Could not remove <highlight>%s<end> as alt." % alt) @command(command="alts", params=[Any("character")], access_level="all", description="Show alts of another character") def alts_list_other_cmd(self, channel, sender, reply, args): name = args[0].capitalize() char_id = self.character_manager.resolve_char_to_id(name) if not char_id: reply("Could not find character <highlight>%s<end>." % name) return alts = self.alts_manager.get_alts(char_id) count = 0 blob = "" for alt in alts: count += 1 blob += "<highlight>%s<end> (%d/<green>%d<end>) %s %s\n" % ( alt['char'][0]['name'], alt['char'][0]['level'], alt['char'][0]['ai_level'], alt['char'][0]['faction'], alt['char'][0]['profession']) reply(ChatBlob("Alts for %s (%d)" % (name, count), blob))
class ConfigController: def __init__(self): pass def inject(self, registry): self.db: DB = registry.get_instance("db") self.text: Text = registry.get_instance("text") self.access_manager = registry.get_instance("access_manager") self.command_manager = registry.get_instance("command_manager") self.event_manager = registry.get_instance("event_manager") self.setting_manager = registry.get_instance("setting_manager") def start(self): pass @command(command="config", params=[], access_level="superadmin", description="Shows configuration options for the bot") def config_list_cmd(self, channel, sender, reply, args): pipeline = [{ '$project': { 'module': 1, 'count_enabled': { '$cond': { 'if': { '$eq': ['$enabled', 1] }, 'then': 1, 'else': 0 } }, 'count_disabled': { '$cond': { 'if': { '$eq': ['$enabled', 0] }, 'then': 1, 'else': 0 } } } }, { '$group': { '_id': '$module', 'count_enabled': { '$sum': '$count_enabled' }, 'count_disabled': { '$sum': '$count_disabled' } } }, { '$sort': { '_id': 1 } }] cmd_config = self.db.client['command_config'].aggregate(pipeline) event_config = self.db.client['event_config'].aggregate(pipeline) settings = self.db.client['settings'].aggregate(pipeline) final_list = [] lists = [cmd_config, event_config, settings] for lst in lists: for dct in lst: id_to_lookup = dct['_id'] found_duplicate_flag = 0 for final_dict in final_list: if final_dict['_id'] == id_to_lookup: found_duplicate_flag = 1 final_dict['count_enabled'] += dct['count_enabled'] final_dict['count_disabled'] += dct['count_disabled'] break if found_duplicate_flag == 0: final_list.append(dct) count = 0 blob = "" current_group = "" for row in sorted(final_list, key=itemgetter('_id')): count += 1 parts = row['_id'].split(".") group = parts[0] module = parts[1] if group != current_group: current_group = group blob += "\n<header2>" + current_group + "<end>\n" blob += self.text.make_chatcmd( module, "/tell <myname> config mod " + row['_id']) + " " if row['count_enabled'] > 0 and row['count_disabled'] > 0: blob += "<yellow>Partial<end>" elif row['count_disabled'] == 0: blob += "<green>Enabled<end>" else: blob += "<red>Disabled<end>" blob += "\n" reply(ChatBlob("Config (%d)" % count, blob)) @command(command="config", params=[Const("mod"), Any("module_name")], access_level="superadmin", description="Shows configuration options for a specific module") def config_module_list_cmd(self, channel, sender, reply, args): module = args[1].lower() blob = "" data = self.db.client['settings'].find({ 'module': module }).sort('name', 1) data = list(data) if data: blob += "<header2>Settings<end>\n" for row in data: setting = self.setting_manager.get(row['name']) blob += setting.get_description( ) + ": " + self.text.make_chatcmd( setting.get_display_value(), "/tell <myname> config setting " + row['name']) + "\n" data = self.db.client['command_config'].find({ 'module': module }, { '_id': 0, 'channel': 0 }).sort('command', 1) d = list(data) data = [i for n, i in enumerate(d) if i not in d[n + 1:]] if data: blob += "\n<header2>Commands<end>\n" for row in data: command_key = self.command_manager.get_command_key( row['command'], row['sub_command']) blob += self.text.make_chatcmd( command_key, "/tell <myname> config cmd " + command_key) + "\n" data = self.db.client['event_config'].find({ 'module': module }).sort([('event_type', 1), ('handler', 1)]) data = list(data) if data: blob += "\n<header2>Events<end>\n" for row in data: event_type_key = self.event_manager.get_event_type_key( row['event_type'], row['event_sub_type']) blob += row['event_type'] + " - " + row['description'] blob += " " + self.text.make_chatcmd( "On", "/tell <myname> config event " + event_type_key + " " + row['handler'] + " enable") blob += " " + self.text.make_chatcmd( "Off", "/tell <myname> config event " + event_type_key + " " + row['handler'] + " disable") blob += "\n" if blob: reply(ChatBlob("Module (" + module + ")", blob)) else: reply("Could not find module <highlight>%s<end>" % module) @command(command="config", params=[ Const("event"), Any("event_type"), Any("event_handler"), Options(["enable", "disable"]) ], access_level="superadmin", description="Enable or disable an event") def config_event_status_cmd(self, channel, sender, reply, args): event_type = args[1].lower() event_handler = args[2].lower() action = args[3].lower() event_base_type, event_sub_type = self.event_manager.get_event_type_parts( event_type) enabled = 1 if action == "enable" else 0 if not self.event_manager.is_event_type(event_base_type): reply("Unknown event type <highlight>%s<end>." % event_type) return count = self.db.update( 'event_config', { 'event_type': event_base_type, 'event_sub_type': event_sub_type, 'handler': event_handler }, {'enabled': enabled}) if count.matched_count == 0: reply( "Could not find event for type <highlight>%s<end> and handler <highlight>%s<end>." % (event_type, event_handler)) else: reply( "Event type <highlight>%s<end> for handler <highlight>%s<end> has been <highlight>%sd<end> successfully." % (event_type, event_handler, action)) @command(command="config", params=[Const("setting"), Any("setting_name"), Any("new_value")], access_level="superadmin", description="Sets new value for a setting") def config_setting_update_cmd(self, channel, sender, reply, args): setting_name = args[1].lower() new_value = args[2] setting = self.setting_manager.get(setting_name) if setting: try: setting.set_value(new_value) reply( "Setting <highlight>%s<end> has been set to <highlight>%s<end>." % (setting_name, new_value)) except Exception as e: reply("Error! %s" % str(e)) else: reply("Could not find setting <highlight>%s<end>." % setting_name) @command(command="config", params=[Const("setting"), Any("setting_name")], access_level="superadmin", description="Shows configuration options for a setting") def config_setting_show_cmd(self, channel, sender, reply, args): setting_name = args[1].lower() blob = "" setting = self.setting_manager.get(setting_name) if setting: blob += "Current Value: <highlight>%s<end>\n" % str( setting.get_display_value()) blob += "Description: <highlight>%s<end>\n\n" % setting.get_description( ) blob += setting.get_display() reply(ChatBlob("Setting (%s)" % setting_name, blob)) else: reply("Could not find setting <highlight>%s<end>." % setting_name)
class HelpController: def __init__(self): pass def inject(self, registry): self.text = registry.get_instance("text") self.db = registry.get_instance("db") self.access_manager = registry.get_instance("access_manager") self.command_manager = registry.get_instance("command_manager") self.command_alias_manager = registry.get_instance( "command_alias_manager") def start(self): pass @command(command="help", params=[], access_level="all", description="Show a list of commands to get help with") def help_list_cmd(self, channel, sender, reply, args): data = self.db.client['command_config'].find().sort([('module', 1), ('command', 1)]) blob = "" current_group = "" current_module = "" current_command = "" access_level = self.access_manager.get_access_level(sender.char_id) for row in data: if access_level[ "level"] > self.access_manager.get_access_level_by_label( row['access_level'])["level"]: continue parts = row['module'].split(".") group = parts[0] module = parts[1] if group != current_group: current_group = group blob += "\n\n<header2>" + current_group + "<end>" if module != current_module: current_module = module blob += "\n" + module + ":" if row['command'] != current_command: current_command = row['command'] blob += " " + self.text.make_chatcmd( row['command'], "/tell <myname> help " + row['command']) reply(ChatBlob("Help (main)", blob)) @command(command="help", params=[Any("command")], access_level="all", description="Show help for a specific command") def help_detail_cmd(self, channel, sender, reply, args): help_topic = args[0].lower() # check for alias alias = self.command_alias_manager.check_for_alias(help_topic) if alias: help_topic = alias help_text = self.command_manager.get_help_text(sender.char_id, help_topic, channel) if help_text: reply(self.command_manager.format_help_text(help_topic, help_text)) else: reply("Could not find help on <highlight>" + help_topic + "<end>.")
class CharacterInfoController: def __init__(self): pass def inject(self, registry): self.db: DB = registry.get_instance("db") self.text: Text = registry.get_instance("text") self.pork_manager = registry.get_instance("pork_manager") self.character_manager = registry.get_instance("character_manager") @command(command="whois", params=[Any("character")], access_level="all", description="Get whois information for a character") def whois_cmd(self, channel, sender, reply, args): char_name = args[1].capitalize() char_info = self.pork_manager.get_character_info(char_name) char_id = self.character_manager.resolve_char_to_id(char_name) if char_info: blob = "Name: %s\n" % self.get_full_name(char_info) blob += "Profession: %s\n" % char_info['profession'] blob += "Faction: %s\n" % char_info['faction'] blob += "Breed: %s\n" % char_info['breed'] blob += "Gender: %s\n" % char_info['gender'] blob += "Level: %d\n" % char_info['level'] blob += "AI Level: %d\n" % char_info['ai_level'] if char_info['org_id']: blob += "Org: %s (%d)\n" % (char_info['org_name'], char_info['org_id']) blob += "Org Rank: %s (%d)\n" % (char_info['org_rank_name'], char_info['org_rank_id']) else: blob += "Org: <None>\n" blob += "Org Rank: <None>\n" blob += "Head Id: %d\n" % char_info['head_id'] blob += "PVP Rating: %d\n" % char_info['pvp_rating'] blob += "PVP Title: %s\n" % char_info['pvp_title'] blob += "Character Id: %d\n" % char_info['char_id'] blob += "Source: %s\n" % char_info['source'] more_info = self.text.paginate("More Info", blob, 5000, 1)[0] msg = self.format_char_info(char_info) + " " + more_info reply(msg) elif char_id: blob = "<notice>Note: Could not retrieve detailed info for character.<end>\n\n" blob += "Name: <highlight>%s<end>\n" % char_name blob += "Character ID: <highlight>%d<end>" % char_id reply(ChatBlob("Basic Info for %s" % char_name, blob)) else: reply("Could not find info for character <highlight>%s<end>." % char_name) def get_full_name(self, char_info): name = "" if char_info['first_name']: name += char_info['first_name'] + " " name += "\"" + char_info['name'] + "\"" if char_info['last_name']: name += " " + char_info['last_name'] return name def format_char_info(self, char_info): if char_info['org_name'] and char_info['org_rank_name']: return "<highlight>%s<end> (%d/<green>%d<end>) %s %s, %s of %s" % \ (char_info['name'], char_info['level'], char_info['ai_level'], char_info['faction'], char_info['profession'], char_info['org_rank_name'], char_info['org_name']) else: return "<highlight>%s<end> (%d/<green>%d<end>) %s %s" % ( char_info['name'], char_info['level'], char_info['ai_level'], char_info['faction'], char_info['profession'])
class SystemController: def __init__(self): pass def inject(self, registry): self.bot = registry.get_instance("mangopie") self.access_manager = registry.get_instance("access_manager") self.command_manager = registry.get_instance("command_manager") def start(self): pass @command(command="shutdown", params=[], access_level="superadmin", description="Shutdown the bot") def shutdown_cmd(self, channel, sender, reply, args): msg = "The bot is shutting down..." self.bot.send_org_message(msg) self.bot.send_private_channel_message(msg) if channel not in [ CommandManager.ORG_CHANNEL, CommandManager.PRIVATE_CHANNEL ]: reply(msg) self.bot.shutdown() @command(command="restart", params=[], access_level="superadmin", description="Restart the bot") def restart_cmd(self, channel, sender, reply, args): msg = "The bot is restarting..." self.bot.send_org_message(msg) self.bot.send_private_channel_message(msg) if channel not in [ CommandManager.ORG_CHANNEL, CommandManager.PRIVATE_CHANNEL ]: reply(msg) self.bot.restart() @command(command="checkaccess", params=[Any("character", is_optional=True)], access_level="all", description="Check access level for a character") def checkaccess_cmd(self, channel, sender, reply, args): char_name = args[0].capitalize() if args[0] else sender.name access_level = self.access_manager.get_access_level(char_name) if access_level: reply( "Access level for <highlight>%s<end> is <highlight>%s<end>." % (char_name, access_level["label"])) else: reply("Could not find character <highlight>%s<end>." % char_name) @command(command="macro", params=[Any("command 1|command 2|command 3 ...")], access_level="all", description="Execute multiple commands at once") def macro_cmd(self, channel, sender, reply, args): commands = args[0].split("|") for command_str in commands: self.command_manager.process_command(command_str, channel, sender.char_id, reply)
class ConfigCommandController: def __init__(self): pass def inject(self, registry): self.db: DB = registry.get_instance("db") self.text: Text = registry.get_instance("text") self.access_manager = registry.get_instance("access_manager") self.command_manager = registry.get_instance("command_manager") def start(self): pass @command(command="config", params=[ Const("cmd"), Any("cmd_name"), Options(["enable", "disable"]), Any("channel") ], access_level="superadmin", description="Enable or disable a command") def config_cmd_status_cmd(self, channel, sender, reply, args): cmd_name = args[1].lower() action = args[2].lower() cmd_channel = args[3].lower() command_str, sub_command_str = self.command_manager.get_command_key_parts( cmd_name) enabled = 1 if action == "enable" else 0 if cmd_channel != "all" and not self.command_manager.is_command_channel( cmd_channel): reply("Unknown command channel <highlight>%s<end>." % cmd_channel) return query = {'command': command_str, 'sub_command': sub_command_str} if cmd_channel != "all": query['channel'] = cmd_channel count = self.db.update_all('command_config', query, {'enabled': enabled}) if count.matched_count == 0: reply( "Could not find command <highlight>%s<end> for channel <highlight>%s<end>." % (cmd_name, cmd_channel)) else: if cmd_channel == "all": reply( "Command <highlight>%s<end> has been <highlight>%sd<end> successfully." % (cmd_name, action)) else: reply( "Command <highlight>%s<end> for channel <highlight>%s<end> has been <highlight>%sd<end> successfully." % (cmd_name, channel, action)) @command(command="config", params=[ Const("cmd"), Any("cmd_name"), Const("access_level"), Any("channel"), Any("access_level") ], access_level="superadmin", description="Change access_level for a command") def config_cmd_access_level_cmd(self, channel, sender, reply, args): cmd_name = args[1].lower() cmd_channel = args[3].lower() access_level = args[4].lower() command_str, sub_command_str = self.command_manager.get_command_key_parts( cmd_name) if cmd_channel != "all" and not self.command_manager.is_command_channel( cmd_channel): reply("Unknown command channel <highlight>%s<end>." % cmd_channel) return if self.access_manager.get_access_level_by_label(access_level) is None: reply("Unknown access level <highlight>%s<end>." % access_level) return query = {'command': command_str, 'sub_command': sub_command_str} if cmd_channel != "all": query['channel'] = cmd_channel count = self.db.update_all('command_config', query, {'access_level': access_level}) if count.matched_count == 0: reply( "Could not find command <highlight>%s<end> for channel <highlight>%s<end>." % (cmd_name, cmd_channel)) else: if cmd_channel == "all": reply( "Access level <highlight>%s<end> for command <highlight>%s<end> has been set successfully." % (access_level, cmd_name)) else: reply( "Access level <highlight>%s<end> for command <highlight>%s<end> on channel <highlight>%s<end> has been set successfully." % (access_level, cmd_name, channel)) @command(command="config", params=[Const("cmd"), Any("cmd_name")], access_level="superadmin", description="Show command configuration") def config_cmd_show_cmd(self, channel, sender, reply, args): cmd_name = args[1].lower() command_str, sub_command_str = self.command_manager.get_command_key_parts( cmd_name) blob = "" for command_channel, channel_label in self.command_manager.channels.items( ): cmds = self.command_manager.get_command_configs( command=command_str, sub_command=sub_command_str, channel=command_channel, enabled=None) cmd_configs = list(cmds) if len(cmd_configs) > 0: cmd_config = cmd_configs[0] if cmd_config['enabled'] == 1: status = "<green>Enabled<end>" else: status = "<red>Disabled<end>" blob += "<header2>%s<end> %s (Access Level: %s)\n" % ( channel_label, status, cmd_config['access_level'].capitalize()) # show status config blob += "Status:" enable_link = self.text.make_chatcmd( "Enable", "/tell <myname> config cmd %s enable %s" % (cmd_name, command_channel)) disable_link = self.text.make_chatcmd( "Disable", "/tell <myname> config cmd %s disable %s" % (cmd_name, command_channel)) blob += " " + enable_link + " " + disable_link # show access level config blob += "\nAccess Level:" for access_level in self.access_manager.access_levels: # skip "None" access level if access_level["level"] == 0: continue label = access_level["label"] link = self.text.make_chatcmd( label.capitalize(), "/tell <myname> config cmd %s access_level %s %s" % (cmd_name, command_channel, label)) blob += " " + link blob += "\n" blob += "\n\n" if blob: # include help text blob += "\n\n".join( map(lambda handler: handler["help"], self.command_manager.get_handlers(cmd_name))) reply(ChatBlob("Command (%s)" % cmd_name, blob))