Browse Source

Adding the removal subcommand

Kirk Trombley 5 years ago
parent
commit
c07c7f81a5
1 changed files with 22 additions and 12 deletions
  1. 22 12
      src/plugins/watchlist.py

+ 22 - 12
src/plugins/watchlist.py

@@ -21,7 +21,7 @@ def view(args, data):
     items = data.get_items()
     if len(items) == 0:
         return "This chat hasn't added any items to the watch list!"
-    return "\n".join(f"{i + 1} - {x}" for i, x in enumerate(items))
+    return "\n".join(f"#{i + 1} - {x}" for i, x in enumerate(items))
 
 
 def add(args, data):
@@ -31,7 +31,7 @@ def add(args, data):
     items = data.get_items()
     new_items = items + to_add
     data.set_items(new_items)
-    item_list = "\n".join(f"{i + len(items) + 1} - {x}" for i, x in enumerate(to_add))
+    item_list = "\n".join(f"#{i + len(items) + 1} - {x}" for i, x in enumerate(to_add))
     return f"Added {len(to_add)} item(s):\n" + item_list
 
 
@@ -41,22 +41,32 @@ def pick(args, data):
         return "You have to add items before I can pick one!"
     ind = random.randint(0, len(items) - 1)
     chosen = items[ind]
-    return f"I have selected {ind + 1} - {chosen}! If you want to remove this from the list now, you can use !wlr {ind + 1}"
+    return f"I have selected #{ind + 1} - {chosen}! If you want to remove this from the list now, you can use !wlr {ind + 1}"
 
 
 def remove(args, data):
-    pass
+    ind, _ = pop_arg(args)
+    try:
+        ind = int(ind) - 1
+    except ValueError:
+        return f"Sorry, {ind} is not a valid index."
+    items = data.get_items()
+    if ind < 0 or ind >= len(items):
+        return f"Sorry, {ind + 1} is not valid for the list, which ranges from 1 to {len(items)}"
+    removed = items[ind]
+    del items[ind]
+    data.set_items(items)
+    return f"Done! I have removed {removed} from the list.\nNote this might change all the other numbers, so do a !wlv before you do more removes!"
 
 
 HELP_MSG="""
-This command lets you track a list of things to watch! Useful subcommands are:
+This command lets you track a list of things to watch
+Useful subcommands are:
     !watchlist !help - show this message
-    !watchlist !add - to add new things to the list. You can add multiple by using ;'s.
-        This is actually the default command and aliased to !wl
-    !watchlist !view - to view the current list. This is aliased to !wlv
-    !watchlist !pick - to pick something random from the list. This is aliased to !wlp
-    !watchlist !remove - to remove something from the list, given its number (which shows up on all other commands).
-        This is aliased to !wlr
+    !watchlist !add OR !wl - to add new things to the list, delimited by ;'s.
+    !watchlist !view OR !wlv - to view the current list.
+    !watchlist !pick OR !wlp - to pick something random from the list.
+    !watchlist !remove OR !wlr - to remove something from the list by number.
 """.strip()
 
 subcommands = {
@@ -70,7 +80,7 @@ subcommands = {
 
 @as_plugin
 def watchlist(msg, data: Watchlist):
-    if msg.raw_args.startswith("!"):
+    if msg.raw_args is not None and msg.raw_args.startswith("!"):
         subc, args = pop_arg(msg.raw_args)
         subc = subc[1:].lower()
         subc_fn = subcommands.get(subc, lambda *_: f"Sorry, subcommand must be one of {', '.join(subcommands)}, you used: {subc}")