From: CMDR furrycat Date: Tue, 21 Feb 2017 17:24:41 +0000 (+0000) Subject: Fixed announcements for Python 3.5. X-Git-Url: http://git.furryclan.net/?a=commitdiff_plain;h=4dbb104cef26ac0a37c20a9998d91ddf440638b6;p=furrycat%2Fcatbot.git Fixed announcements for Python 3.5. --- diff --git a/plugin/announcements/announcements.py b/plugin/announcements/announcements.py index eb44f89..3f6c863 100644 --- a/plugin/announcements/announcements.py +++ b/plugin/announcements/announcements.py @@ -46,14 +46,13 @@ class Announcements(DBConnection): self.dbh.commit() self.close_db() - @asyncio.coroutine - def on_ready(self): + async def on_ready(self): waittime = 60 while True: # XXX may not be necessary - test! # Convert to list because we will be sharing the cursor. for announcement in list(self.get_announcements(client)): - yield from self.announce(announcement) + await self.announce(announcement) # Delete old announcements. now = int(time.time()) @@ -85,21 +84,19 @@ class Announcements(DBConnection): else: log.info('Failed to delete old announcement {}'.format(announcement.id)) - yield from asyncio.sleep(waittime) + await asyncio.sleep(waittime) def valid_commands(self): return ['announce', 'announcement'] - @asyncio.coroutine - def handle_command(self, message, command, raw): + async def handle_command(self, message, command, raw): if command not in self.valid_commands(): return PluginCommand.ignored - yield from self.manage_announcements(message, raw) + await self.manage_announcements(message, raw) return PluginCommand.exclusive - @asyncio.coroutine - def handle_help(self, message, command, *args): - yield from self.help_announcements(message, *args) + async def handle_help(self, message, command, *args): + await self.help_announcements(message, *args) def get_all_announcements(self, client, servers = []): for row in self.get_all_from_table(client, 'announcements', servers): @@ -125,8 +122,7 @@ class Announcements(DBConnection): def delete_announcement(self, client, id): return self.delete_from_table(client, 'announcements', id) - @asyncio.coroutine - def can_manage_announcements(self, author, channel, command, **args): + async def can_manage_announcements(self, author, channel, command, **args): # Anyone can list announcements. if command == 'list': log.debug('Anyone can list announcements.') @@ -180,11 +176,10 @@ class Announcements(DBConnection): log.debug('Member {} with role {} on server {} can create announcements.'.format(member.name, member.top_role.name, server.name)) return announcement - yield from cat.hiss(channel) + await cat.hiss(channel) return False - @asyncio.coroutine - def help_announcements(self, message, command = None): + async def help_announcements(self, message, command = None): if command is None: lines = [ 'Commands to manage announcements are:', @@ -288,11 +283,10 @@ class Announcements(DBConnection): ] else: lines = ['*shrugs*'] - yield from bot.say_many(message.channel, lines) + await bot.say_many(message.channel, lines) - @asyncio.coroutine - def list_announcements(self, message): - result = yield from self.can_manage_announcements(message.author, message.channel, 'list') + async def list_announcements(self, message): + result = await self.can_manage_announcements(message.author, message.channel, 'list') if not result: return @@ -325,13 +319,12 @@ class Announcements(DBConnection): results.append(text) if len(results): - yield from bot.say_many(message.channel, results) + await bot.say_many(message.channel, results) else: - yield from cat.shrug(message.channel) + await cat.shrug(message.channel) - @asyncio.coroutine - def show_announcement(self, message, id): - announcement = yield from self.can_manage_announcements(message.author, message.channel, 'show', id = id) + async def show_announcement(self, message, id): + announcement = await self.can_manage_announcements(message.author, message.channel, 'show', id = id) if not announcement: return lines = [] @@ -404,7 +397,7 @@ class Announcements(DBConnection): if announcement.message: lines.append(announcement.message) - yield from bot.say_many(message.channel, lines, attachment = announcement.attachment) + await bot.say_many(message.channel, lines, attachment = announcement.attachment) # Map a database key to a syntax parameter. def announcement_key(self, k): @@ -421,8 +414,7 @@ class Announcements(DBConnection): else: return k - @asyncio.coroutine - def parse_announcement(self, message, raw, editing = False): + async def parse_announcement(self, message, raw, editing = False): # We want to split by colon but don't want to count any which are part of dates or URLs. m = re.match(r'(?:[^:]*\s+(?:(?:at|from|to)\s+"?\d\d\d\d-?\d\d-?\d\d(?:T|\s+)\d\d:?\d\d:?\d\dZ?"?|(?:attachment|sound)\s+https?:\S+))+', raw, re.IGNORECASE) if m is not None: @@ -557,7 +549,7 @@ class Announcements(DBConnection): parsed['probability'] = param ok = True else: - yield from bot.say(message.channel, 'What is {}?'.format(arg)) + await bot.say(message.channel, 'What is {}?'.format(arg)) return if ok: @@ -567,7 +559,7 @@ class Announcements(DBConnection): if not ok: log.info('Failed to parse announcement. Got: {}'.format(parsed)) - yield from bot.say(message.channel, '{}?'.format(arg)) + await bot.say(message.channel, '{}?'.format(arg)) return None if 'channel_id' in parsed: @@ -575,11 +567,11 @@ class Announcements(DBConnection): parsed['server_id'] = 'private' if 'mention' in parsed: if parsed['mention'] != message.author.id: - yield from bot.say(message.channel, "Can't mention someone else in private message!") + await bot.say(message.channel, "Can't mention someone else in private message!") return None parsed['mention'] = None if 'voice_id' in parsed or 'sound' in parsed: - yield from bot.say(message.channel, 'No sounds for private messages!') + await bot.say(message.channel, 'No sounds for private messages!') return None else: if 'voice_id' in parsed and parsed['voice_id'] == parsed['channel_id']: @@ -589,45 +581,44 @@ class Announcements(DBConnection): # Text only. channel = bot.parse_channel(parsed['channel_id'], voice_ok = False) if not channel: - yield from bot.say(message.channel, 'Invalid channel!') + await bot.say(message.channel, 'Invalid channel!') return None server = channel.server parsed['server_id'] = server.id elif 'voice_id' in parsed: voice = bot.parse_channel(parsed['voice_id'], text_ok = False) if not voice: - yield from bot.say(message.channel, 'Invalid voice channel!') + await bot.say(message.channel, 'Invalid voice channel!') return None server = voice.server parsed['server_id'] = server.id if 'start_date' in parsed and 'end_date' in parsed: if parsed['start_date'] > parsed['end_date']: - yield from bot.say(message.channel, "Start date must not be before end date!") + await bot.say(message.channel, "Start date must not be before end date!") return None if editing: if 'id' not in parsed: - yield from bot.say(message.channel, 'Missing ID!') + await bot.say(message.channel, 'Missing ID!') return None else: if 'channel_id' not in parsed: if 'voice_id' in parsed: parsed['channel_id'] = parsed['voice_id'] else: - yield from bot.say(message.channel, 'Missing channel!') + await bot.say(message.channel, 'Missing channel!') return None return parsed - @asyncio.coroutine - def create_new_announcement(self, message, raw): + async def create_new_announcement(self, message, raw): # announcement create [params]: - create = yield from self.parse_announcement(message, raw) + create = await self.parse_announcement(message, raw) if create is None: return - announce = yield from self.can_manage_announcements(message.author, message.channel, 'create', create = Announcement(create)) + announce = await self.can_manage_announcements(message.author, message.channel, 'create', create = Announcement(create)) if not announce: return @@ -635,39 +626,37 @@ class Announcements(DBConnection): if id: create['id'] = id log.info('Created announcement: {}'.format(create)) - yield from bot.say(message.channel, id) - yield from cat.purr(None, bot.voice_channel_for_user(message.author), join = False) + await bot.say(message.channel, id) + await cat.purr(None, bot.voice_channel_for_user(message.author), join = False) else: log.info('Failed to create announcement: {}'.format(create)) - yield from cat.yelp(message.channel) + await cat.yelp(message.channel) - @asyncio.coroutine - def edit_existing_announcement(self, message, raw): - update = yield from self.parse_announcement(message, raw, True) + async def edit_existing_announcement(self, message, raw): + update = await self.parse_announcement(message, raw, True) if update is None: return id = update['id'] del(update['id']) - announce = yield from self.can_manage_announcements(message.author, message.channel, 'edit', id = id) + announce = await self.can_manage_announcements(message.author, message.channel, 'edit', id = id) if not announce: return if not len(update.keys()): - yield from bot.say(message.channel, '?') + await bot.say(message.channel, '?') return if self.update_announcement(client, id, **update): log.info('Edited announcement {}: {}'.format(id, update)) - yield from bot.say(message.channel, ', '.join([self.announcement_key(k) for k in update])) - yield from cat.purr(None, bot.voice_channel_for_user(message.author), join = False) + await bot.say(message.channel, ', '.join([self.announcement_key(k) for k in update])) + await cat.purr(None, bot.voice_channel_for_user(message.author), join = False) else: log.info('Failed to edit announcement {}: {}'.format(id, update)) - yield from cat.yelp(message.channel) + await cat.yelp(message.channel) - @asyncio.coroutine - def delete_existing_announcement(self, message, id): - announcement = yield from self.can_manage_announcements(message.author, message.channel, 'delete', id = id) + async def delete_existing_announcement(self, message, id): + announcement = await self.can_manage_announcements(message.author, message.channel, 'delete', id = id) if not announcement: return if bot.get('dryrun'): @@ -675,13 +664,12 @@ class Announcements(DBConnection): else: log.info('Deleting announcement {}'.format(id)) if self.delete_announcement(client, id): - yield from cat.purr(message.channel) + await cat.purr(message.channel) else: - yield from cat.yelp(message.channel) + await cat.yelp(message.channel) - @asyncio.coroutine - def schedule_announcement(self, message, id, **args): - announcement = yield from self.can_manage_announcements(message.author, message.channel, 'schedule', id = id) + async def schedule_announcement(self, message, id, **args): + announcement = await self.can_manage_announcements(message.author, message.channel, 'schedule', id = id) if not announcement: return @@ -703,51 +691,49 @@ class Announcements(DBConnection): else: log.info('Updating announcement {}: {}'.format(id, update)) if self.update_announcement(client, id, **update): - yield from cat.purr(message.channel) + await cat.purr(message.channel) else: - yield from cat.yelp(message.channel) + await cat.yelp(message.channel) - @asyncio.coroutine - def manage_announcements(self, message, raw): + async def manage_announcements(self, message, raw): m = re.match(r'announce(?:ment)?\s+(.+)', raw, re.IGNORECASE | re.DOTALL) if m is None: - yield from self.list_announcements(message) + await self.list_announcements(message) return text = m.group(1) args = shlex.split(text) command = args[0].lower() if len(args) == 1: if command == 'list': - yield from self.list_announcements(message) + await self.list_announcements(message) return elif command == 'help': - yield from self.help_announcements(message) + await self.help_announcements(message) return else: - yield from cat.yelp(message.channel) + await cat.yelp(message.channel) return if command == 'show': - yield from self.show_announcement(message, args[1]) + await self.show_announcement(message, args[1]) elif command == 'help': - yield from self.help_announcements(message, args[1]) + await self.help_announcements(message, args[1]) elif command == 'delete': - yield from self.delete_existing_announcement(message, args[1]) + await self.delete_existing_announcement(message, args[1]) elif command == 'asap': - yield from self.schedule_announcement(message, args[1], asap = True) + await self.schedule_announcement(message, args[1], asap = True) elif command == 'pause': - yield from self.schedule_announcement(message, args[1], pause = True) + await self.schedule_announcement(message, args[1], pause = True) elif command == 'resume': - yield from self.schedule_announcement(message, args[1], pause = False) + await self.schedule_announcement(message, args[1], pause = False) elif command == 'create': - yield from self.create_new_announcement(message, text) + await self.create_new_announcement(message, text) elif command == 'edit': - yield from self.edit_existing_announcement(message, text) + await self.edit_existing_announcement(message, text) else: - yield from cat.yelp(message.channel) + await cat.yelp(message.channel) - @asyncio.coroutine - def announce(self, announcement): + async def announce(self, announcement): update = {} interval = announcement.interval if interval is None: @@ -788,7 +774,7 @@ class Announcements(DBConnection): for server in client.servers: try: member = server.get_member(announcement.member_id) - channel = yield from client.start_private_message(member) + channel = await client.start_private_message(member) break except: log.exception('announce') @@ -821,13 +807,13 @@ class Announcements(DBConnection): if not announcement.is_private and not bot.parse_boolean(announcement.asap): cutoff = datetime.datetime.utcnow() - datetime.timedelta(0, interval) log.debug('Getting logs from {}.'.format(cutoff)) - result = yield from client.logs_from(channel, after = cutoff) + result = await client.logs_from(channel, after = cutoff) logs = list(result) # Don't spam the same message in a quiet channel even if it hasn't # been posted since the cutoff. if len(logs) < bot.get('message_limit'): log.debug('Fewer than {} messages ({}).'.format(bot.get('message_limit'), len(logs))) - logs = yield from client.logs_from(channel, limit = bot.get('message_limit')) + logs = await client.logs_from(channel, limit = bot.get('message_limit')) for message in logs: raw = re.sub(r'^(