/plugins/customcommands.py

https://gitlab.com/Lett1/SlackDuckBot · Python · 107 lines · 83 code · 23 blank · 1 comment · 33 complexity · e05889506d2776e623234da22a8daae8 MD5 · raw file

  1. import config
  2. import util
  3. import itertools
  4. import db
  5. from plugin import Plugin
  6. from trigger_handler import TriggerHandler
  7. from sqlalchemy import Table, Column, Integer, MetaData, Text, String
  8. from sqlalchemy.sql import select
  9. import logging
  10. class CustomCommands(Plugin):
  11. """Usage: `commands`. Allows creation of simple commands, similiar to moobot on twitch.tv."""
  12. def __init__(self):
  13. super(self.__class__, self).__init__()
  14. self.own_trigger = "commands"
  15. self.trigger_handler = TriggerHandler(self.own_trigger)
  16. metadata = MetaData()
  17. self.customcommands_table = Table('custom_commands', metadata,
  18. Column('pk', Integer, primary_key=True),
  19. Column('trigger', String, unique=True),
  20. Column('message', Text)
  21. )
  22. metadata.create_all(db.get_engine())
  23. self.refresh_commands()
  24. def refresh_commands(self):
  25. with db.get_engine().begin() as conn:
  26. self.commands = conn.execute(select([
  27. self.customcommands_table.c.trigger,
  28. self.customcommands_table.c.message])).fetchall()
  29. self.trigger_handler.triggers = (self.own_trigger,) + tuple(x[0] for x in self.commands)
  30. logging.info("Command Store refreshed")
  31. def add_command(self, trigger, message):
  32. if any([x for x in self.commands if trigger in x[0]]) or trigger in list(itertools.chain.from_iterable([x.trigger_handler.triggers for x in config.PLUGINS if x.trigger_handler])):
  33. return ":warning: Trigger *%s* already exists! Command not added" % trigger
  34. else:
  35. with db.get_engine().begin() as conn:
  36. conn.execute(self.customcommands_table.insert(), trigger=trigger, message=message)
  37. self.refresh_commands()
  38. logging.debug('Added new command with message "%s" and trigger "%s"', trigger, message)
  39. return ':white_check_mark: Added new command with message "%s" and trigger "%s"' % (trigger, message)
  40. def delete_command(self, trigger):
  41. if any([x for x in self.commands if trigger in x[0]]):
  42. with db.get_engine().begin() as conn:
  43. conn.execute(self.customcommands_table.delete().where(self.customcommands_table.c.trigger == trigger))
  44. self.refresh_commands()
  45. logging.debug("Removed command %s", trigger)
  46. return ":white_check_mark: Removed command %s" % trigger
  47. else:
  48. return ":warning: Trigger *%s* does not exist!" % trigger
  49. def on_trigger(self, channel, user, message, trigger, s):
  50. if trigger == "commands": # custom command management
  51. message = util.replaceUrlTags(message)
  52. message = util.replaceIdsInMessage(message)
  53. split_message = message.split(" ")
  54. command = split_message[0]
  55. if command == "list":
  56. if self.commands:
  57. s.send_msg("Available commands:\n" +
  58. "\n".join(["*%s%s*: %s" % (config.COMMAND_LEADER, x[0], x[1]) for x in self.commands]), channel)
  59. else:
  60. s.send_msg(":disappointed: There are no custom commands defined.", channel)
  61. elif command == "add":
  62. if not user["user"]["is_admin"]:
  63. s.send_msg(":no_entry_sign: You need to be admin to do that.", channel)
  64. return
  65. if len(split_message) >= 3:
  66. result = self.add_command(split_message[1], " ".join(split_message[2:]))
  67. s.send_msg(result, channel)
  68. else:
  69. s.send_msg(":x: Missing arguments, use `%scommands add <TRIGGER> <MESSAGE>` to add a new command." % config.COMMAND_LEADER, channel)
  70. elif command == "delete":
  71. if not user["user"]["is_admin"]:
  72. s.send_msg(":no_entry_sign: You need to be admin to do that.", channel)
  73. return
  74. if len(split_message) == 2:
  75. result = self.delete_command(split_message[1])
  76. s.send_msg(result, channel)
  77. else:
  78. s.send_msg(":x: Missing arguments, use `%scommands delete <TRIGGER>` to remove a command." % config.COMMAND_LEADER, channel)
  79. else:
  80. s.send_msg(":warning: Available commands: `list, add*, delete*` (*needs admin rights).", channel)
  81. else:
  82. custom_command = [x for x in self.commands if trigger in x[0]]
  83. if custom_command:
  84. s.send_msg(custom_command[0][1], channel)