Get updated value from database - node.js

How do I watch for a specifc Mongodb-query? Currently I have tried with collection.watch(), but I am only getting the new value.
An example:
user1 = [user3, user4]
user2 = [user4]
If user1 follows user2, user2 should get a sign that user1 has followed. When using mongodb .watch(), I am getting [user4, user1] for user2, but I cant "detect" the new follower.
To be clear: I am searching for a solution like Instagram for instance, when someone follows you, you get a message that a user has started to follow you

Related

Get highest role of a member with mentions

How can I get the highest role name of a mentioned member? I tried something like this but it doesn't work. Thanks! :) btw this is a ban command and I need this bc my bot is crashing when someone is trying to ban a user with a higher rank than bot.
if(message.member.hasPermission('BAN_MEMBERS')){
const user = message.mentions.users.first()
console.log(user.roles.highest.name)
if(!user) return console.log("test1")
if(!args[2]) return console.log("test2")
const ddays = args[1]
What you could do is:
Getting a user's highest role:
Get the UserId from the Mention inside of the Message
Get the Cache from the RoleManager of the Guild in which the Message was sent in
(I don't know if the roles in RoleCache are sorted by position, so sort if needed)
Iterate through the roles in RoleCache and check if the UserId is contained inside a specific role
Get the position of the role
Getting the bot's highest role:
Repeat steps 2-5 for your bot (or integrate them within the previous iteration of the RoleCache)
Then compare both numbers and find out if the bot's "role number" is higher than the one of the user's.

Mongo function to find if superstring exists in array. Nodejs,

A document in my mongo 'companies' collection looks like this:
{
"companyName": "",
"companyIcon": "",
"domains": [
"companyDomainA.com",
"companyDomainB.dev"
],
"allowSubDomains": true
}
In my application the user enters his/her email address.
Using the Nodejs native mongo driver (https://mongodb.github.io/node-mongodb-native), I want to query (find) which company the user belongs to.
The problem is when the user enters the email as name#dept.companyDomainA.com.
I want to be able to query and find the company document of the user based on his email (subdomained 0 or more levels), ie. if the superstring of a string exists in an array in mongo.
(Caveat, I cannot store all the subdomains of the company as they are dynamic and can change at will)
Is there a regular expression way/db schema change way, to achieve this?
Thanks in advance!!
I would do it like this. First find the root domain from the email address. To do that I would split the email and fetch the domain first.
const email = "name#dept.companyDomainA.com";
const domain = email.split('#')[1]; // dept.companyDomainA.com
Now fetch the host (companyDomainA.com) from it. Follow this link.
So, I have found the root domain which is companyDomainA.com. Now run the find query.
db.collection('documents').find({"domains": "companyDomainA.com"});
I didn't test this code.

Discord bot cannot get users collection by ID

I must be missing something because I am fairly sure this should work, since I have used this same method before in a different command in the same bot...
I have a bot that connects to a mysql database. Inside a table in the database that stores the info for a match of a game I'm making, I store the Discord ID's of each player. After pulling those ID's from the table, specifying which one I'd like the user object retrieved for, and trying to use .get() (also attempted to use .find() with a username instead), both result in "undefined"... I have a feeling the bot doesn't find the entire user collection, because when I console.log(client.users), I get Collection [Map] {} So, it looks to be empty? I have also put in the ID itself instead of a variable and it still can't find it. So, I have a feeling the bot just doesn't get the users collection for some reason.
In the below code:
playerIDArray = the player ID's I stored at the start of the match of the game, pulling them from the mysql table. Stored in array format ("player 1's ID" as index 0, "player 2's ID" as index 1, "player 3's ID" as index 2, etc)
playerIndex = the index of the target player in playerIDArray, found in an earlier step before the below code runs, that I know returns what I expect.
const Discord = require('discord.js');
const client = new Discord.Client();
const mysql = require("mysql2/promise");
...
...
var targetUserID = playerIDArray[playerIndex]
var targetUser = client.users.get(targetUserID)
console.log(targetUser)
Each time I run this, I get "undefined" and for the life of me can't figure out why... I just want to retrieve the user group so I can send a message to that user, giving them an opportunity to counter before I modify the mysql table and move cards around that would then need to be immediately altered again to restore what was just removed via a counter card.
I must just be dumb and am missing something but I'm lost as to what to try now... Thanks for any help in advance.
You can only use client.users ( and other discord.js Collections ) after the ready event fired.
So you need to put the code that uses client.users into
client.on('ready', () => { /* CODE */});
ready will be fired after successful client.login('token');.

Firestore - how to listen to realtime changes ignoring the entries that already exist?

When I try the following code given on firebase documentation
doc_ref = db.collection(u'users')
def on_snapshot(doc_snapshot, changes, read_time):
for change in changes:
print(u'new doc:{}'.format(change.document.id))
doc_watch = doc_ref.on_snapshot(on_snapshot)
It prints all the entries even if they already existed before I invoked the listener. I only want to listen to the changes that take place after the listener is invoked and ignore any entries that already exited before I invoked the listener.
Example : if i had 3 documents in my users collection : user1, user2 and user3 already. I run my program and add another document - user4. I my program to print user4 and not user1, user2 or user3.
The SDK doesn't provide a way to query for "everything that doesn't already exist". You should come up with your own query that satisfies what you want. In your case, perhaps you need a timestamp in each on of the documents that indicates when the document was created, and query for only documents with a creation date greater than the current time.

How can I get the current user in jHipster?

Per other answers here my code below should be correct, but I am getting nothing from SecurityUtils. I need to assign a user to a new record in an application. Am I missing something here?
This other response also returns a null user.
User user = userRepository.findOneByLogin(SecurityUtils.getCurrentUserLogin().get()).get();
entity.setUser(user);
entityRepository.save(entity);

Resources