当前位置: 首页>>代码示例>>Python>>正文


Python API.get_channels方法代码示例

本文整理汇总了Python中API.API.get_channels方法的典型用法代码示例。如果您正苦于以下问题:Python API.get_channels方法的具体用法?Python API.get_channels怎么用?Python API.get_channels使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在API.API的用法示例。


在下文中一共展示了API.get_channels方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from API import API [as 别名]
# 或者: from API.API import get_channels [as 别名]
class Server:
    def __init__(self):
        # Chargement de la configuration
        with open("config.json", "r") as json_data:
            config = json.load(json_data)
            host = config["host"]
            port = config["port"]
            sleep_time = config["sleepTime"]
            nick = config["nickname"]
            oper_password = config["oper_password"]
            log_file = config["log_file"]
            log_level = config["log_level"]

        logging.basicConfig(filename=log_file,
                            filemode="a",
                            level=getattr(logging, log_level.upper()),
                            format='%(asctime)s [%(levelname)s | %(filename)s > %(funcName)s] %(message)s',
                            datefmt='%x %X')
        stderr = open(log_file, "a")

        logging.info("=== Starting bot ===")
        logging.info("Loading API")
        self.API = API(self)
        logging.info("Loading plugins")
        self.plugins = Plugins(self.API)

        logging.info("Connecting to the IRC network")
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.connect((host, port))
        self.API.send_command("NICK {}".format(nick))
        self.API.send_command("USER {} 0 * :Bot".format(nick))
        while True:
            motd = self.socket.recv(1024).decode()
            if len(motd) > 0:
                for line in motd.splitlines():
                    logging.info(line)
            if "Current Global Users" in motd:
                break

        self.API.send_command("OPER Bot {}".format(oper_password))
        for line in self.socket.recv(1024).decode().splitlines():
            logging.info(line)

        for channel in self.API.get_channels():
            self.API.send_command("JOIN " + channel)
            for line in self.socket.recv(1024).decode().splitlines():
                logging.info(line)
            self.API.send_command("SAMode {} +o {}".format(channel, nick))
            for line in self.socket.recv(1024).decode().splitlines():
                logging.info(line)
            self.API.send_names_query(channel)

        logging.info("Connected !")
        logging.info("Loading plugins")

        self.plugins.load()

        commands = {
            "PING": self.ping,
            "PRIVMSG": self.privmsg,
            "JOIN": self.join,
            "KICK": self.kick,
            "PART": self.part,
            "MODE": self.mode,
            "QUIT": self.quit
        }

        logging.info("Starting main-loop")
        self.API.send_command("PONG :127.0.0.1")
        self.running = True
        while self.running:
            rlist, wlist, xlist = select([self.socket], [], [], sleep_time)
            if rlist:
                try:
                    msg = self.socket.recv(1024).decode()
                except Exception as ex:
                    logging.exception(ex)
                else:
                    for line in msg.splitlines():
                        logging.info(line)
                        args = line.split(" ")
                        if len(args) >= 1 and args[0] in commands:
                            commands[args[0]](args)

                        elif len(args) >= 2 and args[1] in commands:
                            commands[args[1]](args)

                        else:
                            pass

            self.plugins.loop()

        self.socket.close()

    def ping(self, args: list) -> None:
        # PING :127.0.0.1
        if len(args) >= 2:
            self.API.send_command("PONG " + args[1])

    def privmsg(self, args: list) -> None:
#.........这里部分代码省略.........
开发者ID:Julien00859,项目名称:BotIRC,代码行数:103,代码来源:main.py


注:本文中的API.API.get_channels方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。