Skip to content

Commit bf0c2e2

Browse files
committed
Add despawn command
1 parent 32a48fd commit bf0c2e2

File tree

5 files changed

+163
-1
lines changed

5 files changed

+163
-1
lines changed

src/commands/despawn.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
Copyright (C) 2023 Alexander Emanuelsson (alexemanuelol)
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
https://github.com/alexemanuelol/rustplusplus
18+
19+
*/
20+
21+
const Builder = require('@discordjs/builders');
22+
23+
const DiscordEmbeds = require('../discordTools/discordEmbeds.js');
24+
25+
module.exports = {
26+
name: 'despawn',
27+
28+
getData(client, guildId) {
29+
return new Builder.SlashCommandBuilder()
30+
.setName('despawn')
31+
.setDescription(client.intlGet(guildId, 'commandsStackDesc'))
32+
.addStringOption(option => option
33+
.setName('name')
34+
.setDescription(client.intlGet(guildId, 'theNameOfTheItem'))
35+
.setRequired(false))
36+
.addStringOption(option => option
37+
.setName('id')
38+
.setDescription(client.intlGet(guildId, 'theIdOfTheItem'))
39+
.setRequired(false));
40+
},
41+
42+
async execute(client, interaction) {
43+
const guildId = interaction.guildId;
44+
45+
const verifyId = Math.floor(100000 + Math.random() * 900000);
46+
client.logInteraction(interaction, verifyId, 'slashCommand');
47+
48+
if (!await client.validatePermissions(interaction)) return;
49+
await interaction.deferReply({ ephemeral: true });
50+
51+
const despawnItemName = interaction.options.getString('name');
52+
const despawnItemId = interaction.options.getString('id');
53+
54+
let itemId = null;
55+
if (despawnItemName !== null) {
56+
const item = client.items.getClosestItemIdByName(despawnItemName)
57+
if (item === undefined) {
58+
const str = client.intlGet(guildId, 'noItemWithNameFound', {
59+
name: despawnItemName
60+
});
61+
await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str));
62+
client.log(client.intlGet(guildId, 'warningCap'), str);
63+
return;
64+
}
65+
else {
66+
itemId = item;
67+
}
68+
}
69+
else if (despawnItemId !== null) {
70+
if (client.items.itemExist(despawnItemId)) {
71+
itemId = despawnItemId;
72+
}
73+
else {
74+
const str = client.intlGet(guildId, 'noItemWithIdFound', {
75+
id: despawnItemId
76+
});
77+
await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str));
78+
client.log(client.intlGet(guildId, 'warningCap'), str);
79+
return;
80+
}
81+
}
82+
else if (despawnItemName === null && despawnItemId === null) {
83+
const str = client.intlGet(guildId, 'noNameIdGiven');
84+
await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str));
85+
client.log(client.intlGet(guildId, 'warningCap'), str);
86+
return;
87+
}
88+
const itemName = client.items.getName(itemId);
89+
90+
const despawnDetails = client.rustlabs.getDespawnDetailsById(itemId);
91+
if (despawnDetails === null) {
92+
const str = client.intlGet(guildId, 'couldNotFindDespawnDetails', {
93+
name: itemName
94+
});
95+
await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str));
96+
client.log(client.intlGet(guildId, 'warningCap'), str);
97+
return;
98+
}
99+
100+
const despawnTime = despawnDetails[2].timeString;
101+
102+
client.log(client.intlGet(null, 'infoCap'), client.intlGet(null, 'slashCommandValueChange', {
103+
id: `${verifyId}`,
104+
value: `${despawnItemName} ${despawnItemId}`
105+
}));
106+
107+
const str = client.intlGet(guildId, 'despawnTimeOfItem', {
108+
item: itemName,
109+
time: despawnTime
110+
});
111+
112+
await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(0, str));
113+
client.log(client.intlGet(null, 'infoCap'), str);
114+
},
115+
};

src/handlers/discordCommandHandler.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ module.exports = {
7272
commandLowerCase.startsWith(`${prefix}${client.intlGet(guildId, 'commandSyntaxDecay')}`)) {
7373
response = rustplus.getCommandDecay(command);
7474
}
75+
else if (commandLowerCase.startsWith(`${prefix}${client.intlGet('en', 'commandSyntaxDespawn')}`) ||
76+
commandLowerCase.startsWith(`${prefix}${client.intlGet(guildId, 'commandSyntaxDespawn')}`)) {
77+
response = rustplus.getCommandDespawn(command);
78+
}
7579
else if (commandLowerCase.startsWith(`${prefix}${client.intlGet('en', 'commandSyntaxEvents')}`) ||
7680
commandLowerCase.startsWith(`${prefix}${client.intlGet(guildId, 'commandSyntaxEvents')}`)) {
7781
response = rustplus.getCommandEvents(command);

src/handlers/inGameCommandHandler.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ module.exports = {
7979
commandLowerCase.startsWith(`${prefix}${client.intlGet(guildId, 'commandSyntaxDecay')}`)) {
8080
rustplus.sendInGameMessage(rustplus.getCommandDecay(command));
8181
}
82+
else if (commandLowerCase.startsWith(`${prefix}${client.intlGet('en', 'commandSyntaxDespawn')}`) ||
83+
commandLowerCase.startsWith(`${prefix}${client.intlGet(guildId, 'commandSyntaxDespawn')}`)) {
84+
rustplus.sendInGameMessage(rustplus.getCommandDespawn(command));
85+
}
8286
else if (commandLowerCase.startsWith(`${prefix}${client.intlGet('en', 'commandSyntaxEvents')}`) ||
8387
commandLowerCase.startsWith(`${prefix}${client.intlGet(guildId, 'commandSyntaxEvents')}`)) {
8488
rustplus.sendInGameMessage(rustplus.getCommandEvents(command));

src/languages/en.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
"commandSyntaxDeath": "death",
117117
"commandSyntaxDeaths": "deaths",
118118
"commandSyntaxDecay": "decay",
119+
"commandSyntaxDespawn": "despawn",
119120
"commandSyntaxEvents": "events",
120121
"commandSyntaxHeli": "heli",
121122
"commandSyntaxLanguage": "language",
@@ -189,6 +190,7 @@
189190
"commandsCredentialsSetHosterDesc": "Set the hoster of FCM Credentials.",
190191
"commandsCredentialsSetHosterSteamIdDesc": "SteamId of the FCM Credentials hoster.",
191192
"commandsCredentialsShowDesc": "Show the currently registered FCM Credentials.",
193+
"commandsDespawnDesc": "Display the despawn time of an item.",
192194
"commandsHelpCommandList": "Command List",
193195
"commandsHelpDesc": "Display help message.",
194196
"commandsHelpHowToCredentials": "How-to Register Credentials",
@@ -277,6 +279,7 @@
277279
"couldNotFindCategory": "Could not find category: {category}",
278280
"couldNotFindChannel": "Could not find channel: {channel}",
279281
"couldNotFindCraftDetails": "Could not find craft details for {name}.",
282+
"couldNotFindDespawnDetails": "Could not find despawn details for {name}.",
280283
"couldNotFindGuild": "Could not find guild: {guildId}",
281284
"couldNotFindLanguage": "Could not find language: {language}",
282285
"couldNotFindMessage": "Could not find message {message}",
@@ -320,6 +323,7 @@
320323
"deathCap": "DEATH",
321324
"decayingCap": "DECAYING",
322325
"deleteUnreachableDevicesCap": "DELETE UNREACHABLE DEVICES",
326+
"despawnTimeOfItem": "Despawn time of {item} is {time}.",
323327
"deviceIsCurrentlyOnOff": "{device} is currently {status}.",
324328
"deviceWasTurnedOnOff": "{device} was turned {status}.",
325329
"disabledCap": "DISABLED",

src/structures/RustPlus.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,41 @@ class RustPlus extends RustPlusLib {
11651165
}
11661166
}
11671167

1168+
getCommandDespawn(command) {
1169+
const prefix = this.generalSettings.prefix;
1170+
const commandDespawn = `${prefix}${Client.client.intlGet(this.guildId, 'commandSyntaxDespawn')}`;
1171+
const commandDespawnEn = `${prefix}${Client.client.intlGet('en', 'commandSyntaxDespawn')}`;
1172+
1173+
if (command.toLowerCase().startsWith(`${commandDespawn} `)) {
1174+
command = command.slice(`${commandDespawn} `.length).trim();
1175+
}
1176+
else {
1177+
command = command.slice(`${commandDespawnEn} `.length).trim();
1178+
}
1179+
1180+
const itemId = Client.client.items.getClosestItemIdByName(command);
1181+
if (itemId === undefined) {
1182+
return Client.client.intlGet(this.guildId, 'noItemWithNameFound', {
1183+
name: command
1184+
});
1185+
}
1186+
1187+
const itemName = Client.client.items.getName(itemId);
1188+
const despawnDetails = Client.client.rustlabs.getDespawnDetailsById(itemId);
1189+
if (despawnDetails === null) {
1190+
return Client.client.intlGet(this.guildId, 'couldNotFindDespawnDetails', {
1191+
name: itemName
1192+
});
1193+
}
1194+
1195+
const despawnTime = despawnDetails[2].timeString;
1196+
1197+
return Client.client.intlGet(this.guildId, 'despawnTimeOfItem', {
1198+
item: itemName,
1199+
time: despawnTime
1200+
});
1201+
}
1202+
11681203
getCommandEvents(command) {
11691204
const prefix = this.generalSettings.prefix;
11701205
const commandEvents = `${prefix}${Client.client.intlGet(this.guildId, 'commandSyntaxEvents')}`;
@@ -2256,7 +2291,7 @@ class RustPlus extends RustPlusLib {
22562291
const itemId = Client.client.items.getClosestItemIdByName(command);
22572292
if (itemId === undefined) {
22582293
return Client.client.intlGet(this.guildId, 'noItemWithNameFound', {
2259-
name: name
2294+
name: command
22602295
});
22612296
}
22622297

0 commit comments

Comments
 (0)