|
| 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 | +const Timer = require('../util/timer.js'); |
| 25 | + |
| 26 | +module.exports = { |
| 27 | + name: 'decay', |
| 28 | + |
| 29 | + getData(client, guildId) { |
| 30 | + return new Builder.SlashCommandBuilder() |
| 31 | + .setName('decay') |
| 32 | + .setDescription(client.intlGet(guildId, 'commandsDecayDesc')) |
| 33 | + .addStringOption(option => option |
| 34 | + .setName('name') |
| 35 | + .setDescription(client.intlGet(guildId, 'theNameOfTheItem')) |
| 36 | + .setRequired(false)) |
| 37 | + .addStringOption(option => option |
| 38 | + .setName('id') |
| 39 | + .setDescription(client.intlGet(guildId, 'theIdOfTheItem')) |
| 40 | + .setRequired(false)) |
| 41 | + .addIntegerOption(option => option |
| 42 | + .setName('hp') |
| 43 | + .setDescription(client.intlGet(guildId, 'currentItemHp')) |
| 44 | + .setRequired(false)); |
| 45 | + }, |
| 46 | + |
| 47 | + async execute(client, interaction) { |
| 48 | + const guildId = interaction.guildId; |
| 49 | + |
| 50 | + const verifyId = Math.floor(100000 + Math.random() * 900000); |
| 51 | + client.logInteraction(interaction, verifyId, 'slashCommand'); |
| 52 | + |
| 53 | + if (!await client.validatePermissions(interaction)) return; |
| 54 | + await interaction.deferReply({ ephemeral: true }); |
| 55 | + |
| 56 | + const decayItemName = interaction.options.getString('name'); |
| 57 | + const decayItemId = interaction.options.getString('id'); |
| 58 | + const decayItemHp = interaction.options.getInteger('hp'); |
| 59 | + |
| 60 | + let itemId = null; |
| 61 | + let type = 'items'; |
| 62 | + |
| 63 | + if (decayItemName !== null) { |
| 64 | + let foundName = null; |
| 65 | + if (!foundName) { |
| 66 | + foundName = client.rustlabs.getClosestOtherNameByName(decayItemName); |
| 67 | + if (foundName) { |
| 68 | + if (client.rustlabs.decayData['other'].hasOwnProperty(foundName)) { |
| 69 | + type = 'other'; |
| 70 | + } |
| 71 | + else { |
| 72 | + foundName = null; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if (!foundName) { |
| 78 | + foundName = client.rustlabs.getClosestBuildingBlockNameByName(decayItemName); |
| 79 | + if (foundName) { |
| 80 | + if (client.rustlabs.decayData['buildingBlocks'].hasOwnProperty(foundName)) { |
| 81 | + type = 'buildingBlocks'; |
| 82 | + } |
| 83 | + else { |
| 84 | + foundName = null; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (!foundName) { |
| 90 | + foundName = client.items.getClosestItemIdByName(decayItemName); |
| 91 | + if (foundName) { |
| 92 | + if (!client.rustlabs.decayData['items'].hasOwnProperty(foundName)) { |
| 93 | + foundName = null; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (!foundName) { |
| 99 | + const str = client.intlGet(guildId, 'noItemWithNameFound', { |
| 100 | + name: decayItemName |
| 101 | + }); |
| 102 | + await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str)); |
| 103 | + client.log(client.intlGet(guildId, 'warningCap'), str); |
| 104 | + return; |
| 105 | + } |
| 106 | + itemId = foundName; |
| 107 | + } |
| 108 | + else if (decayItemId !== null) { |
| 109 | + if (client.items.itemExist(decayItemId)) { |
| 110 | + itemId = decayItemId; |
| 111 | + } |
| 112 | + else { |
| 113 | + const str = client.intlGet(guildId, 'noItemWithIdFound', { |
| 114 | + id: decayItemId |
| 115 | + }); |
| 116 | + await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str)); |
| 117 | + client.log(client.intlGet(guildId, 'warningCap'), str); |
| 118 | + return; |
| 119 | + } |
| 120 | + } |
| 121 | + else if (decayItemName === null && decayItemId === null) { |
| 122 | + const str = client.intlGet(guildId, 'noNameIdGiven'); |
| 123 | + await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str)); |
| 124 | + client.log(client.intlGet(guildId, 'warningCap'), str); |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + let itemName = null; |
| 129 | + let decayDetails = null; |
| 130 | + if (type === 'items') { |
| 131 | + itemName = client.items.getName(itemId); |
| 132 | + decayDetails = client.rustlabs.getDecayDetailsById(itemId); |
| 133 | + } |
| 134 | + else { |
| 135 | + itemName = itemId; |
| 136 | + decayDetails = client.rustlabs.getDecayDetailsByName(itemId); |
| 137 | + } |
| 138 | + |
| 139 | + if (decayDetails === null) { |
| 140 | + const str = client.intlGet(guildId, 'couldNotFindDecayDetails', { |
| 141 | + name: itemName |
| 142 | + }); |
| 143 | + await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str)); |
| 144 | + client.log(client.intlGet(guildId, 'warningCap'), str); |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + const details = decayDetails[3]; |
| 149 | + |
| 150 | + const hp = decayItemHp === null ? details.hp : decayItemHp; |
| 151 | + if (hp > details.hp) { |
| 152 | + const str = client.intlGet(guildId, 'hpExceedMax', { |
| 153 | + hp: hp, |
| 154 | + max: details.hp |
| 155 | + }); |
| 156 | + await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str)); |
| 157 | + client.log(client.intlGet(guildId, 'warningCap'), str); |
| 158 | + return; |
| 159 | + } |
| 160 | + |
| 161 | + const decayMultiplier = hp / details.hp; |
| 162 | + |
| 163 | + let decayString = `${itemName} (${hp}/${details.hp}) `; |
| 164 | + const decayStrings = []; |
| 165 | + if (details.decayString !== null) { |
| 166 | + let str = `${client.intlGet(guildId, 'decay')}: `; |
| 167 | + if (hp === details.hp) { |
| 168 | + decayStrings.push(`${str}${details.decayString}`); |
| 169 | + } |
| 170 | + else { |
| 171 | + const time = Timer.secondsToFullScale(Math.floor(details.decay * decayMultiplier)); |
| 172 | + decayStrings.push(`${str}${time}`); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + if (details.decayOutsideString !== null) { |
| 177 | + let str = `${client.intlGet(guildId, 'outside')}: `; |
| 178 | + if (hp === details.hp) { |
| 179 | + decayStrings.push(`${str}${details.decayOutsideString}`); |
| 180 | + } |
| 181 | + else { |
| 182 | + const time = Timer.secondsToFullScale(Math.floor(details.decayOutside * decayMultiplier)); |
| 183 | + decayStrings.push(`${str}${time}`); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + if (details.decayInsideString !== null) { |
| 188 | + let str = `${client.intlGet(guildId, 'inside')}: `; |
| 189 | + if (hp === details.hp) { |
| 190 | + decayStrings.push(`${str}${details.decayInsideString}`); |
| 191 | + } |
| 192 | + else { |
| 193 | + const time = Timer.secondsToFullScale(Math.floor(details.decayInside * decayMultiplier)); |
| 194 | + decayStrings.push(`${str}${time}`); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + if (details.decayUnderwaterString !== null) { |
| 199 | + let str = `${client.intlGet(guildId, 'underwater')}: `; |
| 200 | + if (hp === details.hp) { |
| 201 | + decayStrings.push(`${str}${details.decayUnderwaterString}`); |
| 202 | + } |
| 203 | + else { |
| 204 | + const time = Timer.secondsToFullScale(Math.floor(details.decayUnderwater * decayMultiplier)); |
| 205 | + decayStrings.push(`${str}${time}`); |
| 206 | + } |
| 207 | + } |
| 208 | + decayString += `${decayStrings.join(', ')}.`; |
| 209 | + |
| 210 | + client.log(client.intlGet(null, 'infoCap'), client.intlGet(null, 'slashCommandValueChange', { |
| 211 | + id: `${verifyId}`, |
| 212 | + value: `${decayItemName} ${decayItemId} ${decayItemHp}` |
| 213 | + })); |
| 214 | + |
| 215 | + await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(0, decayString)); |
| 216 | + client.log(client.intlGet(null, 'infoCap'), decayString); |
| 217 | + }, |
| 218 | +}; |
0 commit comments