How can I grab bot sent messages in a users dm with the bot if I dont have message intent?

I am making a bot that lets you communicate through it. 1 user can run a command and the other user will get the message in their dms. Same thing the other way around. I want to make it so messages can be reacted on, deleted, ect. For this I can store the message ID in a database and grab it. The issue is I dont know how. How do I grab a message from a users dm by id? The other option I have is to somehow store the raw message object in my database, the issue is I cant without serializing it or something. Every time I try to deserialize it the bot crashes. I have also tried to manually reconstruct it by passing in a json into a new Message(). I would like some help on solving this. I dont care what way gets it to work, im just trying to get it to work. If there is no way to do it then how would I do is assuming I did get message intent?
10 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
KevinWho
KevinWho2y ago
Node version: v16.13.1 Discord js version: discord.js@14.6.0 What would the code look like? and do I need message intent privilege to do this?
probablyraging
Fetch the DMChannel from the client and then fetch the messages from the DMChannel
KevinWho
KevinWho2y ago
I understand that, but do I need intent privilege to do this?
probablyraging
I would assume DIRECT_MESSAGES
KevinWho
KevinWho2y ago
How would I do that? since the code I found seems to need intent privileges. Also it seems to be based on the guild. Is there a way to grab a dm channel without knowing what server they are in? I have been trying
(await client.users.fetch(userId)).dmChannel.fetch(messageId);
(await client.users.fetch(userId)).dmChannel.fetch(messageId);
but this gives me TypeError: Cannot read properties of null (reading 'fetch') It fails on the second fetch. user.dmChannel is null
probablyraging
Pretty sure DMChannels aren't cached initially, so unless it's already in cache you have to fetch the channel
KevinWho
KevinWho2y ago
I made this. That seems to work. Hopefully this is correct
exports.getDmChannel = async function (client, userId) {
let user = await client.users.fetch(userId);
let dmChannel = user.dmChannel;
if (!user.dmChannel) {
dmChannel = await user.createDM();
}
return dmChannel;
};
exports.getDmChannel = async function (client, userId) {
let user = await client.users.fetch(userId);
let dmChannel = user.dmChannel;
if (!user.dmChannel) {
dmChannel = await user.createDM();
}
return dmChannel;
};
Now I have a list of message ids, how do I get them in bulk so that I dont spam the api? Thanks!
probablyraging
Just do a fetch on the channel's messages property and it will return up to 100 messages If you only want messages sent by the bot, use a filter
KevinWho
KevinWho2y ago
Alright, thanks! Not sure how to close these forum channels but im good now