public.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. from common import userstatus
  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. # Bot crashes once in a while when a user joins a channel, with "IndexError: list index out of range" on "command.split()[0] == "joins"".
  15. try:
  16. command.split()[0]
  17. except:
  18. return # Stop, there is no command.
  19. # The actual commands:
  20. if command == "test":
  21. if cmdtype == "help": #Display help text.
  22. if len(command.split()) is not 1:
  23. return
  24. connection.privmsg(replyto, "Strictly for testing purposes only!")
  25. elif cmdtype == "cmd":
  26. connection.privmsg(replyto, event)
  27. elif command == "cmd" or command == "commands":
  28. if cmdtype == "help": #Display help text.
  29. if len(command.split()) is not 1:
  30. return
  31. connection.privmsg(replyto, "Displays a list of commands.")
  32. elif cmdtype == "cmd":
  33. connection.privmsg(replyto, grey + "Commands: " + CH.ccc(self, "cmd") + CH.ccc(self, "help") + CH.ccc(self, "joins") + CH.ccc(self, "stopgreet")[:-2] + ".")
  34. elif command == "help":
  35. if cmdtype == "help": #Display help text.
  36. if len(command.split()) is not 1:
  37. return
  38. connection.privmsg(replyto, "Explains how to get help on any specific command and hints the user to the commandlist.")
  39. elif cmdtype == "cmd":
  40. connection.privmsg(replyto, "Replace the " + italic + "! " + reset + "prefix of any comand with " + italic + self.helpchar + " " + reset + "for help with a specific command. Request the command list with: " + blue + "!cmd")
  41. connection.privmsg(replyto, grey + "Example: " + reset + blue + self.helpchar + "help")
  42. elif command.split()[0] == "joins":
  43. if cmdtype == "help": #Display help text.
  44. if len(command.split()) is not 1:
  45. return
  46. connection.privmsg(replyto, "Display amount of joins of user and or channel. Channel and user optional.")
  47. connection.privmsg(replyto, grey + "Usage: " + blue + "!joins " + reset + italic + "channel user")
  48. elif cmdtype == "cmd":
  49. # Parse user input
  50. if len(command.split()) == 1: # Command contains only !joins.
  51. user = event.source.nick
  52. if connection.get_nickname() is not event.target: # Channel message.
  53. channel = event.target
  54. elif len(command.split()) == 2: # Command has one argument.
  55. if command.split()[1] in self.channels:
  56. channel = command.split()[1]
  57. else:
  58. user = command.split()[1]
  59. elif len(command.split()) == 3: # Command has two arguments.
  60. if not command.split()[1] in self.channels: # Bot does not inhabit requested channel.
  61. if not command.split()[2] in self.channels: # User did not revert channel and user in command syntax.
  62. connection.action(replyto, "does not inhabit " + red + command.split()[1] + reset + ".")
  63. return
  64. else: # User reverted user and channel in command syntax.
  65. user = command.split()[1]
  66. channel = command.split()[2]
  67. else: # Bot does inhabit requested channel.
  68. user = command.split()[2]
  69. channel = command.split()[1]
  70. elif len(command.split()) < 5: # To many arguments
  71. connection.privmsg(replyto, "To many arguments. For help type " + blue + self.helpchar + "joins" + reset + ".")
  72. return
  73. try:
  74. if user and channel:
  75. userjoins = str(self.db.all("SELECT joins FROM joins WHERE channel_network='" + self.network + "' AND \"user\"='" + user + "' AND user_network='" + self.network + "'"))
  76. userchanneljoins = str(self.db.one("SELECT joins FROM joins WHERE channel='" + channel + "' AND channel_network='" + self.network + "' AND \"user\"='" + user + "' AND user_network='" + self.network + "'"))
  77. channeljoins = str(sum(self.db.all("SELECT joins FROM joins WHERE channel='" + channel + "' AND channel_network='" + self.network + "' AND user_network='" + self.network + "'")))
  78. connection.privmsg(replyto, red + user + reset + " has " + green + str(sum(list(map(int, userjoins[1:-1].split(", "))))) + reset + " joins. Of which " + green + str(userchanneljoins) + reset + " have been in " + red + channel + reset + ", that has " + green + str(channeljoins) + reset + " joins" + reset + " in total.")
  79. return
  80. except:
  81. pass
  82. try:
  83. if user:
  84. userjoins = str(self.db.all("SELECT joins FROM joins WHERE channel_network='" + self.network + "' AND \"user\"='" + user + "' AND user_network='" + self.network + "' AND user_network='" + self.network + "'"))
  85. connection.privmsg(replyto, red + user + reset + " has " + green + str(sum(list(map(int, userjoins[1:-1].split(", "))))) + reset + " joins in channels I monitor.")
  86. return
  87. except:
  88. pass
  89. try:
  90. if channel:
  91. channeljoins = str(sum(self.db.all("SELECT joins FROM joins WHERE channel='" + channel + "' AND channel_network='" + self.network + "' AND user_network='" + self.network + "'")))
  92. connection.privmsg(replyto, red + channel + reset + " has been joined " + green + channeljoins + reset + " times.")
  93. return
  94. except:
  95. pass
  96. connection.privmsg(replyto, "tBKwtWS Was wondering if this programming was sloppy. When you see this message, it was.")
  97. elif command.split()[0] == "stopgreet":
  98. if cmdtype == "help": #Display help text.
  99. if len(command.split()) is not 1:
  100. return
  101. connection.privmsg(replyto, "Stops the bot from greeting you or a specific user. Channel, user and option to resume optional.")
  102. connection.privmsg(replyto, grey + "Usage: " + blue + "!stopgreet " + red + reset + italic + "resume " + red + "channel user")
  103. elif cmdtype == "cmd":
  104. # Check for resume variation of command.
  105. resume = False
  106. try:
  107. if command.split()[1] == "resume":
  108. resume = True
  109. command = command.split(' ', 1)[1] # Remove the resume argument. Which makes the following logic easier.
  110. except:
  111. pass
  112. if len(command.split()) == 1: # No arguments.
  113. if event.target == connection.get_nickname(): # PM.
  114. connection.privmsg(replyto, "Specify at least a channel. For help type " + blue + self.helpchar + "stopgreet" + reset + ".")
  115. return
  116. user = event.source.nick
  117. channel = event.target
  118. if len(command.split()) == 2: # One argument.
  119. if command.split()[1] not in self.channels: # Argument is not a channel the bot inhabits.
  120. if event.target == connection.get_nickname(): # PM.
  121. connection.action(replyto, "does not inhabit " + red + command.split()[1] + reset + ".")
  122. return
  123. # Channel message
  124. if not userstatus.atleast_halfop(self, event.source.nick, self.homechannel) and not userstatus.atleast_halfop(self, event.source.nick, event.target): # Insufficient rights.
  125. connection.privmsg(replyto, "Denied. You need to have at least halfop status in " + red + self.homechannel + reset + " or " + red + event.target + reset + ".")
  126. return
  127. # Stopgreet for x user in current channel
  128. user = command.split()[1]
  129. channel = event.target
  130. else: # Bot inhabit channel.
  131. user = event.source.nick
  132. channel = command.split()[1]
  133. if len(command.split()) == 3: # Two arguments.
  134. if command.split()[1] not in self.channels: # Bot does not inhabit channel.
  135. connection.action(replyto, "does not inhabit " + red + command.split()[1] + reset + ".")
  136. return
  137. if not userstatus.atleast_halfop(self, event.source.nick, self.homechannel) and not userstatus.atleast_halfop(self, event.source.nick, command.split()[1]): # Insufficient rights.
  138. connection.privmsg(replyto, "Denied. You need to have at least halfop status in " + red + self.homechannel + reset + " or " + red + command.split()[1] + reset + ".")
  139. return
  140. user = command.split()[2]
  141. channel = command.split()[1]
  142. if len(command.split()) > 3: # Too many arguments.
  143. connection.privmsg(replyto, "Too many arguments. For help type " + blue + self.helpchar + "stopgreet" + reset + ".")
  144. return
  145. # Check for database record.
  146. result = self.db.one("SELECT id, stopgreet FROM joins WHERE \"user\"='" + user + "' AND user_network='" + self.network + "' AND channel='" + channel + "' AND channel_network='" + self.network + "'")
  147. if not result: # No record in database.
  148. connection.action(replyto, "has not yet had the pleasure of greeting " + red + user + reset + " in " + red + channel + reset + ".")
  149. return
  150. if resume:
  151. stopgreet = False
  152. message = "has already every intention to greet "
  153. else:
  154. stopgreet = True
  155. message = "has already stopped greeting "
  156. if result[1] == stopgreet:
  157. connection.action(replyto, message + red + user + reset + " in " + red + channel + reset + ".")
  158. return
  159. print(str(stopgreet) + str(user) + str(channel))
  160. #try:
  161. print("UPDATE joins SET stopgreet='" + str(stopgreet) + "' WHERE \"user\"='" + user + "' AND user_network='" + self.network + "' AND channel='" + channel + "' AND channel_network='" + self.network + "'")
  162. self.db.run("UPDATE joins SET stopgreet='" + str(stopgreet) + "' WHERE \"user\"='" + user + "' AND user_network='" + self.network + "' AND channel='" + channel + "' AND channel_network='" + self.network + "'")
  163. #except:
  164. #connection.privmsg(replyto, "Failed to update database.")