Setup

This guide will help you get started with the setup of the application client.

To use the client, you need to import the Client class from the vtubers package. The Client class is a WebSocket client that connects to the API and listens for events.

client.ts
import { Client } from "vtubers";

Initializing the Client

Create a new instance of the Client class, passing an array of Intents to specify the events you're interested in. For example:

client.ts
const client = new Client({
  intents: [
    Intents.VideoUpload,
    Intents.UserBans,
    Intents.UserEdit,
    Intents.SecurityReports,
    Intents.UserAuthorization,
  ],
});

The available intents help filter which events you want to receive. The full list can be found in the Intents enum.

Listening for Events

The client emits various events that you can handle with the on method. Below are some key events and how to handle them:

client.ts
// Triggered upon successful connection to the server
client.on("connected", (clientData) => {
  console.log(`Connected as ${clientData.name}`);
});

// Handles errors during the connection lifecycle
client.on("error", (error) => {
  console.error("Error occurred:", error.message);
});

// Fired when a new video is uploaded
client.on("videoUpload", (video) => {
  console.log(`New video uploaded: ${video.title} by ${video.author}`);
});

// Fired when a video is deleted
client.on("videoDelete", (video) => {
  console.log(`Video ${video.title} by ${video.author} has been deleted.`);
});

// Fired when a user is banned
client.on("userBanned", (user) => {
  console.log(`User ${user.username} has been banned.`);
});

// Fired when a user is unbanned
client.on("userUnbanned", (user) => {
  console.log(`User ${user.username} has been unbanned.`);
});

// Fired when a user's information is updated
client.on("userEdit", (user) => {
  console.log(`User ${user.username} updated. Changes: ${JSON.stringify(user.updated_fields)}`);
});

// Fired when a security report is published
client.on("securityReportPublished", (report) => {
  console.log(`New security report: ${report.title}`);
});

// Fired when a security report is deleted
client.on("securityReportDeleted", (report) => {
  console.log(`Security report ${report.title} has been deleted.`);
});

// Fired when a user is authorized with your application
client.on("userAuthed", (user) => {
  console.log(`User ${user.username} has been authorized.`);
});

Authenticating with the Server

To authenticate, call the login method with your application token, which you can retrieve from the Developer Dashboard.

client.ts
const token = process.env.VTUBERSTV_APP_TOKEN || "<YOUR_APP_TOKEN>";

client.login(token).catch((error) => {
  console.error("Failed to log in:", error.message);
});

Replace <YOUR_APP_TOKEN> with your actual application token.

References