Ejemplo n.º 1
0
    def _handle_command(self, bot, source, message, event, format_cb, message_method):
        from_input, to_input = self._parse_message(message)

        with DBManager.create_session_scope(expire_on_commit=False) as db_session:
            if from_input is not None:
                from_user = User.find_or_create_from_user_input(db_session, bot.twitch_helix_api, from_input)

                if from_user is None:
                    bot.execute_now(
                        bot.send_message_to_user,
                        source,
                        f'User "{from_input}" could not be found',
                        event,
                        method=self.settings["action_followage"],
                    )
                    return
            else:
                from_user = source

            if to_input is None:
                to_input = bot.streamer  # TODO make bot.streamer a User() instance?

            to_user = User.find_or_create_from_user_input(db_session, bot.twitch_helix_api, to_input)
            if to_user is None:
                bot.execute_now(
                    bot.send_message_to_user,
                    source,
                    f'User "{to_input}" could not be found',
                    event,
                    method=message_method,
                )
                return

        follow_since = bot.twitch_helix_api.get_follow_since(from_user.id, to_user.id)
        # is_self = source == from_user
        to_user = to_user.name.replace("AdmiralBulldog", "AdmeralllBuldogg")

        if follow_since is not None:
            # Following
            suffix = f"been following {to_user} {format_cb(follow_since)}"
            # if is_self:
            #     message = "You have " + suffix
            # else:
            message = from_user.name + " has " + suffix
        else:
            # Not following
            suffix = f"not following {to_user}"
            # if is_self:
            #     message = "You are " + suffix
            # else:
            message = from_user.name + " is " + suffix

        bot.execute_now(bot.send_message_to_user, source, message, event, method=message_method)
Ejemplo n.º 2
0
    def _handle_command(self, bot, source, message, event, format_cb, message_method):
        from_input, to_input = self._parse_message(message)

        with DBManager.create_session_scope(expire_on_commit=False) as db_session:
            if from_input is not None:
                from_user = User.find_or_create_from_user_input(db_session, bot.twitch_helix_api, from_input)

                if from_user is None:
                    bot.execute_now(
                        bot.send_message_to_user,
                        source,
                        f'User "{from_input}" could not be found',
                        event,
                        method=self.settings["action_followage"],
                    )
                    return
            else:
                from_user = source

            if to_input is None:
                to_input = bot.streamer.login

            to_user = User.find_or_create_from_user_input(db_session, bot.twitch_helix_api, to_input)
            if to_user is None:
                bot.execute_now(
                    bot.send_message_to_user,
                    source,
                    f'User "{to_input}" could not be found',
                    event,
                    method=message_method,
                )
                return

        follow_since = bot.twitch_helix_api.get_follow_since(from_user.id, to_user.id)
        is_self = source == from_user

        if follow_since is not None:
            # Following
            suffix = f"been following {to_user} {format_cb(follow_since)}"
            if is_self:
                message = f"You have {suffix}"
            else:
                message = f"{from_user.name} has {suffix}"
        else:
            # Not following
            suffix = f"not following {to_user}"
            if is_self:
                message = f"You are {suffix}"
            else:
                message = f"{from_user.name} is {suffix}"

        bot.execute_now(bot.send_message_to_user, source, message, event, method=message_method)
Ejemplo n.º 3
0
    def long_timeout(self, bot, message, source, **options):
        errorString = "Invalid usage. !longtimeout user days"
        daysDuration = 0

        if not message or len(message.split(" ")) < 2:
            bot.whisper(source, errorString)
            return False

        splitMsg = message.split(" ")

        try:
            daysDuration = int(splitMsg[1])
            timeoutDuration = daysDuration * 86400
            if timeoutDuration > 1209600:
                timeoutDuration = 1209600

            nowTime = utils.now()
            endTime = nowTime + timedelta(days=daysDuration)
            with DBManager.create_session_scope() as db_session:
                badPerson = User.find_or_create_from_user_input(
                    db_session, bot.twitch_helix_api, splitMsg[0])

                if badPerson.moderator:
                    bot.whisper(source, "You can't timeout mods")
                    return False

                if badPerson.level >= 420:
                    bot.whisper(
                        source,
                        f"{badPerson}'s level is too high, you can't time them out."
                    )
                    return False

                if db_session.query(LongTimeout).filter(
                        LongTimeout.user_id == badPerson.id).count() != 0:
                    bot.whisper(source,
                                f"{badPerson} already exists in the database")
                    return False

                longtimeout = LongTimeout(user_id=badPerson.id,
                                          timeout_start=nowTime,
                                          timeout_end=endTime,
                                          timeout_author=source.name)

                db_session.add(longtimeout)

                bot._timeout(
                    badPerson, timeoutDuration,
                    f"Timed out by {source} for {daysDuration} days total")
                bot.whisper(
                    source,
                    f"Timed out {badPerson} for {daysDuration} days, per your !longtimeout"
                )

        except ValueError:
            bot.whisper(source, errorString)
            return False
        except Exception as e:
            log.error(e)
Ejemplo n.º 4
0
 def user_from_access_token(self, access_token, twitch_helix_api,
                            db_session):
     resp = self.get("",
                     authorization=" ",
                     headers={"Authorization": "OAuth " + access_token})
     if "token" in resp:
         return User.find_or_create_from_user_input(
             db_session, twitch_helix_api, resp["token"]["user_name"])
     return None
Ejemplo n.º 5
0
    def permit_link(self, bot, source, message, **rest):
        parts = message.split(" ")
        user = User.find_or_create_from_user_input(self.db_session, self.bot.twitch_helix_api, parts[0])
        try:
            length = int(parts[1] if len(parts) > 1 else 300)
        except:
            length = 300

        if user.id in self.permitted_users:
            self.bot.say(f"{user} is already permitted")
            return False

        self.permitted_users.append(user.id)
        self.bot.say(f"@{user} has been permitted to post links for {length} seconds (@{source})")
        self.bot.execute_delayed(length, lambda: self.permitted_users.remove(user.id))
Ejemplo n.º 6
0
    def level(bot, source, message, **rest):
        if not message:
            bot.whisper(source, "Usage: !level USERNAME NEW_LEVEL")
            return False

        msg_args = message.split(" ")
        if len(msg_args) < 2:
            return False

        username = msg_args[0].lower()
        new_level = int(msg_args[1])
        if new_level >= source.level:
            bot.whisper(
                source,
                f"You cannot promote someone to the same or higher level as you ({source.level})."
            )
            return False

        # We create the user if the user didn't already exist in the database.
        with DBManager.create_session_scope() as db_session:
            user = User.find_or_create_from_user_input(db_session,
                                                       bot.twitch_helix_api,
                                                       username)
            if user is None:
                bot.whisper(
                    source,
                    f'A user with the name "{username}" could not be found.')
                return False

            if user.level >= source.level:
                bot.whisper(
                    source,
                    f"You cannot change the level of someone who is the same or higher level than you. You are level {source.level}, and {username} is level {user.level}",
                )
                return False

            old_level = user.level
            user.level = new_level

            log_msg = f"{user}'s user level changed from {old_level} to {new_level}"

            bot.whisper(source, log_msg)

            AdminLogManager.add_entry("Userlevel edited", source, log_msg)
Ejemplo n.º 7
0
    def remove_timeout(self, bot, message, source, **rest):
        if not message:
            bot.whisper(source, "Invalid usage. !removetimeout user")
            return False

        with DBManager.create_session_scope() as db_session:
            targetUser = User.find_or_create_from_user_input(
                db_session, bot.twitch_helix_api,
                message.split()[0])
            remTimeout = db_session.query(LongTimeout).filter_by(
                user_id=targetUser.id).one_or_none()
            if not remTimeout:
                bot.whisper(
                    source,
                    f"User '{targetUser}' doesn't exist. See !listtimeouts")
                return False

            bot.whisper(
                source,
                f"{remTimeout.user}'s timeout of {(remTimeout.timeout_end - remTimeout.timeout_start).days} days has been cancelled.",
            )
            db_session.delete(remTimeout)