Advertisement
Guest User

Untitled

a guest
May 27th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import logging
  3. import random
  4. import markovify
  5. import time as t
  6.  
  7. import plugin
  8.  
  9. logger = logging.getLogger(__name__)
  10.  
  11.  
  12. class humanizm(plugin.Plugin):
  13. """Plugin that works similarly to the joke plugin, but it handles humanizm
  14. instead. Theoretically this could be abstracted and combined with jokes,
  15. but in the future we will probably want it to be handled differently, so it
  16. should stay as a separate module.
  17. """
  18. def __init__(self, bot):
  19. super(humanizm, self).__init__(bot)
  20. self.use_command = "!humanizm"
  21. self.add_command = "!addhumanizm"
  22.  
  23. self.init_db()
  24.  
  25.  
  26. @property
  27. def description(self):
  28. return "Shows humanizm added by users."
  29.  
  30.  
  31. @property
  32. def long_desc(self):
  33. return ""
  34.  
  35.  
  36. @property
  37. def commands(self):
  38. return {
  39. "!humanizm": "shows random humanizm",
  40. "!addhumanizm": "adds a new piece of humanizm to the database"
  41. }
  42.  
  43.  
  44. def private_chat_hook(self, steamid, message):
  45. if message.startswith(self.use_command):
  46. self.bot.user.send_msg(steamid, self.get_humanizm())
  47. elif message.startswith(self.add_command):
  48. self.bot.user.send_msg(steamid, self.add_humanizm(steamid, message))
  49.  
  50.  
  51. def group_chat_hook(self, groupid, userid, message):
  52. if message.startswith(self.use_command):
  53. self.bot.user.send_group_msg(groupid, self.get_humanizm())
  54. elif message.startswith(self.add_command):
  55. self.bot.user.send_group_msg(groupid, self.add_humanizm(userid, message))
  56.  
  57.  
  58. def get_humanizm(self):
  59. rows = self.bot.database.select("humanizm", "*")
  60. if len(rows)<1:
  61. return "No humanizm in the database."
  62. else:
  63. col = rows[:,1]
  64. row = col.reshape(1, -1)
  65.  
  66. return ("{}".format(row.encode('utf-8')
  67. ))
  68.  
  69.  
  70. def add_humanizm(self, steamid, message):
  71. tokens = message.split()
  72. if len(tokens)<2:
  73. return "No content. humanizm not added."
  74. else:
  75. self.bot.database.insert("humanizm", "content, author", "?, ?",
  76. (
  77. ' '.join(tokens[1:]),
  78. self.bot.user.get_name_from_steamid(steamid)
  79. )
  80. )
  81. return "humanizm added."
  82.  
  83.  
  84. def init_db(self):
  85. tables = self.bot.database.select("sqlite_master", "name",
  86. "type='table'")
  87. for table in tables:
  88. if table[0] == "humanizm":
  89. return
  90.  
  91. logger.info("Creating humanizm table in the database")
  92. self.bot.database.create_table("humanizm",
  93. "id INTEGER PRIMARY KEY AUTOINCREMENT,"
  94. "content TEXT NOT NULL,"
  95. "author TEXT NOT NULL"
  96. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement