@twitchfy/chatbot
Version:
A powerful node module to make your own Twitch ChatBot
67 lines (66 loc) • 2.28 kB
JavaScript
;
/* eslint-disable @typescript-eslint/ban-ts-comment */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSubscriptonKey = exports.handleEvent = void 0;
const eventsub_1 = require("@twitchfy/eventsub");
const structures_1 = require("../structures");
/**
* Handle the event received from the EventSub.
* @param this The current instance of the chatbot.
* @param message The message received from the EventSub.
* @param subscription The subscription received from EventSub.
* @returns
* @internal
*/
async function handleEvent(message, subscription) {
const key = getSubscriptonKey(subscription.type);
if (!key)
return;
const event = this.events.get(key);
if (!event)
return;
let data;
switch (key) {
case 'ChannelFollow':
data = new structures_1.ChannelFollow(this, message);
break;
case 'ChannelChatClear':
data = new structures_1.ChannelChatClear(this, message);
break;
case 'ChannelUpdate':
data = new structures_1.ChannelUpdate(this, message);
break;
case 'StreamOnline':
data = new structures_1.StreamOnline(this, message);
break;
case 'ChannelChatMessage':
{
const typedMessage = message;
data = new structures_1.Message(this, message, new structures_1.ChatRoom(this, { broadcaster_id: typedMessage.broadcaster.id, broadcaster_login: typedMessage.broadcaster.login, broadcaster_name: typedMessage.broadcaster.displayName }));
}
break;
case 'StreamOffline':
data = new structures_1.StreamOffline(this, message);
break;
default: return;
}
// @ts-expect-error
return await event.run(this, data);
}
exports.handleEvent = handleEvent;
/**
* Get the key of the subscription type.
* @param value The value of the subscription.
* @returns
* @internal
*/
function getSubscriptonKey(value) {
for (const key in eventsub_1.SubscriptionTypes) {
// @ts-expect-error
if (eventsub_1.SubscriptionTypes[key] === value) {
return key;
}
}
return undefined;
}
exports.getSubscriptonKey = getSubscriptonKey;