SquadJS/plugins/discord-rcon/index.js

60 lines
1.8 KiB
JavaScript
Raw Normal View History

export default {
name: 'discord-rcon',
2020-08-22 08:32:39 -05:00
description:
'The `discord-rcon` plugin allows a specified Discord channel to be used as a RCON console to run RCON commands.',
2020-08-22 08:32:39 -05:00
defaultDisabled: false,
optionsSpec: {
discordClient: {
type: 'DiscordConnector',
required: true,
default: 'discord',
description: 'The name of the Discord Connector to use.'
},
channelID: {
type: 'Discord Channel ID',
required: true,
default: 'Discord Channel ID',
description: 'The ID of the channel you wish to turn into a RCON console.'
},
prependAdminNameInBroadcast: {
type: 'Boolean',
required: false,
default: false,
description: "Prepend the admin's name when he makes an announcement."
}
},
init: async (server, options) => {
options.discordClient.on('message', async (message) => {
if (message.author.bot || message.channel.id !== options.channelID) return;
let command = message.content;
if (options.prependAdminNameInBroadcast && command.toLowerCase().startsWith('adminbroadcast'))
command = command.replace(
/^AdminBroadcast /i,
`AdminBroadcast ${message.member.displayName}: `
);
const response = await server.rcon.execute(command);
const responseMessages = [''];
for (const line of response.split('\n')) {
if (responseMessages[responseMessages.length - 1].length + line.length > 1994) {
responseMessages.push(line);
} else {
responseMessages[responseMessages.length - 1] = `${
responseMessages[responseMessages.length - 1]
}\n${line}`;
}
}
for (const responseMessage of responseMessages) {
await message.channel.send(`\`\`\`${responseMessage}\`\`\``);
}
});
}
};