From d137ad40b1ba7f88268e0174be2c541fe4d25c44 Mon Sep 17 00:00:00 2001 From: Myned Date: Thu, 19 Oct 2017 04:39:18 -0400 Subject: [PATCH 1/3] Compressed try/excepts into a suppressing for loop --- src/main/utils/utils.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/main/utils/utils.py b/src/main/utils/utils.py index 6bd2fd5..38aa546 100644 --- a/src/main/utils/utils.py +++ b/src/main/utils/utils.py @@ -124,21 +124,16 @@ def get_kwargs(ctx, args, *, limit=False): if '-d' in remaining or '-dm' in remaining: destination = ctx.author - try: - remaining.remove('-d') - except ValueError: - remaining.remove('-dm') + for flag in ('-d', '-dm'): + with suppress(ValueError): + remaining.remove(flag) if ('-r' in remaining or '-rm' in remaining or '-remove' in remaining) and ctx.author.permissions_in(ctx.channel).manage_messages: rm = True - try: - remaining.remove('-r') - except ValueError: - try: - remaining.remove('-rm') - except ValueError: - remaining.remove('-remove') + for flag in ('-r', '-rm', '-remove'): + with suppress(ValueError): + remaining.remove(flag) if limit: for arg in remaining: From ff2ac1b58778d3b271bbf26c80238f616fd8a032 Mon Sep 17 00:00:00 2001 From: Myned Date: Thu, 19 Oct 2017 04:39:41 -0400 Subject: [PATCH 2/3] Misc testing and shit --- src/main/run.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/main/run.py b/src/main/run.py index cc8cfaa..34c2263 100644 --- a/src/main/run.py +++ b/src/main/run.py @@ -18,7 +18,7 @@ from utils import utils as u # log.basicConfig(level=log.INFO) -bot = commands.Bot(command_prefix=u.config['prefix'], description='Experimental booru bot') +bot = commands.Bot(command_prefix=u.config['prefix'], description='Experimental miscellaneous bot') # Send and print ready message to #testing and console after logon @@ -99,14 +99,33 @@ async def reaction_remove(r, u): print('Removed') bot.remove_listener(on_reaction_remove) +# d.opus.load_opus('opus') + + +async def wait(voice): + asyncio.sleep(5) + await voice.disconnect() + + +def after(voice, error): + coro = voice.disconnect() + future = asyncio.run_coroutine_threadsafe(coro, voice.loop) + future.result() + @bot.command(name=',test', hidden=True) @commands.is_owner() @checks.del_ctx() async def test(ctx): - test = await ctx.send('\N{NUMBER SIGN}\N{COMBINING ENCLOSING KEYCAP}') - await test.add_reaction('\N{THUMBS UP SIGN}') - await test.add_reaction('#\u20e3') + def check(react, user): + return reaction.emoji == '\N{THUMBS UP SIGN}' + # channel = bot.get_channel(int(cid)) + # voice = await channel.connect() + # voice.play(d.AudioSource, after=lambda: after(voice)) + test = await ctx.send('thumbs up!') + while True: + done, pending = await asyncio.wait([bot.wait_for('reaction_add', check=check), bot.wait_for('reaction_remove', check=check)], return_when=asyncio.FIRST_COMPLETED) + await ctx.send('well doneeee') # bot.add_listener(on_reaction_add) # bot.add_listener(on_reaction_remove) From ad5604db149d369a9195a2d5a4a9639c66e77564 Mon Sep 17 00:00:00 2001 From: Myned Date: Thu, 19 Oct 2017 04:40:50 -0400 Subject: [PATCH 3/3] WIP help doc strings, added react add/remove, starred count, formatting --- src/main/cogs/booru.py | 70 +++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 28 deletions(-) diff --git a/src/main/cogs/booru.py b/src/main/cogs/booru.py index b92c62f..c4c4560 100644 --- a/src/main/cogs/booru.py +++ b/src/main/cogs/booru.py @@ -23,8 +23,8 @@ class MsG: self.bot = bot self.color = d.Color(0x1A1A1A) self.LIMIT = 100 - self.RATE_LIMIT = u.RATE_LIMIT self.HISTORY_LIMIT = 100 + self.RATE_LIMIT = u.RATE_LIMIT self.queue = asyncio.Queue() self.qualitifying = False @@ -42,7 +42,7 @@ class MsG: self.qualitifying = True # Tag search - @commands.command(aliases=['rel'], brief='e621 Related tag search', description='e621 | NSFW\nReturn a link search for given tags') + @commands.command(aliases=['rel'], brief='e621 Search for related tags', description='e621 | NSFW\nReturn related tags for a number of given tags', usage='[related|rel]') @checks.del_ctx() async def related(self, ctx, *args): kwargs = u.get_kwargs(ctx, args) @@ -500,7 +500,7 @@ class MsG: raise exc.Left elif reaction.emoji == '\N{BLACK RIGHTWARDS ARROW}' and reaction.message.id == paginator.id and (user is ctx.author or user.id == u.config['owner_id']): raise exc.Right - elif reaction.emoji == '\N{FILE FOLDER}' and reaction.message.id == paginator.id and (user is ctx.author or user.id == u.config['owner_id']): + elif reaction.emoji == '\N{GROWING HEART}' and reaction.message.id == paginator.id and (user is ctx.author or user.id == u.config['owner_id']): raise exc.Save return False @@ -531,15 +531,17 @@ class MsG: paginator = await dest.send(embed=embed) - for emoji in ('\N{NUMBER SIGN}\N{COMBINING ENCLOSING KEYCAP}', '\N{LEFTWARDS BLACK ARROW}', '\N{BLACK RIGHTWARDS ARROW}', '\N{FILE FOLDER}'): + for emoji in ('\N{NUMBER SIGN}\N{COMBINING ENCLOSING KEYCAP}', '\N{LEFTWARDS BLACK ARROW}', '\N{BLACK RIGHTWARDS ARROW}', '\N{GROWING HEART}'): await paginator.add_reaction(emoji) - for emoji in ('\N{WHITE HEAVY CHECK MARK}', '\N{OCTAGONAL SIGN}'): - await ctx.message.add_reaction(emoji) + await ctx.message.add_reaction('\N{OCTAGONAL SIGN}') await asyncio.sleep(1) while not self.bot.is_closed(): try: - await self.bot.wait_for('reaction_add', check=on_reaction, timeout=10 * 60) + done, pending = await asyncio.wait([self.bot.wait_for('reaction_add', check=on_reaction, timeout=10 * 60), + self.bot.wait_for('reaction_remove', check=on_reaction, timeout=10 * 60)], return_when=asyncio.FIRST_COMPLETED) + for future in done: + future.result() except exc.GoTo: await paginator.edit(content='**Enter image number...**') @@ -553,7 +555,7 @@ class MsG: icon_url='http://lh6.ggpht.com/d3pNZNFCcJM8snBsRSdKUhR9AVBnJMcYYrR92RRDBOzCrxZMhuTeoGOQSmSEn7DAPQ=w300') embed.set_image(url=values[c - 1]['url']) - await paginator.edit(content=None, embed=embed) + await paginator.edit(content='\N{HEAVY BLACK HEART}' if values[c - 1]['url'] in starred else None, embed=embed) except exc.Left: if c > 1: @@ -564,7 +566,7 @@ class MsG: icon_url='http://lh6.ggpht.com/d3pNZNFCcJM8snBsRSdKUhR9AVBnJMcYYrR92RRDBOzCrxZMhuTeoGOQSmSEn7DAPQ=w300') embed.set_image(url=values[c - 1]['url']) - await paginator.edit(content=None, embed=embed) + await paginator.edit(content='\N{HEAVY BLACK HEART}' if values[c - 1]['url'] in starred else None, embed=embed) else: await paginator.edit(content='**First image.**') @@ -577,17 +579,17 @@ class MsG: icon_url='http://lh6.ggpht.com/d3pNZNFCcJM8snBsRSdKUhR9AVBnJMcYYrR92RRDBOzCrxZMhuTeoGOQSmSEn7DAPQ=w300') embed.set_image(url=values[c - 1]['url']) - await paginator.edit(content=None, embed=embed) + await paginator.edit(content='\N{HEAVY BLACK HEART}' if values[c - 1]['url'] in starred else None, embed=embed) except exc.Save: if values[c - 1]['url'] not in starred: starred.append(values[c - 1]['url']) - await paginator.edit(content='`{}` **image saved.**'.format(len(starred))) + await paginator.edit(content='\N{HEAVY BLACK HEART}') else: - starred.remove(values[c - 1])['url'] + starred.remove(values[c - 1]['url']) - await paginator.edit(content='**Image removed.**') + await paginator.edit(content='\N{BROKEN HEART}') except exc.Abort: try: @@ -595,13 +597,18 @@ class MsG: except UnboundLocalError: await dest.send('**Exited paginator.**') + + finally: + await ctx.message.add_reaction('\N{WHITE HEAVY CHECK MARK}') except asyncio.TimeoutError: try: await paginator.edit(content='**Paginator timed out.**') except UnboundLocalError: await dest.send('**Paginator timed out.**') - await ctx.message.add_reaction('\N{OCTAGONAL SIGN}') + + finally: + await ctx.message.add_reaction('\N{WHITE HEAVY CHECK MARK}') except exc.NotFound: await ctx.send('**Pool not found.**', delete_after=10) await ctx.message.add_reaction('\N{CROSS MARK}') @@ -611,7 +618,7 @@ class MsG: finally: for url in starred: - await ctx.author.send(url) + await ctx.author.send('`{} / {}`\n{}'.format(starred.index(url) + 1, len(starred), url)) if len(starred) > 5: await asyncio.sleep(self.RATE_LIMIT) @@ -678,7 +685,7 @@ class MsG: raise exc.Left elif reaction.emoji == '\N{BLACK RIGHTWARDS ARROW}' and reaction.message.id == paginator.id and (user is ctx.author or user.id == u.config['owner_id']): raise exc.Right - elif reaction.emoji == '\N{FILE FOLDER}' and reaction.message.id == paginator.id and (user is ctx.author or user.id == u.config['owner_id']): + elif reaction.emoji == '\N{GROWING HEART}' and reaction.message.id == paginator.id and (user is ctx.author or user.id == u.config['owner_id']): raise exc.Save return False @@ -712,15 +719,17 @@ class MsG: paginator = await dest.send(embed=embed) - for emoji in ('\N{NUMBER SIGN}\N{COMBINING ENCLOSING KEYCAP}', '\N{LEFTWARDS BLACK ARROW}', '\N{BLACK RIGHTWARDS ARROW}', '\N{FILE FOLDER}'): + for emoji in ('\N{NUMBER SIGN}\N{COMBINING ENCLOSING KEYCAP}', '\N{LEFTWARDS BLACK ARROW}', '\N{BLACK RIGHTWARDS ARROW}', '\N{GROWING HEART}'): await paginator.add_reaction(emoji) - for emoji in ('\N{WHITE HEAVY CHECK MARK}', '\N{OCTAGONAL SIGN}'): - await ctx.message.add_reaction(emoji) + await ctx.message.add_reaction('\N{OCTAGONAL SIGN}') await asyncio.sleep(1) while not self.bot.is_closed(): try: - await self.bot.wait_for('reaction_add', check=on_reaction, timeout=10 * 60) + done, pending = await asyncio.wait([self.bot.wait_for('reaction_add', check=on_reaction, timeout=10 * 60), + self.bot.wait_for('reaction_remove', check=on_reaction, timeout=10 * 60)], return_when=asyncio.FIRST_COMPLETED) + for future in done: + future.result() except exc.GoTo: await paginator.edit(content='**Enter image number...**') @@ -734,7 +743,7 @@ class MsG: icon_url='http://lh6.ggpht.com/d3pNZNFCcJM8snBsRSdKUhR9AVBnJMcYYrR92RRDBOzCrxZMhuTeoGOQSmSEn7DAPQ=w300') embed.set_image(url=values[c - 1]['url']) - await paginator.edit(content=None, embed=embed) + await paginator.edit(content='\N{HEAVY BLACK HEART}' if values[c - 1]['url'] in starred else None, embed=embed) except exc.Left: if c > 1: @@ -744,7 +753,7 @@ class MsG: embed.set_footer(text='{} / {}'.format(c, len(posts)), icon_url='http://lh6.ggpht.com/d3pNZNFCcJM8snBsRSdKUhR9AVBnJMcYYrR92RRDBOzCrxZMhuTeoGOQSmSEn7DAPQ=w300') embed.set_image(url=values[c - 1]['url']) - await paginator.edit(content=None, embed=embed) + await paginator.edit(content='\N{HEAVY BLACK HEART}' if values[c - 1]['url'] in starred else None, embed=embed) else: await paginator.edit(content='**First image.**') @@ -766,17 +775,17 @@ class MsG: embed.set_footer(text='{} / {}'.format(c, len(posts)), icon_url='http://lh6.ggpht.com/d3pNZNFCcJM8snBsRSdKUhR9AVBnJMcYYrR92RRDBOzCrxZMhuTeoGOQSmSEn7DAPQ=w300') embed.set_image(url=values[c - 1]['url']) - await paginator.edit(content=None, embed=embed) + await paginator.edit(content='\N{HEAVY BLACK HEART}' if values[c - 1]['url'] in starred else None, embed=embed) except exc.Save: if values[c - 1]['url'] not in starred: starred.append(values[c - 1]['url']) - await paginator.edit(content='`{}` **image saved.**'.format(len(starred))) + await paginator.edit(content='\N{HEAVY BLACK HEART}') else: - starred.remove(values[c - 1])['url'] + starred.remove(values[c - 1]['url']) - await paginator.edit(content='**Image removed.**') + await paginator.edit(content='\N{BROKEN HEART}') except exc.Abort: try: @@ -784,13 +793,18 @@ class MsG: except UnboundLocalError: await dest.send('**Exited paginator.**') + + finally: + await ctx.message.add_reaction('\N{WHITE HEAVY CHECK MARK}') except asyncio.TimeoutError: try: await paginator.edit(content='**Paginator timed out.**') except UnboundLocalError: await dest.send('**Paginator timed out.**') - await ctx.message.add_reaction('\N{OCTAGONAL SIGN}') + + finally: + await ctx.message.add_reaction('\N{WHITE HEAVY CHECK MARK}') except exc.NotFound as e: await ctx.send('`{}` **not found.**'.format(e), delete_after=10) await ctx.message.add_reaction('\N{CROSS MARK}') @@ -809,7 +823,7 @@ class MsG: finally: for url in starred: - await ctx.author.send(url) + await ctx.author.send('`{} / {}`\n{}'.format(starred.index(url) + 1, len(starred), url)) if len(starred) > 5: await asyncio.sleep(self.RATE_LIMIT)