aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Volcyy <[email protected]>2018-04-15 18:41:50 +0200
committerGravatar Leon Sandøy <[email protected]>2018-04-15 18:41:50 +0200
commita77906c94341297c3d1b43bd6ce858d1c32a733f (patch)
tree2bc4705c6c41733794dcfdea9a3fb5e8269a1701
parentAllow `__getitem__` / `__setitem__`-like command access (#48) (diff)
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.
-rw-r--r--bot/__init__.py28
-rw-r--r--bot/cogs/tags.py10
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))