コード例 #1
0
    def connect(self):
        # Try to connect
        if self.connected:
            buffer.put('>>We are already connected')
        try:
            self.socket = IrcSocket()
            self.socket.connect(self.host, self.port)
            self.socket.put_raw(f"USER {self.user} 0 * :{self.real}")
            # TODO Support alternate nick
            self.socket.put_raw(f"NICK {self.nick}")
            self.stop_thread = False # used to stop the thread when connection dies
            self.receive_thread = threading.Thread(target=self.get_messages, args=(self.stop_thread, self.buffer,), daemon=True)
            self.receive_thread.start()
            self.connected = True

        except OSError as err:
            logging.error(err)
            self.buffer.put(f'>>Connection failed due to error: {err}')
            self.disconnect()
コード例 #2
0
def main():
    """
    Main method to get started
    opens connection and starts endless while loop for polling IRC Channel
    """

    args = parse()

    # set our settings for this run
    settings["app_dir"] = os.getcwd()
    settings["paperman"] = settings[platform.system()]
    settings["game"] = args.game if args.game else helpers.config_get("game")
    settings["ironman"] = args.ironman if args.ironman else helpers.config_get(
        "ironman")
    settings[
        "language"] = args.language if args.language else helpers.config_get(
            "language")

    # language option
    commands = {
        'truces': '!truces',
        'ideas': '!ideas',
        'ae': '!ae',
        'mods': '!mods'
    }
    if settings["language"] == 'german':
        commands['truces'] = '!waffenstillstand'
        commands['ideas'] = '!ideen'

    # do some logging
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger(__name__)
    logging.info(commands)

    # open connection
    connection = IrcSocket(args)

    # this'll run as long as you don't kill it
    while 1:
        text = connection.sock.recv(2040).decode()
        logger.info(text)

        # answer pings to stay connected
        if 'PING' in text:
            logger.info('Answer ping')
            connection.send_ping('PONG :tmi.twitch.tv\r\n')

        # ignore messages that aren't commands
        # only catches commmands if at the beginning of the message
        elif ':!' not in text:
            pass

        else:
            try:
                user = text[text.index('!') + 1:text.index('@')]
            except ValueError:
                continue
                #some default twitch messages don't fit the bill, but don't give us a user anyway

            text = text[text.rfind(':') + 1:].strip()

            # get ideas
            if text.startswith(commands.get('ideas')):

                connection.send_msg(user.title() + ': ' + getinfo.get_ideas())

            #get mods from settings
            elif text.startswith(commands.get('mods')):

                connection.send_msg(user.title() + ': ' + getinfo.get_mods())

            #get ae
            elif text.startswith(commands.get('ae')):

                connection.send_msg(user.title() + ': ' + getinfo.get_ae())

            #get truces
            elif text.startswith(commands.get('truces')):

                connection.send_msg(user.title() + ': ' + getinfo.get_truces())