statistics.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. from common import log
  2. from commands.common import CommandHelpers as CH
  3. bold = "\x02"
  4. italic = "\x1D"
  5. underline = "\x1F"
  6. reverse = "\x16" # swap background and foreground colors ("reverse video")
  7. reset = "\x0F"
  8. blue = "\x0302"
  9. green = "\x0303"
  10. red = "\x0304"
  11. grey = "\x0314"
  12. def do_command(self, connection, event):
  13. cmdtype, trigger, command, replyto = CH.disect_command(self, event)
  14. if not command: # Do nothing if there is no command.
  15. return
  16. if command == "cmd" or command == "commands":
  17. if cmdtype == "help": #Display help text.
  18. if len(command.split()) is not 1:
  19. return
  20. connection.privmsg(replyto, "Displays a list of commands.")
  21. elif cmdtype == "cmd":
  22. connection.privmsg(replyto, grey + "Statistics commands: " + CH.ccc(self, "joins") + CH.ccc(self, "kicks")[:-2] + ".")
  23. elif command.split()[0] == "joins" or command.split()[0] == "kicks":
  24. if cmdtype == "help": #Display help text.
  25. if len(command.split()) is not 1:
  26. return
  27. connection.privmsg(replyto, "Display amount of " + command.split()[0] + " of user and or channel. Channel and user optional.")
  28. connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + command.split()[0] + " " + reset + italic + "channel user")
  29. elif cmdtype == "cmd":
  30. # Parse user input
  31. user = event.source.nick
  32. if not connection.get_nickname() == event.target: # Channel message.
  33. channel = event.target
  34. if len(command.split()) == 1: # Command contains only !joins.
  35. user = event.source.nick
  36. elif len(command.split()) == 2: # Command has one argument.
  37. if command.split()[1] in self.channels:
  38. channel = command.split()[1]
  39. else:
  40. user = command.split()[1]
  41. elif len(command.split()) == 3: # Command has two arguments.
  42. if not command.split()[1] in self.channels: # Bot does not inhabit requested channel.
  43. if not command.split()[2] in self.channels: # User did not revert channel and user in command syntax.
  44. connection.action(replyto, "does not inhabit " + red + command.split()[1] + reset + ".")
  45. return
  46. else: # User reverted user and channel in command syntax.
  47. user = command.split()[1]
  48. channel = command.split()[2]
  49. else: # Bot does inhabit requested channel.
  50. user = command.split()[2]
  51. channel = command.split()[1]
  52. elif len(command.split()) < 5: # To many arguments
  53. connection.privmsg(replyto, "To many arguments. For help type " + blue + self.helpchar + "joins" + reset + ".")
  54. return
  55. try:
  56. if user and channel:
  57. if command.split()[0] == "joins":
  58. userstat = str(sum(self.db.all("SELECT " + command.split()[0].lower() + " FROM " + command.split()[0].lower() + " WHERE channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'")))
  59. userchannelstat = str(self.db.one("SELECT " + command.split()[0].lower() + " FROM " + command.split()[0].lower() + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'"))
  60. channelstat = str(sum(self.db.all("SELECT " + command.split()[0].lower() + " FROM " + command.split()[0].lower() + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "' AND user_network='" + self.network + "'")))
  61. if userchannelstat == "None":
  62. userchannelstat = "0"
  63. connection.privmsg(replyto, red + user + reset + " has " + green + userstat + reset + " " + command.split()[0] + ". Of which " + green + userchannelstat + reset + " have been in " + red + channel + reset + ", that has " + green + channelstat + reset + " " + command.split()[0] + reset + " in total.")
  64. elif command.split()[0] == "kicks":
  65. try:
  66. givenkicks, receivedkicks = self.db.one("SELECT given, received FROM " + command.split()[0].lower() + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'")
  67. except:
  68. givenkicks = 0
  69. receivedkicks = 0
  70. channelkicks = self.db.all("SELECT given FROM " + command.split()[0].lower() + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "'")
  71. connection.privmsg(replyto, red + user + reset + " has kicked " + green + str(givenkicks) + reset + " and been kicked " + green + str(receivedkicks) + reset + " times in " + red + channel + reset + ", where were " + green + str(sum(channelkicks)) + reset + " kicks in total.")
  72. return
  73. except:
  74. pass
  75. try:
  76. if user:
  77. if command.split()[0] == "joins":
  78. userstat = str(sum(self.db.all("SELECT " + command.split()[0] + " FROM " + command.split()[0] + " WHERE channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'")))
  79. if userstat == "[]":
  80. userstat = "[0]"
  81. connection.privmsg(replyto, red + user + reset + " has " + green + userstat + reset + " " + command.split()[0] + " in channels I monitor.")
  82. elif command.split()[0] == "kicks":
  83. userstat = self.db.all("SELECT given, received FROM kicks WHERE channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'")
  84. kicksgiven = 0
  85. kicksreceived = 0
  86. for record in userstat:
  87. kicksgiven += record[0]
  88. kicksreceived += record[1]
  89. print(record)
  90. print(userstat)
  91. connection.privmsg(replyto, red + user + reset + " has given " + green + str(kicksgiven) + reset + " and received " + green + str(kicksreceived) + reset + " kicks")
  92. return
  93. except:
  94. pass
  95. try:
  96. if channel: # This situation should not occur anymore.
  97. channelstat = str(sum(self.db.all("SELECT " + command.split()[0] + " FROM " + command.split()[0] + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "' AND user_network='" + self.network + "'")))
  98. if command.split()[0] == "joins":
  99. connection.privmsg(replyto, red + channel + reset + " has been " + command.split()[0][:-1] + "ed " + green + channelstat + reset + " times.")
  100. elif command.split()[0] == "kicks":
  101. connection.privmsg(replyto, "There have been " + green + channelstat + reset + " kicks in " + red + command.split()[0] + reset + ".")
  102. return
  103. except:
  104. pass
  105. log.error("No situation applied on statistics.py")