From 3aac112ecb9a1e4b1ab02a11f603e288c673c2eb Mon Sep 17 00:00:00 2001 From: Myned Date: Sun, 31 Dec 2017 21:54:37 -0500 Subject: [PATCH] Added auto-heart command --- src/cogs/booru.py | 71 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 4 deletions(-) diff --git a/src/cogs/booru.py b/src/cogs/booru.py index 9ee8bf9..de9c72a 100644 --- a/src/cogs/booru.py +++ b/src/cogs/booru.py @@ -55,6 +55,11 @@ class MsG: print('STARTED : auto-reversifying in #{}'.format(temp.name)) self.reversifying = True self.bot.loop.create_task(self._reversify()) + if u.tasks['auto_hrt']: + for channel in u.tasks['auto_hrt']: + temp = self.bot.get_channel(channel) + self.bot.loop.create_task(self.queue_for_hearts(channel=temp)) + print(f'STARTED : auto-hearting in #{temp.name}') # if not self.updating: # self.updating = True # self.bot.loop.create_task(self._update_suggested()) @@ -124,34 +129,92 @@ class MsG: while self.hearting: temp = await self.heartqueue.get() + if isinstance(temp[1], d.Embed): await temp[0].send(embed=temp[1]) await asyncio.sleep(self.RATE_LIMIT) + elif isinstance(temp[1], d.Message): + for match in re.finditer('(https?:\/\/[^ ]*\.(?:gif|png|jpg|jpeg))', temp[1].content): + await temp[0].send(match) + + await asyncio.sleep(self.RATE_LIMIT) + + for attachment in temp[1].attachments: + await temp[0].send(attachment.url) + await asyncio.sleep(self.RATE_LIMIT) print('STOPPED : hearting') - async def queue_for_hearts(self, *, message, send): + async def queue_for_hearts(self, *, message=None, send=None, channel=None, reaction=True, timeout=60 * 60): def on_reaction(reaction, user): if reaction.emoji == '\N{HEAVY BLACK HEART}' and reaction.message.id == message.id: raise exc.Save(user) return False + def on_message(msg): if 'stop h' in msg.content.lower(): + raise exc.Abort + return msg.channel.id == channel.id and (re.search('(https?:\/\/[^ ]*\.(?:gif|png|jpg|jpeg))', msg.content) or msg.attachments) + if message: try: + if reaction: await message.add_reaction('\N{HEAVY BLACK HEART}') await asyncio.sleep(1) while self.hearting: try: - await asyncio.gather(*[self.bot.wait_for('reaction_add', check=on_reaction, timeout=60 * 60), - self.bot.wait_for('reaction_remove', check=on_reaction, timeout=60 * 60)]) + await self.bot.wait_for('reaction_add', check=on_reaction, timeout=timeout) except exc.Save as e: - await self.heartqueue.put((e.user, send)) + await self.heartqueue.put((e.user, send if send else message)) except asyncio.TimeoutError: await message.add_reaction('\N{WHITE HEAVY CHECK MARK}') + else: + try: + async for message in channel.history(limit=300): + if re.search('(https?:\/\/[^ ]*\.(?:gif|png|jpg|jpeg))', message.content) or message.attachments: + self.bot.loop.create_task(self._wait_for_reaction(message)) + + while self.hearting: + message = await self.bot.wait_for('message', check=on_message) + self.bot.loop.create_task(self._wait_for_reaction(message)) + + except exc.Abort: + u.tasks['auto_hrt'].remove(channel.id) + u.dump(u.tasks, 'cogs/tasks.pkl') + print('STOPPED : auto-hearting in #{}'.format(channel.name)) + await channel.send('**Stopped queueing messages for hearting in** {}'.format(channel.mention), delete_after=5) + + async def _wait_for_reaction(self, message): + def on_reaction(reaction, user): + if reaction.emoji == '\N{HEAVY BLACK HEART}' and reaction.message.id == message.id: + raise exc.Save(user) + return False + + while self.hearting: + try: + await self.bot.wait_for('reaction_add', check=on_reaction) + + except exc.Save as e: + await self.heartqueue.put((e.user, message)) + + @cmds.command(name='autoheart', aliases=['autohrt']) + @cmds.has_permissions(administrator=True) + async def auto_heart(self, ctx): + try: + if ctx.channel.id not in u.tasks['auto_hrt']: + u.tasks['auto_hrt'].append(ctx.channel.id) + u.dump(u.tasks, 'cogs/tasks.pkl') + self.bot.loop.create_task(self.queue_for_hearts(channel=ctx.channel)) + print('STARTED : auto-hearting in #{}'.format(ctx.channel.name)) + await ctx.send('**Auto-hearting all messages in {}**'.format(ctx.channel.mention), delete_after=5) + else: + raise exc.Exists + + except exc.Exists: await ctx.send('**Already auto-hearting in {}.** Type `stop h(earting)` to stop.'.format(ctx.channel.mention), delete_after=7) + await ctx.message.add_reaction('\N{CROSS MARK}') # @cmds.command() # async def auto_post(self, ctx):