Mongo DB remove 1 item from an array - javascript

I have a buy system in place for my discord bot where it adds something to the inventory array in mongoDB:
data.Inventory[itemTobuy] ++;
It gets stored like this:
pizza: 1
And i have a use system where you can use items in your inventory. I would like to remove the whole thing if theres only 1 instance of this item but if there is more i would like to decrement the value by one. Say from pizza: 5 to pizza: 4.
Althought im only 25% sure on how i would do such things
The full code for the command that adds the item if needed:
const { Client, Message, MessageEmbed } = require('discord.js');
const { User } = require("../../databasing/schemas/User")
const inventory = require('../../databasing/schemas/inventory')
const items = require('../../items/shopItems')
module.exports = {
name: 'buy',
/**
* #param {Client} client
* #param {Message} message
* #param {String[]} args
*/
run: async(client, message, args) => {
userData = await User.findOne({ id: message.author.id }) || new User({ id: message.author.id })
const itemTobuy = args[0].toLowerCase()
embed = new MessageEmbed({ color: "#2F3136" })
if(!args[0]) return message.channel.send({
embeds: [ embed.setTitle('<:redCross:1004290018724020224> | Please mention something to buy!')]
});
const itemid = !!items.find((val) => val.item.toLowerCase() === itemTobuy);
if(!itemid) return message.channel.send({
embeds: [ embed.setTitle(`<:redCross:1004290018724020224> | ${itemTobuy} is not a valid item!`)]
});
itemPrice = items.find((val) => val.item.toLowerCase() === itemTobuy).price;
userBalance = userData.wallet;
if(userBalance < itemPrice) return message.channel.send({
embeds: [embed.setTitle(`<:redCross:1004290018724020224> | You dont have enough money to buy this item(LOL)`)]
});
const param ={
User: message.author.id
}
inventory.findOne(param, async(err, data) => {
if(data){
const hasItem = Object.keys(data.Inventory).includes(itemTobuy);
if(!hasItem){
data.Inventory[itemTobuy] = 1;
} else {
data.Inventory[itemTobuy] ++;
}
await inventory.findOneAndUpdate(param, data);
} else {
new inventory({
User: message.author.id,
Inventory: {
[itemTobuy]: 1,
}
}).save()
}
message.channel.send({
embeds: [embed.setTitle(`<:greenTick:1004290019927785472> | Successfully bought ${itemTobuy}!`)]
});
userData.wallet -= itemPrice;
userData.save()
})
}
}

You can use $inc method to decrease the value of your item. For example:
const param = {
User: message.author.id
}
const item_name = args.splice(0).join(" ").toLowerString();
//Take note that .toLowerString() will transform any string to lowercase.
//If your data is case sensitive, remove the .toLowerString().
inventory.findOne({
param, //User.id
"Inventory.name": item_name //Change the "name" to something on how you call the name of your items.
}, async(err, data) => {
//Take note that we only identify if the item existing or not.
if(data) {
//Here's where you decrease the amount of the item.
await inventory.findOneAndUpdate({
param,
"Inventory.name": item_name
}, {
$inc: {
"Inventory.$.value": -1 //Change the value on how you call the value of your item
}
})
} else {
//Here is where you send a message that the item doesn't exist.
}
})
EDIT:
To get the name and the value of your item, you need to find the curtain name.
First you need to create your inventory as array.
const inv = await inventory.findOne({
param
})
const arr = inv.Inventory;
Your const arr = inv.Inventory; will make it to be an array. Then next step. Find the name
const item_name = args.splice(0).join(" ").toLowerString();
const inv = await inventory.findOne({
param
})
const arr = inv.Inventory;
const item = arr.find(x => x.name == item_name);
After you get the item_name. You can now get the value and the name aswell and post it in somewhere you wanted. Can be embed or normal message.
const item_name = args.splice(0).join(" ").toLowerString();
const inv = await inventory.findOne({
param
})
const arr = inv.Inventory;
const item = arr.find(x => x.name == item_name);
const items_value = item.value;
const items_name = item.name;
const embed = new MessageEmbed()
.addFields(
{name: `Name:`, value: `${items_name}`},
{name: `Value:`, value: `${items_value}`}
)
message.channel.send({embeds: [embed]})

Related

My findIndex is not working as should using Node.js and mongo DB as database

Ihave some issues trying to find a index of an expecific product in a mongo database.
const cart = await this.model.findOne({ user: { $eq: user } });
if (cart) {
const itemFound = cart.products.findIndex(
(item) => item._id === new ObjectId(obj._id)
);
I'm sending the id from the frontend as a string and I transform it with the new ObjectId, the issue is it gives me -1, when I console log the item._id and new ObjectId(obj._id). Are the same but I dont know why it gives me -1.
this is the whole fuction I want to do:
async editCart(obj, user) {
try {
const cart = await this.model.findOne({ user: { $eq: user } });
if (cart) {
const itemFound = cart.products.findIndex(
(item) => item._id === new ObjectId(obj._id)
);
if (itemFound !== -1) {
let product = cart.products[itemFound];
product.count += obj.count;
const saved = await cart.save();
return saved;
} else {
cart.products.push(obj);
const saved = await cart.save();
return saved;
}
} else {
const newCart = new this.model({
products: obj,
user: user,
});
const saved = await newCart.save();
return saved;
}
} catch (error) {
logger.error(`Error to edit cart ${error}`);
throw new Error(error);
}
}
If you find another way to do it I will be really greatfull
You can use .toString() when you want to compare to ObjectId values:
const itemFound = cart.products.findIndex(
(item) => item._id.toString() === obj._id.toString()
);

removing an object from firestore 9 array using arrayRemove()?

I am trying to remove an object from array in in firestore, but encountered an obstacle what are the requirement or the reference to do the removal ? does one key value in the object sufficient to do the remove or should the object by identical to the one that is getting removed ?
const deleteWeek = async () => {
const docRef = doc(db, 'Weeks', id);
await updateDoc(docRef, {
weeks: arrayRemove({
weekId: '7518005f-7b10-44b6-8e0a-5e41081ee064',
}),
});
};
deleteWeek();
}
however week in data base looks like this
{name ,"Week 2"
days : [/*data all kinds*/]
weekId : "7518005f-7b10-44b6-8e0a-5e41081ee064"}
If it's an array of object, then you need to know the whole object to use arrayRemove() For example, if the a document looks like this:
{
...data
weeks: [
{
name: "Week 2",
days: [/*data all kinds*/]
weekId: "7518005f-7b10-44b6-8e0a-5e41081ee064"}
}
]
}
You'll have to pass the entire week object in arrayRemove(). It might be better to store such data in sub-collections instead so you can query/delete a specific one.
Since there is no function in firestore to delete only a element in array, you need to make arrayRemove refer to the same object you want to delete, then create a new object and insert it with arrayUnion method
in my case, i use to below
const { leave,date,email } = req.body;
const attendanceRef = admin.firestore().collection('Attendance').doc(`${email}`);
const attendanceData = await attendanceRef.get();
const attendanceRecord = attendanceData.data().attendance;
const removeTarget = attendanceRecord.find((item) => item.date === date);
await attendanceRef.update({
attendance: admin.firestore.FieldValue.arrayRemove(removeTarget),
})
const obj = {
...removeTarget,
"leave": leave,
}
await attendanceRef.set({
attendance: admin.firestore.FieldValue.arrayUnion(obj),
},{ merge: true })
const newAttendance = await attendanceRef.get();
const newAttendanceRecord = newAttendance.data().attendance;
return await res.json({
message: '퇴근시간이 저장되었습니다.',
attendance:newAttendanceRecord
});
after update, it maybe if error occured.
if error occured, you need all working cancel.
this case, you may want to use batch method
const admin = require('firebase-admin');
module.exports = async function(req,res) {
const { leave,date,email } = req.body;
const batch = admin.firestore().batch();
const attendanceRef = admin.firestore().collection('Attendance').doc(`${email}`);
const attendanceData = await attendanceRef.get();
const attendanceRecord = attendanceData.data().attendance;
const removeTarget = attendanceRecord.find((item) => item.date === date);
// await attendanceRef.update({
// attendance: admin.firestore.FieldValue.arrayRemove(removeTarget),
// })
batch.update(
attendanceRef,{ attendance: admin.firestore.FieldValue.arrayRemove(removeTarget) }
)
const obj = {
...removeTarget,
"leave": leave,
}
// await attendanceRef.set({
// attendance: admin.firestore.FieldValue.arrayUnion(obj),
// },{ merge: true })
batch.set(
attendanceRef, { attendance: admin.firestore.FieldValue.arrayUnion(obj) },{ merge: true }
)
await batch.commit();
const newAttendance = await attendanceRef.get();
const newAttendanceRecord = newAttendance.data().attendance;
return await res.json({message: '퇴근시간이 저장되었습니다.',attendance:newAttendanceRecord});
}
hope help this for you

How to use sqlite to store the cooldown of a command?

I want to try to store my command cooldown in any sort of storage, I think sqlite will be good, but I don't know how to implement it for a cooldown.. I was checking one guide here https://anidiots.guide/coding-guides/sqlite-based-points-system/#you-get-points-and-you-get-points-and-everybody-gets-points, it's for storage points and levels.. Unfortunately I'm not able edit this example into my needs, to store the cooldown for each user.
'use strict';
const SQLite = require("better-sqlite3");
const sql = new SQLite("./cooldowns.sqlite");
const humanizeDuration = require('humanize-duration');
// Require the necessary discord.js classes
const { Client, Intents, Collection } = require('discord.js');
const config = require("./config.json");
const { MessageEmbed } = require('discord.js');
const cooldowns = new Map();
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
client.on('ready', () => {
const table = sql.prepare("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'scores';").get();
if (!table['count(*)']) {
// If the table isn't there, create it and setup the database correctly.
sql.prepare("CREATE TABLE scores (id TEXT PRIMARY KEY, user TEXT, guild TEXT, points INTEGER, level INTEGER);").run();
// Ensure that the "id" row is always unique and indexed.
sql.prepare("CREATE UNIQUE INDEX idx_scores_id ON scores (id);").run();
sql.pragma("synchronous = 1");
sql.pragma("journal_mode = wal");
}
// And then we have two prepared statements to get and set the score data.
client.getCooldown = sql.prepare("SELECT * FROM scores WHERE user = ? AND guild = ?");
client.setCooldown = sql.prepare("INSERT OR REPLACE INTO scores (id, user, guild, points, level) VALUES (#id, #user, #guild, #points, #level);");
client.user.setActivity("thinking...", { type: 'PLAYING' });
console.log('Bot is online!')
});
client.on("messageCreate", message => {
if (message.author.bot) return;
// This is where we'll put our code.
if (message.content.indexOf(config.prefix) !== 0) return;
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === 'mycommand') {
const cooldown = cooldowns.getCooldown(message.author.id);
if (!cooldown) {
cooldowns = {
id: `${message.author.id}`,
user: message.author.id,
cooldown: 0
}
}
if (cooldown) {
const remaining = humanizeDuration(cooldown - Date.now(), { round: true }, { units: ["d","h","m","s"] });
message.reply(`Your cooldown is. ${remaining}`)
return;
}
async function main() {
const messages = await message.channel.messages.fetch({ limit: 1 });
const lastMessage = messages.last();
const isValidDate = (dateString) => new Date(dateString).toString() !== 'Invalid Date'
if(isValidDate(lastMessage.content) == false && lastMessage.content !== 'mycommand')
message.reply("I need your date of birth!.")
if(lastMessage.content === 'mycommand')
message.reply("Please provide me your date of birth.")
if(isValidDate(lastMessage.content) == true && lastMessage.content !== 'mycommand') {
cooldowns.set(message.author.id, Date.now() + 604800000);
message.reply("Check your DMs.")
message.react("emoji"); //react with emoji to the issued command
const predictions = ["Prediction 1", "Prediction 2", "Prediction 3", "Prediction 4", "Prediction 5", "Prediction 6", "Prediction 7"]
const randomprediction = predictions[Math.floor(Math.random() * predictions.length)];
const prediction1 = new MessageEmbed()
.setColor('#ff7518')
.setAuthor(client.user.username, client.user.displayAvatarURL())
.setTitle(`Weekly message` + lastMessage.author.username + `#` + lastMessage.author.discriminator)
.setDescription(randomprediction);
message.author.send({ embeds: [prediction1] });
var random = Math.random()
if (random < 0.9) {
message.author.send("Congrats! You received this message " + '<#' + message.author.id + '>!')
message.channel.send('Congrats again! ' + '<#' + message.author.id + '>');
}
setTimeout(() => cooldowns.delete(message.author.id), 604800000);
client.setCooldown.run(cooldowns);
}
}
}
The problem is I don't know how to edit the sql params, I never used sqlite/anything else before and have 0 experience with it. All I want is to store command in some sort of storage, but I don't know where to start. Any help will be much appreciated!

How to query a firestore search for a name within a document?

What i have set up for my firestore database is one collection called 'funkoPops'. That has documents that are genres of funkoPops, with an array of funkoData that holds all pops for that genre. it looks like this below
I should also note, that the collection funkoPops has hundreds of documents of 'genres' which is basically the funko pop series with the sub collections of funkoData that I web scraped and now need to be able to search through the array field of 'funkoData' to match the name field with the given search parameter.
collection: funkoPops => document: 2014 Funko Pop Marvel Thor Series => fields: funkoData: [
{
image: "string to hold image",
name: "Loki - with helmet",
number: "36"
},
{
image: "string to hold image",
name: "Black and White Loki with Helmet - hot topic exsclusive",
number: "36"
},
{
etc...
}
So how could i run a query in firestore to be able to search in collection('funkoPops'), search through the document fields for name.
I have the ability to search for genres like so, which gives the genre back and the document with the array of data below:
const getFunkoPopGenre = async (req, res, next) => {
try {
console.log(req.params);
const genre = req.params.genre;
const funkoPop = await firestore.collection("funkoPops").doc(genre);
const data = await funkoPop.get();
if (!data.exists) {
res.status(404).send("No Funko Pop found with that search parameter");
} else {
res.send(data.data());
}
} catch (error) {
res.status(400).send(error.message);
}
};
what i am trying to use to search by the field name is below and returns an empty obj:
const getFunkoPopName = async (req, res, next) => {
try {
const name = req.params.name;
console.log({ name });
const funkoPop = await firestore
.collection("funkoPops")
.whereEqualTo("genre", name);
const data = await funkoPop.get();
console.log(data);
res.send(data.data());
} catch (error) {
res.status(400).send(error);
}
};
Any help would be great, thanks!
So the way i went about answering this as it seems from top comment and researching a little more on firebase, you do you have to match a full string to search using firebase queries. Instead, I query all docs in the collection, add that to an array and then forEach() each funkoData. From there i then create a matchArray and go forEach() thru the new funkoData array i got from the first query. Then inside that forEach() I have a new variable in matches which is filter of the array of data, to match up the data field name with .inlcudes(search param) and then push all the matches into the matchArr and res.send(matchArr). Works for partial of the string as well as .includes() matches full and substring. Not sure if that is the best and most efficient way but I am able to query thru over probably 20k data in 1-2 seconds and find all the matches. Code looks like this
try {
const query = req.params.name.trim().toLowerCase();
console.log({ query });
const funkoPops = await firestore.collection("test");
const data = await funkoPops.get();
const funkoArray = [];
if (data.empty) {
res.status(404).send("No Funko Pop records found");
} else {
data.forEach((doc) => {
const funkoObj = new FunkoPop(doc.data().genre, doc.data().funkoData);
funkoArray.push(funkoObj);
});
const matchArr = [];
funkoArray.forEach((funko) => {
const genre = funko.genre;
const funkoData = funko.funkoData;
const matches = funkoData.filter((data) =>
data.name.toLowerCase().includes(query)
);
if (Object.keys(matches).length > 0) {
matchArr.push({
matches,
genre,
});
}
});
if (matchArr.length === 0) {
res.status(404).send(`No Funko Pops found for search: ${query}`);
} else {
res.send(matchArr);
}
}
} catch (error) {
res.status(400).send(error.message);
}
with a little bit of tweaking, i am able to search for any field in my database and match it with full string and substring as well.
update
ended up just combining genre, name, and number searches into one function so that whenver someone searches, the query param is used for all 3 searches at once and will give back data on all 3 searches as an object so that we can do whatever we like in front end:
const getFunkoPopQuery = async (req, res) => {
try {
console.log(req.params);
const query = req.params.query.trim().toLowerCase();
const funkoPops = await firestore.collection("test");
const data = await funkoPops.get();
const funkoArr = [];
if (data.empty) {
res.status(404).send("No Funko Pop records exsist");
} else {
data.forEach((doc) => {
const funkoObj = new FunkoPop(doc.data().genre, doc.data().funkoData);
funkoArr.push(funkoObj);
});
// genre matching if query is not a number
let genreMatches = [];
if (isNaN(query)) {
genreMatches = funkoArr.filter((funko) =>
funko.genre.toLowerCase().includes(query)
);
}
if (genreMatches.length === 0) {
genreMatches = `No funko pop genres with search: ${query}`;
}
// name & number matching
const objToSearch = {
notNullNameArr: [],
notNullNumbArr: [],
nameMatches: [],
numbMatches: [],
};
funkoArr.forEach((funko) => {
const genre = funko.genre;
if (funko.funkoData) {
const funkoDataArr = funko.funkoData;
funkoDataArr.forEach((data) => {
if (data.name) {
objToSearch.notNullNameArr.push({
funkoData: [data],
genre: genre,
});
}
if (data.number) {
objToSearch.notNullNumbArr.push({
funkoData: [data],
genre: genre,
});
}
});
}
});
// find name that includes query
objToSearch.notNullNameArr.forEach((funko) => {
const genre = funko.genre;
const name = funko.funkoData.filter((data) =>
data.name.toLowerCase().includes(query)
);
if (Object.keys(name).length > 0) {
objToSearch.nameMatches.push({
genre,
name,
});
}
});
// find number that matches query
objToSearch.notNullNumbArr.forEach((funko) => {
const genre = funko.genre;
const number = funko.funkoData.filter((data) => data.number === query);
if (Object.keys(number).length > 0) {
objToSearch.numbMatches.push({
genre,
number,
});
}
});
if (objToSearch.nameMatches.length === 0) {
objToSearch.nameMatches = `No funko pops found with search name: ${query}`;
}
if (objToSearch.numbMatches.length === 0) {
objToSearch.numbMatches = `No funko pop numbers found with search: ${query}`;
}
const searchFinds = {
genre: genreMatches,
name: objToSearch.nameMatches,
number: objToSearch.numbMatches,
};
res.send(searchFinds);
}
} catch (error) {
res.status(400).send(error.message);
}
};
If anyone is well suited in backend and knows more about firestore querying, please let me know!

Whenever I react to an embed, It sends double messages. [DiscordAPIError: Unknown Message]

I have a waifu command where it gets an image and a name and puts it in a embed, it also then reacts with the 💖 emoji. I wanted to make it so the first person who clicked the emoji would claim the waifu.
const { Client, MessageEmbed, ReactionCollector} = require('discord.js');
const {
prefix
} = require('../../config');
const superagent = require('superagent');
const {
urlencoded
} = require('body-parser');
module.exports = {
name: 'waifu',
category: 'waifu',
description: 'Random Waifu',
usage: `${prefix}waifu`,
perms: 'Send Messages',
cooldown: 5,
run: async (bot, message, args) => {
const rating1 = 10
const rating2 = Math.floor(Math.random() * rating1)
var rating = rating2
const decimals1 = 100
const decimals2 = Math.floor(Math.random() * decimals1)
var decimals = decimals2
const compatibility1 = 100
const compatibility2 = Math.floor(Math.random() * compatibility1)
var compatibility = compatibility2
const {
waifuID
} = require("../../Database/WaifuNameDB.json")
let randW = Math.floor(Math.random() * Object.keys(waifuID).length)
let randomWaifu = waifuID[randW]
let embed2 = new MessageEmbed()
.setTitle(`🎀${randomWaifu.names}🎀`)
.addField("Claims:", `${randomWaifu.claims}`)
.addField("Rating:", `${rating}.${decimals}/10 ⭐`)
.setImage(`${randomWaifu.img_url}`, innerHeight = '500', innerWidth = '500')
.setColor('#f095d1')
.setFooter(`| Powered by: #Twintails🎀 API `, `https://64.media.tumblr.com/1a1c3bcc08b5a048b90139a56fe7f415/tumblr_o9ku1rVS8z1vnqjx7o2_250.png`)
var mg = await message.channel.send(embed2);
mg.react('💖')
message.delete()
bot.on('messageReactionAdd', async (reaction, user) => {
if (reaction.mg) await reaction.mg.fetch();
if (reaction) await reaction.fetch()
if (user.bot) return;
if (reaction.emoji.name == "💖") {
message.channel.send(`${randomWaifu.names} was claimed!`)
mg.delete()
if(user = message.author) return;
}
})
}
}
It works, but if for example I do /waifu, it sends the embed and says (waifuname) was claimed, but whenever I do /waifu again, everything is the same, but when I click the react button, it says that the previous and the current waifu was claimed.
https://aws1.discourse-cdn.com/business6/uploads/glitch/original/2X/9/98363d9496747899fe23698cb0f98846b1e7136c.jpeg
(Umaru Doma was the previous waifu I rolled)
It also gives an error:
(node:9964) UnhandledPromiseRejectionWarning: DiscordAPIError: Unknown Message
Please help if you can!
I have figured out a way to fix this everyone!
bot.on('messageReactionAdd', (messageReaction, user) => {
if(user.bot) return;
const {
message,
emoji
} = messageReaction;
if (emoji.name === "💖") {
if (!message.id === mg.id) return;
if (message.id === mg.id) {
mg.delete()
message.channel.send(`${randomWaifu.names} was claimed!`)
}
}
});

Categories

Resources