Skip to content

Commit

Permalink
Add despawn command
Browse files Browse the repository at this point in the history
  • Loading branch information
alexemanuelol committed Nov 28, 2023
1 parent 32a48fd commit bf0c2e2
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 1 deletion.
115 changes: 115 additions & 0 deletions src/commands/despawn.js
Original file line number Diff line number Diff line change
@@ -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://www.gnu.org/licenses/>.
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);
},
};
4 changes: 4 additions & 0 deletions src/handlers/discordCommandHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/handlers/inGameCommandHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
4 changes: 4 additions & 0 deletions src/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
"commandSyntaxDeath": "death",
"commandSyntaxDeaths": "deaths",
"commandSyntaxDecay": "decay",
"commandSyntaxDespawn": "despawn",
"commandSyntaxEvents": "events",
"commandSyntaxHeli": "heli",
"commandSyntaxLanguage": "language",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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}",
Expand Down Expand Up @@ -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",
Expand Down
37 changes: 36 additions & 1 deletion src/structures/RustPlus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')}`;
Expand Down Expand Up @@ -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
});
}

Expand Down

0 comments on commit bf0c2e2

Please sign in to comment.