Matching the start of a message with a string in a database
Hello! I'm in the early stages of working on a bot sort of like PluralKit: it detects when a user starts a message with a customized phrase/prefix, and then replaces the message with the same contents in a customized pseudo-profile. However, I'm just working on matching messages with a specified prefix in the database right now.
More in-depth: the user specifies a phrase that gets saved in a MongoDB database, like "sender:". Then, when the user starts a message with "sender:", the bot finds that "sender:" is listed in the prefix database and reacts. For testing purposes, I was just trying to get the bot to reply to any match with "bar", but I want to match specific prefixes with different responses. So if someone sends a message starting with "sender1:", the bot would reply with "foo", but if that same person sends "sender2:" at the start the bot would reply with "bar". The bot also cares about who sent the message, matching which prefixes belong to whom based on user IDs (which the bot logs in the same database entry), so (in the future) a user can only send pseudo-profiles with their own character entries.
How can I have the bot match message starting content to strings in a database, and then reply to the message with different words depending on what the string match was? I'm more concerned about the discord.js code than the MongoDB code right now, so you can say something like "(database search formatting goes here)" if you're not familiar with MongoDB.
(Side note: the code in the screenshot is mainly just proof that I've been trying to figure this out myself first. Unfortunately, I truly have no idea how to code this own my own, so I don't have much remaining code to show. I've deleted most of my attempts before sending this...)
3 Replies
- What's your exact discord.js
npm list discord.js
and node node -v
version?
- Not a discord.js issue? Check out #other-js-ts.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!it looks like you've already found everything that's actually discord.js related
the rest is mostly just adding more logic using the data in your database
this would probably be better suited to #other-js-ts
Got it, thank you! I'll take it over there :)
I think I'll mark this as solved in a couple hours, in case anyone has any other discord.js related input