From c74e0fc94d7650af9895bb7a07606e115b636540 Mon Sep 17 00:00:00 2001 From: Dylan Dizon Date: Thu, 15 Nov 2018 18:58:21 -0500 Subject: [PATCH 1/5] Fix paginator formatting logic --- src/utils/formatter.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/utils/formatter.py b/src/utils/formatter.py index c4f8ab2..660d50d 100644 --- a/src/utils/formatter.py +++ b/src/utils/formatter.py @@ -1,3 +1,5 @@ +import copy + from discord.ext.commands import Paginator @@ -28,8 +30,8 @@ async def paginate( i, start='', prefix='', - kprefix='', - ksuffix='\n', + kprefix='`', + ksuffix='`\n', eprefix='```\n', ejoin=' ', esuffix='\n```', @@ -37,17 +39,21 @@ async def paginate( end=''): paginator = Paginator(prefix=prefix, suffix=suffix) messages = [] + i = copy.deepcopy(i) if start: paginator.add_line(start) if type(i) in (tuple, list, set): + if not i: + i = (' ') paginator.add_line(eprefix + f'{ejoin}'.join(i) + esuffix) elif type(i) is dict: + if not i: + i = {'': ' '} for k, e in sorted(i.items()): - if e and (k not in e) and (len(e) >= 1): - paginator.add_line(kprefix + k + ksuffix + eprefix + f'{ejoin}'.join(e) + esuffix) + paginator.add_line(kprefix if k else '' + k + ksuffix if k else '' + eprefix + f'{ejoin}'.join(e) + esuffix) if end: paginator.add_line(end) From a37e7ccaf7c4db93c144671105e1dfe8bc85de1d Mon Sep 17 00:00:00 2001 From: Dylan Dizon Date: Thu, 15 Nov 2018 18:58:53 -0500 Subject: [PATCH 2/5] Add response for missing command arguments for blacklists --- src/cogs/booru.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cogs/booru.py b/src/cogs/booru.py index c3c0319..c14a26a 100644 --- a/src/cogs/booru.py +++ b/src/cogs/booru.py @@ -1400,6 +1400,8 @@ class MsG: '**Use a flag to manage blacklists.**\n' f'*Type* `{ctx.prefix}help bl` *for more info.*') await ctx.message.add_reaction('\N{CROSS MARK}') + elif not ctx.args: + await ctx.send('\N{CROSS MARK} **Missing arguments**') @blacklist.group( name='get', From 1ef17cd5888254c4779620767a13dc3231a8bb3d Mon Sep 17 00:00:00 2001 From: Dylan Dizon Date: Thu, 15 Nov 2018 19:00:13 -0500 Subject: [PATCH 3/5] Fix potential reference issues --- src/cogs/booru.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/cogs/booru.py b/src/cogs/booru.py index c14a26a..fe6ea29 100644 --- a/src/cogs/booru.py +++ b/src/cogs/booru.py @@ -1422,11 +1422,12 @@ class MsG: 'In accordance with Discord\'s ToS: cub, related tags, and their aliases are blacklisted') async def get_global_blacklist(self, ctx, *args): args, lst = u.kwargs(args) + default = set() if lst == 'blacklist' else {} await formatter.paginate( ctx, - self.blacklists['global'].get(lst, set()), - start=f'\N{NO ENTRY SIGN} **Global {lst}:**\n') + self.blacklists['global'].get(lst, default), + start=f'\N{NO ENTRY SIGN} **Global {lst}:**') @get_blacklist.command( name='channel', @@ -1435,11 +1436,12 @@ class MsG: description='Get channel blacklist') async def get_channel_blacklist(self, ctx, *args): args, lst = u.kwargs(args) + default = set() if lst == 'blacklist' else {} await formatter.paginate( ctx, - self.blacklists['channel'].get(ctx.channel.id, {}).get(lst, set()), - start=f'\N{NO ENTRY SIGN} {ctx.channel.mention} **{lst}:**\n') + self.blacklists['channel'].get(ctx.channel.id, {}).get(lst, default), + start=f'\N{NO ENTRY SIGN} {ctx.channel.mention} **{lst}:**') @get_blacklist.command( name='me', @@ -1448,11 +1450,12 @@ class MsG: description='Get your personal blacklist') async def get_user_blacklist(self, ctx, *args): args, lst = u.kwargs(args) + default = set() if lst == 'blacklist' else {} await formatter.paginate( ctx, - self.blacklists['user'].get(ctx.author.id, {}).get(lst, set()), - start=f'\N{NO ENTRY SIGN} {ctx.author.mention}**\'s {lst}:**\n') + self.blacklists['user'].get(ctx.author.id, {}).get(lst, default), + start=f'\N{NO ENTRY SIGN} {ctx.author.mention}**\'s {lst}:**') @blacklist.group( name='add', @@ -1501,11 +1504,12 @@ class MsG: @cmds.is_owner() async def add_global_tags(self, ctx, *args): tags, lst = u.kwargs(args) + default = set() if lst == 'blacklist' else {} async with ctx.channel.typing(): added = await self._add( tags, - self.blacklists['global'].setdefault(lst, set() if lst == 'blacklist' else {}), + self.blacklists['global'].setdefault(lst, default), alias=True if lst == 'aliases' else False) await formatter.paginate( @@ -1522,11 +1526,12 @@ class MsG: @cmds.has_permissions(manage_channels=True) async def add_channel_tags(self, ctx, *args): tags, lst = u.kwargs(args) + default = set() if lst == 'blacklist' else {} async with ctx.channel.typing(): added = await self._add( tags, - self.blacklists['channel'].setdefault(ctx.channel.id, {}).setdefault(lst, set() if lst == 'blacklist' else {}), + self.blacklists['channel'].setdefault(ctx.channel.id, {}).setdefault(lst, default), alias=True if lst == 'aliases' else False) await formatter.paginate( @@ -1542,11 +1547,12 @@ class MsG: usage='[tags...]') async def add_user_tags(self, ctx, *args): tags, lst = u.kwargs(args) + default = set() if lst == 'blacklist' else {} async with ctx.channel.typing(): added = await self._add( tags, - self.blacklists['user'].setdefault(ctx.author.id, {}).setdefault(lst, set() if lst == 'blacklist' else {}), + self.blacklists['user'].setdefault(ctx.author.id, {}).setdefault(lst, default), alias=True if lst == 'aliases' else False) await formatter.paginate( @@ -1600,11 +1606,12 @@ class MsG: @cmds.is_owner() async def remove_global_tags(self, ctx, *args): tags, lst = u.kwargs(args) + default = set() if lst == 'blacklist' else {} async with ctx.channel.typing(): removed, skipped = self._remove( tags, - self.blacklists['global'].get(lst, set())) + self.blacklists['global'].get(lst, default)) await formatter.paginate( ctx, @@ -1625,11 +1632,12 @@ class MsG: @cmds.has_permissions(manage_channels=True) async def remove_channel_tags(self, ctx, *args): tags, lst = u.kwargs(args) + default = set() if lst == 'blacklist' else {} async with ctx.channel.typing(): removed, skipped = self._remove( tags, - self.blacklists['channel'].get(ctx.channel.id, {}).get(lst, set())) + self.blacklists['channel'].get(ctx.channel.id, {}).get(lst, default)) await formatter.paginate( ctx, @@ -1649,11 +1657,12 @@ class MsG: usage='[tags...]') async def remove_user_tags(self, ctx, *args): tags, lst = u.kwargs(args) + default = set() if lst == 'blacklist' else {} async with ctx.channel.typing(): removed, skipped = self._remove( tags, - self.blacklists['user'].get(ctx.author.id, {}).get(lst, set())) + self.blacklists['user'].get(ctx.author.id, {}).get(lst, default)) await formatter.paginate( ctx, From c0f72cd503cf8aea78cd7f87b54c726923feab5b Mon Sep 17 00:00:00 2001 From: Dylan Dizon Date: Thu, 15 Nov 2018 19:01:08 -0500 Subject: [PATCH 4/5] Remove unnecessary newlines --- src/cogs/booru.py | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/src/cogs/booru.py b/src/cogs/booru.py index fe6ea29..d9aa140 100644 --- a/src/cogs/booru.py +++ b/src/cogs/booru.py @@ -1515,7 +1515,7 @@ class MsG: await formatter.paginate( ctx, added, - start=f'\N{WHITE HEAVY CHECK MARK} **Added to global {lst}:**\n') + start=f'\N{WHITE HEAVY CHECK MARK} **Added to global {lst}:**') @add_tags.command( name='channel', @@ -1537,7 +1537,7 @@ class MsG: await formatter.paginate( ctx, added, - start=f'\N{WHITE HEAVY CHECK MARK} **Added to {ctx.channel.mention} {lst}:**\n') + start=f'\N{WHITE HEAVY CHECK MARK} **Added to {ctx.channel.mention} {lst}:**') @add_tags.command( name='me', @@ -1558,7 +1558,7 @@ class MsG: await formatter.paginate( ctx, added, - start=f'\N{WHITE HEAVY CHECK MARK} **Added to {ctx.author.mention}\'s {lst}:**\n') + start=f'\N{WHITE HEAVY CHECK MARK} **Added to {ctx.author.mention}\'s {lst}:**') @blacklist.group( name='remove', @@ -1616,12 +1616,7 @@ class MsG: await formatter.paginate( ctx, removed, - start=f'\N{WHITE HEAVY CHECK MARK} **Removed from global {lst}:**\n') - if skipped: - await formatter.paginate( - ctx, - skipped, - start=f'\N{CROSS MARK} **Not in global {lst}:**\n') + start=f'\N{WHITE HEAVY CHECK MARK} **Removed from global {lst}:**') @remove_tags.command( name='channel', @@ -1642,12 +1637,7 @@ class MsG: await formatter.paginate( ctx, removed, - start=f'\N{WHITE HEAVY CHECK MARK} **Removed from {ctx.channel.mention} {lst}:**\n') - if skipped: - await formatter.paginate( - ctx, - skipped, - start=f'\N{CROSS MARK} **Not in {ctx.channel.mention} {lst}:**\n') + start=f'\N{WHITE HEAVY CHECK MARK} **Removed from {ctx.channel.mention} {lst}:**') @remove_tags.command( name='me', @@ -1667,12 +1657,7 @@ class MsG: await formatter.paginate( ctx, removed, - start=f'\N{WHITE HEAVY CHECK MARK} **Removed from {ctx.author.mention}\'s {lst}:**\n') - if skipped: - await formatter.paginate( - ctx, - skipped, - start=f'\N{CROSS MARK} **Not in {ctx.author.mention}\'s {lst}:**\n') + start=f'\N{WHITE HEAVY CHECK MARK} **Removed from {ctx.author.mention}\'s {lst}:**') @blacklist.group( name='clear', From eab73cb62b497616ca524f4b26e0cb47311768f4 Mon Sep 17 00:00:00 2001 From: Dylan Dizon Date: Thu, 15 Nov 2018 19:01:55 -0500 Subject: [PATCH 5/5] Fix _remove logic and remove output of skipped tags --- src/cogs/booru.py | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/src/cogs/booru.py b/src/cogs/booru.py index d9aa140..1b7be22 100644 --- a/src/cogs/booru.py +++ b/src/cogs/booru.py @@ -1571,31 +1571,30 @@ class MsG: await ctx.send('**Invalid blacklist**') await ctx.message.add_reaction('\N{CROSS MARK}') - def _remove(self, tags, lst): - removed = [] - skipped = [] + def _remove(self, remove, lst): + removed = set() - if tags: + if remove: if type(lst) is set: - temp = set() - for tag in tags: - if tag not in tags: - temp.add(tag) - else: - removed.append(tag) + for tag in remove: + with suppress(KeyError): + lst.remove(tag) + removed.add(tag) else: - temp = {} - for k, v in lst.items(): - temp[k] = set() - for tag in v: - if tag not in tags: - temp[k].add(tag) - else: - removed.append(tag) - lst.update(temp) + temp = copy.deepcopy(lst) + for k in temp.keys(): + if k in remove: + with suppress(KeyError): + del lst[k] + removed.add(k) + else: + lst[k] = set([tag for tag in lst[k] if tag not in remove]) + lst = temp + removed.update([tag for k, v in lst.items() for tag in v if tag in remove]) + u.dump(self.blacklists, 'cogs/blacklists.pkl') - return removed, skipped + return removed @remove_tags.command( name='global', @@ -1609,7 +1608,7 @@ class MsG: default = set() if lst == 'blacklist' else {} async with ctx.channel.typing(): - removed, skipped = self._remove( + removed = self._remove( tags, self.blacklists['global'].get(lst, default)) @@ -1630,7 +1629,7 @@ class MsG: default = set() if lst == 'blacklist' else {} async with ctx.channel.typing(): - removed, skipped = self._remove( + removed = self._remove( tags, self.blacklists['channel'].get(ctx.channel.id, {}).get(lst, default)) @@ -1650,7 +1649,7 @@ class MsG: default = set() if lst == 'blacklist' else {} async with ctx.channel.typing(): - removed, skipped = self._remove( + removed = self._remove( tags, self.blacklists['user'].get(ctx.author.id, {}).get(lst, default))