How to approach "replacing" (finding/deleting/reposting) a message in a designated channel?
The use case is this: people post an introduction in a public channel invoking my bot, the bot posts it into a read-only roster channelβbut if the user already had an entry in the roster, the old entry is removed first. So each user only has at most one post in the roster channel, but can replace an old one with a new one.
I had a version of this working some years ago using DJS 12, and it did it by fetching the last 100 messages in the roster channel and then searching through them, deleting the message if it found one, and then posting a new one. 100 is far enough back. Is there any more targeted way to accomplish this, short of maintaining an internal database/memory of message IDs that the bot has posted? I ask partly because my v12 code doesn't seem to be working anymore and I'm troubleshooting, but meanwhile wondering if I could just search a channel for something specific so I don't have to parse through all the messages myself.
If my code continues not to work, I'll post what I'm doing, but meanwhile I'll keep playing with it to try to get it working in the current seemingly-inefficient way. Just wondering if there's a better approach. My quick searches in here suggests that the answer might be "no" but it doesn't seem like any answers I read were to questions about quite the use case I have.
5 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!Update: I just found
here: https://discord.js.org/docs/packages/discord.js/14.15.3/GuildMessageManager:Class#fetch
That seems promising for what I want to do. (In my case filtering on author id is not useful because the bot posts everything, but maybe I can use this to construct a more appropriate filtering function.) I will try this. Though it's really just mostly compacting the code footprint from what I had before, while still implementing the same approach. It's still ultimately retrieving all the messages from Discord and then parsing through them locally. If I could retrieve just the message I want rather than 100 of them from the API, that is the dream.
.....
well you can fetch one message from the api, you just need its id
the problem would be really simplified if you just kept the message ids in a db
you can use sqlite which is a "file" db (you don't need another service running it)
or given 100 is far enough it doesn't sound like your data is that big, you could even use json if you really want
Ok, thanks. This confirms for me that there isn't an obvious Discord API trick I'm missing, then. I'll consider keeping local data, definitely a toss-up between whether a stateless bot reacting to events without file support is simpler or whether recordkeeping code is simpler. Just need to ponder my philosophy I guess. Recordkeeping would definitely minimize per-hit computation requirements at least. Thanks for thinking about it and responding!