async def message_filter(self, bot, source, target, message, highlighted): if (message == 'leave' and highlighted) or message == '!leave': # respond to !leave if source in prefs_singleton.read_value('channels'): prefs_singleton.write_value(prefs_singleton.read_value('channels').remove(source)) await bot.part(source) return True return False
async def control_plugins(bot, source, target, message, highlighted): if not ((message.startswith('disable') and highlighted) or message.startswith('!disable') or (message.startswith('enable') and highlighted) or message.startswith('!enable') or (message.startswith('toggle') and highlighted) or message.startswith('!toggle')): return False request_type = message.split()[0].lower().strip() if request_type.startswith('!'): request_type = request_type[1:] if len(message.split()) < 2: request = '' else: request = message.split()[1].lower().strip() if target != prefs_singleton.read_value('botownernick'): await bot.message(source, f"{target}: you're not {prefs_singleton.read_value('botownernick')}!") return True for plugin in bot.plugins: plugin_name = type(plugin).__name__.lower() if plugin_name == request: if request_type == 'disable': plugin.disable() if request_type == 'enable': plugin.enable() if request_type == 'toggle': if plugin.is_enabled(): plugin.disable() else: plugin.enable() await bot.message(source, f'{plugin_name} changed, now: {str(plugin.is_enabled()).upper()}') break else: await bot.message(source, f'no plugin named "{request}" was found!') return True
def _try_read_key(self): api_key = prefs_singleton.read_value( type(self).__name__ + '_wolfram_api_key') if not api_key: prefs_singleton.write_value( type(self).__name__ + '_wolfram_api_key', '') return api_key
async def message_filter(self, bot, source, target, message, highlighted): if target not in prefs_singleton.read_value('whitelistnicks'): message = message.lower() # hiss at buzzfeed/huffpost, characters greater than 128, and on the word 'moist' if 'buzzfeed.com' in message or 'huffingtonpost.com' in message: await bot.message( source, 'hisss f**k off with your huffpost buzzfeed crap') elif not all(ord(c) < 128 for c in message) or 'moist' in message: await bot.message(source, 'hisss') return False
def __init__(self): bot_nick = prefs_singleton.read_value('botnick') bot_real_name = prefs_singleton.read_value('botrealname') server_address = prefs_singleton.read_value('serveraddress') server_port = int(prefs_singleton.read_value('serverport')) self.channels_ = prefs_singleton.read_value('channels') self.PING_TIMEOUT = 185 self.RECONNECT_MAX_ATTEMPTS = None # always try to reconnect super().__init__(bot_nick, realname=bot_real_name) loop = asyncio.get_event_loop() asyncio.ensure_future(self.connect(server_address, server_port, tls=False, tls_verify=False), loop=loop) self.preferences = prefs_singleton self.blacklist = [ 'AbstractFeature', 'Intelligence', 'Renameable', 'Remindable' ] self.plugins = self.load_features(self.blacklist) # brain is just an instance so it can be referenced again if prefs_singleton.read_with_default('Intelligence_enabled', True): from Features.Intelligence import Intelligence brain = Intelligence() self.plugins.append(brain) # renameable needs intelligence to rename itself if prefs_singleton.read_with_default('Renameable_enabled', True): from Features.Renameable import Renameable self.plugins.append(Renameable(brain)) # Remindable needs access to the bot, so it can send a line at any time if prefs_singleton.read_with_default('Remindable_enabled', True): from Features.Remindable import Remindable self.plugins.append(Remindable(self)) self.plugins.sort(key=lambda f: f.priority) loop.run_forever()
async def message_filter(self, bot, source, target, message, highlighted): if message == 'die' or message == '!die': # respond to !die if target == prefs_singleton.read_value('botownernick'): print('received authorized request to die. terminating...') await bot.disconnect() exit(0) else: print('received unauthorized request to die. Ignoring') await bot.message( source, f"{target}: you're not {prefs_singleton.read_value('botownernick')}!" ) return True return False
async def message_filter(self, bot, source, target, message, highlighted): if (message.startswith('remind') and highlighted) or message.startswith('!remind'): # respond to !remind if target not in prefs_singleton.read_value('whitelistnicks'): wait_time, reminder_text = Remindable.parse_remind(message) if reminder_text: await bot.message(source, target + ": I'll remind you about " + reminder_text) reminder_object = {'channel': source, 'remindertext': f'{target}: {reminder_text}', 'remindertime': int(time.time()) + wait_time} existing_reminders = prefs_singleton.read_with_default('reminders', []) existing_reminders.append(reminder_object) prefs_singleton.write_value('reminders', existing_reminders) else: await bot.message(source, target + ': Usage is "!remind [in] 5 (second[s]/minute[s]/hour[s]/day[s]) ' 'reminder text"') return True return False
async def issue_reminder(bot, channel, text, reminder_time): print(f'issuing reminder to {channel} with value {text}') await bot.message(channel, text) # after issuing the reminder, remove it from the list of things to remind # there is a theoretical collision if multiple reminders are targeted at the same second, # only one may be issued then all within that second will be deleted. # it is more likely to have a unique remindertime than unique remindertext, so this choice is acceptable remaining_reminders = list(filter(lambda x: x['remindertime'] != reminder_time, prefs_singleton.read_value('reminders'))) prefs_singleton.write_value('reminders', remaining_reminders or [])
async def check_reminders(bot): for reminder_object in prefs_singleton.read_value('reminders'): # check the reminders if reminder_object['remindertime'] > time.time(): continue # if a reminder has expired await Remindable.issue_reminder(bot, reminder_object['channel'], reminder_object['remindertext'], reminder_object['remindertime'])