Skip to content

Commit

Permalink
Added upkeep command for items
Browse files Browse the repository at this point in the history
  • Loading branch information
alexemanuelol committed Dec 13, 2023
1 parent 15d9526 commit 01fb986
Show file tree
Hide file tree
Showing 9 changed files with 1,080 additions and 9 deletions.
14 changes: 14 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Slash Command | Description
[**/role**](commands.md#role) | Set/Clear a specific role that will be able to see the rustplusplus category content.
[**/storagemonitor**](commands.md#storagemonitors) | Operations on Storage Monitors.
[**/switch**](commands.md#switch) | Operations on Smart Switches.
[**/upkeep**](commands.md#upkeep) | Get the upkeep cost of an item.
[**/uptime**](commands.md#uptime) | Display uptime of the bot and server.
[**/voice**](commands.md#voice) | Operations on Voice Feature.

Expand Down Expand Up @@ -295,6 +296,19 @@ Subcommand | Options | Description | Required

![Discord Slash Command switch Image](images/slash_commands/switch.png)


## **/upkeep**

> **Get the upkeep cost of an item.**
Subcommand | Options | Description | Required
---------- | ------- | ----------- | --------
  | `name` | The name of the item. | `False`
  | `id` | The id of the item. | `False`

![Discord Slash Command upkeep Image](images/slash_commands/upkeep.png)


## **/uptime**

> **Display uptime of the bot and server.**
Expand Down
1 change: 1 addition & 0 deletions docs/full_list_features.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- **/role** - Setup a specific role to use rustplusplus.
- **/storagemonitor** - Change image of paired Storage Monitors.
- **/switch** - Change image of paired Storage Monitors.
- **/upkeep** - Get the upkeep cost of an item.
- **/uptime** - Get the current uptime for rustplusplus.
- **/voice** - Let rustplusplus join voicechat.

Expand Down
Binary file added docs/images/slash_commands/upkeep.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
164 changes: 164 additions & 0 deletions src/commands/upkeep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
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: 'upkeep',

getData(client, guildId) {
return new Builder.SlashCommandBuilder()
.setName('upkeep')
.setDescription(client.intlGet(guildId, 'commandsUpkeepDesc'))
.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 upkeepItemName = interaction.options.getString('name');
const upkeepItemId = interaction.options.getString('id');

let itemId = null;
let type = 'items';

if (upkeepItemName !== null) {
let foundName = null;
if (!foundName) {
foundName = client.rustlabs.getClosestOtherNameByName(upkeepItemName);
if (foundName) {
if (client.rustlabs.upkeepData['other'].hasOwnProperty(foundName)) {
type = 'other';
}
else {
foundName = null;
}
}
}

if (!foundName) {
foundName = client.rustlabs.getClosestBuildingBlockNameByName(upkeepItemName);
if (foundName) {
if (client.rustlabs.upkeepData['buildingBlocks'].hasOwnProperty(foundName)) {
type = 'buildingBlocks';
}
else {
foundName = null;
}
}
}

if (!foundName) {
foundName = client.items.getClosestItemIdByName(upkeepItemName);
if (foundName) {
if (!client.rustlabs.upkeepData['items'].hasOwnProperty(foundName)) {
foundName = null;
}
}
}

if (!foundName) {
const str = client.intlGet(guildId, 'noItemWithNameFound', {
name: upkeepItemName
});
await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str));
client.log(client.intlGet(guildId, 'warningCap'), str);
return;
}
itemId = foundName;
}
else if (upkeepItemId !== null) {
if (client.items.itemExist(upkeepItemId)) {
itemId = itemItemId;
}
else {
const str = client.intlGet(guildId, 'noItemWithIdFound', {
id: upkeepItemId
});
await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str));
client.log(client.intlGet(guildId, 'warningCap'), str);
return;
}
}
else if (upkeepItemName === null && upkeepItemId === null) {
const str = client.intlGet(guildId, 'noNameIdGiven');
await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str));
client.log(client.intlGet(guildId, 'warningCap'), str);
return;
}

let itemName = null;
let upkeepDetails = null;
if (type === 'items') {
itemName = client.items.getName(itemId);
upkeepDetails = client.rustlabs.getUpkeepDetailsById(itemId);
}
else {
itemName = itemId;
upkeepDetails = client.rustlabs.getUpkeepDetailsByName(itemId);
}

if (upkeepDetails === null) {
const str = client.intlGet(guildId, 'couldNotFindUpkeepDetails', {
name: itemName
});
await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(1, str));
client.log(client.intlGet(guildId, 'warningCap'), str);
return;
}

const details = upkeepDetails[3];

const items = [];
for (const item of details) {
const name = client.items.getName(item.id);
const quantity = item.quantity;
items.push(`${quantity} ${name}`);
}

client.log(client.intlGet(null, 'infoCap'), client.intlGet(null, 'slashCommandValueChange', {
id: `${verifyId}`,
value: `${upkeepItemName} ${upkeepItemId}`
}));

const str = client.intlGet(guildId, 'upkeepForItem', {
item: itemName,
cost: items.join(', ')
});

await client.interactionEditReply(interaction, DiscordEmbeds.getActionInfoEmbed(0, str));
client.log(client.intlGet(null, 'infoCap'), str);
},
};
26 changes: 17 additions & 9 deletions src/discordTools/discordEmbeds.js
Original file line number Diff line number Diff line change
Expand Up @@ -1273,15 +1273,23 @@ module.exports = {
});
}

//const upkeepDetails = Client.client.rustlabs.getUpkeepDetailsById(itemId);
//if (upkeepDetails !== null) {
// const details = upkeepDetails[2];
// fields.push({
// name: Client.client.intlGet(guildId, 'upkeep'),
// value: details.timeString,
// inline: true
// });
//}
const upkeepDetails = Client.client.rustlabs.getUpkeepDetailsById(itemId);
if (upkeepDetails !== null) {
const details = upkeepDetails[3];

let upkeepString = '';
for (const item of details) {
const name = Client.client.items.getName(item.id);
const quantity = item.quantity;
upkeepString += `${quantity} ${name}\n`;
}

fields.push({
name: Client.client.intlGet(guildId, 'upkeep'),
value: upkeepString,
inline: true
});
}

const craftDetails = Client.client.rustlabs.getCraftDetailsById(itemId);
if (craftDetails !== null) {
Expand Down
60 changes: 60 additions & 0 deletions src/external/process_rustlabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ const RUSTLABS_ITEM_DECAY_REGEX3 =
const RUSTLABS_ITEM_DECAY_REGEX4 =
/<td>Decay\stime\soutside<\/td>(\n|.){1,3}?<td>(.*?)<\/td>(\n|.)*?<td>HP<\/td>(\n|.){1,3}?<td>(.*?)<\/td>/gm

const RUSTLABS_ITEM_UPKEEP_AREA_REGEX = /<td>Upkeep<\/td>(\n|.)*?<\/tr>/gm
const RUSTLABS_ITEM_UPKEEP_REGEX =
/img\ssrc=".*?\/img\/.*?\/(.*?)\.png"\salt="(.*?)"(\n|.)*?class="icon-in-text">(.*?)</gm

const RUSTLABS_ALL_BUILDING_BLOCKS_REGEX = /\/building\/(.*?)">(.*?)</gm

const RUSTLABS_ALL_OTHER_REGEX = /\/entity\/(.*?)">(.*?)</gm
Expand Down Expand Up @@ -119,6 +123,10 @@ const rustlabsDecayData = new Object();
rustlabsDecayData['items'] = new Object();
rustlabsDecayData['buildingBlocks'] = new Object();
rustlabsDecayData['other'] = new Object();
const rustlabsUpkeepData = new Object();
rustlabsUpkeepData['items'] = new Object();
rustlabsUpkeepData['buildingBlocks'] = new Object();
rustlabsUpkeepData['other'] = new Object();

async function scrape(url) {
try {
Expand Down Expand Up @@ -229,6 +237,7 @@ async function processAllItems() {
processItemDespawn(rustlabsName, shortname, name, data);
processItemStack(rustlabsName, shortname, name, data);
processItemDecay(rustlabsName, shortname, name, data);
processItemUpkeep(rustlabsName, shortname, name, data);

await sleep(SLEEP_TIMEOUT_MS);
}
Expand Down Expand Up @@ -275,6 +284,7 @@ async function processAllBuildingBlocks() {

processItemDurability(rustlabsName, null, name, data, 'buildingBlocks');
processItemDecay(rustlabsName, null, name, data, 'buildingBlocks');
processItemUpkeep(rustlabsName, null, name, data, 'buildingBlocks');

await sleep(SLEEP_TIMEOUT_MS);
}
Expand Down Expand Up @@ -318,6 +328,7 @@ async function processAllOther() {

processItemDurability(rustlabsName, null, name, data, 'other');
processItemDecay(rustlabsName, null, name, data, 'other');
processItemUpkeep(rustlabsName, null, name, data, 'other');

await sleep(SLEEP_TIMEOUT_MS);
}
Expand Down Expand Up @@ -926,6 +937,54 @@ function processItemDecay(rustlabsName, shortname, name, data, type = 'items') {
};
}

function processItemUpkeep(rustlabsName, shortname, name, data, type = 'items') {
let itemId = null;
if (type === 'items') {
itemId = Object.keys(ITEMS).find(e => ITEMS[e].shortname === shortname && ITEMS[e].name === name);
}
else if (type === 'buildingBlocks' || type === 'other') {
itemId = name;
}
if (!itemId) return;

data = data.match(RUSTLABS_ITEM_UPKEEP_AREA_REGEX);
if (data === null || data.length !== 1) {
console.log(' - No upkeep data found.');
return;
}
data = data[0];

let matches = [...data.matchAll(RUSTLABS_ITEM_UPKEEP_REGEX)];
if (matches.length === 0) {
console.log(' - No upkeep data found.');
return;
}

const content = [];
for (const match of matches) {
if (match.length !== 5) {
console.log(' - No upkeep data found.');
return;
}

const upkeepItemShortname = match[1];
const upkeepItemName = Utils.decodeHtml(match[2]);
const upkeepQuantity = match[4];

const upkeepItemId = Object.keys(ITEMS).find(e =>
ITEMS[e].shortname === upkeepItemShortname && ITEMS[e].name === upkeepItemName);

if (!upkeepItemId) return;

content.push({
id: upkeepItemId,
quantity: upkeepQuantity
});
}

rustlabsUpkeepData[type][itemId] = content;
}

async function main() {
await processAll();

Expand All @@ -941,6 +1000,7 @@ async function main() {
Fs.writeFileSync(`${__dirname}/rustlabsDespawnData.json`, JSON.stringify(rustlabsDespawnData, null, 2));
Fs.writeFileSync(`${__dirname}/rustlabsStackData.json`, JSON.stringify(rustlabsStackData, null, 2));
Fs.writeFileSync(`${__dirname}/rustlabsDecayData.json`, JSON.stringify(rustlabsDecayData, null, 2));
Fs.writeFileSync(`${__dirname}/rustlabsUpkeepData.json`, JSON.stringify(rustlabsUpkeepData, null, 2));
}

main();
3 changes: 3 additions & 0 deletions src/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@
"commandsSwitchEditDesc": "Edit the properties of a Smart Switch.",
"commandsSwitchEditIdDesc": "The ID of the Smart Switch.",
"commandsSwitchEditImageDesc": "Set the image that best represent the Smart Switch.",
"commandsUpkeepDesc": "Display the upkeep cost of an item.",
"commandsUptimeBotDesc": "Display uptime of bot.",
"commandsUptimeDesc": "Display uptime of the bot and server.",
"commandsUptimeServerDesc": "Display uptime of server.",
Expand Down Expand Up @@ -291,6 +292,7 @@
"couldNotFindRole": "Could not find role: {roleId}",
"couldNotFindStackDetails": "Could not find stack details for {name}.",
"couldNotFindTeammate": "Could not find teammate: {name}.",
"couldNotFindUpkeepDetails": "Could not find upkeep details for {name}.",
"couldNotFindUser": "Could not find user: {userId}",
"couldNotGetChannelWithId": "Could not get channel with id: {id}.",
"couldNotIdentifyMember": "Could not identify team member: {name}.",
Expand Down Expand Up @@ -726,6 +728,7 @@
"unmutedCap": "UNMUTED",
"updateCap": "UPDATE",
"upkeep": "Upkeep",
"upkeepForItem": "Upkeep for {item} is {cost}.",
"userAddedToBlacklist": "{user} was added to blacklist.",
"userAlreadyInBlacklist": "{user} already in blacklist.",
"userButtonInteraction": "Button Interaction - Guild: {guild}, Channel: {channel}, User: {user}, CustomId: {customid}, VerifyId: {id}.",
Expand Down

0 comments on commit 01fb986

Please sign in to comment.