How to build this db model

Working on a NoSQL implementation of this discord bot me and my friend are setting up. What it'll do is iterate through all messages and get the amount of emojis each user has across all their messages. I was wondering what a good model for this would be? Should we build a model for each User, and then iterate through all their emojis? I have this so far:
type Emoji = {
emoji: string;
count: number;
};

export default class User {
constructor(
public server: string,
public username: string,
public emojis: Emoji[],
) {}
}
type Emoji = {
emoji: string;
count: number;
};

export default class User {
constructor(
public server: string,
public username: string,
public emojis: Emoji[],
) {}
}
This way, we can just do something like:
// psuedocode
const serverUsers = users.filter((user) => {
return user.server === server;
});

users.sort((a, b) => {
const userACount = a.emojis.find((emoji) => {
return (emoji.emoji === 'chart').count;
});

const userBCount = b.emojis.find((emoji) => {
return (emoji.emoji === 'chart').count;
});

return userACount > userBCount; // I know this won't work lol gotta think about it
});
// psuedocode
const serverUsers = users.filter((user) => {
return user.server === server;
});

users.sort((a, b) => {
const userACount = a.emojis.find((emoji) => {
return (emoji.emoji === 'chart').count;
});

const userBCount = b.emojis.find((emoji) => {
return (emoji.emoji === 'chart').count;
});

return userACount > userBCount; // I know this won't work lol gotta think about it
});
13 Replies
dys πŸ™
dys πŸ™β€’2mo ago
Could you the counts of each emoji in your db? It seems like the list of emojis could get voluminous quickly. You have to do a db update for each emojiing event anyway. It'd definitely speed up the calculation. (Also, your sort should return userBCount - userACount since it's expecting a positive, 0, or negative value.)
vince
vinceOPβ€’2mo ago
Hey dys! Do you mean I should get the count of a specific emoji in the backend instead of the frontend? e.g have an endpoint like /get/emoji/:emojiId and that way we're limiting the amount of work the bot has to do?
dys πŸ™
dys πŸ™β€’2mo ago
I actually misread your code. Because you used an array, I assumed you were tracking all the emojis in a list. Your structure is actually what I was suggesting. 😸 How I'd change what you have is to use a map, so you have user.emojiCount = { chart: 10, … } (so you can call user.emojiCount['chart']) rather than user.emojis = [{ emoji: 'chart', count: 10 }, …] & have to find the count.
vince
vinceOPβ€’2mo ago
No worries πŸ˜‚ and that's smart! I'll try that, thank you!! πŸ™‚
Lank81
Lank81β€’3w ago
yeah, I like the change for a list to a map that @dys πŸ™ suggested.
How will the bot function?
I'm guessing something like this?
!count username emoji and it'll return the count for that user?
dys πŸ™
dys πŸ™β€’3w ago
Well, probably /count since slash commands are listed by the system & autocomplete.
vince
vinceOPβ€’3w ago
Yup exactly like dys said! We used to have to do !count syntax but thank god there's slash commands now haha To be more specific, we'll probably just do something like /leaderboard <emoji-name> and it'll give us a sorted list of who has the most emojis on their messages At least for an mvp, we're just having fun with it right now πŸ™‚
Lank81
Lank81β€’3w ago
My buddy made a chat bot for our friend group Keybase channel. It’s pretty good. Does TLDR, imbeds YouTube, keeps score of polls, etc. Always a fun project
vince
vinceOPβ€’3w ago
That sounds awesome! Do you guys have a git repo of it I could take a peak at?
Lank81
Lank81β€’3w ago
No problem @vince here is his repo - https://github.com/pastorhudson/mtb-pykeybasebot
GitHub
GitHub - pastorhudson/mtb-pykeybasebot: Python Keybase Bot Library
Python Keybase Bot Library. Contribute to pastorhudson/mtb-pykeybasebot development by creating an account on GitHub.
vince
vinceOPβ€’3w ago
Thank you!! :)
Lank81
Lank81β€’3w ago
Anytime. If you have questions just let me know. I've only done a little bit of work with it, I'm more of a Java guy instead of Python. But I'll either look into or get ya the answer.
vince
vinceOPβ€’3w ago
That's so nice, thank you! I'll let you know
Want results from more Discord servers?
Add your server