twitch-core
Version:
Twitch bot command client
184 lines (183 loc) • 7.02 kB
JavaScript
;
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());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
const TextCommand_1 = require("../TextCommand");
const TwitchChatCommand_1 = require("../TwitchChatCommand");
class TextCommandsManager extends TwitchChatCommand_1.TwitchChatCommand {
constructor(client, options) {
super(client, Object.assign({ name: 'txt', userlevel: 'regular' }, options));
this.provider = client.provider
.get('text-commands');
}
prepareRun(msg, args) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.provider) {
return msg.reply('Text command provider text-commands.json is not registered!');
}
if (args.length > 1) {
const action = args[0];
args.shift();
const command = args[0];
args.shift();
const opts = args.join(' ');
switch (action) {
case 'set':
this.set(msg, command, opts);
break;
case 'get':
this.get(msg, command);
break;
case 'unset':
this.unset(msg, command);
break;
case 'access':
this.updateUserLevel(msg, command, opts);
break;
case 'type':
this.updateMessageType(msg, command, opts);
break;
case 'rename':
this.updateCommandName(msg, command, opts);
break;
default:
msg.reply(`Action '${action}' is not found!`);
}
}
else {
msg.reply('Manage command is not enough arguments');
}
});
}
set(msg, name, text) {
if (!text.length) {
return msg.reply('Text argument required');
}
const command = this.client.findCommand({ command: name });
const options = {
name,
text,
userlevel: 'everyone',
messageType: 'reply'
};
const findInProvider = this.provider
.get('commands')
.find({ name });
if (findInProvider.value()) {
this.provider
.get('commands')
.find({ name })
.assign({ text })
.write();
}
else {
this.provider
.get('commands')
.push(options)
.write();
}
if (command) {
command.options = Object.assign(Object.assign({}, command.options), { name,
text });
}
else {
this.client.commands.push(new TextCommand_1.TextCommand(this.client, {
name,
text,
userlevel: 'everyone',
messageType: 'reply'
}));
}
msg.reply(`Command created → ${this.client.options.prefix}${name} — ${text}`);
}
get(msg, name) {
const command = this.provider
.get('commands')
.find({ name })
.value();
if (command !== undefined) {
msg.reply(`Options → text: ${command.text}, userlevel: ${command.userlevel}, messageType: ${command.messageType}`);
}
else {
msg.reply(`Command '${name}' is not found`);
}
}
unset(msg, name) {
const command = this.provider
.get('commands')
.find({ name })
.value();
if (command !== undefined) {
this.client.commands =
this.client.commands
.filter(command => command.options.name !== name);
this.provider
.get('commands')
.remove({ name })
.write();
msg.reply(`Command '${command.name}' deleted`);
}
else {
msg.reply(`Command '${name}' is not found`);
}
}
updateUserLevel(msg, name, userlevel) {
const UserLevels = Object.values(TwitchChatCommand_1.UserLevel);
if (UserLevels.includes(userlevel)) {
this.updateCommandOptions(msg, name, { userlevel });
}
else {
msg.reply(`Available userlevels: ${UserLevels.join(', ')}`);
}
}
updateMessageType(msg, name, messageType) {
const MessageTypes = Object.values(TwitchChatCommand_1.MessageType);
if (MessageTypes.includes(messageType)) {
this.updateCommandOptions(msg, name, { messageType });
}
else {
msg.reply(`Available message types: ${MessageTypes.join(', ')}`);
}
}
updateCommandOptions(msg, name, _a) {
var options = __rest(_a, []);
this.provider
.get('commands')
.find({ name })
.assign(options)
.write();
this.client.commands.forEach(command => {
if (command.options.name === name) {
command.options = Object.assign(Object.assign({}, command.options), options);
}
});
msg.reply(`Command '${name}' updated!`);
}
updateCommandName(msg, command, opts) {
if (opts.length > 0) {
this.updateCommandOptions(msg, command, { name: opts });
}
else {
msg.reply('Message text required');
}
}
}
exports.default = TextCommandsManager;