discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.44k stars 3.97k forks source link

interaction.channel.guildId undefined after ephemeral reply #10495

Closed edocsil47 closed 2 months ago

edocsil47 commented 2 months ago

Which package is this bug report for?

discord.js

Issue description

Responding to interactions with an ephemeral message sometimes sets GuildChannel.guildId to undefined.

After updating to v14.16.1 I've noticed that sometimes when responding to an interaction with an ephemeral message, the guildId of the channel in the cache becomes set to undefined. I was able to create a minimum reproducible example below, but the behavior is still inconsistent. The issue can occur on the first interaction of a session or in subsequent ones.

Logging output 1: image

Logging output 2: image

From my testing it seems to require both the DirectMessages intent and the Channel partial. The application is installed in the guild and its bot user has view message permissions in the channel.

In a help thread I created, Syjalo identified the issue was in GuildChannel.js:68:

The issue is in GuildChannel.js:68 Sometimes the data have guild_id property, but with value of undefined.

I've put the issue as "medium" priority since it broke existing v14.15.3 code, but does have workarounds like using GuildChannel.guild.id instead.

TIA.

Code sample

const { Client, Events, GatewayIntentBits, Partials } = require("discord.js")
const { token } = require("./config.json")

const client = new Client({
    intents: [
        GatewayIntentBits.DirectMessages,
        GatewayIntentBits.Guilds,
    ],
    partials: [
        Partials.Channel,
    ],
})

client.on(Events.InteractionCreate, async interaction => {
    console.log(interaction.channel.guildId) // pass
    await interaction.reply({ content: "content", ephemeral: true })
    console.log(interaction.channel.guildId) // sometimes undefined
    console.log(interaction.client.channels.resolve(interaction.channelId).guildId) // same
})

client.login(token)

Versions

discord.js v14.16.1 node v20.17.0 TypeScript N/A OS Windows 11 23H2

Issue priority

Medium (should be fixed soon)

Which partials do you have configured?

Channel

Which gateway intents are you subscribing to?

Guilds, DirectMessages

I have tested this issue on a development release

1725667851-8a74f144a