@twitchfy/chatbot
Version:
A powerful node module to make your own Twitch ChatBot
86 lines (85 loc) • 2.86 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChannelProfile = void 0;
const eventsub_1 = require("@twitchfy/eventsub");
const Base_1 = require("./Base");
const Channel_1 = require("./Channel");
/**
* Represents a channel profile created when the chatbot joins to a channel.
*/
class ChannelProfile extends Base_1.Base {
/**
* The Id of the channel.
*/
id;
/**
* The events from the channel that the chatbot is subscribed to.
*/
events;
/**
* Creates a new instance of the channel profile.
* @param chatbot The current instance of the chatbot.
* @param data The data of the channel profile.
*/
constructor(chatbot, data) {
super(chatbot);
this.id = data.id;
this.events = data.events;
}
/**
* Adds an event or events to the channel profile and listen to it. If the event is already added, it does nothing.
* @param event The event or events to add.
* @returns
*/
async addEvent(event) {
if (Array.isArray(event)) {
return await this.addMassEvents(event);
}
else {
if (this.hasEvent(event))
return;
this.events.push(event);
await this.chatbot.eventsub.subscribe({ type: eventsub_1.SubscriptionTypes[event], options: { broadcaster_user_id: this.id, moderator_user_id: this.chatbot.userId, user_id: this.chatbot.userId } });
}
}
/**
* Removes an event from the channel profile and stop listening to it. If the event is not added, it does nothing.
* @param event The event to remove.
* @returns
*/
async removeEvent(event) {
if (!this.hasEvent(event))
return;
this.events.splice(this.events.indexOf(event), 1);
await this.chatbot.eventsub.subscriptions.exist(eventsub_1.SubscriptionTypes[event], { broadcaster_user_id: this.id, moderator_user_id: this.chatbot.userId, user_id: this.chatbot.userId })?.delete();
}
/**
* Checks if one event is being listened.
* @param event The event to check.
* @returns
*/
hasEvent(event) {
return this.events.includes(event);
}
/**
* Fetches the current channel of the profile from the API.
* @returns The fetched channel from the API.
*/
async fetch() {
return new Channel_1.Channel(this.chatbot, await this.chatbot.helixClient.getChannel(this.id));
}
/**
* Adds events in mass to the channel profile.
* @param events The events to add.
* @internal
* @returns
*/
async addMassEvents(events) {
for (const event of events) {
if (this.hasEvent(event))
continue;
await this.addEvent(event);
}
}
}
exports.ChannelProfile = ChannelProfile;