@twitchfy/chatbot
Version:
A powerful node module to make your own Twitch ChatBot
84 lines (83 loc) • 3.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChannelManager = void 0;
const eventsub_1 = require("@twitchfy/eventsub");
const Base_1 = require("../Base");
const ChannelProfile_1 = require("../ChannelProfile");
const Channel_1 = require("../Channel");
const util_1 = require("../../util");
/**
* Represents the chatbot channel manager used to join to channels.
*/
class ChannelManager extends Base_1.Base {
/**
* Creates a new instance of the channel manager.
* @param chatbot The current instance of the chatbot.
*/
constructor(chatbot) {
super(chatbot);
}
/**
* Join a channel and listen to messages.
* @param id The id of the channel to join.
* @param events The EventSub events you will listen to. See {@link ChannelEvents}.
* @returns A class representation of the channel profile which contains the events you are subscribed with. See {@link ChannelProfile}.
*/
async join(id, events) {
const existingProfile = this.chatbot.profiles.get(id);
if (existingProfile)
return existingProfile;
const profile = new ChannelProfile_1.ChannelProfile(this.chatbot, { id, events: events || ['ChannelChatMessage'] });
const subscription = await this.chatbot.eventsub.subscribe({ type: eventsub_1.SubscriptionTypes.ChannelChatMessage, options: { broadcaster_user_id: id, user_id: this.chatbot.userId } });
const fn = util_1.handleOnMessage.bind(this.chatbot);
subscription.onMessage(fn);
if (events) {
const parsedEvents = events.reduce((acc, event) => {
if (acc.includes(event))
return acc;
acc.push(event);
return acc;
}, []);
for (const event of parsedEvents) {
const type = eventsub_1.SubscriptionTypes[event];
if (!type)
continue;
await this.chatbot.eventsub.subscribe({ type, options: { moderator_user_id: this.chatbot.userId, user_id: this.chatbot.userId, broadcaster_user_id: id } });
}
}
return profile;
}
/**
* Leave a channel. You will no longer listen to messages and the other events you've subscribed.
* @param id The id of the channel to leave.
* @returns
*/
async leave(id) {
const subscription = this.chatbot.eventsub.subscriptions.exist(eventsub_1.SubscriptionTypes.ChannelChatMessage, { broadcaster_user_id: id, user_id: this.chatbot.userId });
if (!subscription)
return;
await subscription.delete();
const profile = this.chatbot.profiles.get(id);
if (!profile)
return;
for (const event of profile.events) {
const type = eventsub_1.SubscriptionTypes[event];
if (!type)
continue;
const subscription = this.chatbot.eventsub.subscriptions.exist(type, { broadcaster_user_id: id, user_id: this.chatbot.userId, moderator_user_id: this.chatbot.userId });
if (subscription)
await subscription.delete();
}
this.chatbot.profiles.delete(id);
return;
}
/**
* Fetches a channel by id.
* @param id The id of the channel to fetch.
* @returns A class representation of the channel. See {@link Channel}.
*/
async fetch(id) {
return new Channel_1.Channel(this.chatbot, await this.chatbot.helixClient.getChannel(id));
}
}
exports.ChannelManager = ChannelManager;