Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editing of messages fails (Cannot read properties of undefined (reading 'channels')) #10700

Closed
The-LukeZ opened this issue Jan 12, 2025 · 9 comments

Comments

@The-LukeZ
Copy link

The-LukeZ commented Jan 12, 2025

Which package is this bug report for?

discord.js

Issue description

  1. Have a message
  2. Try to edit it
  3. Error: Cannot read properties of undefined (reading 'channels')

Code sample

let message: Message | null = null;
try {
  let channel = (await getAndValidateChannel(
    parsedLink.channelId,
    ctx.guild,
    ChannelType.GuildText,
    ChannelType.GuildAnnouncement
  )) as TextChannel | NewsChannel;
  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 {
  panel = await panelData.where[panelData.action](messageArgs);
} 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
  • Server OS: Linux Ubuntu 24.04.1 LTS

Issue priority

Medium (should be fixed soon)

Which partials do you have configured?

Channel

Which gateway intents are you subscribing to?

Guilds, GuildMembers, GuildWebhooks, GuildMessages, DirectMessages, MessageContent, AutoModerationConfiguration, AutoModerationExecution

I have tested this issue on a development release

No response

@monbrey
Copy link
Member

monbrey commented Jan 12, 2025

Please include the full error stack to assist in debugging the issue.

@The-LukeZ
Copy link
Author

Here you go :)
I just replaced the bot's root directory with <directory>

TypeError: Cannot read properties of undefined (reading 'channels')
    at get channel (/home/<directory>/node_modules/discord.js/src/structures/Message.js:507:24)
    at Message.edit (/home/<directory>/node_modules/discord.js/src/structures/Message.js:828:15)
    at Object.submit (file:///home/<directory>/dist/components/utils/ticketsConfig/panel.js:544:56)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async Object.run (file:///home/<directory>/dist/components/configTickets.js:16:17)
    at async Client.<anonymous> (file:///home/<directory>/dist/index.js:131:13)

@monbrey
Copy link
Member

monbrey commented Jan 12, 2025

This error suggests that Message#client is undefined, which should never happen using a Message instead that was constructed by discord.js. Are you constructing your own message object, or incorrectly typecasting somewhere?

@The-LukeZ
Copy link
Author

I'm fetching it. Look at the code from the initial report.

@monbrey
Copy link
Member

monbrey commented Jan 12, 2025

The line panel = await panelData.where[panelData.action](messageArgs); lifts the edit or send functions from its this context, which breaks its internal bindings. You cannot call instance methods which rely on this context in this way.

@The-LukeZ
Copy link
Author

The-LukeZ commented Jan 12, 2025

well, it's also not working when I do it like this:

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
  }
}

I've tested it.

@The-LukeZ
Copy link
Author

Also, I've looked through my code and I saw this:

/**
 *
 * @param channelId - The channel ID to fetch
 * @param guild - The guild to fetch the channel from
 * @param channelTypes - The channel types to validate the channel for
 * @returns The channel if it exists and is of the correct type, otherwise `null`
 */
async function getAndValidateChannel(
  channelId: string,
  guild: Guild,
  ...channelTypes: ChannelType[]
) {
  const _channel = await guild.channels.fetch(channelId);
  if (
    !(
      channelTypes.length
        ? channelTypes
        : [ChannelType.GuildText, ChannelType.GuildAnnouncement]
    ).includes(_channel.type)
  )
    return null;
  return _channel;
}

// Later

let channel = (await getAndValidateChannel(
  parsedLink.channelId,
  ctx.guild,
  ChannelType.GuildText,
  ChannelType.GuildAnnouncement
)) as TextChannel | NewsChannel;

Maybe it is because of ctx.guild. ctx is a ModalMessageModalSubmitInteraction

@monbrey
Copy link
Member

monbrey commented Jan 12, 2025

I recommend opening a thread in the support section of our Discord server for further help debugging this. I don't see this as a library bug, as Message#client is always defined for messages properly constructed by discord.js.

Your error message and stack shows that either Message#client or the this context has been compromised in some way.

I also wouldn't call this a "minimal reproducible code sample" as it includes your own custom validation functions, some of which you chose to omit, and may be impacting the end result.

@The-LukeZ
Copy link
Author

I recommend opening a thread in the support section of our Discord server for further help debugging this. I don't see this as a library bug, as Message#client is always defined for messages properly constructed by discord.js.

Your error message and stack shows that either Message#client or the this context has been compromised in some way.

I also wouldn't call this a "minimal reproducible code sample" as it includes your own custom validation functions, some of which you chose to omit, and may be impacting the end result.

Okay, will do tomorrow 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants