From 18ffdefffb13c3af89a454108e00784f4494ad6e Mon Sep 17 00:00:00 2001 From: Niels Simenon Date: Mon, 15 Nov 2021 20:44:34 +0100 Subject: [PATCH] Added say command with operator config. --- config/default.js | 3 ++- src/app.js | 4 ++-- src/games/say.js | 23 +++++++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 src/games/say.js diff --git a/config/default.js b/config/default.js index 3c5fd83..a881fae 100644 --- a/config/default.js +++ b/config/default.js @@ -11,6 +11,7 @@ module.exports = { birthdate: new Date(1952, 11, 10), avatar: 'https://i.imgur.com/IZwrjjG.png', }, + operators: ['admin'], uniqueUsername: true, socket: 'ws://127.0.0.1:3000/socket', api: 'http://127.0.0.1:3000/api', @@ -20,7 +21,7 @@ module.exports = { color: 'var(--message-56)', }, channels: ['GamesNight'], - games: ['mash', 'trivia', 'ping', 'duck'], + games: ['mash', 'trivia', 'duck', 'ping', 'say'], trivia: { mode: 'first', // first or timeout rounds: 10, diff --git a/src/app.js b/src/app.js index e54d722..a353d8b 100644 --- a/src/app.js +++ b/src/app.js @@ -209,10 +209,10 @@ function getGames(bot) { const games = config.games.reduce((acc, key) => { const game = require(`./games/${key.game || key}`); // eslint-disable-line global-require, import/no-dynamic-require - const sendMessage = (body, roomId) => { + const sendMessage = (body, roomId, options) => { bot.socket.transmit('message', { roomId, - body: `[${game.name || key}] ${body}`, + body: options?.label === false ? body : `[${game.name || key}] ${body}`, style: config.style, }); }; diff --git a/src/games/say.js b/src/games/say.js new file mode 100644 index 0000000..dab728e --- /dev/null +++ b/src/games/say.js @@ -0,0 +1,23 @@ +'use strict'; + +const config = require('config'); + +function onCommand([rawRoomName, ...words], context) { + if (!config.operators?.includes(context.user.username)) { + return; + } + + const roomName = rawRoomName.replace(/#+/, ''); + const message = words.join(' '); + const room = Object.values(context.bot.rooms).find((botRoom) => botRoom.name === roomName); + + if (!room) { + return; + } + + context.sendMessage(message, room.id, { label: false }); +} + +module.exports = { + onCommand, +};