コード例 #1
0
ファイル: irc.py プロジェクト: rdunc/rybot
    def connect(self):
        config = self.config
        server = config["server"]
        port = config["port"]
        username = config["username"]
        password = config["oauth_password"]

        Log.info("Connecting to {0} on port {1}...".format(server, port))

        try:
            self.socket.settimeout(10)
            self.socket.connect((server, port))
            self.socket.settimeout(None)

            Log.info("Successfully connected to {0} on port {1}!".format(server, port))
            Log.info("Joining channel #{1}...".format(username, self.channel))

            self.socket.send(bytes("CAP REQ {0}\r\n".format(":twitch.tv/membership"), self.encoding_type))
            self.socket.send(bytes("PASS {0}\r\n".format(password), self.encoding_type))
            self.socket.send(bytes("NICK {0}\r\n".format(username), self.encoding_type))
            self.socket.send(bytes("JOIN #{0}\r\n".format(self.channel), self.encoding_type))

            Log.info("Successfully joined channel #{1}!".format(username, self.channel))

            return self.socket
        except:
            Log.error("Failed to connect to {0} on port {1}.".format(server, port))
            sys.exit(1)
コード例 #2
0
def run(argv):
    try:
        opts, args = getopt.getopt(argv, "hc:", ["channel="])

        if not opts:
            Log.error("You forgot to enter a channel.")
            Log.info("Usage: run.py -c <channel>\n")
            sys.exit(1)
        else:
            for opt, arg in opts:
                if opt == "-help":
                    print("\nUsage: run.py -c <channel>")
                    sys.exit(1)

                if opt in ("-c", "--channel"):
                    channel = arg

            if os.path.exists("bot/config/config.py"):
                if config["username"]:
                    if config["oauth_password"]:
                        RyBot(channel, config).run()
                    else:
                        Log.error("Password was invalid. Please make sure you configured the config.py file correctly.")
                else:
                    Log.error("Username was invalid. Please make sure you configured the config.py file correctly.")
            else:
                Log.error("No config file was found. Please make sure you configured the config.py file correctly.")
    except getopt.GetoptError:
        print("\nUsage: run.py -c <channel>")
        sys.exit(1)
コード例 #3
0
    def start_economy(self):
        config = self.config
        points_timer = config["give_points_timer"]
        debug = config["debug"]

        Log.economy("Giving points.")

        self.economy.give_points()
        threading.Timer(points_timer, self.start_economy).start()
コード例 #4
0
    def announce_subscription(self, message):
        if self.config["enable_announcer"]:
            message_split = message.split()

            self.irc.send_message(
                self.socket, self.channel,
                "Thank you {0} for subscribing!".format(message_split[0]))

        Log.announce(message)
コード例 #5
0
    def start_pong(self):
        config = self.config
        socket = self.socket
        debug = config["debug"]

        self.irc.send_pong(self.socket, ":tmi.twitch.tv")
        threading.Timer(58, self.start_pong).start()

        if debug:
            Log.debug("Pong sent.")
コード例 #6
0
ファイル: economy.py プロジェクト: rdunc/rybot
    def check_points(self, sender):
        points = self.load()

        try:
            total_points = points[sender]
        except KeyError:
            total_points = 0

        Log.commander("Checking how many points {0} has.".format(sender))
        self.irc.send_message(self.socket, self.channel, "@{0} -> You have {1} {2}.".format(sender, total_points, RyBotHelper.pluralize(total_points, "point")))
コード例 #7
0
    def add(self, sender, syntax, response):
        socket = self.socket
        channel = self.channel
        interal_commands = ["!command", "!points"]

        if not syntax or not response or syntax in interal_commands:
            return self.irc.send_message(
                socket, channel,
                "@{0} -> Invalid syntax. Use !command new !<syntax> <response> instead."
                .format(sender))

        if not self.exists(syntax):
            command = ""

            for x in response:
                command += x + " "

            commands = self.load()
            commands[syntax] = command[:-2]

            self.save(commands)
            self.irc.send_message(
                socket, channel,
                "@{0} -> Command successfully created!".format(sender))
            return Log.commander(
                "Command {0} successfully created!".format(syntax))
        else:
            return self.irc.send_message(
                socket, channel,
                "@{0} -> That command already exists. Use !command update !<syntax> <response> instead."
                .format(sender))
コード例 #8
0
    def run(self, syntax):
        socket = self.socket
        channel = self.channel
        commands = self.load()
        command = commands[syntax]

        if self.exists(syntax):
            self.irc.send_message(socket, channel, command)
            return Log.commander("Running command {0}".format(syntax))
コード例 #9
0
    def update(self, sender, syntax):
        if self.exists(syntax):
            commands = self.load()
            commands[syntax] = command[:-2]

            self.save(commands)
            self.irc.send_message(
                socket, channel,
                "@{0} -> Command successfully created!".format(sender))
            return Log.commander(
                "Command {0} successfully created!".format(syntax))
コード例 #10
0
    def delete(self, sender, syntax):
        socket = self.socket
        channel = self.channel
        commands = self.load()

        if self.exists(syntax):
            commands.pop(syntax, None)
            self.save(commands)
            self.irc.send_message(
                socket, channel,
                "@{0} -> Command successfully deleted!".format(sender))
            return Log.commander(
                "Command {0} successfully deleted!".format(syntax))
コード例 #11
0
    def parse_data(self):
        data = ""
        config = self.config
        encoding_type = config["encoding_type"]
        buffer_size = config["socket_buffer_size"]
        debug = config["debug"]

        try:
            data = data + self.socket.recv(buffer_size).decode("ascii")
        except UnicodeDecodeError:
            Log.error("Message cannot be decoded.")

        data_split = re.split(r"[~\r\n]+", data)
        data = data_split.pop()

        for line in data_split:
            line = str.rstrip(line)
            line = str.split(line)

            try:
                if line[0] == "PING":
                    self.irc.send_pong(self.socket, line[1])

                    if debug:
                        Log.debug("Pong sent.")

                if line[1] == "PRIVMSG":
                    sender = IrcHelper.get_sender(line[0])
                    message = IrcHelper.get_message(line)

                    print("-> [{0}{1}{2}]: {3}".format(ColorHelper.white(),
                                                       sender,
                                                       ColorHelper.reset_all(),
                                                       message))

                    if sender == "twitchnotify":
                        self.announcer.announce_subscription(message)

                    if config["enable_commander"]:
                        IrcHelper.parse_message(self.commander_commands,
                                                self.commander_economy, sender,
                                                message)
            except IndexError:
                if debug:
                    print(data)
                    Log.error("Index out of range error occured.")
コード例 #12
0
    def give_points(self):
        config = self.config
        debug = config["debug"]
        point_timer = config["give_points_timer"]
        api_chatters_url = config["twitch_chatters_url"]
        economy_path = "db/" + self.channel + "/economy.json"

        try:
            twitch_request = requests.get(api_chatters_url + self.channel +
                                          "/chatters")
            chatters_json = twitch_request.json()

            if debug:
                time_1 = Benchmark.start()

            with open(economy_path, "r") as of:
                file_chatters = of.read()
                of.close()

            if len(file_chatters) > 0:
                file_chatters = json.loads(file_chatters)

            if debug:
                Log.economy("Current file chatters count: {0}".format(
                    len(file_chatters)))

            api_chatters = chatters_json["chatters"]["viewers"]
            chatters_dictionary = {}

            for i in api_chatters:
                chatters_dictionary[i] = 1

                if debug:
                    Log.economy("1 point was added to: {0}".format(i))

            if len(file_chatters) > 0:
                merged_chatters = [chatters_dictionary, file_chatters]
                merged_chatters = sum(
                    (Counter(dict(i)) for i in merged_chatters), Counter())
            else:
                merged_chatters = chatters_dictionary

            with open(economy_path, "w") as of:
                json.dump(merged_chatters, of)
                of.close()

            Log.economy("1 point was added to {0} {1}".format(
                len(merged_chatters),
                RyBotHelper.pluralize(len(merged_chatters), "chatter")))

            if debug:
                Log.economy("Current chatters from API: {0}".format(
                    len(chatters_dictionary)))
                Benchmark.stop(time_1)
        except json.decoder.JSONDecodeError:
            Log.error(
                "Problem decoding the JSON. Unable to distribute points.")
        except requests.exceptions.ConnectionError:
            Log.error("Unable to connect to the Twitch API.")
        except TypeError:
            Log.error("Error finding the viewers.")
        except FileNotFoundError:
            Log.error("Economy file not found. Unable to distribute points.")
コード例 #13
0
 def start():
     Log.benchmark("Started.")
     return time.time()
コード例 #14
0
 def stop(time_1):
     time_2 = time.time()
     Log.benchmark("Finished. Function took {0} seconds to complete.".format(time_2 - time_1))