Jasmine Azrail
Jasmine Azrail

Reputation: 33

Discord JS v12: How do you get a message's content by it's ID?

I'm relatively new to discord.js, and I've started building a bot project that allows a user to create a message via command, have that message stored in a hidden channel on my private server, and then said message can be extracted through the message ID.

I have the write working and it returns the message ID of the message sent in the hidden channel, but I'm completely stumped on the get command. I've tried searching around online but every method I tried would return errors like "Cannot read property 'fetch' of undefined" or "'channel' is not defined". Here are some examples of what I tried, any help would be appreciated. Note that my args is already accurate, and "args[0]" is the first argument after the command. "COMMAND_CHANNEL" is the channel where the command is being executed while "MESSAGE_DATABASE" is the channel where the targeted message is stored.

let msgValue = channel.messages.cache.get(args[0])
client.channels.cache.get(COMMAND_CHANNEL).send(msgValue.content)
let msgValue = msg.channel.message.fetch(args[0])
    .then(message => client.channels.cache.get(COMMAND_CHANNEL).send(msgValue.content))
    .catch(console.error);

I even tried using node-fetch to call the discord API itself

const api = require("node-fetch")
let msgValue = api(`https://discordapp.com/api/v8/channels/${MESSAGE_DATABASE}/messages/${args[0]}`)
    .then(message => client.channels.cache.get(COMMAND_CHANNEL).send(msgValue.content))
    .catch(console.error);

Am I missing something or am I making some sort of mistake?

Edit: Thanks for the help! I finished my bot, it's just a little experimental bot that allows you to create secret messages that can only be viewed through their ID upon executing the command :get_secret_message <message_id>. I posted it on top.gg but it hasn't been approved yet, so in the meantime if anyone wants to mess around with it here is the link: https://discord.com/api/oauth2/authorize?client_id=800368784484466698&permissions=76800&scope=bot

List of commands:

:write_secret_message - Write a secret message, upon execution the bot will DM you the message ID.

:get_secret_message <message_id> - Get a secret message by its ID, upon execution the bot will DM you the message content.

:invite - Get the bot invite link.

NOTE: Your DMs must be turned on or the bot won't be able to DM any of the info.

My test message ID: 800372849155637290

Upvotes: 2

Views: 4849

Answers (2)

ignis
ignis

Reputation: 442

fetch returns the result as promise so you need to use the then to access that value instead of assigning it to a variable (msgValue). Also you made a typo (channel.message -> channel.messages).
I would recommend using something like this:

msg.channel.messages
    .fetch(args[0])
    .then(message => {
        client.channels
            .fetch(COMMAND_CHANNEL)
            .then(channel => channel.send(message.content))
            .catch(console.error)
    })
    .catch(console.error)

Upvotes: 1

T. Dirks
T. Dirks

Reputation: 3676

I think you were quite close with the second attempt you posted, but you made one typo and the way you store the fetched message is off.

The typo is you wrote msg.channel.message.fetch(args[0]) but it should be msg.channel.messages.fetch(args[0]) (the typo being the missing s after message). See the messages property of a TextChannel.

Secondly, but this is only a guess really as I can't be sure since you didn't provide much of your code, when you try to fetch the message, you are doing so from the wrong channel. You are trying to fetch the message with a given ID from in the channel the command was executed from (the msg.channel). Unless this command was executed from the "MESSAGE_DATABASE" channel, you would need to fetch the message by ID from the "MESSAGE_DATABASE" channel instead of the msg.channel.

Thirdly, if you fetch a message, the response from the Promise can be used in the .then method. You tried to assign the response to a variable msgValue with let msgValue = msg.channel.message.fetch(args[0]) but that won't do what you'll expect it to do. This will actual assign the entire Promise to the variable. What I think you want to do is just use the respone from the Promise directly in the .then method.

So taking all that, please look at the snippet of code below, with inspiration taken from the MessageManager .fetch examples. Give it a try and see if it works.

// We no longer need to store the value of the fetch in a variable since that won't work as you expect it would.
// Also we're now fetching the message from the MESSAGE_DATABASE channel.
client.channels.cache.get(MESSAGE_DATABASE).fetch(args[0])
    // The fetched message will be given as a parameter to the .then method.
    .then(fetchedMessage => client.channels.cache.get(COMMAND_CHANNEL).send(fetchedMessage.content))
    .catch(console.error);

Upvotes: 0

Related Questions