twitch2ma
Version:
Twitch chat bot that runs commands on the MA GrandMA2 using Telnet.
115 lines • 3.84 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SACNConfig = exports.SACNLock = exports.Parameter = exports.Command = exports.TwitchConfig = exports.MaConfig = exports.Config = exports.ConfigError = void 0;
const Ajv = require("ajv");
const configSchema = require("../resources/config.schema.json");
const _ = require("lodash");
class ConfigError extends Error {
constructor(message) {
super(message);
Object.setPrototypeOf(this, ConfigError.prototype);
this.name = ConfigError.name;
}
}
exports.ConfigError = ConfigError;
class Config {
constructor(config) {
let ajv = new Ajv({
allErrors: true,
useDefaults: true
});
ajv.validate(configSchema, config);
if (_.isArray(ajv.errors)) {
throw new ConfigError(`Config file is invalid: ${ajv.errorsText(ajv.errors, { dataVar: "config" })}!`);
}
this.timeout = config.timeout;
this.ma = new MaConfig(config.ma);
this.twitch = new TwitchConfig(config.twitch);
this.sacn = new SACNConfig(config.sacn);
this.commands = new Array();
for (const command of config.commands) {
this.commands.push(new Command(command));
}
this.availableCommands = _.map(this.commands, command => `!${command.chatCommand}`).join(", ");
this.commandMap = _.zipObject(_.map(this.commands, command => command.chatCommand), this.commands);
}
getCommand(chatCommand) {
return _.get(this.commandMap, chatCommand);
}
}
exports.Config = Config;
class MaConfig {
constructor(maConfig) {
this.host = maConfig.host;
this.user = maConfig.user;
this.password = maConfig.password;
}
}
exports.MaConfig = MaConfig;
class TwitchConfig {
constructor(twitchConfig) {
this.clientId = twitchConfig.clientId;
this.accessToken = twitchConfig.accessToken;
this.channel = twitchConfig.channel;
}
}
exports.TwitchConfig = TwitchConfig;
class Command {
constructor(command) {
this.chatCommand = command.chatCommand;
this.consoleCommand = command.consoleCommand;
this.message = command.message;
this.help = command.help;
if (command.sacn) {
this.sacn = new SACNLock(command.sacn);
}
this.parameters = new Array();
if (_.isArray(command.parameters)) {
for (const parameter of command.parameters) {
this.parameters.push(new Parameter(parameter));
}
this.parameterMap = _.zipObject(_.map(this.parameters, parameter => parameter.parameter), this.parameters);
this.availableParameters = _.map(this.parameters, parameter => parameter.parameter).join(", ");
}
else {
this.parameterMap = {};
}
}
getChatCommand() {
return this.chatCommand;
}
getParameter(parameter) {
return _.get(this.parameterMap, parameter);
}
}
exports.Command = Command;
class Parameter {
constructor(parameter) {
this.parameter = parameter.parameter;
this.consoleCommand = parameter.consoleCommand;
this.message = parameter.message;
if (parameter.sacn) {
this.sacn = new SACNLock(parameter.sacn);
}
}
getChatCommand() {
return this.parameter;
}
}
exports.Parameter = Parameter;
class SACNLock {
constructor(sacn) {
this.universe = sacn.universe;
this.channel = sacn.channel;
}
}
exports.SACNLock = SACNLock;
class SACNConfig {
constructor(sacn) {
this.lockMessage = sacn.lockMessage;
this.interface = sacn.interface;
this.timeout = sacn.timeout;
}
}
exports.SACNConfig = SACNConfig;
//# sourceMappingURL=Config.js.map