watchlist.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import pickle
  2. import random
  3. from rollbot import as_plugin, as_group_singleton, pop_arg
  4. @as_group_singleton
  5. class Watchlist:
  6. items: "binary"
  7. def get_items(self):
  8. if self.items is None:
  9. return []
  10. return pickle.loads(self.items)
  11. def set_items(self, new_items):
  12. new_items = new_items or []
  13. self.items = pickle.dumps(new_items)
  14. def view(args, data):
  15. items = data.get_items()
  16. if len(items) == 0:
  17. return "This chat hasn't added any items to the watch list!"
  18. return "\n".join(f"#{i + 1} - {x}" for i, x in enumerate(items))
  19. def add(args, data):
  20. to_add = [x.strip() for x in args.split(";") if len(x) > 0 and not x.isspace()]
  21. if len(to_add) == 0:
  22. return "Sorry, doesn't look like you provided any valid items! You can also add multiple items using ; as a delimiter."
  23. items = data.get_items()
  24. new_items = items + to_add
  25. data.set_items(new_items)
  26. item_list = "\n".join(f"#{i + len(items) + 1} - {x}" for i, x in enumerate(to_add))
  27. return f"Added {len(to_add)} item(s):\n" + item_list
  28. def pick(args, data):
  29. items = data.get_items()
  30. if len(items) == 0:
  31. return "You have to add items before I can pick one!"
  32. ind = random.randint(0, len(items) - 1)
  33. chosen = items[ind]
  34. return f"I have selected #{ind + 1} - {chosen}! If you want to remove this from the list now, you can use !wlr {ind + 1}"
  35. def remove(args, data):
  36. ind, _ = pop_arg(args)
  37. try:
  38. ind = int(ind) - 1
  39. except ValueError:
  40. return f"Sorry, {ind} is not a valid index."
  41. items = data.get_items()
  42. if ind < 0 or ind >= len(items):
  43. return f"Sorry, {ind + 1} is not valid for the list, which ranges from 1 to {len(items)}"
  44. removed = items[ind]
  45. del items[ind]
  46. data.set_items(items)
  47. 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!"
  48. HELP_MSG="""
  49. This command lets you track a list of things to watch
  50. Useful subcommands are:
  51. !watchlist !help - show this message
  52. !watchlist !add OR !wla - to add new things to the list, delimited by ;'s.
  53. !watchlist !view OR !wlv - to view the current list.
  54. !watchlist !pick OR !wlp - to pick something random from the list.
  55. !watchlist !remove OR !wlr - to remove something from the list by number.
  56. """.strip()
  57. subcommands = {
  58. "help": lambda *_: HELP_MSG,
  59. "view": view,
  60. "add": add,
  61. "pick": pick,
  62. "remove": remove,
  63. }
  64. @as_plugin
  65. def watchlist(subc, data: Watchlist):
  66. if subc:
  67. subc_fn = subcommands.get(subc.command, None)
  68. if subc_fn:
  69. return subc_fn(subc.raw_args, data)
  70. return f"Sorry, subcommand must be one of {', '.join(subcommands)}, you used: {subc.command}"
  71. return subcommands["help"]() # default to help message
  72. # some quick aliases for convenience, !wl is aliased in the config file instead
  73. @as_plugin
  74. def wlp(msg, data: Watchlist):
  75. return pick(msg.raw_args, data)
  76. @as_plugin
  77. def wlv(msg, data: Watchlist):
  78. return view(msg.raw_args, data)
  79. @as_plugin
  80. def wlr(msg, data: Watchlist):
  81. return remove(msg.raw_args, data)
  82. @as_plugin
  83. def wla(msg, data: Watchlist):
  84. return add(msg.raw_args, data)