DiscordRcon plugin

This commit is contained in:
Thomas Smyth 2020-10-05 23:10:40 +01:00
parent 122a4a5ec0
commit 47b8b3321a
4 changed files with 105 additions and 3 deletions

View File

@ -207,6 +207,29 @@ The following is a list of plugins built into SquadJS, you can click their title
]</code></pre>
</details>
<details>
<summary>DiscordRcon</summary>
<h2>DiscordRcon</h2>
<p>The <code>DiscordRcon</code> plugin allows a specified Discord channel to be used as a RCON console to run RCON commands</p>
<h3>Options</h3>
<h4>discordClient (Required)</h4>
<h6>Description</h6>
<p>Discord connector name.</p>
<h6>Default</h6>
<pre><code>discord</code></pre>
<h4>channelID (Required)</h4>
<h6>Description</h6>
<p>ID of channel to turn into RCON console.</p>
<h6>Default</h6>
<pre><code></code></pre><h6>Example</h6>
<pre><code>667741905228136459</code></pre>
<h4>prependAdminNameInBroadcast</h4>
<h6>Description</h6>
<p>Prepend admin names when making announcements.</p>
<h6>Default</h6>
<pre><code>false</code></pre>
</details>
<details>
<summary>IntervalledBroadcasts</summary>
<h2>IntervalledBroadcasts</h2>

View File

@ -74,9 +74,16 @@
]
},
{
"plugin": "IntervalledBroadcasts",
"plugin": "DiscordRcon",
"enabled": true,
"broadcasts": ["TEST TEST TEST"],
"discordClient": "discord",
"channelID": "",
"prependAdminNameInBroadcast": false
},
{
"plugin": "IntervalledBroadcasts",
"enabled": false,
"broadcasts": [],
"interval": 300000
},
{

View File

@ -0,0 +1,71 @@
import BasePlugin from './base-plugin.js';
export default class DiscordRcon extends BasePlugin {
static get description() {
return (
'The <code>DiscordRcon</code> plugin allows a specified Discord channel to be used as a RCON console to ' +
'run RCON commands'
);
}
static get defaultEnabled() {
return false;
}
static get optionsSpecification() {
return {
discordClient: {
required: true,
description: 'Discord connector name.',
connector: 'discord',
default: 'discord'
},
channelID: {
required: true,
description: 'ID of channel to turn into RCON console.',
default: '',
example: '667741905228136459'
},
prependAdminNameInBroadcast: {
required: false,
description: 'Prepend admin names when making announcements.',
default: false
}
};
}
constructor(server, options) {
super();
options.discordClient.on('message', async (message) => {
if (message.author.bot || message.channel.id !== options.channelID) return;
let command = message.content;
if (options.prependAdminNameInBroadcast && command.match(/^adminbroadcast/i))
command = command.replace(
/^AdminBroadcast /i,
`AdminBroadcast ${message.member.displayName}: `
);
for (const responseMessage of this.splitLongResponse(await server.rcon.execute(command)))
await message.channel.send(`\`\`\`${responseMessage}\`\`\``);
});
}
splitLongResponse(response) {
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}`;
}
}
return responseMessages;
}
}

View File

@ -1,8 +1,9 @@
import ChatCommands from './chat-commands.js';
import DiscordRcon from './discord-rcon.js';
import IntervalledBroadcasts from './intervalled-broadcasts.js';
import SeedingMode from './seeding-mode.js';
const plugins = [ChatCommands, IntervalledBroadcasts, SeedingMode];
const plugins = [ChatCommands, DiscordRcon, IntervalledBroadcasts, SeedingMode];
const pluginsByName = {};
for (const plugin of plugins) {