public.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. if not command: # Do nothing if there is no command.
  15. return
  16. # The actual commands:
  17. if command == "test":
  18. if cmdtype == "help": #Display help text.
  19. if len(command.split()) is not 1:
  20. return
  21. connection.privmsg(replyto, "Strictly for testing purposes only!")
  22. elif cmdtype == "cmd":
  23. connection.privmsg(replyto, str(self.channels[self.homechannel].owners()))
  24. if event.target == connection.get_nickname(): # PM
  25. connection.privmsg(replyto, event)
  26. else: # Channel message.
  27. connection.privmsg(replyto, str(self.channels[event.target]))
  28. print(connection.userhost(event.source.nick)) # Empty reply on GamerGalaxy, check what happens on GTAnet.
  29. print(connection.who(event.source.nick))
  30. elif command == "cmd" or command == "commands":
  31. if cmdtype == "help": #Display help text.
  32. if len(command.split()) is not 1:
  33. return
  34. connection.privmsg(replyto, "Displays a list of commands.")
  35. elif cmdtype == "cmd":
  36. connection.privmsg(replyto, grey + "Commands: " + CH.ccc(self, "cmd") + CH.ccc(self, "help") + CH.ccc(self, "stopgreet")[:-2] + ".")
  37. elif command == "help":
  38. if cmdtype == "help": #Display help text.
  39. if len(command.split()) is not 1:
  40. return
  41. connection.privmsg(replyto, "Explains how to get help on any specific command and hints the user to the commandlist.")
  42. elif cmdtype == "cmd":
  43. 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")
  44. connection.privmsg(replyto, grey + "Example: " + reset + blue + self.helpchar + "help")
  45. elif command.split()[0] == "stopgreet":
  46. if cmdtype == "help": #Display help text.
  47. if len(command.split()) is not 1:
  48. return
  49. connection.privmsg(replyto, "Stops the bot from greeting you or a specific user. Channel, user and option to resume optional.")
  50. connection.privmsg(replyto, grey + "Usage: " + blue + "!stopgreet " + red + reset + italic + "resume " + red + "channel user")
  51. elif cmdtype == "cmd":
  52. # Check for resume variation of command.
  53. resume = False
  54. try:
  55. if command.split()[1] == "resume":
  56. resume = True
  57. command = command.split(' ', 1)[1] # Remove the resume argument. Which makes the following logic easier.
  58. except:
  59. pass
  60. if len(command.split()) == 1: # No arguments.
  61. if event.target == connection.get_nickname(): # PM.
  62. connection.privmsg(replyto, "Specify at least a channel. For help type " + blue + self.helpchar + "stopgreet" + reset + ".")
  63. return
  64. user = event.source.nick
  65. channel = event.target
  66. if len(command.split()) == 2: # One argument.
  67. if command.split()[1] not in self.channels: # Argument is not a channel the bot inhabits.
  68. if event.target == connection.get_nickname(): # PM.
  69. connection.action(replyto, "does not inhabit " + red + command.split()[1] + reset + ".")
  70. return
  71. # Channel message
  72. if not userstatus.atleast_halfop(self, event.source.nick, self.homechannel) and not userstatus.atleast_halfop(self, event.source.nick, event.target): # Insufficient rights.
  73. connection.privmsg(replyto, "Denied. You need to have at least halfop status in " + red + self.homechannel + reset + " or " + red + event.target + reset + ".")
  74. return
  75. # Stopgreet for x user in current channel
  76. user = command.split()[1]
  77. channel = event.target
  78. else: # Bot inhabit channel.
  79. user = event.source.nick
  80. channel = command.split()[1]
  81. if len(command.split()) == 3: # Two arguments.
  82. if command.split()[1] not in self.channels: # Bot does not inhabit channel.
  83. connection.action(replyto, "does not inhabit " + red + command.split()[1] + reset + ".")
  84. return
  85. 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.
  86. connection.privmsg(replyto, "Denied. You need to have at least halfop status in " + red + self.homechannel + reset + " or " + red + command.split()[1] + reset + ".")
  87. return
  88. user = command.split()[2]
  89. channel = command.split()[1]
  90. if len(command.split()) > 3: # Too many arguments.
  91. connection.privmsg(replyto, "Too many arguments. For help type " + blue + self.helpchar + "stopgreet" + reset + ".")
  92. return
  93. # Check for database record.
  94. result = self.db.one("SELECT id, stopgreet FROM joins WHERE LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "' AND LOWER(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "'")
  95. if not result: # No record in database.
  96. connection.action(replyto, "has not yet had the pleasure of greeting " + red + user + reset + " in " + red + channel + reset + ".")
  97. return
  98. if resume:
  99. stopgreet = False
  100. message = "has already every intention to greet "
  101. else:
  102. stopgreet = True
  103. message = "has already stopped greeting "
  104. if result[1] == stopgreet:
  105. connection.action(replyto, message + red + user + reset + " in " + red + channel + reset + ".")
  106. return
  107. print(str(stopgreet) + str(user) + str(channel))
  108. try:
  109. self.db.run("UPDATE joins SET stopgreet='" + str(stopgreet) + "' WHERE LOWER(\"user\")=LOWER('" + user + "') AND user_network='" + self.network + "' AND lower(channel)=LOWER('" + channel + "') AND channel_network='" + self.network + "'")
  110. except:
  111. connection.privmsg(replyto, "Failed to update database.")