Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf8 -*-
  3.  
  4. import irc.client
  5. import irc.bot
  6. import string
  7.  
  8. irc.client.ServerConnection.buffer_class.encoding = 'latin-1'
  9.  
  10. #------------------------------------------------------------------------------
  11. # Root commands
  12. #------------------------------------------------------------------------------
  13. class KillCommand:
  14. def execute(self, bot, server, event):
  15. bot.die("Bye bye ! <3")
  16.  
  17. class NickCommand:
  18. def execute(self, bot, server, event):
  19. server.nick(event.arguments[0].split(" ")[1])
  20.  
  21. class JoinCommand:
  22. def execute(self, bot, server, event):
  23. server.join(event.arguments[0].split(" ")[1])
  24.  
  25. class PartCommand:
  26. def execute(self, bot, server, event):
  27. arguments = event.arguments[0].split(" ");
  28. server.part(arguments[1] if len(arguments) > 1 else event.target)
  29.  
  30. class MsgCommand:
  31. def execute(self, bot, server, event):
  32. arguments = event.arguments[0].split(" ")
  33. if irc.client.is_channel(arguments[1]):
  34. server.privmsg(arguments[1], " ".join(arguments[2:]))
  35. else:
  36. server.privmsg(event.target, " ".join(arguments[1:]))
  37.  
  38. #------------------------------------------------------------------------------
  39. # User commands
  40. #------------------------------------------------------------------------------
  41. class WakeCommand:
  42. def execute(self, bot, server, event):
  43. server.privmsg(event.target, " ".join(event.arguments[0].split(' ')[1:]) + ", réveille toi!")
  44.  
  45. #------------------------------------------------------------------------------
  46. # Bot class
  47. #------------------------------------------------------------------------------
  48. class Bot(irc.bot.SingleServerIRCBot):
  49. def __init__(self, nick, channel, server, port = 6667):
  50. self.defaultChannel = channel
  51.  
  52. irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nick, "gnidmoo's bot")
  53.  
  54. self.rootUsers = (
  55. "gnidmoo"
  56. )
  57.  
  58. self.rootCommands = {
  59. "!kill": KillCommand(),
  60. "!nick": NickCommand(),
  61. "!join": JoinCommand(),
  62. "!part": PartCommand(),
  63. "!msg": MsgCommand()
  64. }
  65.  
  66. self.userCommands = {
  67. "!wake": WakeCommand()
  68. }
  69.  
  70. def on_welcome(self, server, event):
  71. server.join(self.defaultChannel)
  72.  
  73. def on_pubmsg(self, server, event):
  74. message = event.arguments[0] + ' '
  75. if message[0] == '!':
  76. command = message.split(' ')[0];
  77. if command in self.rootCommands and event.source.nick in self.rootUsers:
  78. self.rootCommands[command].execute(self, server, event)
  79. elif command in self.userCommands:
  80. self.userCommands[command].execute(self, server, event)
  81.  
  82. def on_privmsg(self, server, event):
  83. self.on_pubmsg(server, event)
  84.  
  85. if __name__ == "__main__":
  86. import sys
  87.  
  88. Bot(nick, chan, server).start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement