143 lines
5.2 KiB
JavaScript
143 lines
5.2 KiB
JavaScript
const { EmbedBuilder } = require('discord.js');
|
|
const { Inventory, addInventory } = require('../structures/inventory');
|
|
|
|
module.exports = {
|
|
name: "search",
|
|
debug: false,
|
|
global: false,
|
|
description: "Search a users inventory",
|
|
usage: "[user]",
|
|
permissions: {
|
|
channel: ["VIEW_CHANNEL", "SEND_MESSAGES", "EMBED_LINKS"],
|
|
member: [],
|
|
},
|
|
options: [
|
|
{
|
|
name: "user",
|
|
description: "User to search",
|
|
value: "user",
|
|
type: 6,
|
|
required: true,
|
|
},
|
|
{
|
|
name: "item-number",
|
|
description: "Item to view",
|
|
value: 1,
|
|
type: 4,
|
|
required: false,
|
|
},
|
|
],
|
|
SlashCommand: {
|
|
/**
|
|
*
|
|
* @param {require("../structures/QuarksBot")} client
|
|
* @param {import("discord.js").Message} message
|
|
* @param {string[]} args
|
|
* @param {*} param3
|
|
*/
|
|
run: async (client, interaction, args, { GuildDB }) => {
|
|
if (GuildDB.customChannelStatus==true&&!GuildDB.allowedChannels.includes(interaction.channel_id)) {
|
|
return interaction.send({ content: `You are not allowed to use the bot in this channel.` });
|
|
}
|
|
|
|
if (GuildDB.onlyOfficersSearch && !client.exists(GuildDB.officerRole)) {
|
|
return interaction.send({ content: 'The officer role is required to use this command but has not been configured for this server.' });
|
|
}
|
|
|
|
if (GuildDB.onlyOfficersSearch && !interaction.member.roles.includes(GuildDB.officerRole)) {
|
|
return interaction.send({ content: 'You don\'t have the permissions to use this command.' });
|
|
}
|
|
|
|
let targetUserID = args[0].value.replace('<@!', '').replace('>', '');
|
|
let targetUserInventory = await client.dbo.collection("inventories").findOne({"inventory.userID":targetUserID}).then(inventory => inventory);
|
|
let inventory;
|
|
|
|
// This lame line of code to get username without ping on discord
|
|
const User = client.users.cache.get(targetUserID);
|
|
|
|
if (!targetUserInventory) {
|
|
inventory = {
|
|
userID: interaction.member.user.id,
|
|
items: [{
|
|
name: 'Wallet',
|
|
type: 'wallet',
|
|
description: '`/inventory wallet` to view wallet',
|
|
cash: 0.00,
|
|
ID: {
|
|
firstname: '',
|
|
lastname: '',
|
|
dob: ''
|
|
}
|
|
}]
|
|
}
|
|
|
|
// Register inventory for user with item givn
|
|
let newInventory = new Inventory();
|
|
newInventory.createInventory(targetUserID, GuildDB.serverID);
|
|
newInventory.save().catch(err => {
|
|
if (err) return client.sendInternalError(interaction, err);
|
|
});
|
|
} else inventory = targetUserInventory.inventory;
|
|
|
|
if (!client.exists(inventory.guilds[GuildDB.serverID])) {
|
|
const success = addInventory(inventory.guilds, GuildDB.serverID, targetUserID, client);
|
|
if (!success) return client.sendInternalError(interaction, 'Failed to add inventory');
|
|
}
|
|
|
|
// Display Inventory
|
|
if (args[1]) {
|
|
if (args[1].value < 0 || args[1].value > inventory.guilds[GuildDB.serverID].items.length) {
|
|
let embed = new EmbedBuilder()
|
|
.setTitle("Please provide a valid item number")
|
|
.setColor(client.config.Colors.Red)
|
|
|
|
return interaction.send({ embeds: [embed] });
|
|
}
|
|
|
|
let item = inventory.guilds[GuildDB.serverID].items[args[1].value-1];
|
|
let itemEmbed = new EmbedBuilder().setColor(client.config.Colors.Default);
|
|
if (item.type == 'wallet') {
|
|
// Display wallet
|
|
|
|
let ID = `${client.exists(item.ID.firstname) ? item.ID.firstname : '...'} ${client.exists(item.ID.lastname) ? item.ID.lastname : '...'} | ${client.exists(item.ID.dob) ? item.ID.dob : 'N/A'}`
|
|
|
|
itemEmbed.setTitle(`${User.tag.split('#')[0]}'s Wallet`);
|
|
itemEmbed.addFields(
|
|
{ name: '**Cash**', value: `$${item.cash.toFixed(2)}`, inline: true },
|
|
{ name: '**ID**', value: ID, inline: false });
|
|
|
|
} else if (item.type == 'firearm') {
|
|
// Display firearm
|
|
|
|
itemEmbed.setTitle(`${User.tag.split('#')[0]}'s Firearm`);
|
|
itemEmbed.addFields(
|
|
{ name: 'Serial #', value: item.serial, inline: false },
|
|
{ name: 'Type', value: item.weaponType, inline: false },
|
|
{ name: 'Owner', value: item.ownerName, inline: false }
|
|
);
|
|
|
|
} else {
|
|
// Display generic item here
|
|
|
|
itemEmbed.setTitle(`Item information`);
|
|
itemEmbed.addFields({ name: item.name, value: item.description, inline: false });
|
|
}
|
|
|
|
return interaction.send({ embeds: [itemEmbed], flags: (1 << 6) });
|
|
}
|
|
|
|
let inventoryEmbed = new EmbedBuilder()
|
|
.setColor(client.config.Colors.Default)
|
|
.setTitle(`${User.tag.split('#')[0]}'s Inventory`);
|
|
|
|
let description = "```";
|
|
for (let i = 0; i < inventory.guilds[GuildDB.serverID].items.length; i++) {
|
|
description += `${i+1}. ${inventory.guilds[GuildDB.serverID].items[i].name} - ${inventory.guilds[GuildDB.serverID].items[i].description}\n`;
|
|
}
|
|
description += "```";
|
|
inventoryEmbed.setDescription(description);
|
|
|
|
return interaction.send({ embeds: [inventoryEmbed], flags: (1 << 6) });
|
|
},
|
|
},
|
|
} |