from sys import exit from random import randint from time import sleep import config as cfg from irc import IRC class Ramen: """Functions for ramenkan.""" irc = IRC() irc.debug = cfg.debug def run(self): """Connect to the server and channels, initialise the listener in the application loop.""" self.irc.connect(cfg.server, cfg.bot_nick) self.irc.join_channels(cfg.channels) while 1: sleep(2) data = self.irc.receive(debug=cfg.debug) self.irc.keep_alive(data) self.msg = self.irc.parse_msg(data, cfg.req_prefix) for chan in cfg.channels: self.handle(chan) def handle(self, channel): """Listen for requests in a channel and pass them to handler functions.""" if self.msg["req_chan"] == cfg.bot_nick: # Respond to some commands only from admin user if self.msg["user"].lower() == cfg.admin_user.lower() and \ cfg.admin_code in self.msg["req"]: self.handle_admin_req(self.msg["req"], cfg.admin_user) # Respond only in the channel the request was made if channel == self.msg["req_chan"]: # General commands if self.msg["req"] == "rollcall" or self.msg["req"] == "help": self.handle_rollcall() elif self.msg["req"] == ("water " + cfg.bot_nick): self.handle_water() elif self.msg["req"] == ("botsnack " + cfg.bot_nick): self.handle_botsnack() def handle_admin_req(self, req, admin_user): """Perform admin functions.""" if "exit" in req: self.irc.send("PRIVMSG", "Okay, okay, I'll leave. (´・ω・`)", \ recvr=admin_user) self.irc.send("QUIT", ":noodling off") exit("Shutting down ...") def handle_rollcall(self): resp = ( "一、二、三、らーめん缶! " "Hello, I am a ramen vending machine. " "Please type a code for service: " "!help " "Support: +81 012-700-1MIO どうぞめしあがれ。" ) self.irc.send("PRIVMSG", resp, recvr=self.msg["req_chan"]) def handle_water(self): resp = [ ("\x01ACTION happily pours the hot liquid into a bowl of noodles " "and offers it to ") + self.msg["nick"] + "\x01", "( ^_^)o自自o(^_^ )Cheers!", "Water Level [/////////] 200% - Thanks! (^▽^)" ] self.irc.send("PRIVMSG", resp[randint(0, len(resp)-1)], \ recvr=self.msg["req_chan"]) def handle_botsnack(self): self.irc.send("PRIVMSG", "Ramen time anytime! ヽ(´▽`)/", \ recvr=self.msg["req_chan"]) app = Ramen() app.run()