Skip to content

Commit

Permalink
Temporary solution to fcm notifications problem
Browse files Browse the repository at this point in the history
  • Loading branch information
alexemanuelol committed Sep 7, 2024
1 parent c30143c commit a5d6956
Show file tree
Hide file tree
Showing 16 changed files with 843 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ node_modules
.env
instances
credentials
authtokens
maps
logs
temp
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ RUN npm install
COPY . /app

VOLUME [ "/app/credentials" ]
VOLUME [ "/app/authtokens" ]
VOLUME [ "/app/instances" ]
VOLUME [ "/app/logs" ]
VOLUME [ "/app/maps" ]
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ services:
app:
volumes:
- ./credentials:/app/credentials
- ./authtokens:/app/authtokens
- ./instances:/app/instances
- ./logs:/app/logs
- ./maps:/app/maps
Expand Down
4 changes: 4 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ function createMissingDirectories() {
Fs.mkdirSync(Path.join(__dirname, 'credentials'));
}

if (!Fs.existsSync(Path.join(__dirname, 'authtokens'))) {
Fs.mkdirSync(Path.join(__dirname, 'authtokens'));
}

if (!Fs.existsSync(Path.join(__dirname, 'maps'))) {
Fs.mkdirSync(Path.join(__dirname, 'maps'));
}
Expand Down
286 changes: 286 additions & 0 deletions src/commands/authtoken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
/*
Copyright (C) 2024 Alexander Emanuelsson (alexemanuelol)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
https://github.com/alexemanuelol/rustplusplus
*/

const _ = require('lodash');
const Builder = require('@discordjs/builders');

const Config = require('../../config');
const DiscordEmbeds = require('../discordTools/discordEmbeds.js');
const DiscordMessages = require('../discordTools/discordMessages.js');
const DiscordTools = require('../discordTools/discordTools.js');
const InstanceUtils = require('../util/instanceUtils.js');
const AuthTokenListenerTemp = require('../util/AuthTokenListener.js');

module.exports = {
name: 'authtoken',

getData(client, guildId) {
return new Builder.SlashCommandBuilder()
.setName('authtoken')
.setDescription('Set/Remove Authentication Token.')
.addSubcommand(subcommand => subcommand
.setName('add')
.setDescription('Add Authentication Token.')
.addStringOption(option => option
.setName('token')
.setDescription('Authentication Token.')
.setRequired(true))
.addStringOption(option => option
.setName('steam_id')
.setDescription('Steam ID.')
.setRequired(true))
.addStringOption(option => option
.setName('issued_date')
.setDescription('Issued date of the Authentication Token.')
.setRequired(true))
.addStringOption(option => option
.setName('expire_date')
.setDescription('Expire date of the Authentication Token.')
.setRequired(true))
.addBooleanOption(option => option
.setName('hoster')
.setDescription('Host the bot')
.setRequired(false)))
.addSubcommand(subcommand => subcommand
.setName('remove')
.setDescription('Remove Authentication Token.')
.addStringOption(option => option
.setName('steam_id')
.setDescription('The SteamId of the Authentication Token to be removed.')
.setRequired(false)))
.addSubcommand(subcommand => subcommand
.setName('show')
.setDescription('Show the currently registered authentication token users.'))
.addSubcommand(subcommand => subcommand
.setName('set_hoster')
.setDescription('Set the main hoster.')
.addStringOption(option => option
.setName('steam_id')
.setDescription('The SteamId of the new hoster.')
.setRequired(false)));
},

async execute(client, interaction) {
const verifyId = Math.floor(100000 + Math.random() * 900000);
client.logInteraction(interaction, verifyId, 'slashCommand');

if (!await client.validatePermissions(interaction)) return;
await interaction.deferReply({ ephemeral: true });

switch (interaction.options.getSubcommand()) {
case 'add': {
addAuthToken(client, interaction, verifyId);
} break;

case 'remove': {
removeAuthToken(client, interaction, verifyId);
} break;

case 'show': {
showAuthTokenUsers(client, interaction, verifyId);
} break;

case 'set_hoster': {
setHoster(client, interaction, verifyId);
} break;

default: {
} break;
}
},
};

async function addAuthToken(client, interaction, verifyId) {
const guildId = interaction.guildId;
const authTokens = InstanceUtils.readAuthTokensFile(guildId);
const steamId = interaction.options.getString('steam_id');
const isHoster = interaction.options.getBoolean('host') || Object.keys(authTokens).length === 1;

if (Object.keys(authTokens) !== 1 && isHoster) {
if (Config.discord.needAdminPrivileges && !client.isAdministrator(interaction)) {
const str = client.intlGet(interaction.guildId, 'missingPermission');
client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str));
client.log(client.intlGet(null, 'warningCap'), str);
return;
}
}

if (steamId in authTokens) {
const str = `Authentication Token for steamId: ${steamId} is already registered.`;
await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str));
client.log(client.intlGet(null, 'warningCap'), str);
return;
}

authTokens[steamId] = new Object();
authTokens[steamId].auth_token = interaction.options.getString('token');
authTokens[steamId].issued_date = interaction.options.getString('issued_date');
authTokens[steamId].expire_date = interaction.options.getString('expire_date');
authTokens[steamId].discordUserId = interaction.member.user.id;

if (isHoster) authTokens.hoster = steamId;

InstanceUtils.writeAuthTokensFile(guildId, authTokens);

await AuthTokenListenerTemp.startNewAuthTokenListener(client, interaction.guildId, steamId);
if (!isHoster) {
const rustplus = client.rustplusInstances[guildId];
if (rustplus && rustplus.team.leaderSteamId === steamId) {
rustplus.updateLeaderRustPlusLiteInstance();
}
}

client.log(client.intlGet(null, 'infoCap'), client.intlGet(null, 'slashCommandValueChange', {
id: `${verifyId}`,
value: `add, ${steamId}, ` +
`${authTokens[steamId].discordUserId}, ` +
`${isHoster}, ` +
`${authTokens[steamId].token}, ` +
`${authTokens[steamId].issued_date}, ` +
`${authTokens[steamId].expire_date}`
}));

const str = `Authentication Token were added successfully for steamId: ${steamId}.`
await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(0, str));
client.log(client.intlGet(null, 'infoCap'), str);
}

async function removeAuthToken(client, interaction, verifyId) {
const guildId = interaction.guildId;
const authTokens = InstanceUtils.readAuthTokensFile(guildId);
let steamId = interaction.options.getString('steam_id');

if (steamId && (steamId in authTokens) && authTokens[steamId].discordUserId !== interaction.member.user.id) {
if (Config.discord.needAdminPrivileges && !client.isAdministrator(interaction)) {
const str = client.intlGet(interaction.guildId, 'missingPermission');
client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str));
client.log(client.intlGet(null, 'warningCap'), str);
return;
}
}

if (!steamId) {
for (const authToken of Object.keys(authTokens)) {
if (authToken === 'hoster') continue;

if (authTokens[authToken].discordUserId === interaction.member.user.id) {
steamId = authToken;
break;
}
}
}

if (!(steamId in authTokens)) {
const str = `Authentication Token for steamId: ${steamId} does not exist.`;
await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str));
client.log(client.intlGet(null, 'warningCap'), str);
return;
}

if (!(client.authTokenListenerIntervalIds[guildId])) {
client.authTokenListenerIntervalIds[guildId] = new Object();
}

if (client.authTokenListenerIntervalIds[guildId] &&
client.authTokenListenerIntervalIds[guildId][steamId]) {
clearInterval(client.authTokenListenerIntervalIds[guildId][steamId]);
delete client.authTokenListenerIntervalIds[guildId][steamId];
}

if (client.authTokenReadNotifications[guildId] &&
client.authTokenReadNotifications[guildId][steamId]) {
delete client.authTokenReadNotifications[guildId][steamId];
}

if (steamId === authTokens.hoster) {
authTokens.hoster = null;
}

delete authTokens[steamId];
InstanceUtils.writeAuthTokensFile(guildId, authTokens);

client.log(client.intlGet(null, 'infoCap'), client.intlGet(null, 'slashCommandValueChange', {
id: `${verifyId}`,
value: `remove, ${steamId}`
}));

const str = `Authentication Token for steamId: ${steamId} was removed successfully.`;
await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(0, str));
client.log(client.intlGet(null, 'infoCap'), str);
}

async function showAuthTokenUsers(client, interaction, verifyId) {
client.log(client.intlGet(null, 'infoCap'), client.intlGet(null, 'slashCommandValueChange', {
id: `${verifyId}`,
value: `show`
}));

await DiscordMessages.sendAuthTokensShowMessage(interaction);
}

async function setHoster(client, interaction, verifyId) {
const guildId = interaction.guildId;
const authTokens = InstanceUtils.readAuthTokensFile(guildId);
let steamId = interaction.options.getString('steam_id');

if (Config.discord.needAdminPrivileges && !client.isAdministrator(interaction)) {
const str = client.intlGet(interaction.guildId, 'missingPermission');
client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str));
client.log(client.intlGet(null, 'warningCap'), str);
return;
}

if (!steamId) {
steamId = Object.keys(authTokens).find(e => authTokens[e] &&
authTokens[e].discordUserId === interaction.member.user.id);
}

if (!(steamId in authTokens)) {
const str = `Authentication Token for steamId: ${steamId} does not exist.`;
await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str));
client.log(client.intlGet(null, 'warningCap'), str);
return;
}

authTokens.hoster = steamId;
InstanceUtils.writeAuthTokensFile(guildId, authTokens);

const instance = client.getInstance(guildId);
const rustplus = client.rustplusInstances[guildId];
if (rustplus) {
instance.activeServer = null;
client.setInstance(guildId, instance);
client.resetRustplusVariables(guildId);
rustplus.disconnect();
delete client.rustplusInstances[guildId];
await DiscordMessages.sendServerMessage(guildId, rustplus.serverId);
}

await AuthTokenListenerTemp.startNewAuthTokenListener(client, interaction.guildId, steamId);

client.log(client.intlGet(null, 'infoCap'), client.intlGet(null, 'slashCommandValueChange', {
id: `${verifyId}`,
value: `setHoster, ${steamId}`
}));

const str = `Authentication Token hoster was successfully set to steamId: ${steamId}.`;
await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(0, str));
client.log(client.intlGet(null, 'infoCap'), str);
}
1 change: 1 addition & 0 deletions src/discordEvents/guildCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = {
async execute(client, guild) {
require('../util/CreateInstanceFile')(client, guild);
require('../util/CreateCredentialsFile')(client, guild);
require('../util/CreateAuthTokensFile')(client, guild);
client.fcmListenersLite[guild.id] = new Object();

client.loadGuildIntl(guild.id);
Expand Down
28 changes: 12 additions & 16 deletions src/discordEvents/guildMemberRemove.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,23 @@ module.exports = {
const guildId = member.guild.id;
const userId = member.user.id;

const credentials = InstanceUtils.readCredentialsFile(guildId);
const authTokens = InstanceUtils.readAuthTokensFile(guildId);

const steamId = Object.keys(credentials).find(e => credentials[e] && credentials[e].discordUserId === userId);
const steamId = Object.keys(authTokens).find(e => authTokens[e] && authTokens[e].discordUserId === userId);

if (!(steamId in credentials)) return;
if (!(steamId in authTokens)) return;

if (steamId === credentials.hoster) {
if (client.fcmListeners[guildId]) {
client.fcmListeners[guildId].destroy();
}
delete client.fcmListeners[guildId];
credentials.hoster = null;
if (client.authTokenListenerIntervalsIds[guildId] &&
client.authTokenListenerIntervalsIds[guildId][steamId]) {
clearInterval(client.authTokenListenerIntervalsIds[guildId][steamId]);
delete client.authTokenListenerIntervalsIds[guildId][steamId];
}
else {
if (client.fcmListenersLite[guildId][steamId]) {
client.fcmListenersLite[guildId][steamId].destroy();
}
delete client.fcmListenersLite[guildId][steamId];

if (steamId === authTokens.hoster) {
authTokens.hoster = null;
}

delete credentials[steamId];
InstanceUtils.writeCredentialsFile(guildId, credentials);
delete authTokens[steamId];
InstanceUtils.writeAuthTokensFile(guildId, authTokens);
},
}
1 change: 1 addition & 0 deletions src/discordEvents/ready.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = {
for (const guild of client.guilds.cache) {
require('../util/CreateInstanceFile')(client, guild[1]);
require('../util/CreateCredentialsFile')(client, guild[1]);
require('../util/CreateAuthTokensFile')(client, guild[1]);
client.fcmListenersLite[guild[0]] = new Object();
}

Expand Down
Loading

0 comments on commit a5d6956

Please sign in to comment.