statistics.py 9.0 KB

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