statistics.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 not self.db.one("SELECT games FROM channels WHERE name='" + event.target + "' AND network='" + self.network + "'"):
  17. return # Do noting if the games channel function is off.
  18. if command == "cmd" or command == "commands":
  19. if cmdtype == "help": #Display help text.
  20. if len(command.split()) is not 1:
  21. return
  22. connection.privmsg(replyto, "Displays a list of commands.")
  23. elif cmdtype == "cmd":
  24. connection.privmsg(replyto, grey + "Statistics commands: " + CH.ccc(self, "joins") + CH.ccc(self, "kicks") + CH.ccc(self, "")[:-2] + ".")
  25. elif command.split()[0] == "joins" or command.split()[0] == "kicks":
  26. if cmdtype == "help": #Display help text.
  27. if len(command.split()) is not 1:
  28. return
  29. connection.privmsg(replyto, "Display amount of " + command.split()[0] + " of user and or channel. Channel and user optional.")
  30. connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + command.split()[0] + " " + reset + italic + "channel user")
  31. elif cmdtype == "cmd":
  32. # Parse user input
  33. user = event.source.nick
  34. if not connection.get_nickname() == event.target: # Channel message.
  35. channel = event.target
  36. if len(command.split()) == 1: # Command contains only !joins.
  37. user = event.source.nick
  38. elif len(command.split()) == 2: # Command has one argument.
  39. if command.split()[1] in self.channels:
  40. channel = command.split()[1]
  41. else:
  42. user = command.split()[1]
  43. elif len(command.split()) == 3: # Command has two arguments.
  44. if not command.split()[1] in self.channels: # Bot does not inhabit requested channel.
  45. if not command.split()[2] in self.channels: # User did not revert channel and user in command syntax.
  46. connection.action(replyto, "does not inhabit " + red + command.split()[1] + reset + ".")
  47. return
  48. else: # User reverted user and channel in command syntax.
  49. user = command.split()[1]
  50. channel = command.split()[2]
  51. else: # Bot does inhabit requested channel.
  52. user = command.split()[2]
  53. channel = command.split()[1]
  54. elif len(command.split()) < 5: # To many arguments
  55. connection.privmsg(replyto, "To many arguments. For help type " + blue + self.helpchar + "joins" + reset + ".")
  56. return
  57. try:
  58. if user and channel:
  59. if command.split()[0] == "joins":
  60. 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 + "'")))
  61. 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 + "'"))
  62. 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 + "'")))
  63. if userchannelstat == "None":
  64. userchannelstat = "0"
  65. 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.")
  66. elif command.split()[0] == "kicks":
  67. try:
  68. 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 + "'")
  69. except:
  70. givenkicks = 0
  71. receivedkicks = 0
  72. channelkicks = self.db.all("SELECT given FROM " + command.split()[0].lower() + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "'")
  73. 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.")
  74. return
  75. except:
  76. pass
  77. try:
  78. if user:
  79. if command.split()[0] == "joins":
  80. 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 + "'")))
  81. if userstat == "[]":
  82. userstat = "[0]"
  83. connection.privmsg(replyto, red + user + reset + " has " + green + userstat + reset + " " + command.split()[0] + " in channels I monitor.")
  84. elif command.split()[0] == "kicks":
  85. userstat = self.db.all("SELECT given, received FROM kicks WHERE channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'")
  86. kicksgiven = 0
  87. kicksreceived = 0
  88. for record in userstat:
  89. kicksgiven += record[0]
  90. kicksreceived += record[1]
  91. print(record)
  92. print(userstat)
  93. connection.privmsg(replyto, red + user + reset + " has given " + green + str(kicksgiven) + reset + " and received " + green + str(kicksreceived) + reset + " kicks")
  94. return
  95. except:
  96. pass
  97. try:
  98. if channel: # This situation should not occur anymore.
  99. 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 + "'")))
  100. if command.split()[0] == "joins":
  101. connection.privmsg(replyto, red + channel + reset + " has been " + command.split()[0][:-1] + "ed " + green + channelstat + reset + " times.")
  102. elif command.split()[0] == "kicks":
  103. connection.privmsg(replyto, "There have been " + green + channelstat + reset + " kicks in " + red + command.split()[0] + reset + ".")
  104. return
  105. except:
  106. pass
  107. log.error("No situation applied on statistics.py")