MongoDB won't create a messageCount value - javascript

so I'm making a message count command with discord.js and MongoDB but the "messageCount" value just never gets created. I searched it up and looked at docs but I couldn't find what was wrong.
Codes:
message-counter.js file:
const mongo = require("mongoose")
const schema = require("./schemas/rank/message-count-schema")
module.exports = client => {
client.on('messageCreate', async (message) => {
const { author } = message
const { id } = author
mongo.connect(
process.env.MONGO_URI,
{ keepAlive: true }
)
const dataQuery = await schema.findOne({ _id: id })
if (!dataQuery) {
const newSchem = schema(
{
_id: id
},
{
$setOnInsert: { messageCount: 1 }
},
{
$inc: { messageCount: 1 }
},
{
upsert: true
}
)
await newSchem.save()
}
else {
dataQuery.updateOne(
{
$inc: { messageCount: 1 }
},
{
upsert: true
}
)
}
})
}
message-count-schema.js file:
const mongoose = require('mongoose');
const messageCountSchema = new mongoose.Schema({
_id: {
type: String,
required: true
},
messageCount: {
type: Number,
required: true
}
})
module.exports = mongoose.model('message-count', messageCountSchema);
Can someone tell me what's wrong and how to fix it? I'm not asking to be spoon fed, just what's wrong.

The thing is $setOnInsert is only valid for update operations when the upsert value is set to true. It exists in these methods => updateOne(), updateMany, findAndModify(). In this case, when it not an update operation, the $setOnInsert doesn't run which is why your data doesn't have the messageCount value.

Like caladan said before, and adding to that you need .findbyId() if you want to use _id, is your message count is per user or per guild?
if per user you can add to your schema a userID String item and use
const member = await message.mentions.members.first() || message.guild.members.cache.get(args[0]) || message.author;
const dataQuery = await schema.findOne({ UserID: member.id })
const messagecount = dataQuery.messageCount;
console.log(messagecount)
If per guild, you can add GildId item in your schema, and use:
const dataQuery = await schema.findOne({ GuildId: message.guild.id })
const messagecount = dataQuery.messageCount;
console.log(messagecount)

Related

How to add an array of roles to member at once discord.js v13

I want to add the "Auto-Role System" to my discord bot.
I was doing well but it went into an error, You can check the end of the article for errors.
What I want to do is:
The owner uses the command by mentioning a role or a bunch of roles
Bot stores them in an array and then saves it on the database
When a user joined the guild, the bot gives that roles array to a member
So at first, we need to make a model for the database so I did create one:
// Guild.js
const mongoose = require('mongoose');
const guildConfigSchema = mongoose.Schema({
guildId: { type: String, match: /\d{18}/igm, required: true },
autoRoleDisabled: {
type: Boolean,
},
autoRoleRoles: {type: Array},
});
module.exports = mongoose.model('guild', guildConfigSchema);
Then I coded the setup command:
const role = message.mentions.roles.first();
if (!role) return message.channel.send('Please Mention the Role you want to add to other Auto Roles.');
Schema.findOne({ guildId: message.guild.id }, async (err, data) => {
if (data) {
data.autoRoleDisabled = false;
data.autoRoleRoles.push(role.id);
data.save();
} else {
new Schema({
guildId: message.guild.id,
autoRoleDisabled: false,
$push: { autoRoleRoles: role.id }
}).save();
}
message.channel.send('Role Added: ' + `<#&${role.id}>`);
})
In the end we need to make it work:
// Main.js
client.on("guildMemberAdd", async (member) => {
// ****Auto-Role****
const Welcome = require('./models/Guild');
try {
Welcome.findOne({ guildId: member.guild.id }, async (err, data) => {
if (!data) {
return;
} else {
if (data.autoRoleDisabled == false) {
let roles = data.autoRoleRoles;
roles.forEach(r => {
guildRrole = member.guild.roles.cache.find(role => role.id)
member.roles.add(guildRrole);
})
} else {
return;
}
}
});
} catch (e) {
console.log(e);
}
});
But it doesn't work and gives an error:
Error: cyclic dependency detected
at serializeObject (C:\Users\Pooyan\Desktop\PDMBot\node_modules\bson\lib\bson\parser\serializer.js:333:34)
And I think the problem is from pushing role IDs in the array.
Notes: I am using discord.js#13.8.0 and Node.js v16
You can simply change
guildRrole = member.guild.roles.cache.find(role => role.id)
member.roles.add(guildRrole);
to
guildRrole = member.guild.roles.cache.get(r);
if (!guildRrole) return;
member.roles.add(guildRrole);
because you are not finding role with id, you just did .find(role => role.id) which just gives role id so you have to check role id with your role id if you want to do with .find() like:
.find((role) => {
role.id === r
})

How to push data with Mongoose to a nested array in MongoDB

I'm trying to push data to a nested array in mongodb. I'm using mongoose as well.
This is just mock code to see if i can get it working:
User model:
import mongoose from "mongoose";
const CoinSchema = new mongoose.Schema({
coinID: { type: String },
});
const CoinsSchema = new mongoose.Schema({
coin: [CoinSchema],
});
const WatchlistSchema = new mongoose.Schema({
watchlistName: { type: String },
coins: [CoinsSchema],
});
const NameSchema = new mongoose.Schema({
firstName: { type: String },
lastName: { type: String },
username: { type: String },
});
const UserSchema = new mongoose.Schema({
name: [NameSchema],
watchlists: [WatchlistSchema],
test: String,
});
const User = mongoose.model("User", UserSchema);
export default User;
route:
fastify.put("/:id", async (request, reply) => {
try {
const { id } = request.params;
const newCoin = request.body;
const updatedUser = await User.findByIdAndUpdate(id, {
$push: { "watchlists[0].coins[0].coin": newCoin },
});
await updatedUser.save();
// console.dir(updatedUser, { depth: null });
reply.status(201).send(updatedUser);
} catch (error) {
reply.status(500).send("could not add to list");
}
});
request.body // "coinID": "test"
I've tried a lot of different ways to push this data but still no luck. I still get 201 status codes in my terminal which indicates something has been pushed to the DB, but when I check nothing new is there.
Whats the correct way to target nested arrays and push data to them?
It's not perfect but you could get the user document, update the user's watchlist, and then save the updated watchlist like so:
fastify.put("/:id", async (request, reply) => {
try {
const { id } = request.params;
const newCoin = request.body;
// get the user
let user = await User.findById(id);
// push the new coin to the User's watchlist
user.watchlists[0].coins[0].coin.push(newCoin);
//update the user document
const updatedUser = await User.findOneAndUpdate({ _id: id },
{
watchlists: user.watchlists,
},
{
new: true,
useFindAndModify: false
}
);
reply.status(201).send(updatedUser);
} catch (error) {
reply.status(500).send("could not add to list");
}
});

My customizable welcome channel feature for my Discord bot isn't working, it looks like a problem with MongoDB but I'm unable to figure it out

My customizable welcome channel feature for my Discord bot isn't working. I use MongoDB so I can make it customizable per-server.
There are 3 relevant files: welcome.js (my schema file), guildMemberAdd.js (my event file) and setwelcome.js (the command I use to set the welcome channel.)
The welcome.js file:
const mongoose = require('mongoose');
const schema = mongoose.Schema({
_id: {
type: String,
required: true
},
welcomeChannelId: {
type: String,
required: true
},
welcomeText: {
type: String,
required: true
}
});
module.exports = mongoose.model('welcome', schema);
The guildMemberAdd.js file:
const { MessageEmbed } = require('discord.js');
const schema = require('../models/welcome.js')
const welcomeData = {}
module.exports = {
name: 'guildMemberAdd',
async execute(member) {
const g = member.guild;
const ms = require('ms');
const timeSpan = ms('10 days');
//Alt detection
const createdAt = new Date(member.user.createdAt).getTime();
const difference = Date.now() - createdAt;
if (difference < timeSpan) {
member.send('Bye, alt.');
member.ban({ reason: 'This is an alt.' });
}
//Welcome Users
let data = welcomeData[member.guild.id]
if (!data) {
const results = await schema.find({
_id: member.guild.id
})
if (!results) {
return
}
const { welcomeChannelId, welcomeText } = results
const channel = member.guild.channels.cache.get(welcomeChannelId)
data = welcomeData[member.guild.id] = [channel, welcomeText]
}
data[0].send({
content: data[1].replace(/#/g, `<#${member.id}>`)
})
},
};
The setwelcome.js file
const { MessageEmbed } = require('discord.js');
const schema = require('../../models/welcome.js')
module.exports = {
name: 'setwelcome',
description: 'Sets the welcome message for the server.',
options: [{
name: 'channel',
description: 'The channel to set as the welcome channel.',
type: 'CHANNEL',
required: true
},
{
name: 'message',
description: 'The welcome message.',
type: 'STRING',
required: true
}],
async execute(interaction) {
const channel = await interaction.options.getChannel('channel')
const message = await interaction.options.getString('message')
if (
channel.type !== 'GUILD_TEXT' &&
channel.type !== 'GUILD_NEWS' &&
channel.type !== 'GUILD_NEWS_THREAD' &&
channel.type !== 'GUILD_PUBLIC_THREAD' &&
channel.type !== 'GUILD_PRIVATE_THREAD'
)
return interaction.reply('That is not a valid channel type.');
await schema.findOneAndUpdate({
_id: interaction.guild.id,
},
{
_id: interaction.guild.id,
welcomeChannelId: channel.id,
welcomeText: message
},
{
upsert: true
})
await interaction.reply(`Welcome channel is set to ${channel} and welcome message is set to \`${message}\`.`)
},
};
When a new member joins the guild, it throws this error:
/home/runner/MultiBot/events/guildMemberAdd.js:38
data[0].send({
^
TypeError: Cannot read properties of undefined (reading 'send')
at Object.execute (/home/runner/MultiBot/events/guildMemberAdd.js:38:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Please help me out, thanks in advance!
This means that message is nullish. Make sure the argument is actually there. There are 3 ways:
Making the slash command options required (recommended)
Returning early if the argument is not found
if (!message) return; //after the 'message' declaration
Setting a default value, this uses the logical OR operator (the nullish coalescing operator will also suffice)
const message = await interaction.options.getString('message') || "Welcome to the server!"
It might be because data is an array/object and doesn't have a send method. You would need to find the channel before sending it with something like
const channel = await client.channels.fetch(welcomeChannelId);
replace const { welcomeChannelId, welcomeText } = results with const { welcomeChannelId, welcomeText } = results[0] as results is an array
or
use const results = await schema.findOne({ _id: member.guild.id })
to have results be only one object

i want to make the command that makes changes in the roles work in dm's in javasript

I made a bot that verifies people with API and stores the data in mongoose but I want the code to work in discord DMS but I have no clue how to make it give roles in a specific server when the command is run in DMS this is my code:
const fetch = require('node-fetch')
const ignSchema = require('../schemas/ign-schema')
const mongo = require('../mongo')
module.exports = {
commands: ['verifyme'],
minArgs: 0,
maxArgs: null,
expectedArgs: "<minecraft name>",
callback: async(message, arguments, text) => {
const playerName = arguments.join(' ')
fetch(`https://api.hypixel.net/player?key=MYAPIKEY&name=${playerName}`)
.then(response => response.json())
.then(async data => {
player = data
const target = message.author
const author2 = message.author.tag
const uuid = data["player"]["uuid"]
const discordid = data["player"]["socialMedia"]["links"]["DISCORD"]
let verifyRole = message.guild.roles.cache.find(role => role.name === '[Verified]');
let memberTarget = message.guild.members.cache.get(target.id);
const guildId = message.guild.id
const userId = message.author.id
const UUID = uuid
const _id = UUID
if (discordid == author2) {
await mongo().then(async mongoose => {
try {
const results2 = await ignSchema.findOne({
_id,
})
const {
UUID,
userData,
discordName
} = results2
if (UUID == uuid) {
if (author2 == discordName) {
if (message.member.roles.cache.some(role => role.name === "[Verified]")) {
message.reply('you are already verified')
} else {
memberTarget.roles.add(verifyRole)
message.reply('welcome back')
}
} else {
message.reply(`you already used this minecraft account to verify ${discordName}, if you want to change this please contact <#390929478790152192>`)
mongoose.connection.close()
return
}
} else {}
} catch {
const userData = {
timestamp: new Date().getTime(),
}
await mongo().then(async(mongoose) => {
try {
await ignSchema.findOneAndUpdate({
_id
}, {
UUID,
discordName: author2,
HypixelName: playerName,
userId: userId,
guildId: guildId,
$push: {
userData: userData
},
}, {
upsert: true
})
memberTarget.roles.add(verifyRole)
message.reply('you are succesfully verified')
} finally {
mongoose.connection.close()
}
})
}
})
} else {
message.reply(`change your linked discord account in hypixel from ${discordid} to ${author2},`)
}
console.log(`${discordid}`)
})
},
}
and this is the error I get:
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'roles' of null
If there is more than one server the bot is in, this is not possible without making major assumptions as to the guild they are in.
If there is only one server, message.guild.roles can be changed to client.guilds.cache.get('your_servers_id').roles.

MongoDB/Express: Why does Array.includes return false instead of true?

I'm working on an tiny app that allows user to participate in polls, but I'm having problems checking if the current user has already voted in the poll. Everything else works fine, save for the IIFE that checks for said condition, as seen in the code snippet included. Indeed, I'm getting false as opposed to true with the data I have i.e. I already seeded the DB with sample data, including a random poll that contains the array of IDs for users who've already voted. I tried testing one ID against said array, which returns false as opposed to the expected true. What gives?
Below are the relevant snippets.
Model
import mongoose from 'mongoose';
const Schema = mongoose.Schema;
const ChoiceSchema = new Schema({
name: { type: String },
votes: { type: Number }
});
const PollSchema = new Schema({
title: { type: String },
category: { type: String },
choices: [ChoiceSchema],
addedBy: { type: Schema.Types.ObjectId, ref: 'User' },
votedBy: [{ type: Schema.Types.ObjectId, ref: 'User' }]
});
const Poll = mongoose.model('Poll', PollSchema);
export default Poll;
Controllers
import Poll from '../models/poll';
export default {
fetchAllPolls: async (req, res) => {
/*...*/
},
fetchSpecificPoll: async (req, res) => {
/*...*/
},
voteInPoll: async (req, res) => {
const { category, pollId } = req.params;
const { name, choiceId, voterId } = req.body;
try {
const poll = await Poll.findById(pollId);
const choice = await poll.choices.id(choiceId);
const votedChoice = {
name,
votes: choice.votes + 1,
};
// Check if user has already voted in poll
const hasVoted = ((votersIds, id) => votersIds.includes(id))(
poll.votedBy,
voterId
);
if (!voterId) {
res
.status(400)
.json({ message: 'Sorry, you must be logged in to vote' });
} else if (voterId && hasVoted) {
res.status(400).json({ message: 'Sorry, you can only vote once' });
} else {
await choice.set(votedChoice);
await poll.votedBy.push(voterId);
poll.save();
res.status(200).json({
message: 'Thank you for voting. Find other polls at: ',
poll,
});
}
} catch (error) {
res.status(404).json({ error: error.message });
}
},
createNewPoll: async (req, res) => {
/*...*/
},
};
I think you are trying to compare ObjectId with String representing the mongo id.
This should work:
const hasVoted = ((votersIds, id) => votersIds.findIndex((item) => item.toString() === id) !== -1)(
poll.votedBy,
voterId
);
or
const hasVoted = ((votersIds, id) => votersIds.findIndex((item) => item.equals(new ObjectId(id))) !== -1)(
poll.votedBy,
voterId
);
EDIT:
As #JasonCust suggested, a simpler form should be:
const hasVoted = poll.votedBy.some(voter => voter.equals(voterId));
It is more than likely that poll.votedBy is not an array of ID strings. If you are using it as a reference field then it is an array of BSON objects which would fail using includes because it uses the sameValueZero algorithm to compare values. If that is true then you could either convert all of the IDs to strings first or you could use find and the equals methods to find a match.
Update: showing actual code example
Also, some would provide an easier method for returning a boolean value.
const hasVoted = poll.votedBy.some((voter) => voter.equals(voterId));

Categories