UNPKG

@twitchfy/chatbot

Version:

A powerful node module to make your own Twitch ChatBot

46 lines (45 loc) 2.13 kB
"use strict"; /* eslint-disable @typescript-eslint/ban-types */ /* eslint-disable @typescript-eslint/ban-ts-comment */ Object.defineProperty(exports, "__esModule", { value: true }); exports.handleOnMessage = void 0; const resolvePermissions_1 = require("./resolvePermissions"); const optionsParser_1 = require("./optionsParser"); const structures_1 = require("../structures"); /** * Handle the message received from the chat. * @param this The current instance of the chatbot. * @param data The message received from the chat. * @internal */ async function handleOnMessage(data) { if (this.userId === data.chatter.id) return; const content = data.message.content; // @ts-expect-error const prefix = this.__prefix(new structures_1.TwitchContext(this, { ...data })).find((x) => content.startsWith(x)); if (!prefix) return; const mentionAtStart = data.message.reply && data.message.fragments[0].type === 'mention' ? data.message.fragments[0] : null; const args = content.slice(mentionAtStart ? mentionAtStart.content.length + 1 + prefix.length : prefix.length).replace('\u{E0000}', ' ').trim().split(/ +/g); const commandName = args.shift(); const command = this.commands.get(commandName); if (!command) return; const options = (0, optionsParser_1.optionsParser)(this, args.join(' '), command.options || {}, data, this.optionOperator); const commandOptions = command.options || {}; Object.keys(options).filter((x) => !commandOptions[x]).map((x) => delete options[x]); // @ts-expect-error const context = new structures_1.TwitchContext(this, { ...data, prefix, commandName: commandName, options }); if (command.permissions) { const result = await (0, resolvePermissions_1.resolvePermissions)(command.permissions, context); if (!result.passed && command.onPermissionFallback) { return await command.onPermissionFallback(context, result.requiredPerms); } else if (!result.passed) { return; } } await command.run(context); } exports.handleOnMessage = handleOnMessage;