From a77906c94341297c3d1b43bd6ce858d1c32a733f Mon Sep 17 00:00:00 2001 From: Volcyy Date: Sun, 15 Apr 2018 18:41:50 +0200 Subject: Allow `__delitem__`-like tag deletion. (#49) * Allow delitem-like tag deletion. * Add `tags.keys()` as an alias for `tags.get()`. * Allow `=` in `value`, simplify argument cleaning. --- bot/__init__.py | 28 +++++++++++++++++----------- bot/cogs/tags.py | 10 ++++++++++ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/bot/__init__.py b/bot/__init__.py index 40c4eefdc..c7d360408 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -201,17 +201,23 @@ def _get_word(self) -> str: key = clean_argument(self.buffer[self.index:equals_pos]) # Value: The second argument, specified after the `=` - value = ( - clean_argument( - self.buffer.split("=")[1] - ) - .replace("'", "\\'") # escape any unescaped quotes - ) - log.trace(f"Command mimicks setitem. Key: {key!r}, value: {value!r}.") - - # Use the cog's `set` command. - result = self.buffer[self.previous:self.index] + ".set" - args = f'"{key}" "{value}"' + right_hand = self.buffer.split("=", maxsplit=1)[1].strip() + + # If the value is None or '', mimick `bot.tags.delete(key)` + if right_hand in ("None", "''", '""'): + log.trace(f"Command mimicks delitem. Key: {key!r}.") + result = self.buffer[self.previous:self.index] + ".delete" + args = f'"{key}"' + + # Otherwise, assume assignment, for example `bot.tags['this'] = 'that'` + else: + # Escape any unescaped quotes + value = clean_argument(right_hand).replace("'", "\\'") + log.trace(f"Command mimicks setitem. Key: {key!r}, value: {value!r}.") + + # Use the cog's `set` command. + result = self.buffer[self.previous:self.index] + ".set" + args = f'"{key}" "{value}"' # Syntax is god knows what, pass it along # in the future, this should probably return / throw SyntaxError diff --git a/bot/cogs/tags.py b/bot/cogs/tags.py index 645835f53..b3569b782 100644 --- a/bot/cogs/tags.py +++ b/bot/cogs/tags.py @@ -349,6 +349,16 @@ class Tags: return await ctx.send(embed=embed) + @command(name="tags.keys()") + async def keys_command(self, ctx: Context): + """ + Alias for `tags.get()` with no arguments. + + :param ctx: discord message context + """ + + return await ctx.invoke(self.get_command) + def setup(bot): bot.add_cog(Tags(bot)) -- cgit v1.2.3