💬

Example plugin

// @ts-check
/**
 * Minimal Night plugin: thanks every follower who has no conversation yet.
 * Doubles as the type-check fixture for the generated SDK types.
 */
const { Process, Stats, Manyvids, SdkError, utils } = loader.SDK;

export default class ExamplePlugin extends Process {
    /** @type {import("../index.js").PluginManifest} */
    static manifest = {
        name: "example_plugin",
        title: "Example plugin",
        short_description: "Thanks new followers",
        long_description: "Sends every new follower a thank-you message.",
        author: "you",
        version: "1.0.0",
        sdk_version: "0.6.0",
        module: "manyvids",
    };

    /**
     * @param {string} id
     * @param {import("../index.js").Account} account
     * @param {string} access_token_id
     */
    constructor(id, account, access_token_id) {
        super(id, account, access_token_id);
        this.output_log = new Stats(this.id, "output");
        // Every call made through `mv` runs as this process's account.
        this.mv = Manyvids.bind(this);
        this.before_start(() => this.output_log.add({ event: "started" }));
    }

    async exec() {
        const me = await this.mv.Account.get_me();
        if (!me.isLoggedIn) throw "Account is logged out";

        const creator = new Manyvids.Creator({ legacyId: me.user.userId }, this);
        while (!creator.is_finished && this.state !== "stopped") {
            for (const raw of await creator.next_followers({ type_filter: "MEMBER" })) {
                const follower = new Manyvids.User(raw, this);
                try {
                    await follower.init();
                    if (follower.has_conversation) continue;
                    await follower.send_message({ message: `Thanks for the follow, ${follower.display_name}!` });
                    this.output_log.add({ event: "thanked", main_data: follower.handle });
                } catch (e) {
                    if (e instanceof SdkError && e.code === "REJECTED") {
                        this.output_log.add({ event: "skipped", main_data: e.message });
                        continue;
                    }
                  throw e;
                }
              await utils.sleep(5000);
            }
        }

        /** @type {import("../index.js").Page<unknown>} */
        const following = await this.mv.Users.list_following({ user_id: me.user.userId, type_filter: "CREATOR" });
        this.output_log.add({ event: "following_page", main_data: following.items.length });

        // @ts-expect-error — user_id is required
        await this.mv.Users.get_profile({});
    }
}