2020-07-06 19:42:50 -05:00
|
|
|
import { COPYRIGHT_MESSAGE } from 'core/config';
|
|
|
|
import { RCON_CHAT_MESSAGE } from 'squad-server/events/rcon';
|
|
|
|
|
|
|
|
export default async function(server, discordClient, channelID, options = {}) {
|
|
|
|
if (!server) {
|
|
|
|
throw new Error('DiscordChatAdminRequest must be provided with a reference to the server.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!discordClient) {
|
|
|
|
throw new Error('DiscordChatAdminRequest must be provided with a Discord.js client.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!channelID) {
|
|
|
|
throw new Error('DiscordChatAdminRequest must be provided with a channel ID.');
|
|
|
|
}
|
|
|
|
|
|
|
|
options = {
|
2020-07-08 16:04:10 -05:00
|
|
|
color: 16761867,
|
2020-07-14 13:49:47 -05:00
|
|
|
ignoreChats: [],
|
|
|
|
adminPrefix: '!admin',
|
|
|
|
pingGroups: [],
|
|
|
|
pingDelay: 60 * 1000,
|
2020-07-06 19:42:50 -05:00
|
|
|
...options
|
|
|
|
};
|
|
|
|
|
2020-07-14 13:49:47 -05:00
|
|
|
let lastPing = null;
|
|
|
|
|
2020-07-06 19:42:50 -05:00
|
|
|
const channel = await discordClient.channels.fetch(channelID);
|
|
|
|
|
|
|
|
server.on(RCON_CHAT_MESSAGE, async info => {
|
2020-07-14 13:49:47 -05:00
|
|
|
if (options.ignoreChats.includes(info.chat)) return;
|
|
|
|
if (!info.message.startsWith(`${options.adminPrefix}`)) return;
|
2020-07-06 19:42:50 -05:00
|
|
|
|
|
|
|
const playerInfo = await server.getPlayerBySteamID(info.steamID);
|
2020-07-14 13:49:47 -05:00
|
|
|
const trimmedMessage = info.message.replace(options.adminPrefix, '').trim();
|
2020-07-06 19:42:50 -05:00
|
|
|
|
2020-07-08 16:04:10 -05:00
|
|
|
if (trimmedMessage.length === 0) {
|
2020-07-14 13:36:17 -05:00
|
|
|
await server.rcon.warn(
|
|
|
|
info.steamID,
|
|
|
|
`Please specify what you would like help with when requesting an admin.`
|
|
|
|
);
|
2020-07-08 16:04:10 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-14 13:36:17 -05:00
|
|
|
const message = {
|
2020-07-06 19:42:50 -05:00
|
|
|
embed: {
|
|
|
|
title: `${playerInfo.name} has requested admin support!`,
|
|
|
|
color: options.color,
|
|
|
|
fields: [
|
|
|
|
{
|
|
|
|
name: 'Player',
|
|
|
|
value: playerInfo.name,
|
|
|
|
inline: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'SteamID',
|
|
|
|
value: `[${playerInfo.steamID}](https://steamcommunity.com/profiles/${info.steamID})`,
|
|
|
|
inline: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Team & Squad',
|
|
|
|
value: `Team: ${playerInfo.teamID}, Squad: ${playerInfo.squadID || 'Unassigned'}`
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Message',
|
|
|
|
value: trimmedMessage
|
|
|
|
}
|
|
|
|
],
|
|
|
|
timestamp: info.time.toISOString(),
|
|
|
|
footer: {
|
|
|
|
text: COPYRIGHT_MESSAGE
|
|
|
|
}
|
|
|
|
}
|
2020-07-14 13:36:17 -05:00
|
|
|
};
|
|
|
|
|
2020-08-01 05:40:16 -05:00
|
|
|
if (
|
|
|
|
options.pingGroups.length > 0 &&
|
|
|
|
(lastPing === null || Date.now() - options.pingDelay > lastPing)
|
|
|
|
) {
|
2020-07-14 13:49:47 -05:00
|
|
|
message.content = options.pingGroups.map(groupID => `<@&${groupID}>`).join(' ');
|
2020-07-14 13:36:17 -05:00
|
|
|
lastPing = Date.now();
|
|
|
|
}
|
|
|
|
|
|
|
|
channel.send(message);
|
|
|
|
|
|
|
|
await server.rcon.warn(
|
|
|
|
info.steamID,
|
|
|
|
`An admin has been notified, please wait for us to get back to you.`
|
|
|
|
);
|
2020-07-06 19:42:50 -05:00
|
|
|
});
|
|
|
|
}
|