Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #12 from Abhijith4124/dev
Browse files Browse the repository at this point in the history
1.2
  • Loading branch information
Abhijith4124 authored Feb 18, 2024
2 parents 475a257 + 3eae6c4 commit babb706
Show file tree
Hide file tree
Showing 14 changed files with 349 additions and 310 deletions.
3 changes: 2 additions & 1 deletion commands/moderation/banplayer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const {getServerPlayersInfo, kickPlayer, banPlayer} = require("../../utils/palworld/palworldRCONWrapper");
const {SlashCommandBuilder} = require("discord.js");
const {PermissionFlagsBits} = require("discord-api-types/v10");
const {banUser} = require("../../utils/palworld/userManager");
module.exports = {
data: new SlashCommandBuilder()
.setName("banplayer")
Expand All @@ -23,7 +24,7 @@ module.exports = {
let serverName = interaction.options.getString("server");
let playerSteamId = interaction.options.getString("playersteamid");

await banPlayer(interaction, {
await banUser(interaction, {
serverName: serverName,
playerSteamId: playerSteamId
});
Expand Down
49 changes: 32 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const {cleanUp} = require("./utils/cleaner/cleaner");
const {ActivityType} = require("discord-api-types/v10");
const db = new JSONdb('./data/data.json');
const logger = require('./utils/discord/logger');
const {deleteCommandsFromGuild, deployCommandsToGuild} = require("./utils/discord/commandsDeploymentManager");

const CURRENT_BOT_VERSION = 1;

const client = new Client({
intents: [
Expand Down Expand Up @@ -99,31 +102,43 @@ client.on(Events.InteractionCreate, async interaction => {
}
});

const rest = new REST().setToken(config.token);

(async () => {
try {

await rest.put(Routes.applicationCommands(config.botApplicationId), { body: [] })
.catch(console.error);

await rest.put(
Routes.applicationCommands(config.botApplicationId),
{ body: client.commands.map(command => command.data.toJSON()) }
);
} catch (error) {
console.error(error);
}
})();
client.on(Events.GuildCreate, (guild) => {
deployCommandsToGuild(client, guild.id).then(() => {
if (config.debug) {
console.log(`Deployed Commands to ${guild.name}`);
}
}).catch(e => {
console.error(e);
});
})

client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);

client.user.setActivity({
type: ActivityType.Custom,
name: "customstatus",
state: config.customStatusMessage
})
});

if (!db.get(`BotVersion`)) {
db.set(`BotVersion`, 0);
}

if (db.get(`BotVersion`) < CURRENT_BOT_VERSION) {
for (const guild of readyClient.guilds.cache.values()) {
deleteCommandsFromGuild(client, guild.id).then(() => {
deployCommandsToGuild(client, guild.id).then(() => {
db.set(`BotVersion`, CURRENT_BOT_VERSION);
}).catch(e => {
console.error(e);
});
}).catch(e => {
console.error(e)
})
}

}
startRCONService(client, db);
});

Expand Down
4 changes: 2 additions & 2 deletions modalHandlers/editpalserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ async function handleModal(interaction) {
return;
}

const serverVersionResponse = serverInfoResponse.data.serverVersion;
const serverNameResponse = serverInfoResponse.data.serverName;
const serverVersionResponse = serverInfoResponse.serverInfo.serverVersion;
const serverNameResponse = serverInfoResponse.serverInfo.serverName;

//Replace the Server Details
guildPalServers.splice(guildPalServers.findIndex(server => server.serverName === serverName), 1, {
Expand Down
4 changes: 2 additions & 2 deletions modalHandlers/newpalserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ async function handleModal(interaction) {
return;
}

const serverVersionResponse = serverInfoResponse.data.serverVersion;
const serverNameResponse = serverInfoResponse.data.serverName;
const serverVersionResponse = serverInfoResponse.serverInfo.serverVersion;
const serverNameResponse = serverInfoResponse.serverInfo.serverName;

const guildPalServersKey = `${interaction.guild.id}_PalServers`;

Expand Down
14 changes: 13 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
Expand All @@ -12,7 +13,9 @@
"dependencies": {
"discord-api-types": "^0.37.69",
"discord.js": "^14.14.1",
"simple-json-db": "^2.0.0"
"minecraft-rcon-client": "^2.1.2",
"simple-json-db": "^2.0.0",
"srcds-rcon": "^2.2.1"
},
"devDependencies": {
"eslint": "^8.56.0"
Expand Down
40 changes: 31 additions & 9 deletions services/rconService.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ async function startRCONService(client, db) {

const statusChannel = await client.channels.cache.get(statusChannelId);

if (!client.guilds.cache.get(guildId)) {
if (config.debug) {
console.log(`[RCON Service]: Failed to find Guild ${serverName}`);
}
continue;
}

if (!client.guilds.cache.get(guildId).members.me.permissionsIn(statusChannel).has("SendMessages")) {
if (config.debug) {
console.log(`[RCON Service]: Permission Denied to Send Status Message to ${serverName}`);
Expand Down Expand Up @@ -300,6 +307,8 @@ async function startRCONService(client, db) {
if (config.debug) {
console.log(`[RCON Service]: Skipping Server Check since Server ${serverName} is offline...`);
}

//Changing Only Online here because in case the RCON times out, we don't want to reset the player list and bug join/leave.
serverData.online = false;
db.set(serverDataKey, serverData);
continue;
Expand Down Expand Up @@ -402,27 +411,40 @@ async function startRCONService(client, db) {
//Non Whitelisted Players are online
for (const nonWhitelistedPlayer of nonWhitelistedPlayers) {
//Giving some time before kicking to prevent players from being stuck in loading screen!
await kickPlayer(host, RCONPort, password, nonWhitelistedPlayer.steamid);
let kickPlayerResponse = await kickPlayer(host, RCONPort, password, nonWhitelistedPlayer.steamid);

let nonWhitelistedPlayerName = nonWhitelistedPlayer.name;
let nonWhitelistedPlayerSteamId = nonWhitelistedPlayer.steamid;
let nonWhitelistedPlayerUId = nonWhitelistedPlayer.playeruid;

await logToWhitelistLogChannel(client, guildId, serverName, "Non Whitelisted Player Kicked",
`Player \`${nonWhitelistedPlayerName}\` with Steam ID \`${nonWhitelistedPlayerSteamId}\`
if (kickPlayerResponse.status === "success") {
await logToWhitelistLogChannel(client, guildId, serverName, "Non Whitelisted Player Kicked",
`Player \`${nonWhitelistedPlayerName}\` with Steam ID \`${nonWhitelistedPlayerSteamId}\`
and UID \`${nonWhitelistedPlayerUId}\` has been Kicked from the server.`,
nonWhitelistedPlayerName, nonWhitelistedPlayerSteamId, nonWhitelistedPlayerUId);
nonWhitelistedPlayerName, nonWhitelistedPlayerSteamId, nonWhitelistedPlayerUId);
}else {
await logToWhitelistLogChannel(client, guildId, serverName, "Failed to Kick Non Whitelisted Player!",
`Player \`${nonWhitelistedPlayerName}\` with Steam ID \`${nonWhitelistedPlayerSteamId}\`
and UID \`${nonWhitelistedPlayerUId}\` cannot be kicked! \n Error Message: ${kickPlayerResponse.message}`,
nonWhitelistedPlayerName, nonWhitelistedPlayerSteamId, nonWhitelistedPlayerUId);
}
}
}

//Logging Name Spoofing Players here, we are not kicking here since they are kicked in the step above.
if (nameSpoofingPlayers.length > 0) {
for (const nameSpoofer of nameSpoofingPlayers) {
await kickPlayer(host, RCONPort, password, nameSpoofer.steamid);

await logToWhitelistLogChannel(client, guildId, serverName, "Whitelisted Player Caught Name Spoofing!",
`Player \`${nameSpoofer.originalName}\` caught Spoofing the Name: \`${nameSpoofer.name}\` with Steam ID \`${nameSpoofer.steamid}\` and UID \`${nameSpoofer.playeruid}\` has been Kicked from the server for name spoofing.`,
nameSpoofer.originalName, nameSpoofer.steamid, nameSpoofer.playeruid);
let kickPlayerResponse = await kickPlayer(host, RCONPort, password, nameSpoofer.steamid);

if (kickPlayerResponse.status === "success") {
await logToWhitelistLogChannel(client, guildId, serverName, "Whitelisted Player Caught Name Spoofing!",
`Player \`${nameSpoofer.originalName}\` caught Spoofing the Name: \`${nameSpoofer.name}\` with Steam ID \`${nameSpoofer.steamid}\` and UID \`${nameSpoofer.playeruid}\` has been Kicked from the server for name spoofing.`,
nameSpoofer.originalName, nameSpoofer.steamid, nameSpoofer.playeruid);
}else {
await logToWhitelistLogChannel(client, guildId, serverName, "Failed to Kick Name Spoofing Player",
`Player \`${nameSpoofer.originalName}\` caught Spoofing the Name: \`${nameSpoofer.name}\` with Steam ID \`${nameSpoofer.steamid}\` and UID \`${nameSpoofer.playeruid}\` but cannot be kicked from the server.`,
nameSpoofer.originalName, nameSpoofer.steamid, nameSpoofer.playeruid);
}
}
}
}else {
Expand Down
33 changes: 33 additions & 0 deletions utils/discord/commandsDeploymentManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const {REST, Routes} = require("discord.js");
const config = require("../../config.json");


const rest = new REST().setToken(config.token);
async function deployCommandsToGuild(client, guildId) {
try {
if (config.debug) {
console.log(`Started refreshing ${client.commands.size} application (/) commands.`);
}

await rest.put(Routes.applicationGuildCommands(config.botApplicationId, guildId), { body: client.commands.map(command => command.data.toJSON()) });

if (config.debug) {
console.log(`Deployed New Commands`)
}
}catch (e) {
console.error(e)
}
}

async function deleteCommandsFromGuild(client, guildId) {
try {
await rest.put(Routes.applicationGuildCommands(config.botApplicationId, guildId), { body: [] });
if (config.debug) {
console.log('Successfully deleted all application commands.')
}
}catch (e) {
console.error(e)
}
}

module.exports = {deployCommandsToGuild, deleteCommandsFromGuild}
7 changes: 4 additions & 3 deletions utils/discord/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ async function logToGameLogChannel(client, guild, serverName, title, message) {
const gameLogChannelIdKey = `${guild}_${serverName.replaceAll(" ", "_")}_GameLogChannelId`;
const gameLogChannelId = client.db.get(gameLogChannelIdKey);

if (gameLogChannelId) {

if (gameLogChannelId && client.guilds.cache.get(guild)) {
if (!client.guilds.cache.get(guild).members.me.permissionsIn(gameLogChannelId).has("SendMessages")) {
if (config.debug) {
console.log(`[LOGGER]: Permission Denied to Log Game Logs to ${serverName}`);
Expand Down Expand Up @@ -55,7 +54,7 @@ async function logToWhitelistLogChannel(client, guild, serverName, title, messag
const whitelistLogChannelIdKey = `${guild}_${serverName.replaceAll(" ", "_")}_WhitelistLogChannelId`;
const whitelistLogChannelId = client.db.get(whitelistLogChannelIdKey);

if (whitelistLogChannelId) {
if (whitelistLogChannelId && client.guilds.cache.get(guild)) {

if (!client.guilds.cache.get(guild).members.me.permissionsIn(whitelistLogChannelId).has("SendMessages")) {
if (config.debug) {
Expand Down Expand Up @@ -103,6 +102,8 @@ async function logToWhitelistLogChannel(client, guild, serverName, title, messag
.setCustomId("banplayer")
.setLabel("Ban Player")
.setStyle(ButtonStyle.Danger).setEmoji("🔨")
}else {
actionButton.setLabel("").setDisabled(true);
}

const actionRow = new ActionRowBuilder().addComponents(actionButton);
Expand Down
Loading

0 comments on commit babb706

Please sign in to comment.