statistics.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from commands.common import CommandHelpers as CH
  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. def do_command(self, connection, event):
  12. cmdtype, trigger, command, replyto = CH.disect_command(self, event)
  13. if not command: # Do nothing if there is no command.
  14. return
  15. if not self.db.one("SELECT games FROM channels WHERE name='" + event.target + "' AND network='" + self.network + "'") and not event.target == connection.get_nickname():
  16. return # Do noting if the games channel function is off and it's a channel message.
  17. if command == "cmd" or command == "commands":
  18. if cmdtype == "help": #Display help text.
  19. if len(command.split()) is not 1:
  20. return
  21. connection.privmsg(replyto, "Displays a list of commands.")
  22. elif cmdtype == "cmd":
  23. connection.privmsg(replyto, grey + "Statistics commands: " + CH.ccc(self, "joins") + CH.ccc(self, "kicks") + CH.ccc(self, "messages")[:-2] + ".")
  24. elif command.split()[0] == "joins" or command.split()[0] == "kicks" or command.split()[0] == "messages":
  25. if cmdtype == "help": #Display help text.
  26. if len(command.split()) is not 1:
  27. return
  28. connection.privmsg(replyto, "Display amount of " + command.split()[0] + " of user and or channel. Channel and user optional.")
  29. connection.privmsg(replyto, grey + "Usage: " + blue + self.cmdchar + command.split()[0] + " " + reset + italic + "channel user")
  30. elif cmdtype == "cmd":
  31. # Parse user input
  32. user = event.source.nick
  33. channel = None
  34. channelonly= False
  35. if not connection.get_nickname() == event.target: # Channel message.
  36. channel = event.target
  37. if len(command.split()) == 1: # Command contains only !joins.
  38. user = event.source.nick
  39. elif len(command.split()) == 2: # Command has one argument.
  40. if command.split()[1] in self.channels:
  41. channel = command.split()[1]
  42. channelonly = True
  43. else:
  44. user = command.split()[1]
  45. elif len(command.split()) == 3: # Command has two arguments.
  46. if not command.split()[1] in self.channels: # Bot does not inhabit requested channel.
  47. if not command.split()[2] in self.channels: # User did not revert channel and user in command syntax.
  48. connection.action(replyto, "does not inhabit " + red + command.split()[1] + reset + ".")
  49. return
  50. else: # User reverted user and channel in command syntax.
  51. user = command.split()[1]
  52. channel = command.split()[2]
  53. else: # Bot does inhabit requested channel.
  54. user = command.split()[2]
  55. channel = command.split()[1]
  56. elif len(command.split()) < 5: # To many arguments
  57. connection.privmsg(replyto, "To many arguments. For help type " + blue + self.helpchar + "joins" + reset + ".")
  58. return
  59. if channel: # User and channel.
  60. if command.split()[0] == "joins":
  61. 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 + "'")))
  62. 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 + "'"))
  63. 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 + "'")))
  64. if userchannelstat == "None":
  65. userchannelstat = "0"
  66. if channelonly:
  67. connection.privmsg(replyto, red + channel + reset + " has " + green + channelstat + reset + " " + command.split()[0] + reset + " in total.")
  68. elif userstat == "0" and not channelonly: # No user joins on record.
  69. connection.action(replyto, "has no record of any joins for " + red + user + reset + ".")
  70. else: # User joins on record.
  71. 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.")
  72. elif command.split()[0] == "kicks":
  73. try:
  74. 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 + "'")
  75. except:
  76. givenkicks = 0
  77. receivedkicks = 0
  78. if channelonly:
  79. connection.privmsg(replyto, red + channel + reset + " has " + green + channelstat + reset + " " + command.split()[0] + reset + " in total.")
  80. elif givenkicks == 0: # No kicks on record.
  81. connection.action(replyto, "has no record of any kicks for " + red + user + reset + ".")
  82. else: # Kciks on record.
  83. channelkicks = self.db.all("SELECT given FROM " + command.split()[0].lower() + " WHERE LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "'")
  84. 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.")
  85. else: # Only user.
  86. if command.split()[0] == "joins":
  87. 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 + "'")))
  88. if userstat == "0": # No statistics on user.
  89. connection.action(replyto, "has no record of any " + command.split()[0] + " for " + red + user + reset + ".")
  90. else: # Got statistics on user.
  91. connection.privmsg(replyto, red + user + reset + " has " + green + userstat + reset + " " + command.split()[0] + " in channels I monitor.")
  92. elif command.split()[0] == "kicks":
  93. userstat = self.db.all("SELECT given, received FROM kicks WHERE channel_network='" + self.network + "' AND LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "'")
  94. kicksgiven = 0
  95. kicksreceived = 0
  96. for record in userstat:
  97. kicksgiven += record[0]
  98. kicksreceived += record[1]
  99. if kicksgiven == 0 and kicksreceived == 0:
  100. connection.action(replyto, red + user + reset + " has no record of any kicks for " + red + user + reset + " in " + red + channel + reset + ".")
  101. else:
  102. connection.privmsg(replyto, red + user + reset + " has given " + green + str(kicksgiven) + reset + " and received " + green + str(kicksreceived) + reset + " kicks")
  103. # userstatfields = ()
  104. # userchannelstatfields = ()
  105. # channelstatfields = ()
  106. # userstatvalues = ()
  107. # userchannelstatvalues = ()
  108. # channelstatvalues = ()
  109. # userstatparameters = ()
  110. # userchannelstatparameters = ()
  111. # channelstatparameters = ()
  112. # try:
  113. # if user and channel:
  114. #
  115. # return
  116. # except:
  117. # pass
  118. # try:
  119. #
  120. # return
  121. # except:
  122. # pass
  123. # try:
  124. # if channel: # This situation should not occur anymore.
  125. # 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 + "'")))
  126. # if command.split()[0] == "joins":
  127. # connection.privmsg(replyto, red + channel + reset + " has been " + command.split()[0][:-1] + "ed " + green + channelstat + reset + " times.")
  128. # elif command.split()[0] == "kicks":
  129. # connection.privmsg(replyto, "There have been " + green + channelstat + reset + " kicks in " + red + command.split()[0] + reset + ".")
  130. # return
  131. # except:
  132. # pass