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

Error happens when trying to edit a followup message: [Could not find the channel where this message came from in the cache!] #10441

Closed
glink25 opened this issue Aug 12, 2024 · 8 comments

Comments

@glink25
Copy link

glink25 commented Aug 12, 2024

Which package is this bug report for?

discord.js

Issue description

when I was trying to edit a message which was "followupped" by an interaction, it crashed and tolds an error:

Error [ChannelNotCached]: Could not find the channel where this message came from in the cache!
    at Message.edit (/home/glink25/linkbot/node_modules/discord.js/src/structures/Message.js:749:46)
    at Timeout.eval (/home/glink25/linkbot/main.ts:7:1644)
    at <anonymous> (node:internal/timers:246:4663) {
  code: 'ChannelNotCached'
}

the code below can reproduce on my environment

import {
    REST,
    Routes,
    Client,
    GatewayIntentBits,
    Message,
    ActionRowBuilder,
    ButtonBuilder,
    ButtonStyle,
    Partials,
  } from 'discord.js';
  import { CONFIG } from './config';
  
  const commands = [
    {
      name: 'follow',
      description: 'followup test',
    },
  ];
  
  const registerCommands = async () => {
    const rest = new REST({
      version: '10',
      userAgentAppendix: 'DiscordBot',
    }).setToken(CONFIG.TOKEN);
  
    try {
      console.log('Started refreshing application (/) commands.');
  
      await rest.put(Routes.applicationCommands(CONFIG.CLIENT_ID), {
        body: commands,
      });
  
      console.log('Successfully reloaded application (/) commands.');
    } catch (error) {
      console.error(error);
    }
  };
  
  registerCommands();
  const client = new Client({ intents: [
    GatewayIntentBits.Guilds,
  ]});
  
  client.on('ready', () => {
    console.log(`Logged in as ${client.user?.tag}!`);
  });
  
  client.on("interactionCreate",async(interaction)=>{
    if(!interaction.isChatInputCommand())return
    if(interaction.commandName!=="follow")return
    await interaction.reply("hhh")
    const msg = await interaction.followUp("followup")
    setTimeout(() => {
      msg.edit("new follow") // this will throw an error
    }, 1000);
  })
  
  client.login(CONFIG.TOKEN);
  

It seem the edit() function is unable to use on a followup message?

Code sample

import {
    REST,
    Routes,
    Client,
    GatewayIntentBits,
    Message,
    ActionRowBuilder,
    ButtonBuilder,
    ButtonStyle,
    Partials,
  } from 'discord.js';
  import { CONFIG } from './config';
  
  const commands = [
    {
      name: 'follow',
      description: 'followup test',
    },
  ];
  
  const registerCommands = async () => {
    const rest = new REST({
      version: '10',
      userAgentAppendix: 'DiscordBot',
    }).setToken(CONFIG.TOKEN);
  
    try {
      console.log('Started refreshing application (/) commands.');
  
      await rest.put(Routes.applicationCommands(CONFIG.CLIENT_ID), {
        body: commands,
      });
  
      console.log('Successfully reloaded application (/) commands.');
    } catch (error) {
      console.error(error);
    }
  };
  
  registerCommands();
  const client = new Client({ intents: [
    GatewayIntentBits.Guilds,
  ]});
  
  client.on('ready', () => {
    console.log(`Logged in as ${client.user?.tag}!`);
  });
  
  client.on("interactionCreate",async(interaction)=>{
    if(!interaction.isChatInputCommand())return
    if(interaction.commandName!=="follow")return
    await interaction.reply("hhh")
    const msg = await interaction.followUp("followup")
    setTimeout(() => {
      msg.edit("new follow") // this will throw an error
    }, 1000);
  })
  
  client.login(CONFIG.TOKEN);

Versions

  • discord.js 14.12.1
  • Node.js 1v18.20.3
  • TypeScript 5.1.6
  • macOS Sonoma 14.4

Issue priority

Low (slightly annoying)

Which partials do you have configured?

No Partials

Which gateway intents are you subscribing to?

Guilds

I have tested this issue on a development release

No response

@imnaiyar
Copy link
Contributor

Is this on User App? In which case it is understandable as channels might not be cached. Use .editReply(...)

@glink25
Copy link
Author

glink25 commented Aug 13, 2024

Is this on User App? In which case it is understandable as channels might not be cached. Use .editReply(...)

Is there a way to edit a follow-up message without overriding the previous one? I’m creating a quiz bot that asks 5 random questions. Each time a user selects an option, I want to record their choice without replacing the previous question message. However, editReply() overwrites the last message, and followup() creates a new message. I want all the questions and their answers to be displayed together

@monbrey
Copy link
Member

monbrey commented Aug 13, 2024

editReply accepts a message option telling it which reply to edit, which supports followUps

@glink25
Copy link
Author

glink25 commented Aug 14, 2024

editReply accepts a message option telling it which reply to edit, which supports followUps

But when I was trying to use .edit() function on a followUp message, it throws an error, that's what I am confused about

@monbrey
Copy link
Member

monbrey commented Aug 14, 2024

What's confusing?

We're telling you to use interaction.editReply(), which can edit any reply to the interaction, including followUps.

Do not use message.edit().

@glink25
Copy link
Author

glink25 commented Aug 15, 2024

What's confusing?

We're telling you to use interaction.editReply(), which can edit any reply to the interaction, including followUps.

Do not use message.edit().

Got it, thanks for answering my question, this issue could be closed

@SpaceEEC SpaceEEC closed this as not planned Won't fix, can't repro, duplicate, stale Aug 15, 2024
@glink25
Copy link
Author

glink25 commented Aug 15, 2024

followup message should be edit like this:

const msg = await interaction.followUp("followup")
await interaction.editReply({ message:msg, content:"new msg" }) // now followup message change to "new msg"

@imnaiyar
Copy link
Contributor

followup message should be edit like this:

const msg = await interaction.followUp("followup")
await interaction.editReply({ message:msg, content:"new msg" }) // now followup message change to "new msg"

Yes, again, you should refer to docs. It has all the answers

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

4 participants