common.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import random
  2. bold = "\x02"
  3. italic = "\x1D"
  4. underline = "\x1F"
  5. reverse = "\x16" # swap background and foreground colors ("reverse video")
  6. reset = "\x0F"
  7. blue = "\x0302"
  8. green = "\x0303"
  9. red = "\x0304"
  10. grey = "\x0314"
  11. class CommandHelpers():
  12. def disect_command(self, event):
  13. trigger = event.arguments[0]
  14. command = trigger[1:].lower() #Command without prefix.
  15. # Determine command type.
  16. if trigger.startswith(self.cmdchar):
  17. cmdtype = "cmd"
  18. elif trigger.startswith(self.helpchar):
  19. cmdtype = "help"
  20. else:
  21. cmdtype = False
  22. # Determine where to reply to.
  23. if event.type == "pubmsg":
  24. replyto = event.target
  25. elif event.type == "privmsg":
  26. replyto = event.source.nick
  27. else:
  28. replyto = False
  29. return(cmdtype, trigger, command, replyto)
  30. def ccc(self, command, rights=False, event=False): # Commandlist colour coding and channel rights filter.
  31. if rights:
  32. show = False
  33. if rights["homechan"] == "owner":
  34. if self.channels[self.homechannel].is_owner(event.source.nick):
  35. show = True
  36. if rights["homechan"] == "admin":
  37. if self.channels[self.homechannel].is_owner(event.source.nick) or self.channels[self.homechannel].is_admin(event.source.nick):
  38. show = True
  39. if rights["homechan"] == "oper":
  40. if self.channels[self.homechannel].is_owner(event.source.nick) or self.channels[self.homechannel].is_admin(event.source.nick) or self.channels[self.homechannel].is_oper(event.source.nick):
  41. show = True
  42. if not event.target == self.connection.get_nickname(): # Channel message.
  43. if rights["chan"] == "owner":
  44. if self.channels[event.target].is_owner(event.source.nick):
  45. show = True
  46. if rights["chan"] == "admin":
  47. if self.channels[event.target].is_owner(event.source.nick) or self.channels[event.target].is_admin(event.source.nick):
  48. show = True
  49. if rights["chan"] == "oper":
  50. if self.channels[event.target].is_owner(event.source.nick) or self.channels[event.target].is_admin(event.source.nick) or self.channels[event.target].is_oper(event.source.nick):
  51. show = True
  52. if show:
  53. return blue + self.cmdchar + command + reset + ", "
  54. else:
  55. return blue + self.cmdchar + command + reset + ", "
  56. class AdminHelpers():
  57. def get_channelfunctions(self, channel):
  58. channelfunctions = self.db.one("SELECT autojoin, aggressiveness, join_greeting, statistics_commands, games, chat FROM channels WHERE LOWER(name)=LOWER('" + channel + "') AND network='" + self.network + "'")
  59. if channelfunctions[0]:
  60. autojoin = "on"
  61. else:
  62. autojoin = "off"
  63. if channelfunctions[2]:
  64. joingreet = "on"
  65. else:
  66. joingreet = "off"
  67. if channelfunctions[3]:
  68. statscmds = "on"
  69. else:
  70. statscmds = "off"
  71. if channelfunctions[4]:
  72. games = "on"
  73. else:
  74. games = "off"""
  75. if channelfunctions[5]:
  76. chat = "on"
  77. else:
  78. chat = "off"""
  79. return ("autojoin " + green + autojoin + reset + ", aggressiveness " + green + channelfunctions[1] + reset + ", join_greeting " + green + joingreet + reset + ", statistics_commands " + green + statscmds + reset + ", games " + green + games + reset + ", chat " + green + chat + reset + ".")
  80. def is_channelfunction(value):
  81. if value.lower() in ["autojoin", "aggressiveness", "join_greeting", "statistics_commands", "games", "chat"]:
  82. return True
  83. else:
  84. return False
  85. def describe_channelfunction(function):
  86. if function == "autojoin":
  87. message = "Join the channel automaticly after connecting."
  88. elif function == "aggressiveness":
  89. message = "How to respond to kick, ban and mode events. Options: " + blue + "passive" + reset + ", " + blue + "defense_only" + reset + ", " + blue + "equal_retalliation" + reset + ", " + blue + "battlebot" + reset + "."
  90. elif function == "join_greeting":
  91. message = "Greet users joining the channel on the first few joins, and later on large amounts."
  92. elif function == "statistics_commands":
  93. message = "Enable use of statistics commands."
  94. elif function == "games":
  95. message = "Enable use of game commands."
  96. elif function == "chat":
  97. message = "Respond to and initiate chat."
  98. else:
  99. message = "Sorry, this function has no description yet."
  100. return message
  101. def is_aggressiveness(value):
  102. if value.lower() in ["passive", "defense_only", "equal_retalliation", "battlebot"]:
  103. return True
  104. else:
  105. return False
  106. class GameHelpers():
  107. def roll_dice(amount, type):
  108. rolls = []
  109. for iterations in range(amount):
  110. rolls.append(random.randint(1, type))
  111. return rolls
  112. def get_info(self, user):
  113. user = user.lower()
  114. all_joins = self.db.all("SELECT joins FROM joins WHERE LOWER(\"user\")=%s AND user_network='" + self.network + "'", (user, ))
  115. all_kicks = self.db.all("SELECT given, received FROM kicks WHERE LOWER(\"user\")=%s AND user_network='" + self.network + "'", (user, ))
  116. all_messages = self.db.all("SELECT messages, messages_words, messages_characters, actions, actions_words, actions_characters, notices, notices_words, notices_characters FROM messages WHERE LOWER(\"user\")=%s AND user_network='" + self.network + "'", (user, ))
  117. joins =0
  118. for record in all_joins:
  119. joins += record
  120. given = 0
  121. received = 0
  122. for record in all_kicks:
  123. given += int(record[0])
  124. received += int(record[1])
  125. messages = 0
  126. messages_words = 0
  127. messages_characters = 0
  128. actions = 0
  129. actions_words = 0
  130. actions_characters = 0
  131. notices = 0
  132. notices_words = 0
  133. notices_characters = 0
  134. for record in all_messages:
  135. messages += int(record[0])
  136. messages_words += int(record[1])
  137. messages_characters += int(record[2])
  138. actions += int(record[3])
  139. actions_words += int(record[4])
  140. actions_characters += int(record[5])
  141. notices += int(record[6])
  142. notices_words += int(record[7])
  143. notices_characters += int(record[8])
  144. userrecord = self.db.one("SELECT xp_spent, level FROM users WHERE LOWER(name)=%s AND network='" + self.network + "'", (user, ))
  145. total_xp = (joins + (given * received) + messages + (messages_words / 4) + (messages_characters / 10) + ((actions + (actions_words / 4) + (actions_characters / 10)) * 2) + ((notices + (notices_words / 4) + (notices_characters / 10)) / 2)) / 20
  146. xp = total_xp - userrecord[0]
  147. total_messages = messages + actions + notices
  148. total_words = messages_words + actions_words + notices_words
  149. if joins < 1:
  150. joins = 1
  151. if total_messages < 1:
  152. total_messages = 1
  153. karma = ((((messages / 30) - joins) / 5) + ((total_words / 10) - total_messages) / 40) - (given * received)
  154. return userrecord[1], xp, userrecord[0], karma
  155. class StatisticsHelpers():
  156. def add_message_stats(stats):
  157. messages = 0
  158. words = 0
  159. characters = 0
  160. for record in stats:
  161. messages += record[0]
  162. words += record[1]
  163. characters += record[2]
  164. return messages, words, characters