Advertisement
Guest User

Chatango Bot

a guest
Mar 26th, 2012
8,775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 23.90 KB | None | 0 0
  1. import ch
  2. import random
  3. import sys
  4. import re
  5. import time
  6.  
  7. Trusted = ['Vici0usX']
  8.  
  9. dancemoves = [
  10.         "(>^.^)>",
  11.         "(v^.^)v",
  12.         "v(^.^v)",
  13.         "<(^.^<)"
  14. ]
  15.  
  16. bios = [
  17.     "where is the bios?",
  18.     "where do i get bios",
  19.    
  20.     "where do I get the bios",
  21.     "Bios files?",
  22. ]
  23.  
  24. #################################################
  25.  
  26. # Manager
  27.  
  28. #################################################
  29.  
  30. class MyManager(ch.RoomManager):
  31.  
  32.         """
  33.        Extends RoomManager to override and implement its unimplemented functions
  34.        as if RoomManager were an abstract class, as well as add new functions.
  35.        """
  36.        
  37.         #####
  38.        
  39.         # Overridden functions from RoomManager
  40.        
  41.         #####
  42.        
  43.         def onInit(self):
  44.  
  45.                 """
  46.                Called on init.
  47.                """
  48.                
  49.                 pass
  50.                
  51.         def onConnect(self, room):
  52.  
  53.                 """
  54.                Called when connected to the room.
  55.  
  56.                @type room: Room
  57.                @param room: room where the event occured
  58.                """
  59.                
  60.                 output = "Successfully connected to " + room.getName() + "!" + \
  61.                                 "\t(" + self._getLongTimeStamp() + ")"
  62.  
  63.                 print(output)
  64.  
  65.         def onReconnect(self, room):
  66.  
  67.                 """
  68.                Called when reconnected to the room.
  69.  
  70.                @type room: Room
  71.                @param room: room where the event occured
  72.                """
  73.                
  74.                 output = "Reconnected to " + room.getName() + "."
  75.  
  76.                 print(output)
  77.                
  78.         def onConnectFail(self, room):
  79.  
  80.                 """
  81.                Called when the connection failed.
  82.  
  83.                @type room: Room
  84.                @param room: room where the event occured
  85.                """
  86.  
  87.                 self.reconnect()
  88.  
  89.         def onDisconnect(self, room):
  90.  
  91.                 """
  92.                Called when the client gets disconnected.
  93.  
  94.                @type room: Room
  95.                @param room: room where the event occured
  96.                """
  97.                
  98.                 output = "Disconnected from " + room.getName() + "." + \
  99.                                 "\t(" + self._getLongTimeStamp() + ")"
  100.  
  101.                 print(output)
  102.  
  103.         def onLoginFail(self, room):
  104.  
  105.                 """
  106.                Called on login failure, disconnects after.
  107.  
  108.                @type room: Room
  109.                @param room: room where the event occured
  110.                """
  111.  
  112.                 pass
  113.  
  114.         def onFloodBan(self, room):
  115.  
  116.                 """
  117.                Called when either flood banned or flagged.
  118.  
  119.                @type room: Room
  120.                @param room: room where the event occured
  121.                """
  122.                
  123.                 output = "Flagged.  Please wait 15 minutes."
  124.  
  125.                 print(output)
  126.  
  127.         def onFloodBanRepeat(self, room):
  128.  
  129.                 """
  130.                Called when trying to send something when floodbanned.
  131.  
  132.                @type room: Room
  133.                @param room: room where the event occured
  134.                """
  135.  
  136.                 output = "Can't post:  still flagged."
  137.                
  138.                 print(output)
  139.  
  140.         def onFloodWarning(self, room):
  141.  
  142.                 """
  143.                Called when an overflow warning gets received.
  144.  
  145.                @type room: Room
  146.                @param room: room where the event occured
  147.                """
  148.  
  149.                 print("\a")
  150.                 room.setSilent(True)
  151.                
  152.                 output = "This is a flood warning for " + room.getName() + "."
  153.                
  154.                 print(output)
  155.  
  156.         def onMessageDelete(self, room, user, message):
  157.  
  158.                 """
  159.                Called when a message gets deleted.
  160.  
  161.                @type room: Room
  162.                @param room: room where the event occured
  163.  
  164.                @type user: User
  165.                @param user: owner of deleted message
  166.  
  167.                @type message: Message
  168.                @param message: message that got deleted
  169.                """
  170.  
  171.                 output = "Post deleted.\n\t" + \
  172.                                 "Author:\t" + user.name + "\n\t" + \
  173.                                 "IP:\t\t" + message.ip
  174.                
  175.                 print(output)
  176.  
  177.         def onModChange(self, room):
  178.  
  179.                 """
  180.                Called when the moderator list changes.
  181.  
  182.                @type room: Room
  183.                @param room: room where the event occured
  184.                """
  185.  
  186.                 output = "The mod list has been updated."
  187.                
  188.                 print(output)
  189.  
  190.         def onModAdd(self, room, user):
  191.  
  192.                 """
  193.                Called when a moderator gets added.
  194.  
  195.                @type room: Room
  196.                @param room: room where the event occured
  197.                """
  198.                
  199.                 output = "Someone was promoted to chat mod.\n\t" + \
  200.                                 "User:\t" + user.name + "\n\t" + \
  201.                                 "Time:\t" + self._getShortTimeStamp()
  202.  
  203.                 print(output)
  204.  
  205.         def onModRemove(self, room, user):
  206.  
  207.                 """
  208.                Called when a moderator gets removed.
  209.  
  210.                @type room: Room
  211.                @param room: room where the event occured
  212.                """
  213.                
  214.                 output = "Someone was demoted from chat mod.\n\t" + \
  215.                                 "User:\t" + user.name + "\n\t" + \
  216.                                 "Time\t" + self._getShortTimeStamp()
  217.  
  218.                 print(output)
  219.  
  220.         def onMessage(self, room, user, message):
  221.  
  222.                 """
  223.                Called when a message gets received.
  224.  
  225.                @type room: Room
  226.                @param room: room where the event occured
  227.  
  228.                @type user: User
  229.                @param user: owner of message
  230.  
  231.                @type message: Message
  232.                @param message: received message
  233.                        """
  234.                        
  235.                 # Log the message
  236.                
  237.                 output = "Message posted to room.\n\t" + \
  238.                                 "Author:\t" + user.name + "\n\t" + \
  239.                                 "IP:\t\t" + message.ip + "\n\t" + \
  240.                                 "Body:\t" + message.getBody() + "\n\t" + \
  241.                                 "Time:\t" + self._getShortTimeStamp()
  242.                
  243.                 print(output)
  244.                                
  245.                 # Obey authorized commands, if any
  246.                
  247.                 # Insert how to check for and execute commands here.  Below is an
  248.                 # example.
  249.  
  250.                
  251.                 if(message.getBody() == "Where is the bot?"):
  252.                         room.message("Here I am!")
  253.                        
  254.                 if(message.getBody() == "Where do I get bios?"):
  255.                         room.message("BIOS files are located at: http://www.roms4droid.com/bios.php")
  256.  
  257.                 if(message.getBody() == "Where is the bios"):
  258.                         room.message("BIOS files are located at: http://www.roms4droid.com/bios.php")
  259.  
  260.                 if(message.getBody() == "Where can I get bios?"):
  261.                         room.message("BIOS files are located at: http://www.roms4droid.com/bios.php")
  262.  
  263.                 if not message.body.lower().find("bios")
  264.                 and not message.body.lower().find("where"):
  265.                         room.message("BIOS files are located at: http://www.roms4droid.com/bios.php")
  266.  
  267.                 if(message.getBody() == "I need bios files"):
  268.                         room.message("BIOS files are located at: http://www.roms4droid.com/bios.php")
  269.                        
  270.                 if(message.getBody() == "hello"):
  271.                         room.message("Hello there! Welcome to Roms4Droid. For a list of commands, type !help")
  272.  
  273.                 if(message.getBody() == "hi"):
  274.                         room.message("Hello there! Welcome to Roms4Droid. For a list of commands, type !help")
  275.  
  276.                 if(message.getBody() == "help"):
  277.                         room.message("Need Help? Visit our online how-to guides: http://www.roms4droid.com/guides.php or post a topic in the forum: http://www.roms4droid.com/forum/index.php")
  278.                        
  279.                 if(message.getBody() == "Bye"):
  280.                         room.message("Good bye! Thanks for coming :)")
  281.                        
  282.                 if room.getLevel(self.user) > 0:
  283.                         print(user.name, message.ip, message.body)
  284.                 else:
  285.                         print(user.name, message.body)
  286.                 if self.user == user: return
  287.                 if message.body[0] == "!":
  288.                         data = message.body[1:].split(" ", 1)
  289.                         if len(data) > 1:
  290.                                 cmd, args = data[0], data[1]
  291.                         else:
  292.                                 cmd, args = data[0], ""
  293.                         if   cmd == "delay":
  294.                                 self.setTimeout(int(args), room.message, ":D")
  295.                         elif cmd == "randomuser":
  296.                                 room.message(random.choice(room.usernames))
  297.                         elif cmd == "ival":
  298.                                 self.setInterval(int(args), room.message, ":D")
  299.                         elif cmd == "mylvl":
  300.                                 room.message("Your mod level: %i" %(room.getLevel(user)))
  301.                         elif cmd == "mods":
  302.                                 room.message(", ".join(room.modnames + [room.ownername]))
  303.                         elif cmd == "help":
  304.                                 room.message("Commands: !bios, !roms, !guides, !mods, !mylvl")
  305.                         elif cmd == "bios":
  306.                                 room.message("BIOS Files are located at: http://www.roms4droid.com/bios.php")
  307.                         elif cmd == "roms":
  308.                                 room.message("ROM Files are located at: http://www.roms4droid.com/roms.php")
  309.                         #elif cmd == "leave"):
  310.                 #room.message("As you wish, my lord.")
  311.                 #room.disconnect()
  312.                 #break
  313.                         elif cmd == "guides":
  314.                                 room.message("Guides are located at: http://www.roms4droid.com/guides.php")
  315.                         elif cmd == "dance":
  316.                                 for i, msg in enumerate(dancemoves):
  317.                                         self.setTimeout(i / 2, room.message, msg)
  318.  
  319.         def onHistoryMessage(self, room, user, message):
  320.  
  321.                 """
  322.                Called when a message gets received from history.
  323.  
  324.                @type room: Room
  325.                @param room: room where the event occured
  326.  
  327.                @type user: User
  328.                @param user: owner of message
  329.  
  330.                @type message: Message
  331.                @param message: the message that got added
  332.                """
  333.  
  334.                 pass
  335.  
  336.         def onJoin(self, room, user):
  337.  
  338.                 """
  339.                Called when a user joins. Anonymous users get ignored here.
  340.  
  341.                @type room: Room
  342.                @param room: room where the event occured
  343.  
  344.                @type user: User
  345.                @param user: the user that has joined
  346.                """
  347.  
  348.                 output = "A known user joined the room.\n\t" + \
  349.                                 "User:\t" + user.name + "\n\t" + \
  350.                                 "Time:\t" + self._getShortTimeStamp()
  351.                
  352.                 print(output)
  353.  
  354.  
  355.         def onLeave(self, room, user):
  356.  
  357.                 """
  358.                Called when a user leaves. Anonymous users get ignored here.
  359.  
  360.                @type room: Room
  361.                @param room: room where the event occured
  362.  
  363.                @type user: User
  364.                @param user: the user that has left
  365.                """
  366.  
  367.                 output = "A known user left the room.\n\t" + \
  368.                                 "User:\t" + user.name + "\n\t" + \
  369.                                 "Time:\t" + self._getShortTimeStamp()
  370.                
  371.                 print(output)
  372.  
  373.         def onRaw(self, room, raw):
  374.  
  375.                 """
  376.                Called before any command parsing occurs.
  377.  
  378.                @type room: Room
  379.                @param room: room where the event occured
  380.  
  381.                @type raw: str
  382.                @param raw: raw command data
  383.                """
  384.  
  385.                 pass
  386.  
  387.         def onPing(self, room):
  388.  
  389.                 """
  390.                Called when a ping gets sent.
  391.  
  392.                @type room: Room
  393.                @param room: room where the event occured
  394.                """
  395.                
  396.                 output = "Ping sent."
  397.                
  398.                 print(output)
  399.  
  400.         def onUserCountChange(self, room):
  401.  
  402.                 """
  403.                Called when the user count changes.
  404.  
  405.                @type room: Room
  406.                @param room: room where the event occured
  407.                """
  408.                
  409.                 output = "There are now " + str(room.getUserCount()) + \
  410.                                 " members in this room."
  411.  
  412.                 print(output)
  413.  
  414.         def onBan(self, room, user, target):
  415.  
  416.                 """
  417.                Called when a user gets banned.
  418.  
  419.                @type room: Room
  420.                @param room: room where the event occured
  421.  
  422.                @type user: User
  423.                @param user: user that banned someone
  424.  
  425.                @type target: User
  426.                @param target: user that got banned
  427.                """
  428.  
  429.                 output = "A user has been banned.\n\t" + \
  430.                                 "Banned:\t" + target.name + "\n\t" + \
  431.                                 "Banner:\t" + user.name + "\n\t" + \
  432.                                 "Time:\t" + self._getShortTimeStamp()
  433.                
  434.                 print(output)
  435.  
  436.         def onUnban(self, room, user, target):
  437.  
  438.                 """
  439.                Called when a user gets unbanned.
  440.  
  441.                @type room: Room
  442.                @param room: room where the event occured
  443.  
  444.                @type user: User
  445.                @param user: user that unbanned someone
  446.  
  447.                @type target: User
  448.                @param target: user that got unbanned
  449.                """
  450.  
  451.                 output = "A user has been unbanned.\n\t" + \
  452.                                 "Unbanned:\t" + target.name + "\n\t" + \
  453.                                 "Unbanner:\t" + user.name + "\n\t" + \
  454.                                 "Time:\t" + self._getShortTimeStamp()
  455.                
  456.                 print(output)
  457.                        
  458.  
  459.         def onBanlistUpdate(self, room):
  460.  
  461.                 """
  462.                Called when a banlist gets updated.
  463.  
  464.                @type room: Room
  465.                @param room: room where the event occured
  466.                """
  467.  
  468.                 pass
  469.  
  470.         def onUnbanlistUpdate(self, room):
  471.  
  472.                 """
  473.                Called when a unbanlist gets updated.
  474.  
  475.                @type room: Room
  476.                @param room: room where the event occured
  477.                """
  478.  
  479.                 pass
  480.  
  481.         def onPMConnect(self, pm):
  482.        
  483.                 """
  484.                Called when connected to private chat.
  485.  
  486.                @type pm: PM
  487.                @param pm: private chat
  488.                """
  489.                
  490.                 output = "Successfully connected to private chat!\t(" + \
  491.                                 self._getLongTimeStamp() + ")"
  492.                
  493.                 print(output)
  494.        
  495.         def onPMDisconnect(self, pm):
  496.        
  497.                 """
  498.                Called when disconnected from private chat.
  499.                
  500.                @type pm: PM
  501.                @param pm: private chat
  502.                """
  503.                
  504.                 output = "Disconnected from private chat.\t(" + \
  505.                                 self._getLongTimeStamp() + ")"
  506.                
  507.                 print(output)
  508.        
  509.         def onPMPing(self, pm):
  510.                
  511.                 """
  512.                Called when a ping is sent on private chat.
  513.                
  514.                @type pm: PM
  515.                @param pm: private chat
  516.                """
  517.                
  518.                 output = "Ping sent."
  519.                
  520.                 print(output)
  521.        
  522.         def onPMMessage(self, pm, user, body):
  523.        
  524.                 """
  525.                Called when a message is received.
  526.                
  527.                @type pm: PM
  528.                @param pm: private chat
  529.                
  530.                @type user: User
  531.                @param user: owner of message
  532.                
  533.                @type body: str
  534.                @param body: content of the private message
  535.                """
  536.                
  537.                 pass
  538.        
  539.         def onPMOfflineMessage(self, pm, user, body):
  540.                 pass
  541.        
  542.         def onPMContactlistReceive(self, pm):
  543.                 pass
  544.        
  545.         def onPMBlocklistReceive(self, pm):
  546.                 pass
  547.        
  548.         def onPMContactAdd(self, pm, user):
  549.                
  550.                 """
  551.                Called after a contact is added in private chat.
  552.                
  553.                @type pm: PM
  554.                @param pm: private chat
  555.                
  556.                @type user: User
  557.                @param user: new contact
  558.                """
  559.                
  560.                 output = "New contact added in private chat.\n\t" + \
  561.                                 "User:\t" + user.name + "\n\t" + \
  562.                                 "Time:\t" + self.getShortTimeStamp()
  563.                
  564.                 print(output)
  565.        
  566.         def onPMContactRemove(self, pm, user):
  567.                
  568.                 """
  569.                Called after a contact is removed from private chat.
  570.                
  571.                @type pm: PM
  572.                @param pm: private chat
  573.                
  574.                @type user: User
  575.                @param user: removed contact
  576.                """
  577.                
  578.                 output = "Existing contact removed from private chat.\n\t" + \
  579.                                 "User:\t" + user.name + "\n\t" + \
  580.                                 "Time:\t" + self.getShortTimeStamp()
  581.                
  582.                 print(output)
  583.        
  584.         def onPMBlock(self, pm, user):
  585.                
  586.                 """
  587.                Called after a user is blocked from contacting the bot via PM.
  588.                
  589.                @type pm: PM
  590.                @param pm: private chat
  591.                
  592.                @type user: User
  593.                @param user: blocked user
  594.                """
  595.                
  596.                 output = "User blocked from sending PMs.\n\t" + \
  597.                                 "User:\t" + user.name + "\n\t" + \
  598.                                 "Time:\t" + self.getShortTimeStamp()
  599.                
  600.                 print(output)
  601.        
  602.         def onPMUnblock(self, pm, user):
  603.                
  604.                 """
  605.                Called after a user is repermitted to contact the bot via PM.
  606.                
  607.                @type pm: PM
  608.                @param pm: private chat
  609.                
  610.                @type user: User
  611.                @param user: unblocked user
  612.                """
  613.                
  614.                 output = "User unblocked from sending PMs.\n\t" + \
  615.                                 "User:\t" + user.name + "\n\t" + \
  616.                                 "Time:\t" + self.getShortTimeStamp()
  617.                
  618.                 print(output)
  619.        
  620.         def onPMContactOnline(self, pm, user):
  621.                
  622.                 """
  623.                Called when a user in the bot's contact list comes online.
  624.                
  625.                @type pm: PM
  626.                @param pm: private chat
  627.                
  628.                @type user: User
  629.                @param user: user who came online
  630.                """
  631.                
  632.                 # Insert code for private messages here
  633.                
  634.                 output = "A user is now online.\n\t" + \
  635.                                 "User:\t" + user.name + "\n\t" + \
  636.                                 "Time:\t" + self.getShortTimeStamp()
  637.                
  638.                 print(output)
  639.        
  640.         def onPMContactOffline(self, pm, user):
  641.                
  642.                 """
  643.                Called when a user in the bot's contact list goes offline.
  644.                
  645.                @type pm: PM
  646.                @param pm: private chat
  647.                
  648.                @type user: User
  649.                @param user: user who went offline
  650.                """
  651.                
  652.                 output = "A user has gone offline.\n\t" + \
  653.                                 "User:\t" + user.name + "\n\t" + \
  654.                                 "Time:\t" + self.getShortTimeStamp()
  655.                
  656.                 print(output)
  657.        
  658.         def onEventCalled(self, room, evt, *args, **kw):
  659.  
  660.                 """
  661.                Called on every room-based event.
  662.  
  663.                @type room: Room
  664.                @param room: room where the event occured
  665.  
  666.                @type evt: str
  667.                @param evt: the event
  668.                """
  669.  
  670.                 pass
  671.                
  672.         #####
  673.        
  674.         # Overridden functions from RoomManager
  675.        
  676.         #####
  677.        
  678.         def _getShortTimeStamp(self):
  679.                
  680.                 """
  681.                Returns the time that an event was registered.
  682.                
  683.                @rtype: str
  684.                @return: timestamp of an event
  685.                """
  686.                
  687.                 return time.strftime("%H:%M:%S UTC", time.gmtime())
  688.        
  689.         def _getLongTimeStamp(self):
  690.                
  691.                 """
  692.                Returns the date and time when an event took place.
  693.                
  694.                The local variable "format" may be changed by you the programmer if
  695.                you so desire.  To determine how to display the timestamp in your
  696.                preferred manner, please consult the Python documentation at
  697.                http://docs.python.org/library/time.html#time.strftime
  698.                
  699.                As-is, the formatting would create a time stamp in this style:
  700.                
  701.                        Sunday, Janurary 1 2012, 23:59:59 UTC
  702.                
  703.                @rtype: str
  704.                @return: timestamp of an event
  705.                """
  706.                
  707.                 # This is the formatting of the time stamp.  If it doesn't make sense
  708.                 # to you because you aren't from where I am or if you simply just
  709.                 # don't like this formatting, please consult the Python documentation
  710.                 # at http://docs.python.org/library/time.html#time.strftime
  711.                 #
  712.                 # As-is, the formatting would create this style of time stamp.
  713.                 #       Sunday, January 1 2012, 00:00:00 +0:00:00
  714.                
  715.                 format = "%A, %B %d %Y, %H:%M:%S UTC"
  716.                
  717.                 return time.strftime(format, time.gmtime())
  718.        
  719. #################################################
  720.  
  721. # Run the bot
  722.  
  723. #################################################
  724.  
  725. manager = MyManager()
  726. roomList = ["Roms4Droid", "Danball5"]                       # List of Chatango room names here
  727. usePM = False                                   # True if you want to PM, False otherwise.
  728.  
  729. manager.easy_start(roomList, "R4DBot", "mathias87", usePM)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement