1
0
mirror of https://github.com/hack-chat/main.git synced 2024-03-22 13:20:33 +08:00
hack-chat-main/commands/channels/setmotd.js

142 lines
3.7 KiB
JavaScript
Raw Normal View History

2024-01-06 15:32:14 +08:00
/**
* @author Marzavec ( https://github.com/marzavec )
* @summary Change motd
* @version 1.0.0
* @description Update the channel motd to something new
* @module setmotd
*/
import {
isChannelModerator,
} from '../utility/_UAC.js';
import {
getChannelSettings,
updateChannelSettings,
} from '../utility/_Channels.js';
import {
2024-01-09 03:09:59 +08:00
Errors,
2024-01-06 15:32:14 +08:00
MaxMOTDLength,
} from '../utility/_Constants.js';
/**
* Executes when invoked by a remote client
* @param {Object} env - Environment object with references to core, server, socket & payload
* @public
* @return {void}
*/
export async function run({
core, server, socket, payload,
}) {
if (server.police.frisk(socket, 6)) {
return server.reply({
2024-01-09 03:09:59 +08:00
cmd: 'warn',
2024-01-06 15:32:14 +08:00
text: 'Issuing commands too quickly. Wait a moment before trying again.',
2024-01-09 03:09:59 +08:00
id: Errors.Global.RATELIMIT,
2024-01-06 15:32:14 +08:00
channel: socket.channel, // @todo Multichannel
}, socket);
}
// increase rate limit chance and ignore if not channel mod or better
if (!isChannelModerator(socket.level)) {
return server.police.frisk(socket, 10);
}
if (typeof payload.motd !== 'string' || payload.motd.length >= MaxMOTDLength) {
return server.reply({
2024-01-09 03:09:59 +08:00
cmd: 'warn',
2024-01-06 15:32:14 +08:00
text: `Failed to set motd: Invalid motd, max length: ${MaxMOTDLength}`,
2024-01-09 03:09:59 +08:00
MaxLength: MaxMOTDLength,
id: Errors.SetMOTD.TOO_LONG,
2024-01-06 15:32:14 +08:00
channel: socket.channel, // @todo Multichannel
}, socket);
}
const channelSettings = getChannelSettings(core.appConfig.data, socket.channel);
channelSettings.motd = payload.motd;
updateChannelSettings(core.appConfig.data, socket.channel, channelSettings);
server.broadcast({
2024-01-09 03:09:59 +08:00
cmd: 'info', // @todo Add numeric info code as `id`
2024-01-06 15:32:14 +08:00
text: `MOTD changed by [${socket.trip}]${socket.nick}, new motd:`,
channel: socket.channel, // @todo Multichannel
}, { channel: socket.channel });
server.broadcast({
2024-01-09 03:09:59 +08:00
cmd: 'info', // @todo Add numeric info code as `id`
2024-01-06 15:32:14 +08:00
text: channelSettings.motd,
channel: socket.channel, // @todo Multichannel
}, { channel: socket.channel });
return true;
}
/**
* Automatically executes once after server is ready to register this modules hooks
* @param {Object} server - Reference to server environment object
* @public
* @return {void}
*/
export function initHooks(server) {
server.registerHook('in', 'chat', this.chatCheck.bind(this), 29);
}
/**
* Executes every time an incoming chat command is invoked
* @param {Object} env - Environment object with references to core, server, socket & payload
* @public
* @return {(Object|boolean|string)} Object = same/altered payload,
* false = suppress action,
* string = error
*/
export function chatCheck({
core, server, socket, payload,
}) {
if (typeof payload.text !== 'string') {
return false;
}
if (payload.text.startsWith('/setmotd')) {
this.run({
core,
server,
socket,
payload: {
cmd: 'setmotd',
motd: payload.text.substring(8).trim(),
},
});
return false;
}
return payload;
}
/**
* The following payload properties are required to invoke this module:
* "motd"
* @public
* @typedef {Array} setmotd/requiredData
*/
export const requiredData = ['motd'];
/**
* Module meta information
* @public
* @typedef {Object} setmotd/info
* @property {string} name - Module command name
* @property {string} category - Module category name
* @property {string} description - Information about module
* @property {string} usage - Information about module usage
*/
export const info = {
name: 'setmotd',
category: 'channels',
description: 'Update the channel motd to something new',
usage: `
API: { cmd: 'setmotd', motd: '[new motd]' }
Text: /setmotd <new motd>`,
};