UNPKG

twitch-core

Version:
51 lines (50 loc) 2.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CommandRoutes = void 0; const BaseRoute_1 = require("./BaseRoute"); class CommandRoutes extends BaseRoute_1.BaseRoute { constructor(client) { super(client); this.commands = this.client.provider.get('commands'); this.router.get('', this.getCommands.bind(this)); this.router.get('/:name', this.getCommandByName.bind(this)); this.router.put('/:name', this.updateCommand.bind(this)); } getCommands(req, res) { const commands = this.commands.getState(); return this.response(res, 200, { commands }); } getCommandByName(req, res) { const { name } = req.params; const command = this.commands.find({ name }).value(); if (command) { return this.response(res, 200, { command }); } else { return this.response(res, 404, `Command '${name}' not found`); } } updateCommand(req, res, next) { try { const body = req.body; const { name } = req.params; if (!body || !name) this.response(res, 400, 'Missing request body!'); const command = this.commands.find({ name }); if (!command.value()) return this.response(res, 404, `Command '${name}' is not found!`); const commandOptions = command.assign(body).write(); this.client.commands.forEach(command => { if (command.options.name === name) { command.options = Object.assign(Object.assign({}, command.options), commandOptions); this.client.logger.info(`Command '${body.name}' updated!`); } }); return this.response(res, 200, { command: commandOptions }); } catch (err) { next(err); } } } exports.CommandRoutes = CommandRoutes;