#Message cannot be edited

36 messages · Page 1 of 1 (latest)

indigo scarab
#

My code:

// The channel is fetched with ctx.guild.channels.fetch(parsedLink.channelId)
let message: Message | null = null;
try {
  message = await channel.messages.fetch(parsedLink.messageId);
  // Validate message and stuff, but not important.
} catch (err) {
  console.error(inspect(err));
  return;
}

let data = {
  action: "edit",
  where: message,
};

setCacheData(ctx.guildId, ctx.user.id, data);

// Later, in another function

/*
- panelData.action can only be "edit" or "send"
- panelData.where is a message when panelData.action is "edit" ; a Text-/NewsChannel when panelData.action is "send"
- messageArgs is of type MessageEditOptions
*/

let panel: Message;
try {
  if (panelData.action == "edit") {
    panel = await panelData.where.edit(messageArgs); // panelData.where is a Message
  } else {
    panel = await panelData.where.send(messageArgs); // panelData.where is a Text or NewsChannel
  }
} catch (err) {
  await ctx.reply({
    embeds: [
      {
        title: `Panel not ${panelData.where instanceof Message ? "edited" : "sent"}`,
        description: `${err}`,
        color: 0xff0000,
      },
    ],
  });
  return;
}

Versions

  • discord.js: 14.17.3
  • npm: 11.0.0
  • Typescript: 5.2.6
  • node: 22.13.0 LTS

I always get the error, that the edition of the panel failed. Does anyone have an idea what's wrong here?
-# This post is the continuation of https://github.com/discordjs/discord.js/issues/10700

haughty bridgeBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • Not a discord.js issue? Check out #1081585952654360687.
  • 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!
  • Marked as resolved by OP
cyan lynx
#

Can you console.log(panelData.where)

indigo scarab
#

yeah, it's a Message object

cyan lynx
#

Can you show it?

indigo scarab
#

uuuh, not now - I can show it later since I'm not at my pc.

indigo scarab
#

okay, I now have the message object here. Got it with inspect(panelData.where)

#

the client is just missing lol

#

I will try to replicate it with another bot.

cyan lynx
# indigo scarab the client is just missing lol

No it‘s not. It’s just not an enumerable property so doesn’t get included here. Did you meddle with djs internals in any way? The classes prototype or anything like that? console.log(panelData.where.client)

indigo scarab
#

nope, I didn't.

indigo scarab
#

well, I couldn't replicate it so I will try something else.

#

I now did a npm ci. testing again...

#

well, client wasn't found - it's undefined.
I will build in logging statements to trace where the client is lost.

#

okay, as it turns out - the client is there after the fetch, and after the cache data gets initialized. (img 1)
But right when I retrieve it the next time, it's gone. (img 2)

ornate tusk
#

Well, there's your issue then. Object.assign only copies the enumerable properties. <Message>.client is not enumerable

summer rampartBOT
#

mdn Object.assign()
The Object.assign() static method copies all enumerable own properties from one or more source objects to a target object. It returns the modified target object.

indigo scarab
#

bruh...

#

but what can I do to fix this then?

ornate tusk
#

Do not use it? Why do you even need to use it? From the looks of it, you are assigning it to an empty object, why not simply return it as is?

indigo scarab
#

wdym?

#

oh, you mean I should just modify the original data instead of assigning it?

#

OR could I just do this?
let newData = { ...originalData };

ornate tusk
#

I'm not sure what you are hoping to achieve by doing this
Object.assign({}, cachedData)

indigo scarab
#

well... I also don't 😂 I may have forgot to make a comment why I I decided to do this back then haha

ornate tusk
indigo scarab
#

okay, well I think I have to refactor this logic then xD

Thanks to y'all 😄

cyan lynx
indigo scarab
#

it was

#

it was logged as Message { ... }
that's the funny part

cyan lynx
#

I saw that it was logged like that. Which made me say you didn’t log it where your issue was

#

You logged the original, not the one you Object.assign()ed

indigo scarab
# indigo scarab

uhm, this one was logged when the error was thrown when trying to edit the message.

indigo scarab
#

Okay, I've now followed the advice and didn't create new instances every time. I think the issue is the NodeCache library but I have to test that.