Discord JS - Counting boosts - discord

I have one counter left to create. And for this one I don't really know which direction to take.
I believe counting the number of members with the boost role will be wrong as members can have boosted the server twice.
module.exports = async(client) => {
const guild = client.guilds.cache.get('912706237806829598');
setInterval(async () => {
const boostCount = (await guild.members.fetch()).filter(????????).size;
const channel = guild.channels.cache.get('960832075349499925');
channel.setName(`╭🌕・Boosts: ${boostCount.toLocaleString()}`);
console.log('Updating Boost Count');
console.log(boostCount);
}, 600000);
}
I don't know if I get achieve this by putting something inside filter. Looking at the members is probably not correct as well.
Really need some assistance for this one.

Making use of guild.premiumSubscriptionCount would help you out. It will count all the boosts in total including double boosts.

Related

firebase firestore how to read from a subcollection [duplicate]

I thought I read that you can query subcollections with the new Firebase Firestore, but I don't see any examples. For example I have my Firestore setup in the following way:
Dances [collection]
danceName
Songs [collection]
songName
How would I be able to query "Find all dances where songName == 'X'"
Update 2019-05-07
Today we released collection group queries, and these allow you to query across subcollections.
So, for example in the web SDK:
db.collectionGroup('Songs')
.where('songName', '==', 'X')
.get()
This would match documents in any collection where the last part of the collection path is 'Songs'.
Your original question was about finding dances where songName == 'X', and this still isn't possible directly, however, for each Song that matched you can load its parent.
Original answer
This is a feature which does not yet exist. It's called a "collection group query" and would allow you query all songs regardless of which dance contained them. This is something we intend to support but don't have a concrete timeline on when it's coming.
The alternative structure at this point is to make songs a top-level collection and make which dance the song is a part of a property of the song.
UPDATE
Now Firestore supports array-contains
Having these documents
{danceName: 'Danca name 1', songName: ['Title1','Title2']}
{danceName: 'Danca name 2', songName: ['Title3']}
do it this way
collection("Dances")
.where("songName", "array-contains", "Title1")
.get()...
#Nelson.b.austin Since firestore does not have that yet, I suggest you to have a flat structure, meaning:
Dances = {
danceName: 'Dance name 1',
songName_Title1: true,
songName_Title2: true,
songName_Title3: false
}
Having it in that way, you can get it done:
var songTitle = 'Title1';
var dances = db.collection("Dances");
var query = dances.where("songName_"+songTitle, "==", true);
I hope this helps.
UPDATE 2019
Firestore have released Collection Group Queries. See Gil's answer above or the official Collection Group Query Documentation
Previous Answer
As stated by Gil Gilbert, it seems as if collection group queries is currently in the works. In the mean time it is probably better to use root level collections and just link between these collection using the document UID's.
For those who don't already know, Jeff Delaney has some incredible guides and resources for anyone working with Firebase (and Angular) on AngularFirebase.
Firestore NoSQL Relational Data Modeling - Here he breaks down the basics of NoSQL and Firestore DB structuring
Advanced Data Modeling With Firestore by Example - These are more advanced techniques to keep in the back of your mind. A great read for those wanting to take their Firestore skills to the next level
What if you store songs as an object instead of as a collection? Each dance as, with songs as a field: type Object (not a collection)
{
danceName: "My Dance",
songs: {
"aNameOfASong": true,
"aNameOfAnotherSong": true,
}
}
then you could query for all dances with aNameOfASong:
db.collection('Dances')
.where('songs.aNameOfASong', '==', true)
.get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
console.log(doc.id, " => ", doc.data());
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
NEW UPDATE July 8, 2019:
db.collectionGroup('Songs')
.where('songName', isEqualTo:'X')
.get()
I have found a solution.
Please check this.
var museums = Firestore.instance.collectionGroup('Songs').where('songName', isEqualTo: "X");
museums.getDocuments().then((querySnapshot) {
setState(() {
songCounts= querySnapshot.documents.length.toString();
});
});
And then you can see Data, Rules, Indexes, Usage tabs in your cloud firestore from console.firebase.google.com.
Finally, you should set indexes in the indexes tab.
Fill in collection ID and some field value here.
Then Select the collection group option.
Enjoy it. Thanks
You can always search like this:-
this.key$ = new BehaviorSubject(null);
return this.key$.switchMap(key =>
this.angFirestore
.collection("dances").doc("danceName").collections("songs", ref =>
ref
.where("songName", "==", X)
)
.snapshotChanges()
.map(actions => {
if (actions.toString()) {
return actions.map(a => {
const data = a.payload.doc.data() as Dance;
const id = a.payload.doc.id;
return { id, ...data };
});
} else {
return false;
}
})
);
Query limitations
Cloud Firestore does not support the following types of queries:
Queries with range filters on different fields.
Single queries across multiple collections or subcollections. Each query runs against a single collection of documents. For more
information about how your data structure affects your queries, see
Choose a Data Structure.
Logical OR queries. In this case, you should create a separate query for each OR condition and merge the query results in your app.
Queries with a != clause. In this case, you should split the query into a greater-than query and a less-than query. For example, although
the query clause where("age", "!=", "30") is not supported, you can
get the same result set by combining two queries, one with the clause
where("age", "<", "30") and one with the clause where("age", ">", 30).
I'm working with Observables here and the AngularFire wrapper but here's how I managed to do that.
It's kind of crazy, I'm still learning about observables and I possibly overdid it. But it was a nice exercise.
Some explanation (not an RxJS expert):
songId$ is an observable that will emit ids
dance$ is an observable that reads that id and then gets only the first value.
it then queries the collectionGroup of all songs to find all instances of it.
Based on the instances it traverses to the parent Dances and get their ids.
Now that we have all the Dance ids we need to query them to get their data. But I wanted it to perform well so instead of querying one by one I batch them in buckets of 10 (the maximum angular will take for an in query.
We end up with N buckets and need to do N queries on firestore to get their values.
once we do the queries on firestore we still need to actually parse the data from that.
and finally we can merge all the query results to get a single array with all the Dances in it.
type Song = {id: string, name: string};
type Dance = {id: string, name: string, songs: Song[]};
const songId$: Observable<Song> = new Observable();
const dance$ = songId$.pipe(
take(1), // Only take 1 song name
switchMap( v =>
// Query across collectionGroup to get all instances.
this.db.collectionGroup('songs', ref =>
ref.where('id', '==', v.id)).get()
),
switchMap( v => {
// map the Song to the parent Dance, return the Dance ids
const obs: string[] = [];
v.docs.forEach(docRef => {
// We invoke parent twice to go from doc->collection->doc
obs.push(docRef.ref.parent.parent.id);
});
// Because we return an array here this one emit becomes N
return obs;
}),
// Firebase IN support up to 10 values so we partition the data to query the Dances
bufferCount(10),
mergeMap( v => { // query every partition in parallel
return this.db.collection('dances', ref => {
return ref.where( firebase.firestore.FieldPath.documentId(), 'in', v);
}).get();
}),
switchMap( v => {
// Almost there now just need to extract the data from the QuerySnapshots
const obs: Dance[] = [];
v.docs.forEach(docRef => {
obs.push({
...docRef.data(),
id: docRef.id
} as Dance);
});
return of(obs);
}),
// And finally we reduce the docs fetched into a single array.
reduce((acc, value) => acc.concat(value), []),
);
const parentDances = await dance$.toPromise();
I copy pasted my code and changed the variable names to yours, not sure if there are any errors, but it worked fine for me. Let me know if you find any errors or can suggest a better way to test it with maybe some mock firestore.
var songs = []
db.collection('Dances')
.where('songs.aNameOfASong', '==', true)
.get()
.then(function(querySnapshot) {
var songLength = querySnapshot.size
var i=0;
querySnapshot.forEach(function(doc) {
songs.push(doc.data())
i ++;
if(songLength===i){
console.log(songs
}
console.log(doc.id, " => ", doc.data());
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
It could be better to use a flat data structure.
The docs specify the pros and cons of different data structures on this page.
Specifically about the limitations of structures with sub-collections:
You can't easily delete subcollections, or perform compound queries across subcollections.
Contrasted with the purported advantages of a flat data structure:
Root-level collections offer the most flexibility and scalability, along with powerful querying within each collection.

can't get the find products from the state named products

I have tried to do a map in same place, It works. but whenever I try to do find or filter it doesn't return anything...I have tried everything to find the bug, but couldn't find it.
Your issue is that the id in your for loop is the index of the object, you have to compare the product.id with the addedProducts passing the index, try this:
useEffect(() => {
const addedProducts = getStoredCart()
if (products.length) {
for (const id in addedProducts) {
const foundProduct = products.find(product => product.id === addedProducts[id])
console.log('found Product', foundProduct)
}
}
}, [products])
Other thing, whenever you make a question, give as much information as possible, and don't take print of the code, put it directly in the post with the code sample tool, this way you make the things easier for the one that is helping you. Hope my answer was useful for you!

Optimizing Firestore queries for our content app

We are building a content app using Firestore.
The basic requirement is that there is one master collection, let's say 'content'. The number of documents could run into 1000s.
content1, content2, content3 ... content9999
We want to serve our users with content from this collection, making sure they don't see the same content twice, and every time they are in the app there's new content for them.
At the same time, we don't want the same sequence of our content to be served to each user. Some randomisation would be good.
user1: content9, content123, content17, content33, content902 .. and so on
user2: content854, content79, content190, content567 ... and so on
I have been breaking my head as to how without duplicating the master collection can we possibly achieve this solution. Duplicating the master collection would just be so expensive, but will do the job.
Also, how can we possibly write cost-effective and performance-optimised queries especially when we want to maintain randomisation in the sequence of these content pieces?
Here is my suggestion. Please view it as pseudo-code as I did not run it.
If the content document ids are not previsible
You have to store and maintain which user has seen which content, for example in a collection: /seen/uid_contentId
See here a clever way to get a random document from a collection. You need to store the size of the collection, perhaps as a document in another collection. So here is how you could do it:
const snapshot = await firestore.doc(`/userSeen/${uid}`).get(); // do it only once
const alreadySeen = snapshot.exists ? snapshot.data.contents : [];
async function getContent(uid) {
for (let trials = 0; trials < 10; trials++) { // limit the cost
const startAt = Math.random() * contentCollectionSize;
const snapshot = await firestore.collection("/contents").startAt(startAt).limit(1).get();
const document = snapshot.empty ? null : snapshot.docs[0]; // a random content
if(document.exists && !alreadySeen.includes(document.id)) {
alreadySeen.push(document.id);
await firestore.doc(`/userSeen/${uid}`).set({contents: arrayUnion(document.id)}); // mark it as seen
return document;
}
}
return null;
}
Here you may have to make several queries to Firestore (capped to 10 to limit the cost), because you are not able to compute the content document ids on the client side.
If the content document ids follow a simple pattern: 1, 2, 3, ...
To save up costs and performance, you should store all the seen contents for each user in a single document (the limit is 1MB, that is more than 250,000 integers!). Then you download this document once per user, and check on the client side if a random content was already seen.
const snapshot = await firestore.doc(`/userSeen/${uid}`).get(); // do it only once
const alreadySeen = snapshot.exists ? snapshot.data.contents : [];
async function getContent(uid) {
let idx = Math.random() * contentCollectionSize;
for (let trials = 0; trials < contentCollectionSize; trials++) {
idx = idx + 1 < contentCollectionSize ? idx + 1 : 0;
if(alreadySeen.includes(idx)) continue; // this shortcut reduces the number of Firestore queries
const document = await firestore.doc(`/contents/${idx}`).get();
if(document.exists){
alreadySeen.push(idx);
await firestore.doc(`/userSeen/${uid}`).set({contents: arrayUnion(idx)}); // mark it as seen
return document;
}
}
return null;
}
As you can see, this is much cheaper if you use previsible document ids for your content. But perhaps someone will have a better idea.
I have another idea. You could generate scalars of content :D
Create another collection - scalars
Add field type array
Code a function which will walk through content collection and will generate sets of content items randomly or taking into account other attributes like popularity, demographic, behaviour of users.
Generate 1000 of sets of content items in scalars collection, and do this once a month for example.
You can even measure effectiveness of each scalar in the context of attracting back users and promote those that are more atractive.
Once you have collection of scalars containing sets of collection items, you can assign users to a scalar. And present content items accordingly.

Setting state before render with ref to firestore

I am working through a simple logical problem, but I cannot seem to have things work smoothly. Let me share my most convincing code experiment and then I'll share some thoughts.
useEffect(() => {
firebase
.firestore()
.collection("someCollection")
.orderBy("date", "desc")
.onSnapshot(docs => {
let documents = []
if (canGetUpdatesFromFirestore.current) {
docs.forEach((doc) => {
documents.push(doc.data())
})
if(documents.length > 3) {
documents.splice(4, 0, {questionPostId: 0})
documents.splice(5, 0, {questionPostId: 1})
}
setAllQuestions(documents)
setUsers(documents)
}
})
if (searchValue.length > 2) {
canGetUpdatesFromFirestore.current = false;
functions.searchForSearchVal(searchValue, "Sexuality")
.then((result) => {
setAllQuestions(result);
})
} else {
canGetUpdatesFromFirestore.current = true
}
}, [searchValue])
function setUsers(docs){
let arrFinal = []
let copyOfAllQuestions = ""
for(let i = 0; i< docs.length; i++) {
console.log("HERE")
if (docs[i].postedBy) {
docs[i].ref.get().then(userFire => {
copyOfAllQuestions = {
...allQuestions,
...{hasPremium: userFire.data().hasPremium}
}
})
arrFinal.push(copyOfAllQuestions)
}
}
setAllQuestions(arrFinal)
}
Let me share some of my current state and what I am trying to accomplish.
I have a that display allQuestions. Each question data has a ref to its user document in firestore. For each question I need to check if that user hasPremium. How should I go about doing that the correct way?
The problem currently is that I can get the data from my Users collection through the ref, but I have to refresh my state in order for it all to show.
Could someone help me get on the right path / think correctly on this one please?
One approach that I put forward is to embrace data denormalization. That is, rather than putting references to other documents (Users) inside of the Questions document, put all the relevant user information directly into the Questions document.
This is antithetical to SQL database approaches, but that's okay because Firestore is "NoSQL". Embrace the anti-SQL-idity!!
Essentially, in your Question document you want to copy in whatever information is required in your app when working with a Question, and avoid doing "joins" by fetching other documents. You don't need to copy in all of the User document into a Question document - just the elements needed when your app is working with a Question.
For example, maybe in the question all you need is:
question: {
name: ...,
type: ...,
lastUpdated: ...,
postedBy: {
email: ...,
displayName: ...,
avatarUrl: ...,
hasPremium: true,
}
}
With data duplicated, you often need a mechanism to keep duplicate data up-to-date from its "source". So you might consider a Cloud Function trigger for onUpdate() of User documents, and when a relevant value is modified (email, displayName, avatarUrl, and/or hasPremium) then you would loop through all questions that are postedBy that user and update accordingly.
The rules-of-thumb here are:
all data needed for one screen/function in your app goes into a SINGLE document
NoSQL document stores are used where reads are frequent and writes are infrequent
NoSQL data stores (typically) do not have "joins" - so don't design your app to require them (which is what your code above is doing: joining Question and Users)
often you don't care about updating ALL instances of duplicated data (e.g. if a user updates their displayName today, should you update a Question they posted 3 years ago? -- different apps/business needs will give different answers)

how to identify label from result of speech command model in reactjs?

I am using tensorflow-models/speech-commands model to detect speech commands using ReactJs app, I'm able to initialize the recognizer in app and getting results also, but not sure how to identify the label based on the result of the model.
componentDidMount () {
fetch("http://localhost:3001/ITEMS").then(resp => resp.json())
.then(result => this.setState({
products: result
},() => {
this.call()
}));
}
async call() {
const recognizer = speechCommands.create('BROWSER_FFT')
await recognizer.ensureModelLoaded();
console.log("CALL",recognizer)
recognizer.listen(result => {
// - result.scores contains the probability scores that correspond to
// recognizer.wordLabels().
// - result.spectrogram contains the spectrogram of the recognized word.
console.log("Result",result)
}, {
includeSpectrogram: true,
probabilityThreshold: 0.75
});
// Stop the recognition in 10 seconds.
setTimeout(() =>{
console.log("Stopped listening")
recognizer.stopListening()}, 10000);
}
as you can see I'm initializing the recognizer on did mount and getting below results on speech commands from me, but not sure how to exactly identify the label model detected from result.
I believe I should refer scores property, but which one exactly is the predicted, not sure. Please help.
.scores contains the probability that the given speech is a certain word.
Which one exactly is the predicted
It depends of what is intended. Is the word with the highest priority or the topk considered to be the predicted values ?
Whatever the case, the indexes needed to be retrieved in .score and be used to retrieve corresponding words in .words
retrieve the word with highest probability:
recognizer.wordsLabels()[result.scores.indexOf(Math.max(...result.scores))];
retrieve the topk words
result.score.sort((a, b) => b-a).slice(0,k).map(s => result.scores.indexOf(s)).map(i => recognizer.wordsLabels()[i])

Resources