diff --git a/src/commands/despawn.js b/src/commands/despawn.js new file mode 100644 index 000000000..311932a0c --- /dev/null +++ b/src/commands/despawn.js @@ -0,0 +1,115 @@ +/* + Copyright (C) 2023 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://github.com/alexemanuelol/rustplusplus + +*/ + +const Builder = require('@discordjs/builders'); + +const DiscordEmbeds = require('../discordTools/discordEmbeds.js'); + +module.exports = { + name: 'despawn', + + getData(client, guildId) { + return new Builder.SlashCommandBuilder() + .setName('despawn') + .setDescription(client.intlGet(guildId, 'commandsStackDesc')) + .addStringOption(option => option + .setName('name') + .setDescription(client.intlGet(guildId, 'theNameOfTheItem')) + .setRequired(false)) + .addStringOption(option => option + .setName('id') + .setDescription(client.intlGet(guildId, 'theIdOfTheItem')) + .setRequired(false)); + }, + + async execute(client, interaction) { + const guildId = interaction.guildId; + + const verifyId = Math.floor(100000 + Math.random() * 900000); + client.logInteraction(interaction, verifyId, 'slashCommand'); + + if (!await client.validatePermissions(interaction)) return; + await interaction.deferReply({ ephemeral: true }); + + const despawnItemName = interaction.options.getString('name'); + const despawnItemId = interaction.options.getString('id'); + + let itemId = null; + if (despawnItemName !== null) { + const item = client.items.getClosestItemIdByName(despawnItemName) + if (item === undefined) { + const str = client.intlGet(guildId, 'noItemWithNameFound', { + name: despawnItemName + }); + await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str)); + client.log(client.intlGet(guildId, 'warningCap'), str); + return; + } + else { + itemId = item; + } + } + else if (despawnItemId !== null) { + if (client.items.itemExist(despawnItemId)) { + itemId = despawnItemId; + } + else { + const str = client.intlGet(guildId, 'noItemWithIdFound', { + id: despawnItemId + }); + await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str)); + client.log(client.intlGet(guildId, 'warningCap'), str); + return; + } + } + else if (despawnItemName === null && despawnItemId === null) { + const str = client.intlGet(guildId, 'noNameIdGiven'); + await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str)); + client.log(client.intlGet(guildId, 'warningCap'), str); + return; + } + const itemName = client.items.getName(itemId); + + const despawnDetails = client.rustlabs.getDespawnDetailsById(itemId); + if (despawnDetails === null) { + const str = client.intlGet(guildId, 'couldNotFindDespawnDetails', { + name: itemName + }); + await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str)); + client.log(client.intlGet(guildId, 'warningCap'), str); + return; + } + + const despawnTime = despawnDetails[2].timeString; + + client.log(client.intlGet(null, 'infoCap'), client.intlGet(null, 'slashCommandValueChange', { + id: `${verifyId}`, + value: `${despawnItemName} ${despawnItemId}` + })); + + const str = client.intlGet(guildId, 'despawnTimeOfItem', { + item: itemName, + time: despawnTime + }); + + await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(0, str)); + client.log(client.intlGet(null, 'infoCap'), str); + }, +}; diff --git a/src/handlers/discordCommandHandler.js b/src/handlers/discordCommandHandler.js index d0a9d841a..d064a3d61 100644 --- a/src/handlers/discordCommandHandler.js +++ b/src/handlers/discordCommandHandler.js @@ -72,6 +72,10 @@ module.exports = { commandLowerCase.startsWith(`${prefix}${client.intlGet(guildId, 'commandSyntaxDecay')}`)) { response = rustplus.getCommandDecay(command); } + else if (commandLowerCase.startsWith(`${prefix}${client.intlGet('en', 'commandSyntaxDespawn')}`) || + commandLowerCase.startsWith(`${prefix}${client.intlGet(guildId, 'commandSyntaxDespawn')}`)) { + response = rustplus.getCommandDespawn(command); + } else if (commandLowerCase.startsWith(`${prefix}${client.intlGet('en', 'commandSyntaxEvents')}`) || commandLowerCase.startsWith(`${prefix}${client.intlGet(guildId, 'commandSyntaxEvents')}`)) { response = rustplus.getCommandEvents(command); diff --git a/src/handlers/inGameCommandHandler.js b/src/handlers/inGameCommandHandler.js index 0c2679337..5e0d604d7 100644 --- a/src/handlers/inGameCommandHandler.js +++ b/src/handlers/inGameCommandHandler.js @@ -79,6 +79,10 @@ module.exports = { commandLowerCase.startsWith(`${prefix}${client.intlGet(guildId, 'commandSyntaxDecay')}`)) { rustplus.sendInGameMessage(rustplus.getCommandDecay(command)); } + else if (commandLowerCase.startsWith(`${prefix}${client.intlGet('en', 'commandSyntaxDespawn')}`) || + commandLowerCase.startsWith(`${prefix}${client.intlGet(guildId, 'commandSyntaxDespawn')}`)) { + rustplus.sendInGameMessage(rustplus.getCommandDespawn(command)); + } else if (commandLowerCase.startsWith(`${prefix}${client.intlGet('en', 'commandSyntaxEvents')}`) || commandLowerCase.startsWith(`${prefix}${client.intlGet(guildId, 'commandSyntaxEvents')}`)) { rustplus.sendInGameMessage(rustplus.getCommandEvents(command)); diff --git a/src/languages/en.json b/src/languages/en.json index 2b11a2ede..a4c7c430e 100644 --- a/src/languages/en.json +++ b/src/languages/en.json @@ -116,6 +116,7 @@ "commandSyntaxDeath": "death", "commandSyntaxDeaths": "deaths", "commandSyntaxDecay": "decay", + "commandSyntaxDespawn": "despawn", "commandSyntaxEvents": "events", "commandSyntaxHeli": "heli", "commandSyntaxLanguage": "language", @@ -189,6 +190,7 @@ "commandsCredentialsSetHosterDesc": "Set the hoster of FCM Credentials.", "commandsCredentialsSetHosterSteamIdDesc": "SteamId of the FCM Credentials hoster.", "commandsCredentialsShowDesc": "Show the currently registered FCM Credentials.", + "commandsDespawnDesc": "Display the despawn time of an item.", "commandsHelpCommandList": "Command List", "commandsHelpDesc": "Display help message.", "commandsHelpHowToCredentials": "How-to Register Credentials", @@ -277,6 +279,7 @@ "couldNotFindCategory": "Could not find category: {category}", "couldNotFindChannel": "Could not find channel: {channel}", "couldNotFindCraftDetails": "Could not find craft details for {name}.", + "couldNotFindDespawnDetails": "Could not find despawn details for {name}.", "couldNotFindGuild": "Could not find guild: {guildId}", "couldNotFindLanguage": "Could not find language: {language}", "couldNotFindMessage": "Could not find message {message}", @@ -320,6 +323,7 @@ "deathCap": "DEATH", "decayingCap": "DECAYING", "deleteUnreachableDevicesCap": "DELETE UNREACHABLE DEVICES", + "despawnTimeOfItem": "Despawn time of {item} is {time}.", "deviceIsCurrentlyOnOff": "{device} is currently {status}.", "deviceWasTurnedOnOff": "{device} was turned {status}.", "disabledCap": "DISABLED", diff --git a/src/structures/RustPlus.js b/src/structures/RustPlus.js index 1c0df3c51..fa4790f60 100644 --- a/src/structures/RustPlus.js +++ b/src/structures/RustPlus.js @@ -1165,6 +1165,41 @@ class RustPlus extends RustPlusLib { } } + getCommandDespawn(command) { + const prefix = this.generalSettings.prefix; + const commandDespawn = `${prefix}${Client.client.intlGet(this.guildId, 'commandSyntaxDespawn')}`; + const commandDespawnEn = `${prefix}${Client.client.intlGet('en', 'commandSyntaxDespawn')}`; + + if (command.toLowerCase().startsWith(`${commandDespawn} `)) { + command = command.slice(`${commandDespawn} `.length).trim(); + } + else { + command = command.slice(`${commandDespawnEn} `.length).trim(); + } + + const itemId = Client.client.items.getClosestItemIdByName(command); + if (itemId === undefined) { + return Client.client.intlGet(this.guildId, 'noItemWithNameFound', { + name: command + }); + } + + const itemName = Client.client.items.getName(itemId); + const despawnDetails = Client.client.rustlabs.getDespawnDetailsById(itemId); + if (despawnDetails === null) { + return Client.client.intlGet(this.guildId, 'couldNotFindDespawnDetails', { + name: itemName + }); + } + + const despawnTime = despawnDetails[2].timeString; + + return Client.client.intlGet(this.guildId, 'despawnTimeOfItem', { + item: itemName, + time: despawnTime + }); + } + getCommandEvents(command) { const prefix = this.generalSettings.prefix; const commandEvents = `${prefix}${Client.client.intlGet(this.guildId, 'commandSyntaxEvents')}`; @@ -2256,7 +2291,7 @@ class RustPlus extends RustPlusLib { const itemId = Client.client.items.getClosestItemIdByName(command); if (itemId === undefined) { return Client.client.intlGet(this.guildId, 'noItemWithNameFound', { - name: name + name: command }); }