def __init__(self, room_name, password, nickname, creator, code_struct, bots, paused=False, pause_text='', uuid=None, variables=None, initialized=False): super().__init__(room_name, password) self.bots = bots # Bot data self.code_struct = code_struct # Bot info self.uuid = uuid or str(uuid_module.uuid4()) self.filename = os.path.join(Snapshot.snapshot_dir, 'current', self.uuid + '.json') self.agent_id = None self.room_name = room_name self.password = password self.nickname = nickname self.creator = creator self.help_text = EuphUtils.mention(self.nickname) + ' is a bot created by "' + creator + '"' + (' using ' + EuphUtils.mention(bots.botbot.nickname) if self.bots.botbot else '') + '.\n\n@' + self.nickname + ' responds to !ping, !help @' + self.nickname + ', and the following regexes:\n' + ('\n'.join(self.code_struct.get_regexes()) if len(self.code_struct.get_regexes()) > 0 else '(None)') + '\n\nTo pause this bot, use the command "!pause ' + EuphUtils.mention(self.nickname) + '".\nTo kill this bot, use the command "!kill ' + EuphUtils.mention(self.nickname) + '".\nThis bot has UUID ' + self.uuid + '.' # Bot state self.paused = paused self.start_time = time.time() self.last_times = set() self.variables = variables or {} self.code_struct.variables = self.variables self.initialized = initialized # Bot state info self.pause_text = pause_text self.generic_pause_text = 'To restore this bot, type "!restore ' + EuphUtils.mention(self.nickname) + '", or to kill this bot, type "!kill ' + EuphUtils.mention(self.nickname) + '".' self.write_to_file()
def interbot(self, mention_name, target_room_name, message, sender, send_time, sender_agent_id, room_name): for bot in self.bots: if EuphUtils.mention(bot.nickname).lower() == EuphUtils.mention( mention_name).lower() and (not target_room_name or bot.room_name.lower() == target_room_name.lower()): bot.recv_message(message, None, None, sender, sender_agent_id, send_time, room_name)
def retrieve(self, nickname=None, mention_name=None, room_name=None): # Ideally, this method should be accessible from the front-end, i.e. # through the !list command in Euphoria. matching_bots = [] for bot in self.bots: if nickname and bot.nickname.lower() != nickname.lower(): continue if mention_name and EuphUtils.mention(bot.nickname).lower() != EuphUtils.mention(mention_name).lower(): continue if room_name and bot.room_name.lower() != room_name.lower(): continue matching_bots.append(bot) return matching_bots
def retrieve(self, nickname=None, mention_name=None, room_name=None): # Ideally, this method should be accessible from the front-end, i.e. # through the !list command in Euphoria. matching_bots = [] for bot in self.bots: if nickname and bot.nickname.lower() != nickname.lower(): continue if mention_name and EuphUtils.mention(bot.nickname).lower( ) != EuphUtils.mention(mention_name).lower(): continue if room_name and bot.room_name.lower() != room_name.lower(): continue matching_bots.append(bot) return matching_bots
def get_description(self, bot=None): if not bot: if len(self.bots) == 0: return '(None)' return '\n'.join(map(self.get_description, self.bots)) return EuphUtils.mention( bot.nickname) + ' (created by "' + bot.creator + '")' + ( '' if self.botbot and self.botbot.room_name == bot.room_name else (' (in &' + bot.room_name.lower() + ')')) + (' (paused)' if bot.paused else '')
def init(self): log.write(EuphUtils.mention(self.nickname) + ' has started.') self.send_chat('Hello, world!') if Snapshot.is_enabled(): messages = Snapshot.load_current(self.bots) for message in messages: self.send_chat(message) else: self.send_chat('Snapshots are not enabled.')
def __init__(self, room_name, password, nickname, creator, code_struct, bots, paused=False, pause_text='', uuid=None, variables=None, initialized=False): super().__init__(room_name, password) self.bots = bots # Bot data self.code_struct = code_struct # Bot info self.uuid = uuid or str(uuid_module.uuid4()) self.filename = os.path.join(Snapshot.snapshot_dir, 'current', self.uuid + '.json') self.agent_id = None self.room_name = room_name self.password = password self.nickname = nickname self.creator = creator self.help_text = EuphUtils.mention( self.nickname ) + ' is a bot created by "' + creator + '"' + ( ' using ' + EuphUtils.mention(bots.botbot.nickname) if self.bots.botbot else '' ) + '.\n\n@' + self.nickname + ' responds to !ping, !help @' + self.nickname + ', and the following regexes:\n' + ( '\n'.join(self.code_struct.get_regexes()) if len(self.code_struct.get_regexes()) > 0 else '(None)' ) + '\n\nTo pause this bot, use the command "!pause ' + EuphUtils.mention( self.nickname ) + '".\nTo kill this bot, use the command "!kill ' + EuphUtils.mention( self.nickname) + '".\nThis bot has UUID ' + self.uuid + '.' # Bot state self.paused = paused self.start_time = time.time() self.last_times = set() self.variables = variables or {} self.code_struct.variables = self.variables self.initialized = initialized # Bot state info self.pause_text = pause_text self.generic_pause_text = 'To restore this bot, type "!restore ' + EuphUtils.mention( self.nickname ) + '", or to kill this bot, type "!kill ' + EuphUtils.mention( self.nickname) + '".' self.write_to_file()
def recv_message(self, content='', parent=None, this_message=None, sender='', sender_agent_id='', send_time=0, room_name=''): if EuphUtils.command('!kill', self.nickname).match(content): if self.bots.is_bot(sender_agent_id): return self.kill(msg_id=this_message) elif self.paused and EuphUtils.command('!restore', self.nickname).match(content): if self.bots.is_bot(sender_agent_id): return self.restore(this_message) elif EuphUtils.command('!pause', self.nickname).match(content): if self.bots.is_bot(sender_agent_id): return if self.paused: self.pause(pause_text='/me is already paused.', set_pause_text=False, reply_to=this_message) else: self.pause(pause_text='/me has been paused by "' + sender + '".', reply_to=this_message) elif self.paused and EuphUtils.command('!help', self.nickname).match(content): self.pause(reply_to=this_message) elif EuphUtils.command('!antighost').match(content): #just reset the nick to the same thing it already is self.change_nick(self.nickname) else: default_variables = { 'sender': sender, '@sender': EuphUtils.mention(sender), 'atsender': EuphUtils.mention(sender), 'self': self.nickname, '@self': EuphUtils.mention(self.nickname), 'atself': EuphUtils.mention(self.nickname), 'creator': self.creator, '@creator': EuphUtils.mention(self.creator), 'atcreator': EuphUtils.mention(self.creator), 'room': room_name, 'uptimeutc': EuphUtils.uptime_utc(self.start_time), 'uptime': EuphUtils.uptime_dhms(self.start_time), 'uuid': self.uuid, 'variables': None, 'groups': None } self.variables.update(default_variables) if not self.paused: messages = self.code_struct.get_messages(content, sender) else: messages = [] current_time = time.time() message = None for message in messages: for i, j in self.variables.items(): message = message.replace('(' + i + ')', str(j)) if len(message) == 0: continue if EuphUtils.command('!ping', '').match(message): continue match = re.match(r'!to\s+@(\S+)(?:\s+&(\S+))?\s+(.*)', message, re.IGNORECASE + re.DOTALL) if match: if self.spam_check(current_time, this_message): self.bots.interbot( match.group(1), match.group(2).lower() if match.group(2) else None, match.group(3), sender, sender_agent_id, send_time, room_name) else: return continue match = re.match(r'!nick\s+(.*)', message, re.IGNORECASE + re.DOTALL) if match: new_nickname = match.group(1) # I run the spam check here to ensure that the bot isn't # spamming nick commands to the server. if self.spam_check(current_time, this_message): self.change_nick(new_nickname) self.variables.update({ 'self': new_nickname, '@self': EuphUtils.mention(new_nickname), }) # Questionable, since there's no guarantee that # the nick change succeeded, but in my opinion, # the case where the nick change fails is an edge # case, and this will behave as expected 99% of # the time. else: return continue match = re.match(r'!var\s+(\S+)(?:\s+=)?\s+(.*)', message, re.IGNORECASE + re.DOTALL) if match: name = match.group(1) value = match.group(2) if name not in default_variables: self.set_variable(name, value) continue match = re.match(r'!delvar\s+(\S+)', message, re.IGNORECASE + re.DOTALL) if match: name = match.group(1) if name not in default_variables: self.del_variable(name) continue match = re.match(r'!resetvars\b', message, re.IGNORECASE + re.DOTALL) if match: self.reset_variables(keep=default_variables) continue match = re.match(r'!inline\s+(.*)', message, re.IGNORECASE + re.DOTALL) if match: if self.spam_check(current_time, this_message): self.send_chat(match.group(1), parent) else: return continue match = re.match(r'!break\b', message, re.IGNORECASE + re.DOTALL) if match: break if self.spam_check(current_time, this_message): self.send_chat(message, this_message) else: return if message is None and content.startswith('!'): if EuphUtils.command('ping', '').match(content[1:]): self.send_chat('Pong!', this_message) elif EuphUtils.command('ping', self.nickname).match(content[1:]): self.send_chat('Pong!', this_message) elif EuphUtils.command('help', self.nickname).match(content[1:]): self.send_chat(self.help_text, this_message) elif EuphUtils.command('uuid', self.nickname).match(content[1:]): self.send_chat('This bot has UUID {0}.'.format(self.uuid), this_message) elif EuphUtils.command('uptime', self.nickname).match(content[1:]): if not self.paused: self.send_chat(EuphUtils.uptime_str(self.start_time), this_message) else: self.send_chat( '/me is paused, so it currently has no uptime.', this_message)
other bots. Type "!help @BotBot" to learn more.\ ''' def main(): botbot = BotBot(room_name, password, nickname, help_text, short_help_text) eu.executable.start(botbot) def get_args(): parser = argparse.ArgumentParser(prog='botbot', description='A meta-bot for Euphoria.', epilog='For details, read the README.md file at https://github.com/ArkaneMoose/BotBot/blob/master/README.md') parser.add_argument('config-file', nargs='?', help='optional path to a JSON configuration file') parser.add_argument('-r', '--room', help='room in Euphoria where @BotBot should reside') parser.add_argument('-p', '--password', help='password for room if necessary') parser.add_argument('-n', '--nickname', help='custom nickname for @BotBot') parser.add_argument('-s', '--snapshot-dir', help='directory where snapshots will be read and written') return parser.parse_args() if __name__ == '__main__': args = vars(get_args()) if args.get('config-file'): with open(args.get('config-file')) as f: config = json.load(f) else: config = {} room_name = args['room'] or config.get('room', room_name) password = args['password'] or config.get('password', password) nickname = args['nickname'] or config.get('nickname', nickname) help_text = config.get('helpText', help_text.replace('@BotBot', EuphUtils.mention(nickname))) short_help_text = config.get('shortHelpText', short_help_text.replace('@BotBot', EuphUtils.mention(nickname))) Snapshot.snapshot_dir = args['snapshot_dir'] or config.get('snapshotDirectory', Snapshot.snapshot_dir) main()
def interbot(self, mention_name, target_room_name, message, sender, send_time, sender_agent_id, room_name): for bot in self.bots: if EuphUtils.mention(bot.nickname).lower() == EuphUtils.mention(mention_name).lower() and (not target_room_name or bot.room_name.lower() == target_room_name.lower()): bot.recv_message(message, None, None, sender, sender_agent_id, send_time, room_name)
def get_description(self, bot=None): if not bot: if len(self.bots) == 0: return '(None)' return '\n'.join(map(self.get_description, self.bots)) return EuphUtils.mention(bot.nickname) + ' (created by "' + bot.creator + '")' + ('' if self.botbot and self.botbot.room_name == bot.room_name else (' (in &' + bot.room_name.lower() + ')')) + (' (paused)' if bot.paused else '')
def recv_message(self, content='', parent=None, this_message=None, sender='', sender_agent_id='', send_time=0, room_name=''): if EuphUtils.command('!kill', self.nickname).match(content): if self.bots.is_bot(sender_agent_id): return self.kill(msg_id=this_message) elif self.paused and EuphUtils.command('!restore', self.nickname).match(content): if self.bots.is_bot(sender_agent_id): return self.restore(this_message) elif EuphUtils.command('!pause', self.nickname).match(content): if self.bots.is_bot(sender_agent_id): return if self.paused: self.pause(pause_text='/me is already paused.', set_pause_text=False, reply_to=this_message) else: self.pause(pause_text='/me has been paused by "' + sender + '".', reply_to=this_message) elif self.paused and EuphUtils.command('!help', self.nickname).match(content): self.pause(reply_to=this_message) elif EuphUtils.command('!antighost').match(content): #just reset the nick to the same thing it already is self.change_nick(self.nickname) elif not self.send_user_messages(content, parent, this_message, sender, sender_agent_id, send_time, room_name) and content.startswith('!'): if EuphUtils.command('ping', '').match(content[1:]): self.send_chat('Pong!', this_message) elif EuphUtils.command('ping', self.nickname).match(content[1:]): self.send_chat('Pong!', this_message) elif EuphUtils.command('help', self.nickname).match(content[1:]): self.send_chat(self.help_text, this_message) elif EuphUtils.command('uuid', self.nickname).match(content[1:]): self.send_chat('This bot has UUID {0}.'.format(self.uuid), this_message) elif EuphUtils.command('uptime', self.nickname).match(content[1:]): if not self.paused: self.send_chat(EuphUtils.uptime_str(self.start_time), this_message) else: self.send_chat('/me is paused, so it currently has no uptime.', this_message)
def handle_chat(self, message): if message.get('truncated'): return if self.bots.is_bot(message['sender']['id']): return if message['content'].startswith('!'): command = message['content'][1:] sender = message['sender']['name'] msg_id = message['id'] # !ping match = EuphUtils.command('ping', '').match(command) if match: self.send_chat('Pong!', msg_id) return # !ping @BotBot match = EuphUtils.command('ping', self.nickname).match(command) if match: self.send_chat('Pong!', msg_id) return # !uptime @BotBot match = EuphUtils.command('uptime', self.nickname).match(command) if match: self.send_chat(EuphUtils.uptime_str(self.start_time), msg_id) return # !list @BotBot match = EuphUtils.command('list', self.nickname).match(command) if match: self.send_chat('Currently running bots created with ' + EuphUtils.mention(self.nickname) + ' (' + self.bots.get_numberofrunningbots() + '):\n' + self.bots.get_description(), msg_id) return # !help match = EuphUtils.command('help', '').match(command) if match: self.send_chat(self.short_help_text, msg_id) return # !help @BotBot match = EuphUtils.command('help', self.nickname).match(command) if match: self.send_chat(self.help_text, msg_id) return # !killall @BotBot match = EuphUtils.command('killall', self.nickname).match(command) if match: self.send_chat('Killing all bots...', msg_id) self.bots.killall() return # !createbot match = EuphUtils.command('createbot').match(command) if match: match = re.match(r'(?:&(\S+)\s+)?@(\S{1,36})\S*\s*(.*)', match.group(1), re.DOTALL) if match: try: self.bots.create(match.group(2), match.group(1).lower() if match.group(1) else self.room_name, None if match.group(1) else self.password, sender, match.group(3)) self.send_chat('Created ' + EuphUtils.mention(match.group(2)) + ((' in &' + match.group(1).lower()) if match.group(1) else '') + '.', msg_id) except: self.send_chat('Failed to create ' + EuphUtils.mention(match.group(2)) + ((' in &' + match.group(1).lower()) if match.group(1) else '') + '. Is your code valid?', msg_id) self.send_chat('Error details:\n' + ''.join(traceback.format_exception_only(sys.exc_info()[0], sys.exc_info()[1])), msg_id) else: self.send_chat('It looks like you are trying to create a bot. Please make sure that you use the syntax described in ' + EuphUtils.mention(self.nickname) + '\'s help text, which can be viewed by sending the command "!help ' + EuphUtils.mention(self.nickname) + '".', msg_id) return #!sendbot match = EuphUtils.command('sendbot').match(command) if match: match = re.match(r'(?:&(\S+)\s+)?@(\S{1,36})\S*(?:\s+(\d+))?', match.group(1), re.DOTALL) if match: destination_room = match.group(1) desired_bot = match.group(2) identifier = match.group(3) matching_bots = self.bots.retrieve(mention_name=desired_bot) if len(matching_bots) == 0: self.send_chat('Sorry, no bots named ' + EuphUtils.mention(desired_bot) + ' were found.', msg_id) elif len(matching_bots) > 1 and not identifier: bot_description_list = [] for bot in matching_bots: bot_description_list.append(str(len(bot_description_list) + 1) + ': ' + self.bots.get_description(bot=bot)) bot_descriptions = '\n'.join(bot_description_list) self.send_chat('Multiple bots named ' + EuphUtils.mention(desired_bot) + ' were found.\n' + 'Select the one you want by sending the command: ' + '"!sendbot' + (' &' + destination_room if destination_room else '') + ' @' + desired_bot + ' [number of desired bot]"\n\n' + bot_descriptions, msg_id) else: try: matching_bot = matching_bots[(int(identifier) - 1) if identifier else 0] try: self.bots.create(matching_bot.nickname, destination_room.lower() if destination_room else self.room_name, None if destination_room else self.password, matching_bot.creator, matching_bot.code_struct.parse_string) self.send_chat('Created ' + EuphUtils.mention(matching_bot.nickname) + ((' in &' + destination_room.lower()) if destination_room else '') + '.', msg_id) except: self.send_chat('Failed to create ' + EuphUtils.mention(matching_bot.nickname) + ((' in &' + destination_room.lower()) if destination_room else '') + '.', msg_id) self.send_chat('Error details:\n' + ''.join(traceback.format_exception_only(sys.exc_info()[0], sys.exc_info()[1])), msg_id) except ValueError: self.send_chat('Unable to parse number of the desired bot.', msg_id) except IndexError: bot_description_list = [] for bot in matching_bots: bot_description_list.append(str(len(bot_description_list) + 1) + ': ' + self.bots.get_description(bot=bot)) bot_descriptions = '\n'.join(bot_description_list) self.send_chat('The number of desired bot is out of range.\n' + 'Select the bot you want by sending the command: ' + '"!sendbot' + (' &' + destination_room if destination_room else '') + ' @' + desired_bot + ' [number of desired bot]"\n\n' + bot_descriptions, msg_id) else: self.send_chat('It looks like you are trying to send a bot. Please make sure that you use the syntax described in ' + EuphUtils.mention(self.nickname) + '\'s help text, which can be viewed by sending the command "!help ' + EuphUtils.mention(self.nickname) + '".', msg_id) return #!save match = EuphUtils.command('save', self.nickname).match(command) if match: messages = Snapshot.create(self.bots) for message in messages: self.send_chat(message, msg_id) return #!load match = EuphUtils.command('load', self.nickname).match(command) if match: if not Snapshot.is_enabled(): self.send_chat('Snapshots are not enabled.', msg_id) return match = re.match(r'([^\s\\/]+\.tar\.gz\b|latest)', match.group(1), re.IGNORECASE) if match: filepath = Snapshot.get_filepath(match.group(0)) else: filepath = None if filepath: self.send_chat('A snapshot will be created so that the current state can be restored if necessary.', msg_id) messages = Snapshot.create(self.bots) for message in messages: self.send_chat(message, msg_id) self.send_chat('Killing all bots...', msg_id) self.bots.killall(False) messages = Snapshot.load(filepath, self.bots) for message in messages: self.send_chat(message, msg_id) else: self.send_chat('Please provide a valid filename.', msg_id) return #!restart match = EuphUtils.command('restart', self.nickname).match(command) if match: self.send_chat('Killing all bots...', msg_id) self.bots.killall(announce=False, delete_file=False) self.send_chat('/me is restarting...', msg_id) self.quit()
help='room in Euphoria where @BotBot should reside') parser.add_argument('-p', '--password', help='password for room if necessary') parser.add_argument('-n', '--nickname', help='custom nickname for @BotBot') parser.add_argument( '-s', '--snapshot-dir', help='directory where snapshots will be read and written') return parser.parse_args() if __name__ == '__main__': args = vars(get_args()) if args.get('config-file'): with open(args.get('config-file')) as f: config = json.load(f) else: config = {} room_name = args['room'] or config.get('room', room_name) password = args['password'] or config.get('password', password) nickname = args['nickname'] or config.get('nickname', nickname) help_text = config.get( 'helpText', help_text.replace('@BotBot', EuphUtils.mention(nickname))) short_help_text = config.get( 'shortHelpText', short_help_text.replace('@BotBot', EuphUtils.mention(nickname))) Snapshot.snapshot_dir = args['snapshot_dir'] or config.get( 'snapshotDirectory', Snapshot.snapshot_dir) main()
def send_user_messages(self, content='', parent=None, this_message=None, sender='', sender_agent_id='', send_time=0, room_name='', init=False): default_variables = { 'sender': sender, '@sender': EuphUtils.mention(sender), 'atsender': EuphUtils.mention(sender), 'self': self.nickname, '@self': EuphUtils.mention(self.nickname), 'atself': EuphUtils.mention(self.nickname), 'creator': self.creator, '@creator': EuphUtils.mention(self.creator), 'atcreator': EuphUtils.mention(self.creator), 'room': room_name, 'uptimeutc': EuphUtils.uptime_utc(self.start_time), 'uptime': EuphUtils.uptime_dhms(self.start_time), 'uuid': self.uuid, 'variables': None, 'groups': None } self.variables.update(default_variables) if not self.paused: if init: messages = self.code_struct.get_init_messages() else: messages = self.code_struct.get_messages(content, sender) else: messages = [] current_time = time.time() message = None for message in messages: for i, j in self.variables.items(): message = message.replace('(' + i + ')', str(j)) if len(message) == 0: continue if EuphUtils.command('!ping', '').match(message): continue match = re.match(r'!to\s+@(\S+)(?:\s+&(\S+))?\s+(.*)', message, re.IGNORECASE + re.DOTALL) if match: if self.spam_check(current_time, this_message): self.bots.interbot( match.group(1), match.group(2).lower() if match.group(2) else None, match.group(3), sender, sender_agent_id, send_time, room_name) else: break continue match = re.match(r'!nick\s+(.*)', message, re.IGNORECASE + re.DOTALL) if match: new_nickname = match.group(1) # I run the spam check here to ensure that the bot isn't # spamming nick commands to the server. if self.spam_check(current_time, this_message): self.change_nick(new_nickname) self.variables.update({ 'self': new_nickname, '@self': EuphUtils.mention(new_nickname), }) # Questionable, since there's no guarantee that # the nick change succeeded, but in my opinion, # the case where the nick change fails is an edge # case, and this will behave as expected 99% of # the time. else: break continue match = re.match(r'!var\s+(\S+)(?:\s+=)?\s+(.*)', message, re.IGNORECASE + re.DOTALL) if match: name = match.group(1) value = match.group(2) if name not in default_variables: self.set_variable(name, value) continue match = re.match(r'!delvar\s+(\S+)', message, re.IGNORECASE + re.DOTALL) if match: name = match.group(1) if name not in default_variables: self.del_variable(name) continue match = re.match(r'!resetvars\b', message, re.IGNORECASE + re.DOTALL) if match: self.reset_variables(keep=default_variables) continue match = re.match(r'!inline\s+(.*)', message, re.IGNORECASE + re.DOTALL) if match: if self.spam_check(current_time, this_message): self.send_chat(match.group(1), parent or this_message) else: break continue match = re.match(r'!break\b', message, re.IGNORECASE + re.DOTALL) if match: break if self.spam_check(current_time, this_message): self.send_chat(message, this_message) else: break return message is not None
def send_user_messages(self, content='', parent=None, this_message=None, sender='', sender_agent_id='', send_time=0, room_name='', init=False): default_variables = { 'sender': sender, '@sender': EuphUtils.mention(sender), 'atsender': EuphUtils.mention(sender), 'self': self.nickname, '@self': EuphUtils.mention(self.nickname), 'atself': EuphUtils.mention(self.nickname), 'creator': self.creator, '@creator': EuphUtils.mention(self.creator), 'atcreator': EuphUtils.mention(self.creator), 'room': room_name, 'uptimeutc': EuphUtils.uptime_utc(self.start_time), 'uptime': EuphUtils.uptime_dhms(self.start_time), 'uuid': self.uuid, 'variables': None, 'groups': None } self.variables.update(default_variables) if not self.paused: if init: messages = self.code_struct.get_init_messages() else: messages = self.code_struct.get_messages(content, sender) else: messages = [] current_time = time.time() message = None for message in messages: for i, j in self.variables.items(): message = message.replace('(' + i + ')', str(j)) if len(message) == 0: continue if EuphUtils.command('!ping', '').match(message): continue match = re.match(r'!to\s+@(\S+)(?:\s+&(\S+))?\s+(.*)', message, re.IGNORECASE + re.DOTALL) if match: if self.spam_check(current_time, this_message): self.bots.interbot(match.group(1), match.group(2).lower() if match.group(2) else None, match.group(3), sender, sender_agent_id, send_time, room_name) else: break continue match = re.match(r'!nick\s+(.*)', message, re.IGNORECASE + re.DOTALL) if match: new_nickname = match.group(1) # I run the spam check here to ensure that the bot isn't # spamming nick commands to the server. if self.spam_check(current_time, this_message): self.change_nick(new_nickname) self.variables.update({ 'self': new_nickname, '@self': EuphUtils.mention(new_nickname), }) # Questionable, since there's no guarantee that # the nick change succeeded, but in my opinion, # the case where the nick change fails is an edge # case, and this will behave as expected 99% of # the time. else: break continue match = re.match(r'!var\s+(\S+)(?:\s+=)?\s+(.*)', message, re.IGNORECASE + re.DOTALL) if match: name = match.group(1) value = match.group(2) if name not in default_variables: self.set_variable(name, value) continue match = re.match(r'!delvar\s+(\S+)', message, re.IGNORECASE + re.DOTALL) if match: name = match.group(1) if name not in default_variables: self.del_variable(name) continue match = re.match(r'!resetvars\b', message, re.IGNORECASE + re.DOTALL) if match: self.reset_variables(keep=default_variables) continue match = re.match(r'!inline\s+(.*)', message, re.IGNORECASE + re.DOTALL) if match: if self.spam_check(current_time, this_message): self.send_chat(match.group(1), parent or this_message) else: break continue match = re.match(r'!break\b', message, re.IGNORECASE + re.DOTALL) if match: break if self.spam_check(current_time, this_message): self.send_chat(message, this_message) else: break return message is not None
def recv_message(self, content='', parent=None, this_message=None, sender='', sender_agent_id='', send_time=0, room_name=''): if EuphUtils.command('!kill', self.nickname).match(content): if self.bots.is_bot(sender_agent_id): return self.kill(msg_id=this_message) elif self.paused and EuphUtils.command('!restore', self.nickname).match(content): if self.bots.is_bot(sender_agent_id): return self.restore(this_message) elif EuphUtils.command('!pause', self.nickname).match(content): if self.bots.is_bot(sender_agent_id): return if self.paused: self.pause(pause_text='/me is already paused.', set_pause_text=False, reply_to=this_message) else: self.pause(pause_text='/me has been paused by "' + sender + '".', reply_to=this_message) elif self.paused and EuphUtils.command('!help', self.nickname).match(content): self.pause(reply_to=this_message) elif EuphUtils.command('!antighost').match(content): #just reset the nick to the same thing it already is self.change_nick(self.nickname) elif not self.send_user_messages( content, parent, this_message, sender, sender_agent_id, send_time, room_name) and content.startswith('!'): if EuphUtils.command('ping', '').match(content[1:]): self.send_chat('Pong!', this_message) elif EuphUtils.command('ping', self.nickname).match(content[1:]): self.send_chat('Pong!', this_message) elif EuphUtils.command('help', self.nickname).match(content[1:]): self.send_chat(self.help_text, this_message) elif EuphUtils.command('uuid', self.nickname).match(content[1:]): self.send_chat('This bot has UUID {0}.'.format(self.uuid), this_message) elif EuphUtils.command('uptime', self.nickname).match(content[1:]): if not self.paused: self.send_chat(EuphUtils.uptime_str(self.start_time), this_message) else: self.send_chat( '/me is paused, so it currently has no uptime.', this_message)