From 0cb8f2876bb4bf79988c01b4d58331d279b10091 Mon Sep 17 00:00:00 2001 From: Myned Date: Sun, 24 Sep 2017 16:33:48 -0400 Subject: [PATCH 01/29] Am g0 by3e --- src/main/run.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/run.py b/src/main/run.py index 671a978..f2705e1 100644 --- a/src/main/run.py +++ b/src/main/run.py @@ -24,7 +24,7 @@ bot = commands.Bot(command_prefix=commands.when_mentioned_or(config['prefix']), @bot.event async def on_ready(): if isinstance(bot.get_channel(config['startup_channel']), discord.TextChannel): - await bot.get_channel(config['startup_channel']).send('Hello how are? **Have day.** 🌈\n[STARTUP-INFO]') + await bot.get_channel(config['startup_channel']).send('H3l1(0) hOw aR3? **H4VE dAy.** 🌈') print('Connected.') print('Username: ' + bot.user.name) print('-------') @@ -35,7 +35,7 @@ async def on_ready(): @commands.is_owner() async def die(ctx): try: - await bot.get_channel(config['shutdown_channel']).send('Am go bye. **Have night.** 💤\n[SHUTDOWN-INFO]') + await bot.get_channel(config['shutdown_channel']).send('Am g0 by3e333333eee. **H4v3 n1GhT.** 💤') await bot.close() print('-------') print('Closed.') From 0f9c96196651cc30fc73813e9e920e1f0102fa07 Mon Sep 17 00:00:00 2001 From: Myned Date: Mon, 25 Sep 2017 15:31:51 -0400 Subject: [PATCH 02/29] Did stuff and things --- src/main/cogs/booru.py | 2 +- src/main/cogs/tools.py | 3 +-- src/main/run.py | 16 +++++++++++----- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/main/cogs/booru.py b/src/main/cogs/booru.py index a22f730..77d971c 100644 --- a/src/main/cogs/booru.py +++ b/src/main/cogs/booru.py @@ -80,7 +80,7 @@ class MsG: await ctx.trigger_typing() await ctx.send('✅ ' + ctx.message.author.mention + ' **Probable match:**\n' + scraper.check_match('http://iqdb.harry.lu/?url=' + url)) except exc.MatchError: - await ctx.send('❌ ' + ctx.message.author.mention + ' **No probable match.**') + await ctx.send('❌ ' + ctx.message.author.mention + ' **No probable match.**', delete_after=10) except Exception: await ctx.send(exc.base) traceback.print_exc(limit=1) diff --git a/src/main/cogs/tools.py b/src/main/cogs/tools.py index 93ccf0c..146eef7 100644 --- a/src/main/cogs/tools.py +++ b/src/main/cogs/tools.py @@ -45,14 +45,13 @@ class Utils: @commands.group(name=',send', aliases=[',s'], hidden=True) @commands.is_owner() + @checks.del_ctx() async def send(self, ctx): pass @send.command(name='guild', aliases=['g', 'server', 's']) - @checks.del_ctx() async def send_guild(self, ctx, guild, channel, *message): await discord.utils.get(self.bot.get_all_channels(), guild__name=guild, name=channel).send(formatter.tostring(message)) @send.command(name='user', aliases=['u', 'member', 'm']) - @checks.del_ctx() async def send_user(self, ctx, user, *message): await discord.utils.get(self.bot.get_all_members(), id=int(user)).send(formatter.tostring(message)) diff --git a/src/main/run.py b/src/main/run.py index f2705e1..bd19697 100644 --- a/src/main/run.py +++ b/src/main/run.py @@ -24,15 +24,15 @@ bot = commands.Bot(command_prefix=commands.when_mentioned_or(config['prefix']), @bot.event async def on_ready(): if isinstance(bot.get_channel(config['startup_channel']), discord.TextChannel): - await bot.get_channel(config['startup_channel']).send('H3l1(0) hOw aR3? **H4VE dAy.** 🌈') + await bot.get_channel(config['startup_channel']).send('H3l1(0) hOw aR3? **H4vE dAy.** 🌈') print('Connected.') print('Username: ' + bot.user.name) print('-------') # Close connection to Discord - immediate offline @bot.command(name=',die', aliases=[',d', ',close', ',kill'], brief='Kills the bot', description='BOT OWNER ONLY\nCloses the connection to Discord', hidden=True) -@checks.del_ctx() @commands.is_owner() +@checks.del_ctx() async def die(ctx): try: await bot.get_channel(config['shutdown_channel']).send('Am g0 by3e333333eee. **H4v3 n1GhT.** 💤') @@ -45,8 +45,8 @@ async def die(ctx): # Invite bot to bot owner's server @bot.command(name=',invite', aliases=[',inv', ',link'], brief='Invite the bot', description='BOT OWNER ONLY\nInvite the bot to a server (Requires admin)', hidden=True) -@checks.del_ctx() @commands.is_owner() +@checks.del_ctx() async def invite(ctx): try: await ctx.send('🔗 https://discordapp.com/oauth2/authorize?&client_id=' + str(config['client_id']) + '&scope=bot&permissions=' + str(config['permissions'])) @@ -57,7 +57,7 @@ async def invite(ctx): @bot.command(brief='[IN TESTING]', description='[IN TESTING]', hidden=True) async def hi(ctx): try: - hello = 'Hello, ' + ctx.message.author.mention + '.' + hello = 'Hewwo, ' + ctx.message.author.mention + '.' if ctx.message.author.id == checks.owner_id: hello += '.. ***Master.*** uwu' elif ctx.message.author.guild_permissions.administrator: @@ -70,9 +70,15 @@ async def hi(ctx): traceback.print_exc(limit=1) @bot.command(hidden=True) +@commands.is_owner() @checks.del_ctx() async def test(ctx): - pass + embed = discord.Embed(title='Title', type='rich', description='Description.', url='https://static1.e621.net/data/4b/3e/4b3ec0c2e8580f418e4ce019dfd5ac32.png', color=discord.Color.from_rgb(255, 255, 255)) + embed = embed.set_image('https://static1.e621.net/data/27/0f/270fd28caa5e6d8bf542a76515848e02.png') + embed = embed.set_footer('Footer') + embed = embed.set_author('Author') + embed = embed.set_thumbnail('https://cdn.discordapp.com/attachments/353251794161500163/357707620561453077/9d803ea3-b7fa-401f-89cf-f32cf21fe772.png') + ctx.send('Embed test', embed=embed) bot.add_cog(info.Info(bot)) bot.add_cog(tools.Utils(bot)) From 3c2f5c5cc07c4a229522db298c0ea335011b7d00 Mon Sep 17 00:00:00 2001 From: Myned Date: Wed, 27 Sep 2017 01:36:27 -0400 Subject: [PATCH 03/29] STUFF AND THINGS OKAY --- src/main/cogs/booru.py | 15 +++++++++++++-- src/main/misc/exceptions.py | 2 ++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/cogs/booru.py b/src/main/cogs/booru.py index 77d971c..53bf163 100644 --- a/src/main/cogs/booru.py +++ b/src/main/cogs/booru.py @@ -37,7 +37,7 @@ import requests import traceback from discord import reaction from discord.ext import commands -from discord.ext.commands import errors +from discord import errors as err from misc import checks from misc import exceptions as exc from utils import formatter, scraper @@ -101,6 +101,10 @@ class MsG: await ctx.send('❌ `' + str(e) + '` **out of bounds.**', delete_after=10) except exc.TagBoundsError as e: await ctx.send('❌ `' + str(e) + '` **out of bounds.** Tags limited to 5, currently.', delete_after=10) + except ValueError: + await ctx.send('❌ `' + args[-1] + '` **not a valid limit.**', delete_after=10) + except exc.NotFound: + await ctx.send('❌ **Tag(s) not found.**', delete_after=10) except Exception: await ctx.send(exc.base) traceback.print_exc() @@ -125,6 +129,10 @@ class MsG: await ctx.send('❌ `' + str(e) + '` **out of bounds.**', delete_after=10) except exc.TagBoundsError as e: await ctx.send('❌ `' + str(e) + '` **out of bounds.** Tags limited to 5, currently.', delete_after=10) + except ValueError: + await ctx.send('❌ `' + args[-1] + '` **not a valid limit.**', delete_after=10) + except exc.NotFound: + await ctx.send('❌ **Tag(s) not found.**', delete_after=10) except Exception: await ctx.send(exc.base) traceback.print_exc(limit=1) @@ -156,7 +164,10 @@ class MsG: raise exc.BoundsError(args[-1]) # Checks for blacklisted tags in endpoint blacklists - try/except is for continuing the parent loop while len(urls) < limit: - request = requests.get('https://' + booru + '.net/post/index.json?limit=6&tags=order:random' + formatter.tostring_commas(args)).json() + headers = {'user-agent': 'Modumind/0.0.1 (Myned)'} + request = requests.get('https://' + booru + '.net/post/index.json?limit=6&tags=order:random' + formatter.tostring_commas(args), headers=headers).json() + if not request: + raise exc.NotFound for post in request: if 'swf' in post['file_ext'] or 'webm' in post['file_ext']: continue diff --git a/src/main/misc/exceptions.py b/src/main/misc/exceptions.py index a56802a..5c98df7 100644 --- a/src/main/misc/exceptions.py +++ b/src/main/misc/exceptions.py @@ -20,5 +20,7 @@ class FlagError(Exception): pass class BlacklistError(Exception): pass +class NotFound(Exception): + pass class Continue(Exception): pass From b4346cb33476f3656fcdb2eb73df292c44e36042 Mon Sep 17 00:00:00 2001 From: Myned Date: Wed, 27 Sep 2017 14:49:21 -0400 Subject: [PATCH 04/29] Added check for shutdown output --- src/main/run.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/run.py b/src/main/run.py index bd19697..cd65615 100644 --- a/src/main/run.py +++ b/src/main/run.py @@ -35,7 +35,8 @@ async def on_ready(): @checks.del_ctx() async def die(ctx): try: - await bot.get_channel(config['shutdown_channel']).send('Am g0 by3e333333eee. **H4v3 n1GhT.** 💤') + if isinstance(bot.get_channel(config['startup_channel']), discord.TextChannel): + await bot.get_channel(config['shutdown_channel']).send('Am g0 by3e333333eee. **H4v3 n1GhT.** 💤') await bot.close() print('-------') print('Closed.') From 8420f3ee57a40a19cc08af07047425c8074ff406 Mon Sep 17 00:00:00 2001 From: Myned Date: Sat, 30 Sep 2017 03:27:57 -0400 Subject: [PATCH 05/29] Adding alias filtering and removed set command --- src/main/cogs/booru.py | 116 ++++++++++++++++++++---------------- src/main/misc/exceptions.py | 2 + 2 files changed, 68 insertions(+), 50 deletions(-) diff --git a/src/main/cogs/booru.py b/src/main/cogs/booru.py index 53bf163..650d8a7 100644 --- a/src/main/cogs/booru.py +++ b/src/main/cogs/booru.py @@ -6,7 +6,7 @@ try: print('\"global_blacklist.json\" loaded.') except FileNotFoundError: with open('global_blacklist.json', 'w+') as iofile: - print('Blacklist file not found: \"global_blacklist.json\" created.') + print('Blacklist file not found: \"global_blacklist.json\" created and loaded.') json.dump([], iofile, indent=4, sort_keys=True) iofile.seek(0) global_blacklist = json.load(iofile) @@ -16,7 +16,7 @@ try: print('\"guild_blacklist.json\" loaded.') except FileNotFoundError: with open('guild_blacklist.json', 'w+') as iofile: - print('Blacklist file not found: \"guild_blacklist.json\" created.') + print('Blacklist file not found: \"guild_blacklist.json\" created and loaded.') json.dump({}, iofile, indent=4, sort_keys=True) iofile.seek(0) guild_blacklist = json.load(iofile) @@ -26,10 +26,20 @@ try: print('\"user_blacklist.json\" loaded.') except FileNotFoundError: with open('user_blacklist.json', 'w+') as iofile: - print('Blacklist file not found: \"user_blacklist.json\" created.') + print('Blacklist file not found: \"user_blacklist.json\" created and loaded.') json.dump({}, iofile, indent=4, sort_keys=True) iofile.seek(0) user_blacklist = json.load(iofile) +try: + with open('aliases.json') as infile: + aliases = json.load(infile) + print('\"aliases.json\" loaded.') +except FileNotFoundError: + with open('aliases.json', 'w+') as iofile: + print('Aliases file not found: \"aliases.json\" created and loaded.') + json.dump({'global_blacklist': {}, 'guild_blacklist': {}, 'user_blacklist': {}}, iofile, indent=4, sort_keys=True) + iofile.seek(0) + aliases = json.load(iofile) import asyncio import discord @@ -42,6 +52,7 @@ from misc import checks from misc import exceptions as exc from utils import formatter, scraper +headers = {'user-agent': 'Modumind/0.0.1 (Myned)'} last_command = {} class MsG: @@ -72,6 +83,18 @@ class MsG: await ctx.send(exc.base) traceback.print_exc(limit=1) + # Tag aliases + @commands.command(name='aliases', aliases=['a'], brief='e621 Tag aliases', description='e621 | NSFW\nSearch aliases for given tag') + @checks.del_ctx() + async def alias(self, ctx, tag): + global headers + aliases = [] + try: + alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() + for dic in alias_request: + aliases.append(dic['name']) + await ctx.send('✅ `' + tag + '` **aliases:**\n`' + formatter.tostring(aliases) + '`') + # Reverse image searches a linked image using the public iqdb @commands.command(name='reverse', aliases=['rev', 'ris'], brief='e621 Reverse image search', description='e621 | NSFW\nReverse-search an image with given URL') @checks.del_ctx() @@ -104,7 +127,9 @@ class MsG: except ValueError: await ctx.send('❌ `' + args[-1] + '` **not a valid limit.**', delete_after=10) except exc.NotFound: - await ctx.send('❌ **Tag(s) not found.**', delete_after=10) + await ctx.send('❌ **Post not found.**', delete_after=10) + except exc.Timeout: + await ctx.send('❌ **Request timed out.**', delete_after=10) except Exception: await ctx.send(exc.base) traceback.print_exc() @@ -132,14 +157,16 @@ class MsG: except ValueError: await ctx.send('❌ `' + args[-1] + '` **not a valid limit.**', delete_after=10) except exc.NotFound: - await ctx.send('❌ **Tag(s) not found.**', delete_after=10) + await ctx.send('❌ **Post not found.**', delete_after=10) + except exc.Timeout: + await ctx.send('❌ **Request timed out.**', delete_after=10) except Exception: await ctx.send(exc.base) traceback.print_exc(limit=1) # Messy code that checks image limit and tags in blacklists async def check_send_urls(self, ctx, booru, args): - global global_blacklist, guild_blacklist, user_blacklist + global global_blacklist, guild_blacklist, user_blacklist, aliases, headers if isinstance(ctx.message.guild, discord.Guild): guild = ctx.message.guild else: @@ -148,11 +175,7 @@ class MsG: user = ctx.message.author urls = [] limit = 1 - # Checks if tags are in the file blacklists - if args: - for tag in args: - if tag == 'swf' or tag == 'webm' or tag in global_blacklist or tag in guild_blacklist.get(str(guild.id), {}).get(str(channel.id), []) or tag in user_blacklist.get(str(user.id), []): - raise exc.TagBlacklisted(tag) + c = 0 if len(args) > 5: raise exc.TagBoundsError(formatter.tostring(args[5:])) # Checks for, defines, and removes limit from end of args @@ -162,9 +185,13 @@ class MsG: args.pop() else: raise exc.BoundsError(args[-1]) + # Checks if tags are in local blacklists + if args: + for tag in args: + if tag == 'swf' or tag == 'webm' or tag in global_blacklist or tag in guild_blacklist.get(str(guild.id), {}).get(str(channel.id), []) or tag in user_blacklist.get(str(user.id), []) or tag in aliases['global_blacklist'].values() or tag in aliases['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), []).values() or tag in aliases['user_blacklist'].get(str(user.id), []).values(): + raise exc.TagBlacklisted(tag) # Checks for blacklisted tags in endpoint blacklists - try/except is for continuing the parent loop while len(urls) < limit: - headers = {'user-agent': 'Modumind/0.0.1 (Myned)'} request = requests.get('https://' + booru + '.net/post/index.json?limit=6&tags=order:random' + formatter.tostring_commas(args), headers=headers).json() if not request: raise exc.NotFound @@ -187,6 +214,9 @@ class MsG: urls.append(post['file_url']) if len(urls) == limit: break + c += 1 + if c == 10: + raise exc.Timeout for url in urls: await ctx.send('`' + formatter.tostring(args) + '`\n' + url) @@ -279,18 +309,24 @@ class MsG: @_add_tags.command(name='global', aliases=['gl', 'g']) @commands.is_owner() async def __add_global_tags(self, ctx, *tags): - global global_blacklist + global global_blacklist, aliases, headers for tag in tags: if tag in global_blacklist: raise exc.TagExists(tag) global_blacklist.extend(tags) + for tag in tags: + alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() + for dic in alias_request: + aliases['global_blacklist'].setdefault(tag, []).append(dic['name']) with open('global_blacklist.json', 'w') as outfile: json.dump(global_blacklist, outfile, indent=4, sort_keys=True) + with open('aliases.json', 'w') as outfile: + json.dump(aliases, outfile, indent=4, sort_keys=True) await ctx.send('✅ **Added to global blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) @_add_tags.command(name='channel', aliases=['ch', 'c']) @commands.has_permissions(manage_channels=True) async def __add_channel_tags(self, ctx, *tags): - global guild_blacklist + global guild_blacklist, aliases, headers if isinstance(ctx.message.guild, discord.Guild): guild = ctx.message.guild else: @@ -300,19 +336,31 @@ class MsG: if tag in guild_blacklist.get(str(guild.id), {}).get(str(channel.id), []): raise exc.TagExists(tag) guild_blacklist.setdefault(str(guild.id), {}).setdefault(str(channel.id), []).extend(tags) + for tag in tags: + alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() + for dic in alias_request: + aliases['guild_blacklist'].setdefault(str(guild.id), {}).setdefault(str(channel.id), {}).setdefault(tag, []).append(dic['name']) with open('guild_blacklist.json', 'w') as outfile: json.dump(guild_blacklist, outfile, indent=4, sort_keys=True) + with open('aliases.json', 'w') as outfile: + json.dump(aliases, outfile, indent=4, sort_keys=True) await ctx.send('✅ **Added to** <#' + str(channel.id) + '> **blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) @_add_tags.command(name='me', aliases=['m']) async def __add_user_tags(self, ctx, *tags): - global user_blacklist + global user_blacklist, aliases, headers user = ctx.message.author for tag in tags: if tag in user_blacklist.get(str(user.id), []): raise exc.TagExists(tag) user_blacklist.setdefault(str(user.id), []).extend(tags) + for tag in tags: + alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() + for dic in alias_request: + aliases['user_blacklist'].setdefault(str(user.id), {}).setdefault(tag, []).append(dic['name']) with open('user_blacklist.json', 'w') as outfile: json.dump(user_blacklist, outfile, indent=4, sort_keys=True) + with open('aliases.json', 'w') as outfile: + json.dump(aliases, outfile, indent=4, sort_keys=True) await ctx.send('✅ ' + user.mention + ' **added:**\n```' + formatter.tostring(tags) + '```', delete_after=5) @blacklist.group(name='remove', aliases=['rm', 'r']) @@ -327,6 +375,7 @@ class MsG: for tag in tags: if tag in global_blacklist: global_blacklist.remove(tag) + aliases['global_blacklist'][tag].clear() else: raise exc.TagError(tag) with open('global_blacklist.json', 'w') as outfile: @@ -344,6 +393,7 @@ class MsG: for tag in tags: if tag in guild_blacklist.get(str(guild.id), {}).get(str(channel.id), []): guild_blacklist.get(str(guild.id), {})[str(channel.id)].remove(tag) + aliases['guild_blacklist'][str(guild.id)][str(channel.id)][tag].clear() else: raise exc.TagError(tag) with open('guild_blacklist.json', 'w') as outfile: @@ -356,47 +406,13 @@ class MsG: for tag in tags: if tag in user_blacklist.get(str(user.id), []): user_blacklist.get[str(user.id)].remove(tag) + aliases['user_blacklist'][str(user.id)][tag].clear() else: raise exc.TagError(tag) with open('user_blacklist.json', 'w') as outfile: json.dump(user_blacklist, outfile, indent=4, sort_keys=True) await ctx.send('✅ ' + user.mention + ' **removed:**\n```' + formatter.tostring(tags) + '```', delete_after=5) - @blacklist.group(name='set', aliases=['s']) - async def _set_blacklist(self, ctx): - if ctx.invoked_subcommand is None: - await ctx.send('❌ **Invalid blacklist.**') - - @_set_blacklist.command(name='global', aliases=['gl', 'g']) - @commands.is_owner() - async def __set_global_blacklist(self, ctx, *tags): - global global_blacklist - global_blacklist = tags[:] - with open('global_blacklist.json', 'w') as outfile: - json.dump(global_blacklist, outfile, indent=4, sort_keys=True) - await ctx.send('✅ **Global blacklist set to:**\n```' + formatter.tostring(global_blacklist) + '```', delete_after=10) - @_set_blacklist.command(name='channel', aliases=['ch', 'c']) - @commands.has_permissions(manage_channels=True) - async def __set_channel_blacklist(self, ctx, *tags): - global guild_blacklist - if isinstance(ctx.message.guild, discord.Guild): - guild = ctx.message.guild - else: - guild = ctx.message.channel - channel = ctx.message.channel - guild_blacklist.setdefault(str(guild.id), {})[str(channel.id)] = tags[:] - with open('guild_blacklist.json', 'w') as outfile: - json.dump(guild_blacklist, outfile, indent=4, sort_keys=True) - await ctx.send('✅ <#' + str(channel.id) + '> **blacklist set to:**\n```' + formatter.tostring(guild_blacklist.get(str(guild.id), {}).get(str(channel.id), [])) + '```', delete_after=10) - @_set_blacklist.command(name='me', aliases=['m']) - async def __set_user_blacklist(self, ctx, *tags): - global user_blacklist - user = ctx.message.author - user_blacklist[str(user.id)] = tags[:] - with open('user_blacklist.json', 'w') as outfile: - json.dump(user_blacklist, outfile, indent=4, sort_keys=True) - await ctx.send('✅ ' + user.mention + '**\'s blacklist set to:**\n```' + formatter.tostring(user_blacklist.get(str(user.id), [])) + '```', delete_after=10) - @blacklist.group(name='clear', aliases=['cl', 'c']) async def _clear_blacklist(self, ctx): if ctx.invoked_subcommand is None: diff --git a/src/main/misc/exceptions.py b/src/main/misc/exceptions.py index 5c98df7..f877ee4 100644 --- a/src/main/misc/exceptions.py +++ b/src/main/misc/exceptions.py @@ -22,5 +22,7 @@ class BlacklistError(Exception): pass class NotFound(Exception): pass +class Timeout(Exception): + pass class Continue(Exception): pass From 97f3d3a1bcef2e46e22573cb628ec63bdd03241a Mon Sep 17 00:00:00 2001 From: Myned Date: Sat, 30 Sep 2017 04:12:24 -0400 Subject: [PATCH 06/29] Combined separate blacklist files into one --- src/main/cogs/booru.py | 175 +++++++++++++++++------------------------ 1 file changed, 72 insertions(+), 103 deletions(-) diff --git a/src/main/cogs/booru.py b/src/main/cogs/booru.py index 650d8a7..bab453d 100644 --- a/src/main/cogs/booru.py +++ b/src/main/cogs/booru.py @@ -1,35 +1,15 @@ import json try: - with open('global_blacklist.json') as infile: - global_blacklist = json.load(infile) - print('\"global_blacklist.json\" loaded.') + with open('blacklists.json') as infile: + blacklists = json.load(infile) + print('\"blacklists.json\" loaded.') except FileNotFoundError: - with open('global_blacklist.json', 'w+') as iofile: - print('Blacklist file not found: \"global_blacklist.json\" created and loaded.') - json.dump([], iofile, indent=4, sort_keys=True) + with open('blacklists.json', 'w+') as iofile: + print('Blacklists file not found: \"blacklists.json\" created and loaded.') + json.dump({'global_blacklist': [], 'guild_blacklist': {}, 'user_blacklist': {}}, iofile, indent=4, sort_keys=True) iofile.seek(0) - global_blacklist = json.load(iofile) -try: - with open('guild_blacklist.json') as infile: - guild_blacklist = json.load(infile) - print('\"guild_blacklist.json\" loaded.') -except FileNotFoundError: - with open('guild_blacklist.json', 'w+') as iofile: - print('Blacklist file not found: \"guild_blacklist.json\" created and loaded.') - json.dump({}, iofile, indent=4, sort_keys=True) - iofile.seek(0) - guild_blacklist = json.load(iofile) -try: - with open('user_blacklist.json') as infile: - user_blacklist = json.load(infile) - print('\"user_blacklist.json\" loaded.') -except FileNotFoundError: - with open('user_blacklist.json', 'w+') as iofile: - print('Blacklist file not found: \"user_blacklist.json\" created and loaded.') - json.dump({}, iofile, indent=4, sort_keys=True) - iofile.seek(0) - user_blacklist = json.load(iofile) + blacklists = json.load(iofile) try: with open('aliases.json') as infile: aliases = json.load(infile) @@ -113,7 +93,7 @@ class MsG: @checks.del_ctx() @checks.is_nsfw() async def e621(self, ctx, *args): - global global_blacklist, guild_blacklist, user_blacklist + global blacklists args = list(args) try: await ctx.trigger_typing() @@ -143,7 +123,7 @@ class MsG: @commands.command(aliases=['e9', '9'], brief='e926 | SFW', description='e926 | SFW\nTag-based search for e926.net\n\nYou can only search 5 tags and 6 images at once for now.\ne9 [tags...] ([# of images])') @checks.del_ctx() async def e926(self, ctx, *args): - global global_blacklist, guild_blacklist, user_blacklist + global blacklists args = list(args) try: await ctx.trigger_typing() @@ -166,7 +146,7 @@ class MsG: # Messy code that checks image limit and tags in blacklists async def check_send_urls(self, ctx, booru, args): - global global_blacklist, guild_blacklist, user_blacklist, aliases, headers + global blacklists, aliases, headers if isinstance(ctx.message.guild, discord.Guild): guild = ctx.message.guild else: @@ -188,7 +168,7 @@ class MsG: # Checks if tags are in local blacklists if args: for tag in args: - if tag == 'swf' or tag == 'webm' or tag in global_blacklist or tag in guild_blacklist.get(str(guild.id), {}).get(str(channel.id), []) or tag in user_blacklist.get(str(user.id), []) or tag in aliases['global_blacklist'].values() or tag in aliases['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), []).values() or tag in aliases['user_blacklist'].get(str(user.id), []).values(): + if tag == 'swf' or tag == 'webm' or tag in blacklists['global_blacklist'] or tag in blacklists['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), []) or tag in blacklists['user_blacklist'].get(str(user.id), []) or tag in aliases['global_blacklist'].values() or tag in aliases['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), []).values() or tag in aliases['blacklists['user_blacklist']'].get(str(user.id), []).values(): raise exc.TagBlacklisted(tag) # Checks for blacklisted tags in endpoint blacklists - try/except is for continuing the parent loop while len(urls) < limit: @@ -199,15 +179,27 @@ class MsG: if 'swf' in post['file_ext'] or 'webm' in post['file_ext']: continue try: - for tag in global_blacklist: + for tag in blacklists['global_blacklist']: if tag in post['tags']: raise exc.Continue - for tag in guild_blacklist.get(str(guild.id), {}).get(str(channel.id), []): + for alias in aliases['global_blacklist'].values(): + for tag in alias: + if tag in post['tags']: + raise exc.Continue + for tag in blacklists['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), []): if tag in post['tags']: raise exc.Continue - for tag in user_blacklist.get(str(user.id), []): + for alias in aliases['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), {}).values(): + for tag in alias: + if tag in post['tags']: + raise exc.Continue + for tag in blacklists['user_blacklist'].get(str(user.id), []): if tag in post['tags']: raise exc.Continue + for alias in aliases['user_blacklist'].get(str(user.id), {}).values(): + for tag in alias: + if tag in post['tags']: + raise exc.Continue except exc.Continue: continue if post['file_url'] not in urls: @@ -238,16 +230,14 @@ class MsG: if isinstance(error, KeyError): return await ctx.send('❌ **Blacklist does not exist.**', delete_after=10) - @blacklist.command(name='update', aliases=['upd', 'up']) + @blacklist.after_invoke async def _update_blacklists(self, ctx): - global global_blacklist, guild_blacklist, user_blacklist - with open('global_blacklist.json', 'w') as outfile: - json.dump(global_blacklist, outfile, indent=4, sort_keys=True) - with open('guild_blacklist.json', 'w') as outfile: - json.dump(guild_blacklist, outfile, indent=4, sort_keys=True) - with open('user_blacklist.json', 'w') as outfile: - json.dump(user_blacklist, outfile, indent=4, sort_keys=True) - await ctx.send('✅ **Blacklists updated.**') + global blacklists, aliases + with open('blacklists.json', 'w') as outfile: + json.dump(blacklists, outfile, indent=4, sort_keys=True) + with open('aliases.json', 'w') as outfile: + json.dump(aliases, outfile, indent=4, sort_keys=True) + print('Updated.') @blacklist.group(name='get', aliases=['g']) async def _get_blacklist(self, ctx): @@ -256,31 +246,31 @@ class MsG: @_get_blacklist.command(name='global', aliases=['gl', 'g']) async def __get_global_blacklist(self, ctx): - global global_blacklist - await ctx.send('đŸšĢ **Global blacklist:**\n```' + formatter.tostring(global_blacklist) + '```') + global blacklists + await ctx.send('đŸšĢ **Global blacklist:**\n```' + formatter.tostring(blacklists['global_blacklist']) + '```') @_get_blacklist.command(name='channel', aliases=['ch', 'c']) async def __get_channel_blacklist(self, ctx): - global guild_blacklist + global blacklists if isinstance(ctx.message.guild, discord.Guild): guild = ctx.message.guild else: guild = ctx.message.channel channel = ctx.message.channel - await ctx.send('đŸšĢ <#' + str(channel.id) + '> **blacklist:**\n```' + formatter.tostring(guild_blacklist.get(str(guild.id), {}).get(str(channel.id), [])) + '```') + await ctx.send('đŸšĢ <#' + str(channel.id) + '> **blacklist:**\n```' + formatter.tostring(blacklists['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), [])) + '```') @_get_blacklist.command(name='me', aliases=['m']) async def __get_user_blacklist(self, ctx): - global user_blacklist + global blacklists user = ctx.message.author - await ctx.send('đŸšĢ ' + user.mention + '**\'s blacklist:**\n```' + formatter.tostring(user_blacklist.get(str(user.id), [])) + '```', delete_after=10) + await ctx.send('đŸšĢ ' + user.mention + '**\'s blacklist:**\n```' + formatter.tostring(blacklists['user_blacklist'].get(str(user.id), [])) + '```', delete_after=10) @_get_blacklist.command(name='here', aliases=['h']) async def __get_here_blacklists(self, ctx): - global global_blacklist, guild_blacklist + global blacklists if isinstance(ctx.message.guild, discord.Guild): guild = ctx.message.guild else: guild = ctx.message.channel channel = ctx.message.channel - await ctx.send('đŸšĢ **__Blacklisted:__**\n\n**Global:**\n```' + formatter.tostring(global_blacklist) + '```\n**<#' + str(channel.id) + '>:**\n```' + formatter.tostring(guild_blacklist.get(str(guild.id), {}).get(str(channel.id), [])) + '```') + await ctx.send('đŸšĢ **__Blacklisted:__**\n\n**Global:**\n```' + formatter.tostring(blacklists['global_blacklist']) + '```\n**<#' + str(channel.id) + '>:**\n```' + formatter.tostring(blacklists['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), [])) + '```') @_get_blacklist.group(name='all', aliases=['a']) async def __get_all_blacklists(self, ctx): if ctx.invoked_subcommand is None: @@ -289,17 +279,17 @@ class MsG: @__get_all_blacklists.command(name='guild', aliases=['g']) @commands.has_permissions(manage_channels=True) async def ___get_all_guild_blacklists(self, ctx): - global guild_blacklist + global blacklists if isinstance(ctx.message.guild, discord.Guild): guild = ctx.message.guild else: guild = ctx.message.channel - await ctx.send('đŸšĢ **__' + guild.name + ' blacklists:__**\n\n' + formatter.dict_tostring(guild_blacklist.get(str(guild.id), {}))) + await ctx.send('đŸšĢ **__' + guild.name + ' blacklists:__**\n\n' + formatter.dict_tostring(blacklists['guild_blacklist'].get(str(guild.id), {}))) @__get_all_blacklists.command(name='user', aliases=['u', 'member', 'm']) @commands.is_owner() async def ___get_all_user_blacklists(self, ctx): - global user_blacklist - await ctx.send('đŸšĢ **__User blacklists:__**\n\n' + formatter.dict_tostring(user_blacklist)) + global blacklists + await ctx.send('đŸšĢ **__User blacklists:__**\n\n' + formatter.dict_tostring(blacklists['user_blacklist'])) @blacklist.group(name='add', aliases=['a']) async def _add_tags(self, ctx): @@ -309,58 +299,46 @@ class MsG: @_add_tags.command(name='global', aliases=['gl', 'g']) @commands.is_owner() async def __add_global_tags(self, ctx, *tags): - global global_blacklist, aliases, headers + global blacklists, aliases, headers for tag in tags: - if tag in global_blacklist: + if tag in blacklists['global_blacklist']: raise exc.TagExists(tag) - global_blacklist.extend(tags) + blacklists['global_blacklist'].extend(tags) for tag in tags: alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() for dic in alias_request: aliases['global_blacklist'].setdefault(tag, []).append(dic['name']) - with open('global_blacklist.json', 'w') as outfile: - json.dump(global_blacklist, outfile, indent=4, sort_keys=True) - with open('aliases.json', 'w') as outfile: - json.dump(aliases, outfile, indent=4, sort_keys=True) await ctx.send('✅ **Added to global blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) @_add_tags.command(name='channel', aliases=['ch', 'c']) @commands.has_permissions(manage_channels=True) async def __add_channel_tags(self, ctx, *tags): - global guild_blacklist, aliases, headers + global blacklists, aliases, headers if isinstance(ctx.message.guild, discord.Guild): guild = ctx.message.guild else: guild = ctx.message.channel channel = ctx.message.channel for tag in tags: - if tag in guild_blacklist.get(str(guild.id), {}).get(str(channel.id), []): + if tag in blacklists['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), []): raise exc.TagExists(tag) - guild_blacklist.setdefault(str(guild.id), {}).setdefault(str(channel.id), []).extend(tags) + blacklists['guild_blacklist'].setdefault(str(guild.id), {}).setdefault(str(channel.id), []).extend(tags) for tag in tags: alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() for dic in alias_request: aliases['guild_blacklist'].setdefault(str(guild.id), {}).setdefault(str(channel.id), {}).setdefault(tag, []).append(dic['name']) - with open('guild_blacklist.json', 'w') as outfile: - json.dump(guild_blacklist, outfile, indent=4, sort_keys=True) - with open('aliases.json', 'w') as outfile: - json.dump(aliases, outfile, indent=4, sort_keys=True) await ctx.send('✅ **Added to** <#' + str(channel.id) + '> **blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) @_add_tags.command(name='me', aliases=['m']) async def __add_user_tags(self, ctx, *tags): - global user_blacklist, aliases, headers + global blacklists, aliases, headers user = ctx.message.author for tag in tags: - if tag in user_blacklist.get(str(user.id), []): + if tag in blacklists['user_blacklist'].get(str(user.id), []): raise exc.TagExists(tag) - user_blacklist.setdefault(str(user.id), []).extend(tags) + blacklists['user_blacklist'].setdefault(str(user.id), []).extend(tags) for tag in tags: alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() for dic in alias_request: aliases['user_blacklist'].setdefault(str(user.id), {}).setdefault(tag, []).append(dic['name']) - with open('user_blacklist.json', 'w') as outfile: - json.dump(user_blacklist, outfile, indent=4, sort_keys=True) - with open('aliases.json', 'w') as outfile: - json.dump(aliases, outfile, indent=4, sort_keys=True) await ctx.send('✅ ' + user.mention + ' **added:**\n```' + formatter.tostring(tags) + '```', delete_after=5) @blacklist.group(name='remove', aliases=['rm', 'r']) @@ -371,46 +349,40 @@ class MsG: @_remove_tags.command(name='global', aliases=['gl', 'g']) @commands.is_owner() async def __remove_global_tags(self, ctx, *tags): - global global_blacklist + global blacklists, aliases for tag in tags: - if tag in global_blacklist: - global_blacklist.remove(tag) + if tag in blacklists['global_blacklist']: + blacklists['global_blacklist'].remove(tag) aliases['global_blacklist'][tag].clear() else: raise exc.TagError(tag) - with open('global_blacklist.json', 'w') as outfile: - json.dump(global_blacklist, outfile, indent=4, sort_keys=True) await ctx.send('✅ **Removed from global blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) @_remove_tags.command(name='channel', aliases=['ch', 'c']) @commands.has_permissions(manage_channels=True) async def __remove_channel_tags(self, ctx, *tags): - global guild_blacklist + global blacklists, aliases if isinstance(ctx.message.guild, discord.Guild): guild = ctx.message.guild else: guild = ctx.message.channel channel = ctx.message.channel for tag in tags: - if tag in guild_blacklist.get(str(guild.id), {}).get(str(channel.id), []): - guild_blacklist.get(str(guild.id), {})[str(channel.id)].remove(tag) + if tag in blacklists['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), []): + blacklists['guild_blacklist'].get(str(guild.id), {})[str(channel.id)].remove(tag) aliases['guild_blacklist'][str(guild.id)][str(channel.id)][tag].clear() else: raise exc.TagError(tag) - with open('guild_blacklist.json', 'w') as outfile: - json.dump(guild_blacklist, outfile, indent=4, sort_keys=True) await ctx.send('✅ **Removed from** <#' + str(channel.id) + '> **blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) @_remove_tags.command(name='me', aliases=['m']) async def __remove_user_tags(self, ctx, *tags): - global user_blacklist + global blacklists, aliases user = ctx.message.author for tag in tags: - if tag in user_blacklist.get(str(user.id), []): - user_blacklist.get[str(user.id)].remove(tag) + if tag in blacklists['user_blacklist'].get(str(user.id), []): + blacklists['user_blacklist'].get[str(user.id)].remove(tag) aliases['user_blacklist'][str(user.id)][tag].clear() else: raise exc.TagError(tag) - with open('user_blacklist.json', 'w') as outfile: - json.dump(user_blacklist, outfile, indent=4, sort_keys=True) await ctx.send('✅ ' + user.mention + ' **removed:**\n```' + formatter.tostring(tags) + '```', delete_after=5) @blacklist.group(name='clear', aliases=['cl', 'c']) @@ -421,29 +393,26 @@ class MsG: @_clear_blacklist.command(name='global', aliases=['gl', 'g']) @commands.is_owner() async def __clear_global_blacklist(self, ctx): - global global_blacklist - del global_blacklist - with open('global_blacklist.json', 'w') as outfile: - json.dump(global_blacklist, outfile, indent=4, sort_keys=True) + global blacklists, aliases + blacklists['global_blacklist'] = [] + aliases['global_blacklist'] = {} await ctx.send('✅ **Global blacklist cleared.**', delete_after=5) @_clear_blacklist.command(name='channel', aliases=['ch', 'c']) @commands.has_permissions(manage_channels=True) async def __clear_channel_blacklist(self, ctx): - global guild_blacklist + global blacklists, aliases if isinstance(ctx.message.guild, discord.Guild): guild = ctx.message.guild else: guild = ctx.message.channel channel = ctx.message.channel - del guild_blacklist.get(str(guild.id), {})[str(channel.id)] - with open('guild_blacklist.json', 'w') as outfile: - json.dump(guild_blacklist, outfile, indent=4, sort_keys=True) + blacklists['guild_blacklist'][str(guild.id)][str(channel.id)] = [] + aliases['guild_blacklist'][str(guild.id)][str(channel.id)] = {} await ctx.send('✅ <#' + str(channel.id) + '> **blacklist cleared.**', delete_after=5) @_clear_blacklist.command(name='me', aliases=['m']) async def __clear_user_blacklist(self, ctx): - global user_blacklist + global blacklists, aliases user = ctx.message.author - del user_blacklist[str(user.id)] - with open('user_blacklist.json', 'w') as outfile: - json.dump(user_blacklist, outfile, indent=4, sort_keys=True) + blacklists['user_blacklist'][str(user.id)] = [] + aliases['user_blacklist'][str(user.id)] = {} await ctx.send('✅ ' + user.mention + '**\'s blacklist cleared.**', delete_after=5) From 9041af8463f875b118ec896e0ee4d71450e83d4b Mon Sep 17 00:00:00 2001 From: Myned Date: Sun, 1 Oct 2017 15:40:35 -0400 Subject: [PATCH 07/29] Shrunk image precheck and fetching checks --- src/main/cogs/booru.py | 113 ++++++++++++++++++++++++----------------- 1 file changed, 67 insertions(+), 46 deletions(-) diff --git a/src/main/cogs/booru.py b/src/main/cogs/booru.py index bab453d..6d974f7 100644 --- a/src/main/cogs/booru.py +++ b/src/main/cogs/booru.py @@ -60,7 +60,7 @@ class MsG: except exc.ImageError: await ctx.send('❌ ' + ctx.message.author.mention + ' **No image found.**') except Exception: - await ctx.send(exc.base) + await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') traceback.print_exc(limit=1) # Tag aliases @@ -73,7 +73,10 @@ class MsG: alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() for dic in alias_request: aliases.append(dic['name']) - await ctx.send('✅ `' + tag + '` **aliases:**\n`' + formatter.tostring(aliases) + '`') + await ctx.send('✅ `' + tag + '` **aliases:**\n```' + formatter.tostring(aliases) + '```') + except Exception: + await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') + traceback.print_exc(limit=1) # Reverse image searches a linked image using the public iqdb @commands.command(name='reverse', aliases=['rev', 'ris'], brief='e621 Reverse image search', description='e621 | NSFW\nReverse-search an image with given URL') @@ -85,7 +88,7 @@ class MsG: except exc.MatchError: await ctx.send('❌ ' + ctx.message.author.mention + ' **No probable match.**', delete_after=10) except Exception: - await ctx.send(exc.base) + await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') traceback.print_exc(limit=1) # Searches for and returns images from e621.net given tags when not blacklisted @@ -109,9 +112,9 @@ class MsG: except exc.NotFound: await ctx.send('❌ **Post not found.**', delete_after=10) except exc.Timeout: - await ctx.send('❌ **Request timed out.**', delete_after=10) + await ctx.send('❌ **Request timed out.**') except Exception: - await ctx.send(exc.base) + await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') traceback.print_exc() @e621.error @@ -139,9 +142,9 @@ class MsG: except exc.NotFound: await ctx.send('❌ **Post not found.**', delete_after=10) except exc.Timeout: - await ctx.send('❌ **Request timed out.**', delete_after=10) + await ctx.send('❌ **Request timed out.**') except Exception: - await ctx.send(exc.base) + await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') traceback.print_exc(limit=1) # Messy code that checks image limit and tags in blacklists @@ -153,6 +156,7 @@ class MsG: guild = ctx.message.channel channel = ctx.message.channel user = ctx.message.author + blacklist = [] urls = [] limit = 1 c = 0 @@ -160,15 +164,23 @@ class MsG: raise exc.TagBoundsError(formatter.tostring(args[5:])) # Checks for, defines, and removes limit from end of args if args and len(args[-1]) == 1: - if int(args[-1]) <= 6 and int(args[-1]) >= 1: + if int(args[-1]) <= 5 and int(args[-1]) >= 1: limit = int(args[-1]) args.pop() else: raise exc.BoundsError(args[-1]) + # Creates temp blacklist based on context + for k, v in aliases['global_blacklist'].items(): + blacklist.extend([k] + v) + for k, v in aliases['guild_blacklist'][str(guild.id)][str(channel.id)].items(): + blacklist.extend([k] + v) + for k, v in aliases['user_blacklist'][str(user.id)].items(): + blacklist.extend([k] + v) + print(blacklist) # Checks if tags are in local blacklists if args: for tag in args: - if tag == 'swf' or tag == 'webm' or tag in blacklists['global_blacklist'] or tag in blacklists['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), []) or tag in blacklists['user_blacklist'].get(str(user.id), []) or tag in aliases['global_blacklist'].values() or tag in aliases['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), []).values() or tag in aliases['blacklists['user_blacklist']'].get(str(user.id), []).values(): + if tag == 'swf' or tag == 'webm' or tag in blacklist: raise exc.TagBlacklisted(tag) # Checks for blacklisted tags in endpoint blacklists - try/except is for continuing the parent loop while len(urls) < limit: @@ -179,27 +191,9 @@ class MsG: if 'swf' in post['file_ext'] or 'webm' in post['file_ext']: continue try: - for tag in blacklists['global_blacklist']: + for tag in blacklist: if tag in post['tags']: raise exc.Continue - for alias in aliases['global_blacklist'].values(): - for tag in alias: - if tag in post['tags']: - raise exc.Continue - for tag in blacklists['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), []): - if tag in post['tags']: - raise exc.Continue - for alias in aliases['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), {}).values(): - for tag in alias: - if tag in post['tags']: - raise exc.Continue - for tag in blacklists['user_blacklist'].get(str(user.id), []): - if tag in post['tags']: - raise exc.Continue - for alias in aliases['user_blacklist'].get(str(user.id), {}).values(): - for tag in alias: - if tag in post['tags']: - raise exc.Continue except exc.Continue: continue if post['file_url'] not in urls: @@ -207,7 +201,7 @@ class MsG: if len(urls) == limit: break c += 1 - if c == 10: + if c == 50: raise exc.Timeout for url in urls: await ctx.send('`' + formatter.tostring(args) + '`\n' + url) @@ -222,7 +216,7 @@ class MsG: @blacklist.error async def blacklist_error(self, ctx, error): if isinstance(error, commands.CheckFailure): - return await ctx.send('❌ **Insufficient permissions.**', delete_after=10) + return await ctx.send('❌ **Insufficient permissions.**') if isinstance(error, exc.TagExists): return await ctx.send('❌ `' + str(exc.TagExists) + '` **already in blacklist.**', delete_after=10) if isinstance(error, exc.TagError): @@ -230,15 +224,6 @@ class MsG: if isinstance(error, KeyError): return await ctx.send('❌ **Blacklist does not exist.**', delete_after=10) - @blacklist.after_invoke - async def _update_blacklists(self, ctx): - global blacklists, aliases - with open('blacklists.json', 'w') as outfile: - json.dump(blacklists, outfile, indent=4, sort_keys=True) - with open('aliases.json', 'w') as outfile: - json.dump(aliases, outfile, indent=4, sort_keys=True) - print('Updated.') - @blacklist.group(name='get', aliases=['g']) async def _get_blacklist(self, ctx): if ctx.invoked_subcommand is None: @@ -308,6 +293,10 @@ class MsG: alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() for dic in alias_request: aliases['global_blacklist'].setdefault(tag, []).append(dic['name']) + with open('blacklists.json', 'w') as outfile: + json.dump(blacklists, outfile, indent=4, sort_keys=True) + with open('aliases.json', 'w') as outfile: + json.dump(aliases, outfile, indent=4, sort_keys=True) await ctx.send('✅ **Added to global blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) @_add_tags.command(name='channel', aliases=['ch', 'c']) @commands.has_permissions(manage_channels=True) @@ -326,6 +315,10 @@ class MsG: alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() for dic in alias_request: aliases['guild_blacklist'].setdefault(str(guild.id), {}).setdefault(str(channel.id), {}).setdefault(tag, []).append(dic['name']) + with open('blacklists.json', 'w') as outfile: + json.dump(blacklists, outfile, indent=4, sort_keys=True) + with open('aliases.json', 'w') as outfile: + json.dump(aliases, outfile, indent=4, sort_keys=True) await ctx.send('✅ **Added to** <#' + str(channel.id) + '> **blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) @_add_tags.command(name='me', aliases=['m']) async def __add_user_tags(self, ctx, *tags): @@ -339,6 +332,10 @@ class MsG: alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() for dic in alias_request: aliases['user_blacklist'].setdefault(str(user.id), {}).setdefault(tag, []).append(dic['name']) + with open('blacklists.json', 'w') as outfile: + json.dump(blacklists, outfile, indent=4, sort_keys=True) + with open('aliases.json', 'w') as outfile: + json.dump(aliases, outfile, indent=4, sort_keys=True) await ctx.send('✅ ' + user.mention + ' **added:**\n```' + formatter.tostring(tags) + '```', delete_after=5) @blacklist.group(name='remove', aliases=['rm', 'r']) @@ -353,9 +350,13 @@ class MsG: for tag in tags: if tag in blacklists['global_blacklist']: blacklists['global_blacklist'].remove(tag) - aliases['global_blacklist'][tag].clear() + del aliases['global_blacklist'][tag] else: raise exc.TagError(tag) + with open('blacklists.json', 'w') as outfile: + json.dump(blacklists, outfile, indent=4, sort_keys=True) + with open('aliases.json', 'w') as outfile: + json.dump(aliases, outfile, indent=4, sort_keys=True) await ctx.send('✅ **Removed from global blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) @_remove_tags.command(name='channel', aliases=['ch', 'c']) @commands.has_permissions(manage_channels=True) @@ -367,22 +368,30 @@ class MsG: guild = ctx.message.channel channel = ctx.message.channel for tag in tags: - if tag in blacklists['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), []): - blacklists['guild_blacklist'].get(str(guild.id), {})[str(channel.id)].remove(tag) - aliases['guild_blacklist'][str(guild.id)][str(channel.id)][tag].clear() + if tag in blacklists['guild_blacklist'][str(guild.id)][str(channel.id)]: + blacklists['guild_blacklist'][str(guild.id)][str(channel.id)].remove(tag) + del aliases['guild_blacklist'][str(guild.id)][str(channel.id)][tag] else: raise exc.TagError(tag) + with open('blacklists.json', 'w') as outfile: + json.dump(blacklists, outfile, indent=4, sort_keys=True) + with open('aliases.json', 'w') as outfile: + json.dump(aliases, outfile, indent=4, sort_keys=True) await ctx.send('✅ **Removed from** <#' + str(channel.id) + '> **blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) @_remove_tags.command(name='me', aliases=['m']) async def __remove_user_tags(self, ctx, *tags): global blacklists, aliases user = ctx.message.author for tag in tags: - if tag in blacklists['user_blacklist'].get(str(user.id), []): - blacklists['user_blacklist'].get[str(user.id)].remove(tag) - aliases['user_blacklist'][str(user.id)][tag].clear() + if tag in blacklists['user_blacklist'][str(user.id)]: + blacklists['user_blacklist'][str(user.id)].remove(tag) + del aliases['user_blacklist'][str(user.id)][tag] else: raise exc.TagError(tag) + with open('blacklists.json', 'w') as outfile: + json.dump(blacklists, outfile, indent=4, sort_keys=True) + with open('aliases.json', 'w') as outfile: + json.dump(aliases, outfile, indent=4, sort_keys=True) await ctx.send('✅ ' + user.mention + ' **removed:**\n```' + formatter.tostring(tags) + '```', delete_after=5) @blacklist.group(name='clear', aliases=['cl', 'c']) @@ -396,6 +405,10 @@ class MsG: global blacklists, aliases blacklists['global_blacklist'] = [] aliases['global_blacklist'] = {} + with open('blacklists.json', 'w') as outfile: + json.dump(blacklists, outfile, indent=4, sort_keys=True) + with open('aliases.json', 'w') as outfile: + json.dump(aliases, outfile, indent=4, sort_keys=True) await ctx.send('✅ **Global blacklist cleared.**', delete_after=5) @_clear_blacklist.command(name='channel', aliases=['ch', 'c']) @commands.has_permissions(manage_channels=True) @@ -408,6 +421,10 @@ class MsG: channel = ctx.message.channel blacklists['guild_blacklist'][str(guild.id)][str(channel.id)] = [] aliases['guild_blacklist'][str(guild.id)][str(channel.id)] = {} + with open('blacklists.json', 'w') as outfile: + json.dump(blacklists, outfile, indent=4, sort_keys=True) + with open('aliases.json', 'w') as outfile: + json.dump(aliases, outfile, indent=4, sort_keys=True) await ctx.send('✅ <#' + str(channel.id) + '> **blacklist cleared.**', delete_after=5) @_clear_blacklist.command(name='me', aliases=['m']) async def __clear_user_blacklist(self, ctx): @@ -415,4 +432,8 @@ class MsG: user = ctx.message.author blacklists['user_blacklist'][str(user.id)] = [] aliases['user_blacklist'][str(user.id)] = {} + with open('blacklists.json', 'w') as outfile: + json.dump(blacklists, outfile, indent=4, sort_keys=True) + with open('aliases.json', 'w') as outfile: + json.dump(aliases, outfile, indent=4, sort_keys=True) await ctx.send('✅ ' + user.mention + '**\'s blacklist cleared.**', delete_after=5) From 41c5f861817818df29b591367088c6ab68aa108c Mon Sep 17 00:00:00 2001 From: Myned Date: Mon, 2 Oct 2017 15:20:04 -0400 Subject: [PATCH 08/29] Fixed KeyError, added limit extension, and playing with last_command --- src/main/cogs/booru.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/cogs/booru.py b/src/main/cogs/booru.py index 6d974f7..771a654 100644 --- a/src/main/cogs/booru.py +++ b/src/main/cogs/booru.py @@ -28,12 +28,12 @@ import traceback from discord import reaction from discord.ext import commands from discord import errors as err +from cogs import tools from misc import checks from misc import exceptions as exc from utils import formatter, scraper headers = {'user-agent': 'Modumind/0.0.1 (Myned)'} -last_command = {} class MsG: @@ -116,6 +116,7 @@ class MsG: except Exception: await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') traceback.print_exc() + tools.command_dict.setdefault(str(ctx.message.author.id), {}).update({'command': ctx.command, 'args': ctx.args}) @e621.error async def e621_error(self, ctx, error): @@ -172,11 +173,10 @@ class MsG: # Creates temp blacklist based on context for k, v in aliases['global_blacklist'].items(): blacklist.extend([k] + v) - for k, v in aliases['guild_blacklist'][str(guild.id)][str(channel.id)].items(): + for k, v in aliases['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), {}).items(): blacklist.extend([k] + v) - for k, v in aliases['user_blacklist'][str(user.id)].items(): + for k, v in aliases['user_blacklist'].get(str(user.id), {}).items(): blacklist.extend([k] + v) - print(blacklist) # Checks if tags are in local blacklists if args: for tag in args: @@ -201,7 +201,7 @@ class MsG: if len(urls) == limit: break c += 1 - if c == 50: + if c == 50 + limit: raise exc.Timeout for url in urls: await ctx.send('`' + formatter.tostring(args) + '`\n' + url) From 02e69edba7093bd910ce72a551601191e0b25501 Mon Sep 17 00:00:00 2001 From: Myned Date: Mon, 2 Oct 2017 15:20:50 -0400 Subject: [PATCH 09/29] Added check for list of permitted ids --- src/main/misc/checks.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/misc/checks.py b/src/main/misc/checks.py index 7376cc6..6876964 100644 --- a/src/main/misc/checks.py +++ b/src/main/misc/checks.py @@ -9,6 +9,7 @@ with open('config.json') as infile: config = json.load(infile) owner_id = config['owner_id'] +listed_ids = config['listed_ids'] def is_owner(): async def predicate(ctx): @@ -22,6 +23,10 @@ def is_mod(): def predicate(ctx): return ctx.message.author.guild_permissions.ban_members return commands.check(predicate) +def is_listed(): + def predicate(ctx): + return ctx.message.author.id in listed_ids + return commands.check(predicate) def owner(ctx): return ctx.message.author.id == owner_id From c45663930e3ddc346f70ee962e72278388c064ca Mon Sep 17 00:00:00 2001 From: Myned Date: Mon, 2 Oct 2017 15:22:12 -0400 Subject: [PATCH 10/29] Added listed ids dict --- src/main/run.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/run.py b/src/main/run.py index cd65615..6fb9ffb 100644 --- a/src/main/run.py +++ b/src/main/run.py @@ -6,7 +6,7 @@ try: print('\"config.json\" loaded.') except FileNotFoundError: with open('config.json', 'w') as outfile: - json.dump({'client_id': 0, 'owner_id': 0, 'permissions': 388160, 'prefix': ',', 'shutdown_channel': 0, 'startup_channel': 0, 'token': 'str'}, outfile, indent=4, sort_keys=True) + json.dump({'client_id': 0, 'listed_ids': [0], 'owner_id': 0, 'permissions': 388160, 'prefix': ',', 'shutdown_channel': 0, 'startup_channel': 0, 'token': 'str'}, outfile, indent=4, sort_keys=True) raise FileNotFoundError('Config file not found: \"config.json\" created with abstract values. Restart \"run.py\" with correct values.') import asyncio From 8d11679435787557e226be20a3c86c28ee7582b2 Mon Sep 17 00:00:00 2001 From: Myned Date: Mon, 2 Oct 2017 15:22:52 -0400 Subject: [PATCH 11/29] Moved around cog adding to better align file creation --- src/main/run.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/run.py b/src/main/run.py index 6fb9ffb..e7eb4c2 100644 --- a/src/main/run.py +++ b/src/main/run.py @@ -81,8 +81,8 @@ async def test(ctx): embed = embed.set_thumbnail('https://cdn.discordapp.com/attachments/353251794161500163/357707620561453077/9d803ea3-b7fa-401f-89cf-f32cf21fe772.png') ctx.send('Embed test', embed=embed) -bot.add_cog(info.Info(bot)) bot.add_cog(tools.Utils(bot)) +bot.add_cog(info.Info(bot)) bot.add_cog(booru.MsG(bot)) bot.run(config['token']) From 832acbba8154a3482e5f68ad8d4da64566f08eb3 Mon Sep 17 00:00:00 2001 From: Myned Date: Mon, 2 Oct 2017 15:24:34 -0400 Subject: [PATCH 12/29] Added complete exception message from cogs/booru also messing with youtube auths and uploading --- src/main/cogs/tools.py | 53 +++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/src/main/cogs/tools.py b/src/main/cogs/tools.py index 146eef7..1bd67c3 100644 --- a/src/main/cogs/tools.py +++ b/src/main/cogs/tools.py @@ -1,38 +1,60 @@ import asyncio import discord +import httplib2 +import mimetypes +import os +import requests_oauthlib as ro +import tempfile import traceback +import webbrowser from discord.ext import commands from cogs import booru from misc import checks from misc import exceptions as exc from utils import formatter +from apiclient.discovery import build +from apiclient.http import MediaFileUpload +from oauth2client.client import flow_from_clientsecrets + +# flow = flow_from_clientsecrets('../client_secrets.json', scope='https://www.googleapis.com/auth/youtube.upload', login_hint='botmyned@gmail.com', redirect_uri='urn:ietf:wg:oauth:2.0:oob') +# flow.params['access_type'] = 'offline' +# webbrowser.open_new(flow.step1_get_authorize_url()) +# credentials = flow.step2_exchange(input('Authorization code: ')) +# youtube = build('youtube', 'v3', http=credentials.authorize(httplib2.Http())) + +tempfile.tempdir = '../temp' + +command_dict = {} + class Utils: def __init__(self, bot): self.bot = bot - def last(): - pass - @commands.command(name='last', aliases=['l', ','], brief='Reinvokes last command', description='Reinvokes previous command executed', hidden=True) async def last_command(self, ctx): + global command_dict try: - # await ctx.invoke(command, args) - await ctx.send('`' + booru.last_command[ctx.message.author.id] + '`') + if command_dict.get(str(ctx.message.author.id), {}).get('args', None) is not None: + args = command_dict.get(str(ctx.message.author.id), {})['args'] + print(command_dict) + await ctx.invoke(command_dict.get(str(ctx.message.author.id), {}).get('command', None), args) except Exception: - await ctx.send(exceptions.base) + await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') traceback.print_exc(limit=1) # [prefix]ping -> Pong! @commands.command(aliases=['p'], brief='Pong!', description='Returns latency from bot to Discord servers, not to user') @checks.del_ctx() async def ping(self, ctx): + global command_dict try: await ctx.send(ctx.message.author.mention + ' 🏓 `' + str(int(self.bot.latency * 1000)) + 'ms`', delete_after=5) except Exception: - await ctx.send(exceptions.base) + await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') traceback.print_exc(limit=1) + command_dict.setdefault(str(ctx.message.author.id), {}).update({'command': ctx.command}) @commands.command(aliases=['pre'], brief='List bot prefixes', description='Shows all used prefixes') @checks.del_ctx() @@ -40,7 +62,7 @@ class Utils: try: await ctx.send('**Prefix:** `,` or ' + ctx.me.mention) except Exception: - await ctx.send(exceptions.base) + await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') traceback.print_exc(limit=1) @commands.group(name=',send', aliases=[',s'], hidden=True) @@ -55,3 +77,18 @@ class Utils: @send.command(name='user', aliases=['u', 'member', 'm']) async def send_user(self, ctx, user, *message): await discord.utils.get(self.bot.get_all_members(), id=int(user)).send(formatter.tostring(message)) + + @commands.command(aliases=['up', 'u', 'vid', 'v']) + @checks.is_listed() + async def upload(self, ctx): + global youtube + try: + print(mimetypes.guess_type(ctx.message.attachments[0].filename)) + with tempfile.TemporaryFile() as temp: + await ctx.message.attachments[0].save(temp) + print(os.path.basename('../temp/*')) + print(mimetypes.guess_type(os.path.basename('../temp/*'))) + # print('https://www.youtube.com/watch?v=' + youtube.videos().insert(part='snippet', body={'categoryId': '24', 'title': 'Test'}, media_body=MediaFileUpload('../temp/*', chunksize=-1)) + except Exception: + await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') + traceback.print_exc(limit=1) From 86311aba96dd23ab0787f501e8d21bb42ea37fc7 Mon Sep 17 00:00:00 2001 From: Myned Date: Mon, 2 Oct 2017 15:24:57 -0400 Subject: [PATCH 13/29] Placeholder for restart bot command --- src/main/restart.py | 0 src/main/run.py | 5 +++++ 2 files changed, 5 insertions(+) create mode 100644 src/main/restart.py diff --git a/src/main/restart.py b/src/main/restart.py new file mode 100644 index 0000000..e69de29 diff --git a/src/main/run.py b/src/main/run.py index e7eb4c2..8999a08 100644 --- a/src/main/run.py +++ b/src/main/run.py @@ -44,6 +44,11 @@ async def die(ctx): await ctx.send(exc.base) traceback.print_exc(limit=1) +@bot.command(aliases=['res', 'r']) +@commands.is_owner() +async def restart(self, ctx): + pass + # Invite bot to bot owner's server @bot.command(name=',invite', aliases=[',inv', ',link'], brief='Invite the bot', description='BOT OWNER ONLY\nInvite the bot to a server (Requires admin)', hidden=True) @commands.is_owner() From ac17187b1aa6370ca7729cd0c1226ed37a744694 Mon Sep 17 00:00:00 2001 From: Myned Date: Mon, 2 Oct 2017 17:29:39 -0400 Subject: [PATCH 14/29] Finished restart command --- src/main/restart.py | 3 +++ src/main/run.py | 28 ++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/main/restart.py b/src/main/restart.py index e69de29..a6c1753 100644 --- a/src/main/restart.py +++ b/src/main/restart.py @@ -0,0 +1,3 @@ +import os, sys + +os.execl(sys.executable, 'python3', 'run.py') diff --git a/src/main/run.py b/src/main/run.py index 8999a08..2dd1f89 100644 --- a/src/main/run.py +++ b/src/main/run.py @@ -11,6 +11,8 @@ except FileNotFoundError: import asyncio import discord +import os +import subprocess import traceback from discord import utils from discord.ext import commands @@ -24,7 +26,7 @@ bot = commands.Bot(command_prefix=commands.when_mentioned_or(config['prefix']), @bot.event async def on_ready(): if isinstance(bot.get_channel(config['startup_channel']), discord.TextChannel): - await bot.get_channel(config['startup_channel']).send('H3l1(0) hOw aR3? **H4vE dAy.** 🌈') + await bot.get_channel(config['startup_channel']).send('Hello how are? **Have day.** 🌈') print('Connected.') print('Username: ' + bot.user.name) print('-------') @@ -36,18 +38,28 @@ async def on_ready(): async def die(ctx): try: if isinstance(bot.get_channel(config['startup_channel']), discord.TextChannel): - await bot.get_channel(config['shutdown_channel']).send('Am g0 by3e333333eee. **H4v3 n1GhT.** 💤') + await bot.get_channel(config['shutdown_channel']).send('Am go bye. **Have night.** 💤') await bot.close() print('-------') print('Closed.') except Exception: - await ctx.send(exc.base) + await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') traceback.print_exc(limit=1) -@bot.command(aliases=['res', 'r']) +@bot.command(name=',restart', aliases=[',res', ',r'], hidden=True) @commands.is_owner() -async def restart(self, ctx): - pass +async def restart(ctx): + try: + if isinstance(bot.get_channel(config['startup_channel']), discord.TextChannel): + await bot.get_channel(config['shutdown_channel']).send('Am go :b: rite becc. **Have noon.** 💤') + process = subprocess.run(['python3', 'restart.py']) + print(process.returncode) + await bot.close() + print('-------') + print('Closed.') + except Exception: + await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') + traceback.print_exc(limit=1) # Invite bot to bot owner's server @bot.command(name=',invite', aliases=[',inv', ',link'], brief='Invite the bot', description='BOT OWNER ONLY\nInvite the bot to a server (Requires admin)', hidden=True) @@ -57,7 +69,7 @@ async def invite(ctx): try: await ctx.send('🔗 https://discordapp.com/oauth2/authorize?&client_id=' + str(config['client_id']) + '&scope=bot&permissions=' + str(config['permissions'])) except Exception: - await ctx.send(exc.base) + await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') traceback.print_exc(limit=1) @bot.command(brief='[IN TESTING]', description='[IN TESTING]', hidden=True) @@ -72,7 +84,7 @@ async def hi(ctx): hello = hello[:7] + '**Mod** ' + hello[7:] await ctx.send(hello) except Exception: - await ctx.send(exc.base) + await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') traceback.print_exc(limit=1) @bot.command(hidden=True) From fd464d948fd93f7cbd9c51a9ca59b94ed60b4e02 Mon Sep 17 00:00:00 2001 From: Myned Date: Mon, 2 Oct 2017 17:32:53 -0400 Subject: [PATCH 15/29] Added ctx.message deletion, small str change --- src/main/run.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/run.py b/src/main/run.py index 2dd1f89..1b3f672 100644 --- a/src/main/run.py +++ b/src/main/run.py @@ -48,10 +48,11 @@ async def die(ctx): @bot.command(name=',restart', aliases=[',res', ',r'], hidden=True) @commands.is_owner() +@checks.del_ctx() async def restart(ctx): try: if isinstance(bot.get_channel(config['startup_channel']), discord.TextChannel): - await bot.get_channel(config['shutdown_channel']).send('Am go :b: rite becc. **Have noon.** 💤') + await bot.get_channel(config['shutdown_channel']).send('Am :b: rite becc. **Have noon.** 💤') process = subprocess.run(['python3', 'restart.py']) print(process.returncode) await bot.close() From c8571d1c7adc3706a8fa026e16a3f80451cb36ad Mon Sep 17 00:00:00 2001 From: Myned Date: Mon, 2 Oct 2017 19:37:58 -0400 Subject: [PATCH 16/29] Added try/except blocks to subcommands --- src/main/cogs/booru.py | 173 ++++++++++++++++++++++------------------- 1 file changed, 91 insertions(+), 82 deletions(-) diff --git a/src/main/cogs/booru.py b/src/main/cogs/booru.py index 771a654..109790d 100644 --- a/src/main/cogs/booru.py +++ b/src/main/cogs/booru.py @@ -27,6 +27,7 @@ import requests import traceback from discord import reaction from discord.ext import commands +from discord.ext.commands import errors as errext from discord import errors as err from cogs import tools from misc import checks @@ -212,15 +213,10 @@ class MsG: async def blacklist(self, ctx): if ctx.invoked_subcommand is None: await ctx.send('❌ **Use a flag to manage blacklists.**\n*Type* `' + ctx.prefix + 'help bl` *for more info.*', delete_after=10) - @blacklist.error async def blacklist_error(self, ctx, error): if isinstance(error, commands.CheckFailure): return await ctx.send('❌ **Insufficient permissions.**') - if isinstance(error, exc.TagExists): - return await ctx.send('❌ `' + str(exc.TagExists) + '` **already in blacklist.**', delete_after=10) - if isinstance(error, exc.TagError): - return await ctx.send('❌ `' + str(exc.TagError) + '` **not in blacklist.**', delete_after=10) if isinstance(error, KeyError): return await ctx.send('❌ **Blacklist does not exist.**', delete_after=10) @@ -228,7 +224,6 @@ class MsG: async def _get_blacklist(self, ctx): if ctx.invoked_subcommand is None: await ctx.send('❌ **Invalid blacklist.**') - @_get_blacklist.command(name='global', aliases=['gl', 'g']) async def __get_global_blacklist(self, ctx): global blacklists @@ -260,7 +255,6 @@ class MsG: async def __get_all_blacklists(self, ctx): if ctx.invoked_subcommand is None: await ctx.send('❌ **Invalid blacklist.**') - @__get_all_blacklists.command(name='guild', aliases=['g']) @commands.has_permissions(manage_channels=True) async def ___get_all_guild_blacklists(self, ctx): @@ -280,24 +274,26 @@ class MsG: async def _add_tags(self, ctx): if ctx.invoked_subcommand is None: await ctx.send('❌ **Invalid blacklist.**') - @_add_tags.command(name='global', aliases=['gl', 'g']) @commands.is_owner() async def __add_global_tags(self, ctx, *tags): global blacklists, aliases, headers - for tag in tags: - if tag in blacklists['global_blacklist']: - raise exc.TagExists(tag) - blacklists['global_blacklist'].extend(tags) - for tag in tags: - alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() - for dic in alias_request: - aliases['global_blacklist'].setdefault(tag, []).append(dic['name']) - with open('blacklists.json', 'w') as outfile: - json.dump(blacklists, outfile, indent=4, sort_keys=True) - with open('aliases.json', 'w') as outfile: - json.dump(aliases, outfile, indent=4, sort_keys=True) - await ctx.send('✅ **Added to global blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) + try: + for tag in tags: + if tag in blacklists['global_blacklist']: + raise exc.TagExists(tag) + blacklists['global_blacklist'].extend(tags) + for tag in tags: + alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() + for dic in alias_request: + aliases['global_blacklist'].setdefault(tag, []).append(dic['name']) + with open('blacklists.json', 'w') as outfile: + json.dump(blacklists, outfile, indent=4, sort_keys=True) + with open('aliases.json', 'w') as outfile: + json.dump(aliases, outfile, indent=4, sort_keys=True) + await ctx.send('✅ **Added to global blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) + except exc.TagExists as e: + await ctx.send('❌ `' + str(e) + '` **already in blacklist.**', delete_after=10) @_add_tags.command(name='channel', aliases=['ch', 'c']) @commands.has_permissions(manage_channels=True) async def __add_channel_tags(self, ctx, *tags): @@ -307,57 +303,65 @@ class MsG: else: guild = ctx.message.channel channel = ctx.message.channel - for tag in tags: - if tag in blacklists['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), []): - raise exc.TagExists(tag) - blacklists['guild_blacklist'].setdefault(str(guild.id), {}).setdefault(str(channel.id), []).extend(tags) - for tag in tags: - alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() - for dic in alias_request: - aliases['guild_blacklist'].setdefault(str(guild.id), {}).setdefault(str(channel.id), {}).setdefault(tag, []).append(dic['name']) - with open('blacklists.json', 'w') as outfile: - json.dump(blacklists, outfile, indent=4, sort_keys=True) - with open('aliases.json', 'w') as outfile: - json.dump(aliases, outfile, indent=4, sort_keys=True) - await ctx.send('✅ **Added to** <#' + str(channel.id) + '> **blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) + try: + for tag in tags: + if tag in blacklists['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), []): + raise exc.TagExists(tag) + blacklists['guild_blacklist'].setdefault(str(guild.id), {}).setdefault(str(channel.id), []).extend(tags) + for tag in tags: + alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() + for dic in alias_request: + aliases['guild_blacklist'].setdefault(str(guild.id), {}).setdefault(str(channel.id), {}).setdefault(tag, []).append(dic['name']) + with open('blacklists.json', 'w') as outfile: + json.dump(blacklists, outfile, indent=4, sort_keys=True) + with open('aliases.json', 'w') as outfile: + json.dump(aliases, outfile, indent=4, sort_keys=True) + await ctx.send('✅ **Added to** <#' + str(channel.id) + '> **blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) + except exc.TagExists as e: + await ctx.send('❌ `' + str(e) + '` **already in blacklist.**', delete_after=10) @_add_tags.command(name='me', aliases=['m']) async def __add_user_tags(self, ctx, *tags): global blacklists, aliases, headers user = ctx.message.author - for tag in tags: - if tag in blacklists['user_blacklist'].get(str(user.id), []): - raise exc.TagExists(tag) - blacklists['user_blacklist'].setdefault(str(user.id), []).extend(tags) - for tag in tags: - alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() - for dic in alias_request: - aliases['user_blacklist'].setdefault(str(user.id), {}).setdefault(tag, []).append(dic['name']) - with open('blacklists.json', 'w') as outfile: - json.dump(blacklists, outfile, indent=4, sort_keys=True) - with open('aliases.json', 'w') as outfile: - json.dump(aliases, outfile, indent=4, sort_keys=True) - await ctx.send('✅ ' + user.mention + ' **added:**\n```' + formatter.tostring(tags) + '```', delete_after=5) + try: + for tag in tags: + if tag in blacklists['user_blacklist'].get(str(user.id), []): + raise exc.TagExists(tag) + blacklists['user_blacklist'].setdefault(str(user.id), []).extend(tags) + for tag in tags: + alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() + for dic in alias_request: + aliases['user_blacklist'].setdefault(str(user.id), {}).setdefault(tag, []).append(dic['name']) + with open('blacklists.json', 'w') as outfile: + json.dump(blacklists, outfile, indent=4, sort_keys=True) + with open('aliases.json', 'w') as outfile: + json.dump(aliases, outfile, indent=4, sort_keys=True) + await ctx.send('✅ ' + user.mention + ' **added:**\n```' + formatter.tostring(tags) + '```', delete_after=5) + except exc.TagExists as e: + await ctx.send('❌ `' + str(e) + '` **already in blacklist.**', delete_after=10) @blacklist.group(name='remove', aliases=['rm', 'r']) async def _remove_tags(self, ctx): if ctx.invoked_subcommand is None: await ctx.send('❌ **Invalid blacklist.**') - @_remove_tags.command(name='global', aliases=['gl', 'g']) @commands.is_owner() async def __remove_global_tags(self, ctx, *tags): global blacklists, aliases - for tag in tags: - if tag in blacklists['global_blacklist']: - blacklists['global_blacklist'].remove(tag) - del aliases['global_blacklist'][tag] - else: - raise exc.TagError(tag) - with open('blacklists.json', 'w') as outfile: - json.dump(blacklists, outfile, indent=4, sort_keys=True) - with open('aliases.json', 'w') as outfile: - json.dump(aliases, outfile, indent=4, sort_keys=True) - await ctx.send('✅ **Removed from global blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) + try: + for tag in tags: + if tag in blacklists['global_blacklist']: + blacklists['global_blacklist'].remove(tag) + del aliases['global_blacklist'][tag] + else: + raise exc.TagError(tag) + with open('blacklists.json', 'w') as outfile: + json.dump(blacklists, outfile, indent=4, sort_keys=True) + with open('aliases.json', 'w') as outfile: + json.dump(aliases, outfile, indent=4, sort_keys=True) + await ctx.send('✅ **Removed from global blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) + except exc.TagError as e: + await ctx.send('❌ `' + str(e) + '` **not in blacklist.**', delete_after=10) @_remove_tags.command(name='channel', aliases=['ch', 'c']) @commands.has_permissions(manage_channels=True) async def __remove_channel_tags(self, ctx, *tags): @@ -367,38 +371,43 @@ class MsG: else: guild = ctx.message.channel channel = ctx.message.channel - for tag in tags: - if tag in blacklists['guild_blacklist'][str(guild.id)][str(channel.id)]: - blacklists['guild_blacklist'][str(guild.id)][str(channel.id)].remove(tag) - del aliases['guild_blacklist'][str(guild.id)][str(channel.id)][tag] - else: - raise exc.TagError(tag) - with open('blacklists.json', 'w') as outfile: - json.dump(blacklists, outfile, indent=4, sort_keys=True) - with open('aliases.json', 'w') as outfile: - json.dump(aliases, outfile, indent=4, sort_keys=True) - await ctx.send('✅ **Removed from** <#' + str(channel.id) + '> **blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) + try: + for tag in tags: + if tag in blacklists['guild_blacklist'][str(guild.id)][str(channel.id)]: + blacklists['guild_blacklist'][str(guild.id)][str(channel.id)].remove(tag) + del aliases['guild_blacklist'][str(guild.id)][str(channel.id)][tag] + else: + raise exc.TagError(tag) + with open('blacklists.json', 'w') as outfile: + json.dump(blacklists, outfile, indent=4, sort_keys=True) + with open('aliases.json', 'w') as outfile: + json.dump(aliases, outfile, indent=4, sort_keys=True) + await ctx.send('✅ **Removed from** <#' + str(channel.id) + '> **blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) + except exc.TagError as e: + await ctx.send('❌ `' + str(e) + '` **not in blacklist.**', delete_after=10) @_remove_tags.command(name='me', aliases=['m']) async def __remove_user_tags(self, ctx, *tags): global blacklists, aliases user = ctx.message.author - for tag in tags: - if tag in blacklists['user_blacklist'][str(user.id)]: - blacklists['user_blacklist'][str(user.id)].remove(tag) - del aliases['user_blacklist'][str(user.id)][tag] - else: - raise exc.TagError(tag) - with open('blacklists.json', 'w') as outfile: - json.dump(blacklists, outfile, indent=4, sort_keys=True) - with open('aliases.json', 'w') as outfile: - json.dump(aliases, outfile, indent=4, sort_keys=True) - await ctx.send('✅ ' + user.mention + ' **removed:**\n```' + formatter.tostring(tags) + '```', delete_after=5) + try: + for tag in tags: + if tag in blacklists['user_blacklist'][str(user.id)]: + blacklists['user_blacklist'][str(user.id)].remove(tag) + del aliases['user_blacklist'][str(user.id)][tag] + else: + raise exc.TagError(tag) + with open('blacklists.json', 'w') as outfile: + json.dump(blacklists, outfile, indent=4, sort_keys=True) + with open('aliases.json', 'w') as outfile: + json.dump(aliases, outfile, indent=4, sort_keys=True) + await ctx.send('✅ ' + user.mention + ' **removed:**\n```' + formatter.tostring(tags) + '```', delete_after=5) + except exc.TagError as e: + await ctx.send('❌ `' + str(e) + '` **not in blacklist.**', delete_after=10) @blacklist.group(name='clear', aliases=['cl', 'c']) async def _clear_blacklist(self, ctx): if ctx.invoked_subcommand is None: await ctx.send('❌ **Invalid blacklist.**') - @_clear_blacklist.command(name='global', aliases=['gl', 'g']) @commands.is_owner() async def __clear_global_blacklist(self, ctx): From 08d7f160f8c62f3782c3de1d51ff6c66f4ffb77e Mon Sep 17 00:00:00 2001 From: Myned Date: Mon, 2 Oct 2017 19:38:44 -0400 Subject: [PATCH 17/29] Compacted class formatter for visibility --- src/main/misc/exceptions.py | 42 ++++++++++++++----------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/src/main/misc/exceptions.py b/src/main/misc/exceptions.py index f877ee4..1ab5988 100644 --- a/src/main/misc/exceptions.py +++ b/src/main/misc/exceptions.py @@ -1,28 +1,18 @@ base = 'â€ŧī¸ **An internal error has occurred.** Please notify my master! đŸē' -class PostError(Exception): - pass -class ImageError(Exception): - pass -class MatchError(Exception): - pass -class TagBlacklisted(Exception): - pass -class BoundsError(Exception): - pass -class TagBoundsError(Exception): - pass -class TagExists(Exception): - pass -class TagError(Exception): - pass -class FlagError(Exception): - pass -class BlacklistError(Exception): - pass -class NotFound(Exception): - pass -class Timeout(Exception): - pass -class Continue(Exception): - pass +class PostError(Exception): pass +class ImageError(Exception): pass +class MatchError(Exception): pass +class TagBlacklisted(Exception): pass +class BoundsError(Exception): pass +class TagBoundsError(Exception): pass +class TagExists(Exception): pass +class TagError(Exception): pass +class FlagError(Exception): pass +class BlacklistError(Exception): pass +class NotFound(Exception): pass +class Timeout(Exception): pass +class InvalidVideoFile(Exception): pass +class MissingAttachment(Exception): pass +class TooManyAttachments(Exception): pass +class Continue(Exception): pass From dd284701eebbcc0e4b5f4088c3ad12e339d3de39 Mon Sep 17 00:00:00 2001 From: Myned Date: Mon, 2 Oct 2017 19:40:51 -0400 Subject: [PATCH 18/29] Added base exception catching --- src/main/cogs/booru.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/main/cogs/booru.py b/src/main/cogs/booru.py index 109790d..de0c9a0 100644 --- a/src/main/cogs/booru.py +++ b/src/main/cogs/booru.py @@ -294,6 +294,9 @@ class MsG: await ctx.send('✅ **Added to global blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) except exc.TagExists as e: await ctx.send('❌ `' + str(e) + '` **already in blacklist.**', delete_after=10) + except Exception: + await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') + traceback.print_exc(limit=1) @_add_tags.command(name='channel', aliases=['ch', 'c']) @commands.has_permissions(manage_channels=True) async def __add_channel_tags(self, ctx, *tags): @@ -319,6 +322,9 @@ class MsG: await ctx.send('✅ **Added to** <#' + str(channel.id) + '> **blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) except exc.TagExists as e: await ctx.send('❌ `' + str(e) + '` **already in blacklist.**', delete_after=10) + except Exception: + await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') + traceback.print_exc(limit=1) @_add_tags.command(name='me', aliases=['m']) async def __add_user_tags(self, ctx, *tags): global blacklists, aliases, headers @@ -339,6 +345,9 @@ class MsG: await ctx.send('✅ ' + user.mention + ' **added:**\n```' + formatter.tostring(tags) + '```', delete_after=5) except exc.TagExists as e: await ctx.send('❌ `' + str(e) + '` **already in blacklist.**', delete_after=10) + except Exception: + await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') + traceback.print_exc(limit=1) @blacklist.group(name='remove', aliases=['rm', 'r']) async def _remove_tags(self, ctx): @@ -362,6 +371,9 @@ class MsG: await ctx.send('✅ **Removed from global blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) except exc.TagError as e: await ctx.send('❌ `' + str(e) + '` **not in blacklist.**', delete_after=10) + except Exception: + await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') + traceback.print_exc(limit=1) @_remove_tags.command(name='channel', aliases=['ch', 'c']) @commands.has_permissions(manage_channels=True) async def __remove_channel_tags(self, ctx, *tags): @@ -385,6 +397,9 @@ class MsG: await ctx.send('✅ **Removed from** <#' + str(channel.id) + '> **blacklist:**\n```' + formatter.tostring(tags) + '```', delete_after=5) except exc.TagError as e: await ctx.send('❌ `' + str(e) + '` **not in blacklist.**', delete_after=10) + except Exception: + await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') + traceback.print_exc(limit=1) @_remove_tags.command(name='me', aliases=['m']) async def __remove_user_tags(self, ctx, *tags): global blacklists, aliases @@ -403,6 +418,9 @@ class MsG: await ctx.send('✅ ' + user.mention + ' **removed:**\n```' + formatter.tostring(tags) + '```', delete_after=5) except exc.TagError as e: await ctx.send('❌ `' + str(e) + '` **not in blacklist.**', delete_after=10) + except Exception: + await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') + traceback.print_exc(limit=1) @blacklist.group(name='clear', aliases=['cl', 'c']) async def _clear_blacklist(self, ctx): From 0ee61c808470373beab82e6fc3a0a258d6a01e39 Mon Sep 17 00:00:00 2001 From: Myned Date: Wed, 4 Oct 2017 16:29:13 -0400 Subject: [PATCH 19/29] Added prune all user command --- src/main/cogs/management.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/main/cogs/management.py diff --git a/src/main/cogs/management.py b/src/main/cogs/management.py new file mode 100644 index 0000000..7115451 --- /dev/null +++ b/src/main/cogs/management.py @@ -0,0 +1,29 @@ +import asyncio +import discord +import traceback +import discord +from discord.ext import commands +from misc import checks +from misc import exceptions as exc + +class Administration: + + def __init__(self, bot): + self.bot = bot + + @commands.command(name='prunealluser') + @commands.is_owner() + async def prune_all_user(self, ctx, uid): + channels = ctx.message.guild.channels + member = ctx.message.guild.get_member(uid) + history = {} + c = 0 + for channel in channels: + history[channel.id] = await channel.history().flatten() + print('Added: ' + channel.id) + for channel, messages in history.items(): + for message in messages: + if message.author is member: + await message.delete() + c += 1 + await ctx.send('✅ `' + c + '` **messages deleted from the server.**') From 48e442b5a2eb59d16cbe98fb91448acf1fdd4798 Mon Sep 17 00:00:00 2001 From: Myned Date: Wed, 11 Oct 2017 02:53:40 -0400 Subject: [PATCH 20/29] Insourced restart command --- src/main/restart.py | 3 --- src/main/run.py | 12 +++++------- 2 files changed, 5 insertions(+), 10 deletions(-) delete mode 100644 src/main/restart.py diff --git a/src/main/restart.py b/src/main/restart.py deleted file mode 100644 index a6c1753..0000000 --- a/src/main/restart.py +++ /dev/null @@ -1,3 +0,0 @@ -import os, sys - -os.execl(sys.executable, 'python3', 'run.py') diff --git a/src/main/run.py b/src/main/run.py index 1b3f672..55788c7 100644 --- a/src/main/run.py +++ b/src/main/run.py @@ -51,15 +51,13 @@ async def die(ctx): @checks.del_ctx() async def restart(ctx): try: - if isinstance(bot.get_channel(config['startup_channel']), discord.TextChannel): - await bot.get_channel(config['shutdown_channel']).send('Am :b: rite becc. **Have noon.** 💤') - process = subprocess.run(['python3', 'restart.py']) - print(process.returncode) - await bot.close() + print('RESTARTING') print('-------') - print('Closed.') + if isinstance(bot.get_channel(config['startup_channel']), d.TextChannel): + await bot.get_channel(config['shutdown_channel']).send('**Restarting...** 💤') + os.execl(sys.executable, 'python3', 'run.py') except Exception: - await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') + await ctx.send('{}\n```{}```'.format(exc.base, traceback.format_exc(limit=1))) traceback.print_exc(limit=1) # Invite bot to bot owner's server From a99b9a962632397787994e2944c33b3daa9c06e9 Mon Sep 17 00:00:00 2001 From: Myned Date: Wed, 11 Oct 2017 02:54:32 -0400 Subject: [PATCH 21/29] Added dev utils to facilitate methods and file access --- src/main/utils/utils.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main/utils/utils.py diff --git a/src/main/utils/utils.py b/src/main/utils/utils.py new file mode 100644 index 0000000..eb007c8 --- /dev/null +++ b/src/main/utils/utils.py @@ -0,0 +1,36 @@ +import json + +try: + with open('background.json') as infile: + background = json.load(infile) + print('\"background.json\" loaded.') +except FileNotFoundError: + with open('background.json', 'w+') as iofile: + print('Background file not found: \"background.json\" created and loaded.') + json.dump({}, iofile, indent=4, sort_keys=True) + iofile.seek(0) + background = json.load(iofile) + +with open('config.json') as infile: + config = json.load(infile) + +def update(out, file): + with open(file, 'w') as outfile: + json.dump(out, outfile, indent=4, sort_keys=True) + +import asyncio + +async def clear(obj, interval=10*60, replace=None): + if replace is None: + if type(obj) is list: + replace = [] + elif type(obj) is dict: + replace = {} + elif type(obj) is int: + replace = 0 + elif type(obj) is str: + replace = '' + + while True: + obj = replace + asyncio.sleep(interval) From e5fa7c35a60942598b6dd1620bb9a5820dfb5ed5 Mon Sep 17 00:00:00 2001 From: Myned Date: Wed, 11 Oct 2017 02:55:49 -0400 Subject: [PATCH 22/29] Changed tostring to better format tag output --- src/main/utils/formatter.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/utils/formatter.py b/src/main/utils/formatter.py index faa36c6..f8ed334 100644 --- a/src/main/utils/formatter.py +++ b/src/main/utils/formatter.py @@ -1,9 +1,13 @@ -def tostring(i): - o = ' ' +def tostring(i, *, random=False): + o = '' if i: for v in i: o += v + ' ' - o = o[1:-1] + o = o[:-1] + elif random is True: + o += 'order:random' + else: + o = ' ' return o def tostring_commas(i): From a439d8d917bc626401a02b7d3ebc4ace7d1ad7a7 Mon Sep 17 00:00:00 2001 From: Myned Date: Wed, 11 Oct 2017 02:57:17 -0400 Subject: [PATCH 23/29] Testing, on_ready cogs, .format conversion, and misc changes --- src/main/run.py | 79 +++++++++++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 32 deletions(-) diff --git a/src/main/run.py b/src/main/run.py index 55788c7..0e68add 100644 --- a/src/main/run.py +++ b/src/main/run.py @@ -10,40 +10,58 @@ except FileNotFoundError: raise FileNotFoundError('Config file not found: \"config.json\" created with abstract values. Restart \"run.py\" with correct values.') import asyncio -import discord +import datetime as dt +import discord as d import os import subprocess +import sys import traceback from discord import utils from discord.ext import commands -from cogs import booru, info, tools +from cogs import booru, info, owner, management, tools from misc import checks from misc import exceptions as exc +from utils import utils as u -bot = commands.Bot(command_prefix=commands.when_mentioned_or(config['prefix']), description='Experimental booru bot') +import logging +logging.basicConfig(level=logging.INFO) + +print('PID {}'.format(os.getpid())) + +bot = commands.Bot(command_prefix=config['prefix'], description='Experimental booru bot') # Send and print ready message to #testing and console after logon @bot.event async def on_ready(): - if isinstance(bot.get_channel(config['startup_channel']), discord.TextChannel): - await bot.get_channel(config['startup_channel']).send('Hello how are? **Have day.** 🌈') - print('Connected.') - print('Username: ' + bot.user.name) + global bot + + bot.add_cog(tools.Utils(bot)) + bot.add_cog(owner.Tools(bot)) + bot.add_cog(management.Administration(bot)) + bot.add_cog(info.Info(bot)) + bot.add_cog(booru.MsG(bot)) + + # bot.loop.create_task(u.clear(booru.temp_urls, 30*60)) + + if isinstance(bot.get_channel(config['startup_channel']), d.TextChannel): + await bot.get_channel(config['startup_channel']).send('**Started.** ☀ī¸') + print('CONNECTED') + print(bot.user.name) print('-------') # Close connection to Discord - immediate offline -@bot.command(name=',die', aliases=[',d', ',close', ',kill'], brief='Kills the bot', description='BOT OWNER ONLY\nCloses the connection to Discord', hidden=True) +@bot.command(name=',die', aliases=[',d'], brief='Kills the bot', description='BOT OWNER ONLY\nCloses the connection to Discord', hidden=True) @commands.is_owner() @checks.del_ctx() async def die(ctx): try: - if isinstance(bot.get_channel(config['startup_channel']), discord.TextChannel): - await bot.get_channel(config['shutdown_channel']).send('Am go bye. **Have night.** 💤') + if isinstance(bot.get_channel(config['startup_channel']), d.TextChannel): + await bot.get_channel(config['shutdown_channel']).send('**Shutting down...** 🌙') await bot.close() print('-------') - print('Closed.') + print('CLOSED') except Exception: - await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') + await ctx.send(exc.base + '\n```' + traceback.format_exc(limit=1) + '```') traceback.print_exc(limit=1) @bot.command(name=',restart', aliases=[',res', ',r'], hidden=True) @@ -66,39 +84,36 @@ async def restart(ctx): @checks.del_ctx() async def invite(ctx): try: - await ctx.send('🔗 https://discordapp.com/oauth2/authorize?&client_id=' + str(config['client_id']) + '&scope=bot&permissions=' + str(config['permissions'])) + await ctx.send('🔗 https://discordapp.com/oauth2/authorize?&client_id={}&scope=bot&permissions={}'.format(config['client_id'], config['permissions']), delete_after=10) except Exception: - await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') + await ctx.send('{}\n```{}```'.format(exc.base, traceback.format_exc(limit=1))) traceback.print_exc(limit=1) @bot.command(brief='[IN TESTING]', description='[IN TESTING]', hidden=True) async def hi(ctx): + user = ctx.message.author try: - hello = 'Hewwo, ' + ctx.message.author.mention + '.' - if ctx.message.author.id == checks.owner_id: + hello = 'Hewwo, {}.'.format(user.mention) + if user.id == checks.owner_id: hello += '.. ***Master.*** uwu' - elif ctx.message.author.guild_permissions.administrator: - hello = hello[:7] + '**Admin** ' + hello[7:] - elif ctx.message.author.guild_permissions.ban_members: - hello = hello[:7] + '**Mod** ' + hello[7:] + elif user.guild_permissions.administrator: + hello = '{} **Admin** {}'.format(hello[:7], hello[7:]) + elif user.guild_permissions.ban_members: + hello = '{} **Mod** {}'.format(hello[:7], hello[7:]) await ctx.send(hello) except Exception: - await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') + await ctx.send('{}\n```{}```'.format(exc.base, traceback.format_exc(limit=1))) traceback.print_exc(limit=1) -@bot.command(hidden=True) +@bot.command(name=',test', hidden=True) @commands.is_owner() @checks.del_ctx() async def test(ctx): - embed = discord.Embed(title='Title', type='rich', description='Description.', url='https://static1.e621.net/data/4b/3e/4b3ec0c2e8580f418e4ce019dfd5ac32.png', color=discord.Color.from_rgb(255, 255, 255)) - embed = embed.set_image('https://static1.e621.net/data/27/0f/270fd28caa5e6d8bf542a76515848e02.png') - embed = embed.set_footer('Footer') - embed = embed.set_author('Author') - embed = embed.set_thumbnail('https://cdn.discordapp.com/attachments/353251794161500163/357707620561453077/9d803ea3-b7fa-401f-89cf-f32cf21fe772.png') - ctx.send('Embed test', embed=embed) - -bot.add_cog(tools.Utils(bot)) -bot.add_cog(info.Info(bot)) -bot.add_cog(booru.MsG(bot)) + embed = d.Embed(title='/post/xxxxxx', url='https://static1.e621.net/data/4b/3e/4b3ec0c2e8580f418e4ce019dfd5ac32.png', timestamp=dt.datetime.utcnow(), color=ctx.me.color) + embed.set_image(url='https://static1.e621.net/data/27/0f/270fd28caa5e6d8bf542a76515848e02.png') + embed.set_footer(text='e621', icon_url='http://ndl.mgccw.com/mu3/app/20141013/18/1413204353554/icon/icon_xl.png') + embed.set_author(name='tags', url=ctx.message.author.avatar_url, icon_url=ctx.message.author.avatar_url) + embed.add_field(name='Link', value='https://static1.e621.net/data/c2/55/c255792b5a307ee6efa51d6bb3edf878.jpg') + await ctx.send(embed=embed) bot.run(config['token']) From 4fd66713cba598e54493328df39c420fc2ed70ac Mon Sep 17 00:00:00 2001 From: Myned Date: Wed, 11 Oct 2017 02:58:17 -0400 Subject: [PATCH 24/29] Added reaction exception classes to raise in except blocks --- src/main/misc/exceptions.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/misc/exceptions.py b/src/main/misc/exceptions.py index 1ab5988..4c3e403 100644 --- a/src/main/misc/exceptions.py +++ b/src/main/misc/exceptions.py @@ -1,5 +1,8 @@ -base = 'â€ŧī¸ **An internal error has occurred.** Please notify my master! đŸē' +base = '⚠ī¸ **An internal error has occurred.** Please notify my master! đŸē' +class Left(Exception): pass +class Right(Exception): pass +class Save(Exception): pass class PostError(Exception): pass class ImageError(Exception): pass class MatchError(Exception): pass @@ -15,4 +18,6 @@ class Timeout(Exception): pass class InvalidVideoFile(Exception): pass class MissingAttachment(Exception): pass class TooManyAttachments(Exception): pass +class CheckFail(Exception): pass +class Abort(Exception): pass class Continue(Exception): pass From 89329f52c366ae87715967b1714477f231d0ba1c Mon Sep 17 00:00:00 2001 From: Myned Date: Wed, 11 Oct 2017 02:59:06 -0400 Subject: [PATCH 25/29] Console command and arbitrary code execution --- src/main/cogs/owner.py | 98 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 src/main/cogs/owner.py diff --git a/src/main/cogs/owner.py b/src/main/cogs/owner.py new file mode 100644 index 0000000..a0495a8 --- /dev/null +++ b/src/main/cogs/owner.py @@ -0,0 +1,98 @@ +import asyncio +import code +import io +import pyrasite as pyr +import re +import sys +import traceback as tb + +import discord as d +from discord.ext import commands + +from misc import checks +from misc import exceptions as exc + +nl = re.compile('\n') + +class Tools: + + def __init__(self, bot): + self.bot = bot + + def format(self, i='', o=''): + if len(o) > 1: return '>>> {}\n{}'.format(i, o) + else: return '>>> {}'.format(i) + async def generate(self, d, i='', o=''): + return await d.send('```python\n{}```'.format(self.format(i, o))) + async def refresh(self, m, i='', o=''): + global nl + output = m.content[10:-3] + if len(nl.findall(output)) <= 20: await m.edit(content='```python\n{}\n{}\n>>>```'.format(output, self.format(i, o))) + else: await m.edit(content='```python\n{}```'.format(self.format(i, o))) + + async def generate_err(self, d, o=''): + return await d.send('```\n{}```'.format(o)) + async def refresh_err(self, m, o=''): + await m.edit(content='```\n{}```'.format(o)) + + @commands.command(name=',console', aliases=[',con', ',c'], hidden=True) + @commands.is_owner() + @checks.del_ctx() + async def console(self, ctx): + def execute(msg): + if msg.content == ',exit' and msg.author is ctx.message.author: + raise exc.CheckFail + elif msg.author is ctx.message.author and msg.channel is ctx.message.channel: return True + else: return False + + try: + console = await self.generate(ctx) + exception = await self.generate_err(ctx) + while True: + exe = await self.bot.wait_for('message', check=execute) + await exe.delete() + sys.stdout = io.StringIO() + sys.stderr = io.StringIO() + try: exec(exe.content) + except Exception: tb.print_exc(limit=1) + await self.refresh(console, exe.content, sys.stdout.getvalue()) + await self.refresh_err(exception, sys.stderr.getvalue()) + await ctx.send(console.content[10:-3]) + sys.stdout = sys.__stdout__ + sys.stderr = sys.__stderr__ + except exc.CheckFail: + await ctx.send('↩ī¸ **Exited console.**') + except Exception: + await ctx.send('{}\n```{}```'.format(exc.base, tb.format_exc(limit=1))) + tb.print_exc(limit=1, file=sys.__stderr__) + finally: + sys.stdout = sys.__stdout__ + sys.stderr = sys.__stderr__ + print('Reset sys output.') + + @commands.command(name='arbitrary', aliases=[',arbit', ',ar']) + @commands.is_owner() + @checks.del_ctx() + async def arbitrary(self, ctx, *, exe): + try: + sys.stdout = io.StringIO() + exec(exe) + await self.generate(ctx, exe, sys.stdout.getvalue()) + except Exception: + await ctx.send('{}\n```{}```'.format(exc.base, tb.format_exc(limit=1))) + tb.print_exc(limit=1) + finally: + sys.stdout = sys.__stdout__ + print('Reset stdout.') + + @commands.group(aliases=[',db'], hidden=True) + @commands.is_owner() + @checks.del_ctx() + async def debug(self, ctx): + console = await self.generate(ctx) + @debug.command(name='inject', aliases=['inj']) + async def _inject(self, ctx, *, input_): + pass + @debug.command(name='inspect', aliases=['ins']) + async def _inspect(self, ctx, *, input_): + pass From 1415c92fcc06e2a637d5cdfa625c332e3e1a0f8a Mon Sep 17 00:00:00 2001 From: Myned Date: Wed, 11 Oct 2017 03:01:13 -0400 Subject: [PATCH 26/29] WIP YouTube upload command --- src/main/cogs/tools.py | 60 +++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/src/main/cogs/tools.py b/src/main/cogs/tools.py index 1bd67c3..875b88d 100644 --- a/src/main/cogs/tools.py +++ b/src/main/cogs/tools.py @@ -1,4 +1,5 @@ import asyncio +import datetime as dt import discord import httplib2 import mimetypes @@ -8,22 +9,18 @@ import tempfile import traceback import webbrowser from discord.ext import commands +#from run import config from cogs import booru from misc import checks from misc import exceptions as exc from utils import formatter from apiclient.discovery import build -from apiclient.http import MediaFileUpload +from apiclient import http from oauth2client.client import flow_from_clientsecrets +youtube = None -# flow = flow_from_clientsecrets('../client_secrets.json', scope='https://www.googleapis.com/auth/youtube.upload', login_hint='botmyned@gmail.com', redirect_uri='urn:ietf:wg:oauth:2.0:oob') -# flow.params['access_type'] = 'offline' -# webbrowser.open_new(flow.step1_get_authorize_url()) -# credentials = flow.step2_exchange(input('Authorization code: ')) -# youtube = build('youtube', 'v3', http=credentials.authorize(httplib2.Http())) - -tempfile.tempdir = '../temp' +tempfile.tempdir = os.getcwd() command_dict = {} @@ -41,7 +38,7 @@ class Utils: print(command_dict) await ctx.invoke(command_dict.get(str(ctx.message.author.id), {}).get('command', None), args) except Exception: - await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') + await ctx.send(exc.base + '\n```' + traceback.format_exc(limit=1) + '```') traceback.print_exc(limit=1) # [prefix]ping -> Pong! @@ -52,7 +49,7 @@ class Utils: try: await ctx.send(ctx.message.author.mention + ' 🏓 `' + str(int(self.bot.latency * 1000)) + 'ms`', delete_after=5) except Exception: - await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') + await ctx.send(exc.base + '\n```' + traceback.format_exc(limit=1) + '```') traceback.print_exc(limit=1) command_dict.setdefault(str(ctx.message.author.id), {}).update({'command': ctx.command}) @@ -60,9 +57,9 @@ class Utils: @checks.del_ctx() async def prefix(self, ctx): try: - await ctx.send('**Prefix:** `,` or ' + ctx.me.mention) + await ctx.send('**Prefix:** `,`') except Exception: - await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') + await ctx.send(exc.base + '\n```' + traceback.format_exc(limit=1) + '```') traceback.print_exc(limit=1) @commands.group(name=',send', aliases=[',s'], hidden=True) @@ -78,17 +75,42 @@ class Utils: async def send_user(self, ctx, user, *message): await discord.utils.get(self.bot.get_all_members(), id=int(user)).send(formatter.tostring(message)) + @commands.command(aliases=['authenticateupload', 'authupload', 'authup', 'auth']) + async def authenticate_upload(self, ctx): + global youtube + flow = flow_from_clientsecrets('client_secrets.json', scope='https://www.googleapis.com/auth/youtube.upload', login_hint='botmyned@gmail.com', redirect_uri='urn:ietf:wg:oauth:2.0:oob') + flow.params['access_type'] = 'offline' + webbrowser.open_new_tab(flow.step1_get_authorize_url()) + credentials = flow.step2_exchange(input('Authorization code: ')) + youtube = build('youtube', 'v3', http=credentials.authorize(http.build_http())) + print('Service built.') @commands.command(aliases=['up', 'u', 'vid', 'v']) @checks.is_listed() async def upload(self, ctx): global youtube + attachments = ctx.message.attachments try: - print(mimetypes.guess_type(ctx.message.attachments[0].filename)) - with tempfile.TemporaryFile() as temp: - await ctx.message.attachments[0].save(temp) - print(os.path.basename('../temp/*')) - print(mimetypes.guess_type(os.path.basename('../temp/*'))) - # print('https://www.youtube.com/watch?v=' + youtube.videos().insert(part='snippet', body={'categoryId': '24', 'title': 'Test'}, media_body=MediaFileUpload('../temp/*', chunksize=-1)) + if not attachments: + raise exc.MissingAttachment + if len(attachments) > 1: + raise exc.TooManyAttachments(len(attachments)) + mime = mimetypes.guess_type(attachments[0].filename)[0] + if 'video/' in mime: + with tempfile.NamedTemporaryFile() as temp: + await attachments[0].save(temp) + else: + raise exc.InvalidVideoFile(mime) + print('https://www.youtube.com/watch?v=' + youtube.videos().insert(part='snippet', body={'categoryId': '24', 'title': 'Test'}, media_body=http.MediaFileUpload(temp.name, chunksize=-1))) + except exc.InvalidVideoFile as e: + await ctx.send('❌ `' + str(e) + '` **not valid video type.**', delete_after=10) + except exc.TooManyAttachments as e: + await ctx.send('❌ `' + str(e) + '` **too many attachments.** Only one attachment is permitted to upload.', delete_after=10) + except exc.MissingAttachment: + await ctx.send('❌ **Missing attachment.**', delete_after=10) except Exception: - await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') + await ctx.send(exc.base + '\n```' + traceback.format_exc(limit=1) + '```') traceback.print_exc(limit=1) + @upload.error + async def upload_error(self, ctx, error): + pass +# http. From a6e2b50b1cd877a27ff550b963cdbf27f7327eab Mon Sep 17 00:00:00 2001 From: Myned Date: Wed, 11 Oct 2017 03:05:05 -0400 Subject: [PATCH 27/29] Pruning all user messages, WIP command structure, channel auto-delete --- src/main/cogs/management.py | 181 ++++++++++++++++++++++++++++++++---- 1 file changed, 164 insertions(+), 17 deletions(-) diff --git a/src/main/cogs/management.py b/src/main/cogs/management.py index 7115451..8fb4d7a 100644 --- a/src/main/cogs/management.py +++ b/src/main/cogs/management.py @@ -1,29 +1,176 @@ import asyncio -import discord +import discord as d import traceback -import discord from discord.ext import commands from misc import checks from misc import exceptions as exc +from utils import utils as u + +RATE_LIMIT = 2.1 class Administration: def __init__(self, bot): self.bot = bot + self.queue = asyncio.Queue() - @commands.command(name='prunealluser') + if u.background.get('management', {}): + if u.background['management'].get('auto_delete', {}): + for channel in u.background['management']['auto_delete']: + temp = self.bot.get_channel(channel) + self.bot.loop.create_task(self.on_message(temp)) + self.bot.loop.create_task(self.delete()) + print('Looping {}'.format(temp.id)) + + # @commands.group(aliases=['pr', 'clear', 'cl']) + # @commands.is_owner() + # @checks.del_ctx() + # async def prune(self, ctx): + # pass + # + # @prune.group(name='all', aliases=['a']) + # async def _all(self, ctx): + # pass + # @_all.group(name='user') + # async def __user(self, ctx, user: d.Member): + # channels = ctx.message.guild.text_channels + # bulk_history = {} + # bulk = {} + # history = [] + # c = 0 + # if ctx.invoked_subcommand is None: + # for channel in channels: + # bulk_history[channel] = await channel.history(limit=None, after=dt.datetime.utcnow() - dt.timedelta(days=14)).flatten() + # await ch_sent.edit(content='🗄 **Cached** `' + str(channels.index(channel) + 1) + '/' + str(len(channels)) + '` **channels.**') + # await asyncio.sleep(RATE_LIMIT) + # for channel, messages in bulk_history.items(): + # bulk[channel] = [message for message in messages if message.author.id == int(uid)] + # for channel, messages in bulk_history.items(): + # bulk[channel] = [bulk[channel][i:i+100] for i in range(0, len(bulk[channel]), 100)] + # await ctx.send('⏱ **Estimated time to delete `bulk-history`:** `' + str(int(RATE_LIMIT * sum([len(v) for v in bulk.values()]) / 60)) + ' mins ' + str(int(RATE_LIMIT * sum([len(v) for v in bulk.values()]) % 60)) + ' secs`') + # check = await ctx.send(ctx.author.mention + ' **Continue?** `Y` or `N`') + # await self.bot.wait_for('message', check=yes, timeout=60) + # del_sent = await ctx.send('🗑 **Deleting messages...**') + # for channel, messages in bulk.items(): + # for chunk in messages: + # c += len(chunk) + # await channel.delete_messages(chunk) + # await del_sent.edit(content='🗑 **Deleted** `' + str(c) + '/' + str(sum([len(v) for v in bulk.values()])) + '` **messages.**') + # await asyncio.sleep(5) + # await ctx.send('✅ `' + str(sum([len(v) for v in bulk.values()])) + '` **of** <@' + uid + '>**\'s messages deleted from** ' + ctx.message.guild.name + '**.**') + # for channel in channels: + # history.extend(await channel.history(limit=None, before=dt.datetime.utcnow() - dt.timedelta(days=14)).flatten()) + # await ch_sent.edit(content='🗄 **Cached** `' + str(channels.index(channel) + 1) + '/' + str(len(channels)) + '` **channels.**') + # await asyncio.sleep(RATE_LIMIT) + + @commands.command(name=',prunefromguild', aliases=[',pfg', ',prunefromserver', ',pfs'], brief='Prune a user\'s messages from the guild', description='about flag centers on message 50 of 101 messages\n\npfg \{user id\} [before|after|about] [\{message id\}]\n\nExample:\npfg \{user id\} before \{message id\}') @commands.is_owner() - async def prune_all_user(self, ctx, uid): - channels = ctx.message.guild.channels - member = ctx.message.guild.get_member(uid) - history = {} - c = 0 - for channel in channels: - history[channel.id] = await channel.history().flatten() - print('Added: ' + channel.id) - for channel, messages in history.items(): - for message in messages: - if message.author is member: - await message.delete() - c += 1 - await ctx.send('✅ `' + c + '` **messages deleted from the server.**') + @checks.del_ctx() + async def prune_all_user(self, ctx, uid, when=None, reference=None): + global RATE_LIMIT + + def yes(msg): + if msg.content.lower() == 'y' and msg.channel is ctx.message.channel and msg.author is ctx.message.author: + return True + elif msg.content.lower() == 'n' and msg.channel is ctx.message.channel and msg.author is ctx.message.author: + raise exc.CheckFail + else: + return False + + channels = ctx.message.guild.text_channels + if reference is not None: + for channel in channels: + try: + ref = await channel.get_message(reference) + except d.errors.NotFound: + continue + history = [] + try: + pru_sent = await ctx.send('⌛ī¸ **Pruning** <@{}>**\'s messages will take some time.**'.format(uid)) + ch_sent = await ctx.send('🗄 **Caching channels...**') + + if when is None: + for channel in channels: + history.extend(await channel.history(limit=None).flatten()) + await ch_sent.edit(content='🗄 **Cached** `{}/{}` **channels.**'.format(channels.index(channel) + 1, len(channels))) + await asyncio.sleep(RATE_LIMIT) + elif when =='before': + for channel in channels: + history.extend(await channel.history(limit=None, before=ref.created_at).flatten()) + await ch_sent.edit(content='🗄 **Cached** `{}/{}` **channels.**'.format(channels.index(channel) + 1, len(channels))) + await asyncio.sleep(RATE_LIMIT) + elif when == 'after': + for channel in channels: + history.extend(await channel.history(limit=None, after=ref.created_at).flatten()) + await ch_sent.edit(content='🗄 **Cached** `{}/{}` **channels.**'.format(channels.index(channel) + 1, len(channels))) + await asyncio.sleep(RATE_LIMIT) + elif when == 'about': + for channel in channels: + history.extend(await channel.history(limit=101, about=ref.created_at).flatten()) + await ch_sent.edit(content='🗄 **Cached** `{}/{}` **channels.**'.format(channels.index(channel) + 1, len(channels))) + await asyncio.sleep(RATE_LIMIT) + + history = [message for message in history if message.author.id == int(uid)] + est_sent = await ctx.send('⏱ **Estimated time to delete history:** `{}m {}s`'.format(int(RATE_LIMIT * len(history) / 60), int(RATE_LIMIT * len(history) % 60))) + cont_sent = await ctx.send('{} **Continue?** `Y` or `N`'.format(ctx.author.mention)) + await self.bot.wait_for('message', check=yes, timeout=60) + await cont_sent.delete() + del_sent = await ctx.send('🗑 **Deleting messages...**') + for message in history: + try: await message.delete() + except d.NotFound: pass + # print('Deleted {}/{} messages.'.format(history.index(message) + 1, len(history))) + await del_sent.edit(content='🗑 **Deleted** `{}/{}` **messages.**'.format(history.index(message) + 1, len(history))) + await asyncio.sleep(RATE_LIMIT) + await del_sent.edit(content='🗑 `{}` **of** <@{}>**\'s messages deleted from** {}**.**'.format(len(history), uid, ctx.message.guild.name)) + except exc.CheckFail: + await ctx.send('❌ **Deletion aborted.**', delete_after=10) + except TimeoutError: + await ctx.send('❌ **Deletion timed out.**', delete_after=10) + except Exception: + await ctx.send('{}\n```{}```'.format(exc.base, traceback.format_exc(limit=1))) + traceback.print_exc(limit=1) + + async def delete(self): + while True: + message = await self.queue.get() + await asyncio.sleep(RATE_LIMIT) + await message.delete() + + async def on_message(self, channel): + def check(msg): + if msg.content == 'stop' and msg.channel is channel and msg.author.guild_permissions.administrator: + raise exc.Abort + elif msg.channel is channel and not msg.pinned: + return True + else: + return False + + try: + while True: + message = await self.bot.wait_for('message', check=check) + await self.queue.put(message) + except d.errors.NotFound: + pass + except exc.Abort: + u.background['management']['auto_delete'].remove(channel.id) + u.update(u.background, 'background.json') + print('Stopped looping {}'.format(channel.id)) + await channel.send('✅ **Stopped deleting messages in** {}**.**'.format(channel.mention), delete_after=5) + except AttributeError: + pass + except Exception: + await channel.send(exc.base + '\n```' + traceback.format_exc(limit=1) + '```') + traceback.print_exc(limit=1) + + @commands.command(name='autodelete', aliases=['autodel', 'ad']) + @commands.has_permissions(administrator=True) + @checks.del_ctx() + async def auto_delete(self, ctx): + channel = ctx.message.channel + u.background.setdefault('management', {}).setdefault('auto_delete', []).append(channel.id) + u.update(u.background, 'background.json') + self.bot.loop.create_task(self.on_message(channel)) + self.bot.loop.create_task(self.delete()) + print('Looping {}'.format(channel.id)) + await ctx.send('✅ **Auto-deleting all messages in this channel.**', delete_after=5) From 79c53fd8d016d95533cbc9cf6be7b38ef76dd33d Mon Sep 17 00:00:00 2001 From: Myned Date: Wed, 11 Oct 2017 03:11:00 -0400 Subject: [PATCH 28/29] WIP temp images to clear periodically --- src/main/cogs/booru.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/cogs/booru.py b/src/main/cogs/booru.py index de0c9a0..99752e9 100644 --- a/src/main/cogs/booru.py +++ b/src/main/cogs/booru.py @@ -22,9 +22,10 @@ except FileNotFoundError: aliases = json.load(iofile) import asyncio -import discord +import discord as d import requests -import traceback +import traceback as tb +import discord as d from discord import reaction from discord.ext import commands from discord.ext.commands import errors as errext @@ -33,8 +34,11 @@ from cogs import tools from misc import checks from misc import exceptions as exc from utils import formatter, scraper +from utils import utils as u -headers = {'user-agent': 'Modumind/0.0.1 (Myned)'} +HEADERS = {'user-agent': 'Modumind/0.0.1 (Myned)'} + +# temp_urls = {} class MsG: @@ -98,6 +102,7 @@ class MsG: @checks.is_nsfw() async def e621(self, ctx, *args): global blacklists + # global temp_urls args = list(args) try: await ctx.trigger_typing() @@ -114,6 +119,8 @@ class MsG: await ctx.send('❌ **Post not found.**', delete_after=10) except exc.Timeout: await ctx.send('❌ **Request timed out.**') + posts = self.check_return_posts(ctx=ctx, booru='e621', tags=args, limit=limit)#, previous=temp_urls.get(ctx.message.author.id, [])) + # temp_urls.setdefault(ctx.message.author.id, []).extend(posts.values()) except Exception: await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') traceback.print_exc() @@ -129,6 +136,7 @@ class MsG: @checks.del_ctx() async def e926(self, ctx, *args): global blacklists + # global temp_urls args = list(args) try: await ctx.trigger_typing() @@ -145,6 +153,8 @@ class MsG: await ctx.send('❌ **Post not found.**', delete_after=10) except exc.Timeout: await ctx.send('❌ **Request timed out.**') + posts = self.check_return_posts(ctx=ctx, booru='e926', tags=args, limit=limit)#, previous=temp_urls.get(ctx.message.author.id, [])) + # temp_urls.setdefault(ctx.message.author.id, []).extend(posts.values()) except Exception: await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') traceback.print_exc(limit=1) From 25c28a509f67578c0f2a8debbcdd79c504e363b7 Mon Sep 17 00:00:00 2001 From: Myned Date: Wed, 11 Oct 2017 03:12:13 -0400 Subject: [PATCH 29/29] Added reaction paginator, WIP .format conversion, del blacklist --- src/main/cogs/booru.py | 308 +++++++++++++++++++++++++---------------- 1 file changed, 188 insertions(+), 120 deletions(-) diff --git a/src/main/cogs/booru.py b/src/main/cogs/booru.py index 99752e9..821eda2 100644 --- a/src/main/cogs/booru.py +++ b/src/main/cogs/booru.py @@ -65,23 +65,23 @@ class MsG: except exc.ImageError: await ctx.send('❌ ' + ctx.message.author.mention + ' **No image found.**') except Exception: - await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') - traceback.print_exc(limit=1) + await ctx.send('{}\n```{}```'.format(exc.base, tb.format_exc(limit=1))) + tb.print_exc() # Tag aliases - @commands.command(name='aliases', aliases=['a'], brief='e621 Tag aliases', description='e621 | NSFW\nSearch aliases for given tag') + @commands.command(aliases=['alias', 'a'], brief='e621 Tag aliases', description='e621 | NSFW\nSearch aliases for given tag') @checks.del_ctx() - async def alias(self, ctx, tag): - global headers + async def aliases(self, ctx, tag): + global HEADERS aliases = [] try: - alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() + alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=HEADERS).json() for dic in alias_request: aliases.append(dic['name']) await ctx.send('✅ `' + tag + '` **aliases:**\n```' + formatter.tostring(aliases) + '```') except Exception: - await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') - traceback.print_exc(limit=1) + await ctx.send('{}\n```{}```'.format(exc.base, tb.format_exc(limit=1))) + tb.print_exc() # Reverse image searches a linked image using the public iqdb @commands.command(name='reverse', aliases=['rev', 'ris'], brief='e621 Reverse image search', description='e621 | NSFW\nReverse-search an image with given URL') @@ -89,133 +89,201 @@ class MsG: async def reverse_image_search(self, ctx, url): try: await ctx.trigger_typing() - await ctx.send('✅ ' + ctx.message.author.mention + ' **Probable match:**\n' + scraper.check_match('http://iqdb.harry.lu/?url=' + url)) + await ctx.send('✅ ' + ctx.message.author.mention + ' **Probable match:**\n' + scraper.check_match('http://iqdb.harry.lu/?url={}'.format(url))) except exc.MatchError: await ctx.send('❌ ' + ctx.message.author.mention + ' **No probable match.**', delete_after=10) except Exception: - await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') - traceback.print_exc(limit=1) + await ctx.send('{}\n```{}```'.format(exc.base, tb.format_exc(limit=1))) + tb.print_exc() + + @commands.command(name='e621p', aliases=['e6p', '6p']) + @checks.del_ctx() + @checks.is_nsfw() + async def e621_paginator(self, ctx, *args): + def react(reaction, user): + if reaction.emoji == 'âŦ…' and reaction.message.content == paginator.content and user is ctx.message.author: raise exc.Left + elif reaction.emoji == 'đŸšĢ' and reaction.message.content == paginator.content and user is ctx.message.author: raise exc.Abort + elif reaction.emoji == '📁' and reaction.message.content == paginator.content and user is ctx.message.author: raise exc.Save + elif reaction.emoji == '➡' and reaction.message.content == paginator.content and user is ctx.message.author: raise exc.Right + else: return False + + user = ctx.message.author + args = list(args) + limit = 100 + + try: + await ctx.trigger_typing() + + c = 1 + posts = self.check_return_posts(ctx=ctx, booru='e621', tags=args, limit=limit) + starred = [] + + embed = d.Embed(title='/post/{}'.format(list(posts.keys())[c-1]), url='https://e621.net/post/show/{}'.format(list(posts.keys())[c-1]), color=ctx.me.color).set_image(url=list(posts.values())[c-1]) + embed.set_author(name=formatter.tostring(args, random=True), url='https://e621.net/post?tags={}'.format(','.join(args)), icon_url=user.avatar_url) + embed.set_footer(text='e621', icon_url='http://ndl.mgccw.com/mu3/app/20141013/18/1413204353554/icon/icon_xl.png') + paginator = await ctx.send(embed=embed) + + await paginator.add_reaction('âŦ…') + await paginator.add_reaction('đŸšĢ') + await paginator.add_reaction('📁') + await paginator.add_reaction('➡') + await asyncio.sleep(1) + + while True: + try: + await self.bot.wait_for('reaction_add', check=react, timeout=5*60) + except exc.Left: + if c > 1: + c -= 1 + embed.title = '/post/{}'.format(list(posts.keys())[c-1]) + embed.url = 'https://e621.net/post/show/{}'.format(list(posts.keys())[c-1]) + embed.set_image(url=list(posts.values())[c-1]) + await paginator.edit(embed=embed) + except exc.Save: + if list(posts.values())[c-1] not in starred: + starred.append(list(posts.values())[c-1]) + except exc.Right: + if c % limit == 0: + await ctx.trigger_typing() + try: posts.update(self.check_return_posts(ctx=ctx, booru='e621', tags=args, limit=limit, previous=posts)) + except exc.NotFound: + await paginator.edit(content='❌ **No more images found.**') + + c += 1 + embed.title = '/post/{}'.format(list(posts.keys())[c-1]) + embed.url = 'https://e621.net/post/show/{}'.format(list(posts.keys())[c-1]) + embed.set_image(url=list(posts.values())[c-1]) + await paginator.edit(embed=embed) + except exc.Abort: await paginator.edit(content='đŸšĢ **Exited paginator.**') + except exc.TagBlacklisted as e: await ctx.send('❌ `{}` **blacklisted.**'.format(e), delete_after=10) + except exc.TagBoundsError as e: await ctx.send('❌ `{}` **out of bounds.** Tags limited to 5, currently.'.format(e), delete_after=10) + except exc.Timeout: await ctx.send('❌ **Request timed out.**') + except asyncio.TimeoutError: await paginator.edit(content='❌ **Paginator timed out.**') + except Exception: + await ctx.send('{}\n```{}```'.format(exc.base, tb.format_exc(limit=1))) + tb.print_exc() + finally: + if starred: + for url in starred: await user.send(url) + + @e621_paginator.error + async def e621_paginator_error(self, ctx, error): + if isinstance(error, errext.CheckFailure): + return await ctx.send('❌ <#' + str(ctx.message.channel.id) + '> **is not an NSFW channel.**', delete_after=10) # Searches for and returns images from e621.net given tags when not blacklisted @commands.command(aliases=['e6', '6'], brief='e621 | NSFW', description='e621 | NSFW\nTag-based search for e621.net\n\nYou can only search 5 tags and 6 images at once for now.\ne6 [tags...] ([# of images])') @checks.del_ctx() @checks.is_nsfw() async def e621(self, ctx, *args): - global blacklists # global temp_urls + args = list(args) + limit = 1 + try: await ctx.trigger_typing() - await self.check_send_urls(ctx, 'e621', args) - except exc.TagBlacklisted as e: - await ctx.send('❌ `' + str(e) + '` **blacklisted.**', delete_after=10) - except exc.BoundsError as e: - await ctx.send('❌ `' + str(e) + '` **out of bounds.**', delete_after=10) - except exc.TagBoundsError as e: - await ctx.send('❌ `' + str(e) + '` **out of bounds.** Tags limited to 5, currently.', delete_after=10) - except ValueError: - await ctx.send('❌ `' + args[-1] + '` **not a valid limit.**', delete_after=10) - except exc.NotFound: - await ctx.send('❌ **Post not found.**', delete_after=10) - except exc.Timeout: - await ctx.send('❌ **Request timed out.**') + # Checks for, defines, and removes limit from args + for arg in args: + if len(arg) == 1: + if int(arg) <= 6 and int(arg) >= 1: + limit = int(arg) + args.remove(arg) + else: raise exc.BoundsError(arg) posts = self.check_return_posts(ctx=ctx, booru='e621', tags=args, limit=limit)#, previous=temp_urls.get(ctx.message.author.id, [])) + for ident, url in posts.items(): + embed = d.Embed(title='/post/{}'.format(ident), url='https://e621.net/post/show/{}'.format(ident), color=ctx.me.color).set_image(url=url) + embed.set_author(name=formatter.tostring(args, random=True), url='https://e621.net/post?tags={}'.format(','.join(args)), icon_url=ctx.message.author.avatar_url) + embed.set_footer(text='e621', icon_url='http://ndl.mgccw.com/mu3/app/20141013/18/1413204353554/icon/icon_xl.png') + await ctx.send(embed=embed) # temp_urls.setdefault(ctx.message.author.id, []).extend(posts.values()) + except exc.TagBlacklisted as e: await ctx.send('❌ `' + str(e) + '` **blacklisted.**', delete_after=10) + except exc.BoundsError as e: await ctx.send('❌ `' + str(e) + '` **out of bounds.**', delete_after=10) + except exc.TagBoundsError as e: await ctx.send('❌ `' + str(e) + '` **out of bounds.** Tags limited to 5, currently.', delete_after=10) + except exc.NotFound as e: await ctx.send('❌ `' + str(e) + '` **not found.**', delete_after=10) + except exc.Timeout: await ctx.send('❌ **Request timed out.**') except Exception: - await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') - traceback.print_exc() + await ctx.send('{}\n```{}```'.format(exc.base, tb.format_exc(limit=1))) + tb.print_exc() tools.command_dict.setdefault(str(ctx.message.author.id), {}).update({'command': ctx.command, 'args': ctx.args}) @e621.error async def e621_error(self, ctx, error): - if isinstance(error, errors.CheckFailure): + if isinstance(error, errext.CheckFailure): return await ctx.send('❌ <#' + str(ctx.message.channel.id) + '> **is not an NSFW channel.**', delete_after=10) # Searches for and returns images from e926.net given tags when not blacklisted @commands.command(aliases=['e9', '9'], brief='e926 | SFW', description='e926 | SFW\nTag-based search for e926.net\n\nYou can only search 5 tags and 6 images at once for now.\ne9 [tags...] ([# of images])') @checks.del_ctx() async def e926(self, ctx, *args): - global blacklists # global temp_urls + args = list(args) + limit = 1 + try: await ctx.trigger_typing() - await self.check_send_urls(ctx, 'e926', args) - except exc.TagBlacklisted as e: - await ctx.send('❌ `' + str(e) + '` **blacklisted.**', delete_after=10) - except exc.BoundsError as e: - await ctx.send('❌ `' + str(e) + '` **out of bounds.**', delete_after=10) - except exc.TagBoundsError as e: - await ctx.send('❌ `' + str(e) + '` **out of bounds.** Tags limited to 5, currently.', delete_after=10) - except ValueError: - await ctx.send('❌ `' + args[-1] + '` **not a valid limit.**', delete_after=10) - except exc.NotFound: - await ctx.send('❌ **Post not found.**', delete_after=10) - except exc.Timeout: - await ctx.send('❌ **Request timed out.**') + # Checks for, defines, and removes limit from args + for arg in args: + if len(arg) == 1: + if int(arg) <= 6 and int(arg) >= 1: + limit = int(arg) + args.remove(arg) + else: raise exc.BoundsError(arg) posts = self.check_return_posts(ctx=ctx, booru='e926', tags=args, limit=limit)#, previous=temp_urls.get(ctx.message.author.id, [])) + for ident, url in posts.items(): + embed = d.Embed(title='/post/{}'.format(ident), url='https://e926.net/post/show/{}'.format(ident), color=ctx.me.color).set_image(url=url) + embed.set_author(name=formatter.tostring(args, random=True), url='https://e621.net/post?tags={}'.format(','.join(args)), icon_url=ctx.message.author.avatar_url) + embed.set_footer(text='e926', icon_url='http://ndl.mgccw.com/mu3/app/20141013/18/1413204353554/icon/icon_xl.png') + await ctx.send(embed=embed) # temp_urls.setdefault(ctx.message.author.id, []).extend(posts.values()) + except exc.TagBlacklisted as e: await ctx.send('❌ `' + str(e) + '` **blacklisted.**', delete_after=10) + except exc.BoundsError as e: await ctx.send('❌ `' + str(e) + '` **out of bounds.**', delete_after=10) + except exc.TagBoundsError as e: await ctx.send('❌ `' + str(e) + '` **out of bounds.** Tags limited to 5, currently.', delete_after=10) + except exc.NotFound as e: await ctx.send('❌ `' + str(e) + '` **not found.**', delete_after=10) + except exc.Timeout: await ctx.send('❌ **Request timed out.**') except Exception: - await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') - traceback.print_exc(limit=1) + await ctx.send('{}\n```{}```'.format(exc.base, tb.format_exc(limit=1))) + tb.print_exc() # Messy code that checks image limit and tags in blacklists - async def check_send_urls(self, ctx, booru, args): - global blacklists, aliases, headers - if isinstance(ctx.message.guild, discord.Guild): - guild = ctx.message.guild - else: - guild = ctx.message.channel + def check_return_posts(self, *, ctx, booru='e621', tags=[], limit=1, previous=[]): + global blacklists, aliases, HEADERS + + if isinstance(ctx.message.guild, d.Guild): guild = ctx.message.guild + else: guild = ctx.message.channel channel = ctx.message.channel user = ctx.message.author blacklist = [] - urls = [] - limit = 1 - c = 0 - if len(args) > 5: - raise exc.TagBoundsError(formatter.tostring(args[5:])) - # Checks for, defines, and removes limit from end of args - if args and len(args[-1]) == 1: - if int(args[-1]) <= 5 and int(args[-1]) >= 1: - limit = int(args[-1]) - args.pop() - else: - raise exc.BoundsError(args[-1]) + # Creates temp blacklist based on context - for k, v in aliases['global_blacklist'].items(): - blacklist.extend([k] + v) - for k, v in aliases['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), {}).items(): - blacklist.extend([k] + v) - for k, v in aliases['user_blacklist'].get(str(user.id), {}).items(): - blacklist.extend([k] + v) + for k, v in aliases['global_blacklist'].items(): blacklist.extend([k] + v) + for k, v in aliases['guild_blacklist'].get(str(guild.id), {}).get(str(channel.id), {}).items(): blacklist.extend([k] + v) + for k, v in aliases['user_blacklist'].get(str(user.id), {}).items(): blacklist.extend([k] + v) # Checks if tags are in local blacklists - if args: - for tag in args: - if tag == 'swf' or tag == 'webm' or tag in blacklist: - raise exc.TagBlacklisted(tag) + if tags: + if len(tags) > 5: raise exc.TagBoundsError(formatter.tostring(tags[5:])) + for tag in tags: + if tag == 'swf' or tag == 'webm' or tag in blacklist: raise exc.TagBlacklisted(tag) + # Checks for blacklisted tags in endpoint blacklists - try/except is for continuing the parent loop - while len(urls) < limit: - request = requests.get('https://' + booru + '.net/post/index.json?limit=6&tags=order:random' + formatter.tostring_commas(args), headers=headers).json() - if not request: - raise exc.NotFound + posts = {} + c = 0 + while len(posts) < limit: + if c == 50 + limit: raise exc.Timeout + request = requests.get('https://{}.net/post/index.json?tags={}'.format(booru, ','.join(['order:random'] + tags)), headers=HEADERS).json() + if len(request) == 0: raise exc.NotFound(formatter.tostring(tags)) + if len(request) < limit: limit = len(request) for post in request: - if 'swf' in post['file_ext'] or 'webm' in post['file_ext']: - continue + if 'swf' in post['file_ext'] or 'webm' in post['file_ext']: continue try: for tag in blacklist: - if tag in post['tags']: - raise exc.Continue - except exc.Continue: - continue - if post['file_url'] not in urls: - urls.append(post['file_url']) - if len(urls) == limit: - break + if tag in post['tags']: raise exc.Continue + except exc.Continue: continue + if post['file_url'] not in posts.values() and post['file_url'] not in previous: posts[post['id']] = post['file_url'] + if len(posts) == limit: break c += 1 - if c == 50 + limit: - raise exc.Timeout - for url in urls: - await ctx.send('`' + formatter.tostring(args) + '`\n' + url) + return posts # Umbrella command structure to manage global, channel, and user blacklists @commands.group(aliases=['bl', 'b'], brief='Manage blacklists', description='Blacklist base command for managing blacklists\n\n`bl get [blacklist]` to show a blacklist\n`bl set [blacklist] [tags]` to replace a blacklist\n`bl clear [blacklist]` to clear a blacklist\n`bl add [blacklist] [tags]` to add tags to a blacklist\n`bl remove [blacklist] [tags]` to remove tags from a blacklist', usage='[flag] [blacklist] ([tags])') @@ -241,7 +309,7 @@ class MsG: @_get_blacklist.command(name='channel', aliases=['ch', 'c']) async def __get_channel_blacklist(self, ctx): global blacklists - if isinstance(ctx.message.guild, discord.Guild): + if isinstance(ctx.message.guild, d.Guild): guild = ctx.message.guild else: guild = ctx.message.channel @@ -255,7 +323,7 @@ class MsG: @_get_blacklist.command(name='here', aliases=['h']) async def __get_here_blacklists(self, ctx): global blacklists - if isinstance(ctx.message.guild, discord.Guild): + if isinstance(ctx.message.guild, d.Guild): guild = ctx.message.guild else: guild = ctx.message.channel @@ -269,7 +337,7 @@ class MsG: @commands.has_permissions(manage_channels=True) async def ___get_all_guild_blacklists(self, ctx): global blacklists - if isinstance(ctx.message.guild, discord.Guild): + if isinstance(ctx.message.guild, d.Guild): guild = ctx.message.guild else: guild = ctx.message.channel @@ -287,14 +355,14 @@ class MsG: @_add_tags.command(name='global', aliases=['gl', 'g']) @commands.is_owner() async def __add_global_tags(self, ctx, *tags): - global blacklists, aliases, headers + global blacklists, aliases, HEADERS try: for tag in tags: if tag in blacklists['global_blacklist']: raise exc.TagExists(tag) blacklists['global_blacklist'].extend(tags) for tag in tags: - alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() + alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=HEADERS).json() for dic in alias_request: aliases['global_blacklist'].setdefault(tag, []).append(dic['name']) with open('blacklists.json', 'w') as outfile: @@ -305,13 +373,13 @@ class MsG: except exc.TagExists as e: await ctx.send('❌ `' + str(e) + '` **already in blacklist.**', delete_after=10) except Exception: - await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') - traceback.print_exc(limit=1) + await ctx.send('{}\n```{}```'.format(exc.base, tb.format_exc(limit=1))) + tb.print_exc() @_add_tags.command(name='channel', aliases=['ch', 'c']) @commands.has_permissions(manage_channels=True) async def __add_channel_tags(self, ctx, *tags): - global blacklists, aliases, headers - if isinstance(ctx.message.guild, discord.Guild): + global blacklists, aliases, HEADERS + if isinstance(ctx.message.guild, d.Guild): guild = ctx.message.guild else: guild = ctx.message.channel @@ -322,7 +390,7 @@ class MsG: raise exc.TagExists(tag) blacklists['guild_blacklist'].setdefault(str(guild.id), {}).setdefault(str(channel.id), []).extend(tags) for tag in tags: - alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() + alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=HEADERS).json() for dic in alias_request: aliases['guild_blacklist'].setdefault(str(guild.id), {}).setdefault(str(channel.id), {}).setdefault(tag, []).append(dic['name']) with open('blacklists.json', 'w') as outfile: @@ -333,11 +401,11 @@ class MsG: except exc.TagExists as e: await ctx.send('❌ `' + str(e) + '` **already in blacklist.**', delete_after=10) except Exception: - await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') - traceback.print_exc(limit=1) + await ctx.send('{}\n```{}```'.format(exc.base, tb.format_exc(limit=1))) + tb.print_exc() @_add_tags.command(name='me', aliases=['m']) async def __add_user_tags(self, ctx, *tags): - global blacklists, aliases, headers + global blacklists, aliases, HEADERS user = ctx.message.author try: for tag in tags: @@ -345,7 +413,7 @@ class MsG: raise exc.TagExists(tag) blacklists['user_blacklist'].setdefault(str(user.id), []).extend(tags) for tag in tags: - alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=headers).json() + alias_request = requests.get('https://e621.net/tag_alias/index.json?aliased_to=' + tag + '&approved=true', headers=HEADERS).json() for dic in alias_request: aliases['user_blacklist'].setdefault(str(user.id), {}).setdefault(tag, []).append(dic['name']) with open('blacklists.json', 'w') as outfile: @@ -356,8 +424,8 @@ class MsG: except exc.TagExists as e: await ctx.send('❌ `' + str(e) + '` **already in blacklist.**', delete_after=10) except Exception: - await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') - traceback.print_exc(limit=1) + await ctx.send('{}\n```{}```'.format(exc.base, tb.format_exc(limit=1))) + tb.print_exc() @blacklist.group(name='remove', aliases=['rm', 'r']) async def _remove_tags(self, ctx): @@ -382,13 +450,13 @@ class MsG: except exc.TagError as e: await ctx.send('❌ `' + str(e) + '` **not in blacklist.**', delete_after=10) except Exception: - await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') - traceback.print_exc(limit=1) + await ctx.send('{}\n```{}```'.format(exc.base, tb.format_exc(limit=1))) + tb.print_exc() @_remove_tags.command(name='channel', aliases=['ch', 'c']) @commands.has_permissions(manage_channels=True) async def __remove_channel_tags(self, ctx, *tags): global blacklists, aliases - if isinstance(ctx.message.guild, discord.Guild): + if isinstance(ctx.message.guild, d.Guild): guild = ctx.message.guild else: guild = ctx.message.channel @@ -408,8 +476,8 @@ class MsG: except exc.TagError as e: await ctx.send('❌ `' + str(e) + '` **not in blacklist.**', delete_after=10) except Exception: - await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') - traceback.print_exc(limit=1) + await ctx.send('{}\n```{}```'.format(exc.base, tb.format_exc(limit=1))) + tb.print_exc() @_remove_tags.command(name='me', aliases=['m']) async def __remove_user_tags(self, ctx, *tags): global blacklists, aliases @@ -429,8 +497,8 @@ class MsG: except exc.TagError as e: await ctx.send('❌ `' + str(e) + '` **not in blacklist.**', delete_after=10) except Exception: - await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') - traceback.print_exc(limit=1) + await ctx.send('{}\n```{}```'.format(exc.base, tb.format_exc(limit=1))) + tb.print_exc() @blacklist.group(name='clear', aliases=['cl', 'c']) async def _clear_blacklist(self, ctx): @@ -440,8 +508,8 @@ class MsG: @commands.is_owner() async def __clear_global_blacklist(self, ctx): global blacklists, aliases - blacklists['global_blacklist'] = [] - aliases['global_blacklist'] = {} + del blacklists['global_blacklist'] + del aliases['global_blacklist'] with open('blacklists.json', 'w') as outfile: json.dump(blacklists, outfile, indent=4, sort_keys=True) with open('aliases.json', 'w') as outfile: @@ -451,13 +519,13 @@ class MsG: @commands.has_permissions(manage_channels=True) async def __clear_channel_blacklist(self, ctx): global blacklists, aliases - if isinstance(ctx.message.guild, discord.Guild): + if isinstance(ctx.message.guild, d.Guild): guild = ctx.message.guild else: guild = ctx.message.channel channel = ctx.message.channel - blacklists['guild_blacklist'][str(guild.id)][str(channel.id)] = [] - aliases['guild_blacklist'][str(guild.id)][str(channel.id)] = {} + del blacklists['guild_blacklist'][str(guild.id)][str(channel.id)] + del aliases['guild_blacklist'][str(guild.id)][str(channel.id)] with open('blacklists.json', 'w') as outfile: json.dump(blacklists, outfile, indent=4, sort_keys=True) with open('aliases.json', 'w') as outfile: @@ -467,8 +535,8 @@ class MsG: async def __clear_user_blacklist(self, ctx): global blacklists, aliases user = ctx.message.author - blacklists['user_blacklist'][str(user.id)] = [] - aliases['user_blacklist'][str(user.id)] = {} + del blacklists['user_blacklist'][str(user.id)] + del aliases['user_blacklist'][str(user.id)] with open('blacklists.json', 'w') as outfile: json.dump(blacklists, outfile, indent=4, sort_keys=True) with open('aliases.json', 'w') as outfile: