UNPKG

twitch-core

Version:
143 lines (142 loc) 5.66 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MessageType = exports.UserLevel = exports.TwitchChatCommand = void 0; var UserLevel; (function (UserLevel) { UserLevel["vip"] = "vip"; UserLevel["everyone"] = "everyone"; UserLevel["regular"] = "regular"; UserLevel["subscriber"] = "subscriber"; UserLevel["moderator"] = "moderator"; UserLevel["broadcaster"] = "broadcaster"; })(UserLevel || (UserLevel = {})); exports.UserLevel = UserLevel; var MessageType; (function (MessageType) { MessageType["reply"] = "reply"; MessageType["actionReply"] = "actionReply"; MessageType["say"] = "say"; MessageType["actionSay"] = "actionSay"; })(MessageType || (MessageType = {})); exports.MessageType = MessageType; class TwitchChatCommand { constructor(client, options) { this.options = options; this.client = client; } /** * Method called when executeCommand * * @param msg * @param chatter */ execute(msg) { return __awaiter(this, void 0, void 0, function* () { }); } /** * Method called when command is executed * TODO: Fix types * * @param msg * @param parameters */ run(msg, parameters) { return __awaiter(this, void 0, void 0, function* () { }); } /** * Prepare the command to be executed * * @param msg * @param parameters */ prepareRun(msg, parameters) { return __awaiter(this, void 0, void 0, function* () { const namedParameters = {}; if (this.options.args && this.options.args.length > 0) { for (let i = 0; i < this.options.args.length; i++) { if (parameters[i]) { if (typeof this.options.args[i].type === 'function') { namedParameters[this.options.args[i].name] = this.options.args[i].type(parameters[i]); } else { namedParameters[this.options.args[i].name] = parameters[i]; } } else { if (this.options.args[i].defaultValue) { namedParameters[this.options.args[i].name] = this.options.args[i].defaultValue; } else { namedParameters[this.options.args[i].name] = null; } } } } yield this.run(msg, namedParameters); }); } /** * Pre validation before to known if can execute command * * @param msg */ preValidate(msg) { var _a; if (msg.messageType !== 'whisper' && this.options.privmsgOnly) { return 'This command is available only via private message'; } if (this.options.botChannelOnly) { if (msg.channel.name !== this.client.getUsername()) { return 'This command can be executed only in the bot channel. Please head to https://twitch.tv/' + this.client.getUsername(); } } if (this.options.userlevel === UserLevel.everyone) { return true; } let validationPassed = false; if (msg.author.isBroadcaster) { validationPassed = true; } if (msg.author.isModerator) { validationPassed = true; } if (this.options.userlevel === UserLevel.regular) { if (!validationPassed && ((_a = this.client.options) === null || _a === void 0 ? void 0 : _a.botOwners.length) > 0 && !this.client.options.botOwners.includes(msg.author.username)) { return 'This command can be executed only from bot owners'; } } if (this.options.userlevel === UserLevel.subscriber) { if (!validationPassed && !msg.author.isSubscriber) { return 'This command can be executed only from the subscribers'; } } if (this.options.userlevel === UserLevel.vip) { if (!validationPassed && !msg.author.isVip) { return 'This command can be executed only from the vips'; } } if (this.options.userlevel === UserLevel.moderator) { if (!validationPassed) { return 'This command can be executed only from the broadcaster'; } } if (this.options.userlevel === UserLevel.broadcaster) { if (!msg.author.isBroadcaster) { return 'This command can be executed only from a mod or the broadcaster'; } } return true; } } exports.TwitchChatCommand = TwitchChatCommand;