statistics.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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, "messages")[:-2] + ".")
  25. elif command.split()[0] == "joins" or command.split()[0] == "kicks" or command.split()[0] == "messages":
  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. # userstatfields = ()
  58. # userchannelstatfields = ()
  59. # channelstatfields = ()
  60. # userstatvalues = ()
  61. # userchannelstatvalues = ()
  62. # channelstatvalues = ()
  63. # userstatparameters = ()
  64. # userchannelstatparameters = ()
  65. # channelstatparameters = ()
  66. try:
  67. if user and channel:
  68. if command.split()[0] == "joins":
  69. 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 + "'")))
  70. 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 + "'"))
  71. 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 + "'")))
  72. if userchannelstat == "None":
  73. userchannelstat = "0"
  74. 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.")
  75. elif command.split()[0] == "kicks":
  76. try:
  77. 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 + "'")
  78. except:
  79. givenkicks = 0
  80. receivedkicks = 0
  81. channelkicks = self.db.all("SELECT given FROM " + command.split()[0].lower() + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "'")
  82. 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.")
  83. return
  84. except:
  85. pass
  86. try:
  87. if user:
  88. if command.split()[0] == "joins":
  89. 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 + "'")))
  90. if userstat == "[]":
  91. userstat = "[0]"
  92. connection.privmsg(replyto, red + user + reset + " has " + green + userstat + reset + " " + command.split()[0] + " in channels I monitor.")
  93. elif command.split()[0] == "kicks":
  94. userstat = self.db.all("SELECT given, received FROM kicks WHERE channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'")
  95. kicksgiven = 0
  96. kicksreceived = 0
  97. for record in userstat:
  98. kicksgiven += record[0]
  99. kicksreceived += record[1]
  100. print(record)
  101. print(userstat)
  102. connection.privmsg(replyto, red + user + reset + " has given " + green + str(kicksgiven) + reset + " and received " + green + str(kicksreceived) + reset + " kicks")
  103. return
  104. except:
  105. pass
  106. try:
  107. if channel: # This situation should not occur anymore.
  108. 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 + "'")))
  109. if command.split()[0] == "joins":
  110. connection.privmsg(replyto, red + channel + reset + " has been " + command.split()[0][:-1] + "ed " + green + channelstat + reset + " times.")
  111. elif command.split()[0] == "kicks":
  112. connection.privmsg(replyto, "There have been " + green + channelstat + reset + " kicks in " + red + command.split()[0] + reset + ".")
  113. return
  114. except:
  115. pass
  116. log.error("No situation applied on statistics.py")