Can I save information in message requests with Discord API?

Viewed 230

I tried to send messages like this

client.api.channels(msg.channel.id).messages.post({
  data: {
    content: 'content',
    test: 'test'
  }
})

I was hoping that since it’s an API post request, it would save the test property.

const msgs = await client.api.channels(msg.channel.id).messages.get();
msg.channel.send(msgs[1].test) //error: cannot send an empty message

Changing .test to .content does send the content of the message the bot sent originally, but .test outputs undefined. Maybe I need to use node-fetch or https so it posts and gets directly?

1 Answers

You are sending the test property to the Discord API via a post request, but that doesn't mean Discord API will store that property to its database. It only cares about the data it knows and the rest gets dropped. All properties Discord stores about a message are listed here.

Discord API doesn't allow you to store arbitrary data inside of a message object.

Of course you could search for workarounds, like storing the data inside of an embed's color (if they are short enough) or an image, etc. I don't think that is a good solution to this problem, but depends on what you want to accomplish.

What you could do is to store the custom data yourself. Create some kind of middleware, that will send a message to the Discord API, retrieve the message object of the newly created message. And associate its id with your custom data and store it somewhere (e.g. your database, JSON file, etc.)

Later you can use a message's id to access your custom data.

Related