Fetching message in a thread

ill explain after i post
2 Replies
d.js toolkit
d.js toolkit3mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - 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!
Violet
Violet3mo ago
so i got a piece of code to fetch the first message in forum thread post which was working before but idk exactly what got changed that its not working properly now so the piece of code i have is
//IDK WHAT THE FUCK IS THIS PART
const messages2 = await thread.messages.fetch()
const messages1 = await thread.messages.fetch({
limit: 1,
after: thread.id,
})
//IDK WHAT THE FUCK IS THIS PART

const message = await thread.messages.fetch({
limit: 1,
after: 0,
})
console.log(message)
//IDK WHAT THE FUCK IS THIS PART
const messages2 = await thread.messages.fetch()
const messages1 = await thread.messages.fetch({
limit: 1,
after: thread.id,
})
//IDK WHAT THE FUCK IS THIS PART

const message = await thread.messages.fetch({
limit: 1,
after: 0,
})
console.log(message)
you might ask what is that comment , well idk either 😐 i didnt have that on my original code , and i only had the
thread.messages.fetch({
limit: 1,
after: 0,
})
thread.messages.fetch({
limit: 1,
after: 0,
})
but without that code between comments , my message is somehow not fetched and if i log message.size it returns 0 ( this whole code happens in
threadCreate
threadCreate
event. its all good till here and when i
console.log(message)
console.log(message)
i get a collection which means it got the message and the message im testing this event on has a Mention user in it. here is the log response
Collection(1) [Map] {
'1216352305750736956' => <ref *1> Message {
channelId: '1216352305750736956',
guildId: '992300928067698759',
id: '1216352305750736956',
createdTimestamp: 1710071388424,
type: 0,
system: false,
content: '@Violet',
author: User {
id: '856956452740792320',
bot: false,
system: false,
flags: [UserFlagsBitField],
username: 'ivioleti',
globalName: 'Violet',
discriminator: '0',
avatar: 'a1c7fdbf18258cf177318958181cebb4',
banner: null,
accentColor: null,
avatarDecoration: null
},
pinned: false,
tts: false,
nonce: null,
embeds: [],
components: [],
attachments: Collection(2) [Map] {
'1216352306090737715' => [Attachment],
'1216352306598117437' => [Attachment]
},
stickers: Collection(0) [Map] {},
position: 0,
roleSubscriptionData: null,
resolved: null,
editedTimestamp: null,
reactions: ReactionManager { message: [Circular *1] },
mentions: MessageMentions {
everyone: false,
users: [Collection [Map]],
roles: Collection(0) [Map] {},
_members: null,
_channels: null,
_parsedUsers: null,
crosspostedChannels: Collection(0) [Map] {},
repliedUser: null
},
webhookId: null,
groupActivityApplication: null,
applicationId: null,
activity: null,
flags: MessageFlagsBitField { bitfield: 0 },
reference: null,
interaction: null
}
}
Collection(1) [Map] {
'1216352305750736956' => <ref *1> Message {
channelId: '1216352305750736956',
guildId: '992300928067698759',
id: '1216352305750736956',
createdTimestamp: 1710071388424,
type: 0,
system: false,
content: '@Violet',
author: User {
id: '856956452740792320',
bot: false,
system: false,
flags: [UserFlagsBitField],
username: 'ivioleti',
globalName: 'Violet',
discriminator: '0',
avatar: 'a1c7fdbf18258cf177318958181cebb4',
banner: null,
accentColor: null,
avatarDecoration: null
},
pinned: false,
tts: false,
nonce: null,
embeds: [],
components: [],
attachments: Collection(2) [Map] {
'1216352306090737715' => [Attachment],
'1216352306598117437' => [Attachment]
},
stickers: Collection(0) [Map] {},
position: 0,
roleSubscriptionData: null,
resolved: null,
editedTimestamp: null,
reactions: ReactionManager { message: [Circular *1] },
mentions: MessageMentions {
everyone: false,
users: [Collection [Map]],
roles: Collection(0) [Map] {},
_members: null,
_channels: null,
_parsedUsers: null,
crosspostedChannels: Collection(0) [Map] {},
repliedUser: null
},
webhookId: null,
groupActivityApplication: null,
applicationId: null,
activity: null,
flags: MessageFlagsBitField { bitfield: 0 },
reference: null,
interaction: null
}
}
but when i try to go into an If statemnt to check if
message.mentions.users.size> 0
message.mentions.users.size> 0
which as i said i mention a user in my test message . it returns an error and when i log
message.mentions
message.mentions
` it returns undefined and my if statement error is like this
if (message.mentions.users.size > 0) {
^
TypeError: Cannot read properties of undefined (reading 'users')
at Timeout._onTimeout (/home/container/Events/Client/thread.js:65:38)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Node.js v21.5.0
if (message.mentions.users.size > 0) {
^
TypeError: Cannot read properties of undefined (reading 'users')
at Timeout._onTimeout (/home/container/Events/Client/thread.js:65:38)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Node.js v21.5.0
i know its a bit long and idk if i explaint it correctly but i cant really get to undrestand whats wrong with it ( i did research before i get to here ) thanks for the tip earlier i tried adding a timeout before fetching the message ( it was 5 second and it was still the same ) do u have any idea how long it takes for the message to exist? like is there an average or it depends on the amount of attachments and it varies nah there is no such code imma try with 20 second delay with two attachments let me share my code before i log the message .
async execute(thread) {
if (thread.parent.id === '1139062893573328966') {
const channel = thread.guild.channels.cache.get(
'1014042418074624071',
)
const Ch_Log = thread.guild.channels.cache.get(
'1202976505542615140',
)

const length = ['First Strike', 'Second Strike', 'Third Strike']



//IDK WHAT THE FUCK IS THIS PART
const messages2 = await thread.messages.fetch()
const messages1 = await thread.messages.fetch({
limit: 1,
after: thread.id,
})
//IDK WHAT THE FUCK IS THIS PART

const message = await thread.messages.fetch({
limit: 1,
after: 0,
})
console.log(message)
}
}
async execute(thread) {
if (thread.parent.id === '1139062893573328966') {
const channel = thread.guild.channels.cache.get(
'1014042418074624071',
)
const Ch_Log = thread.guild.channels.cache.get(
'1202976505542615140',
)

const length = ['First Strike', 'Second Strike', 'Third Strike']



//IDK WHAT THE FUCK IS THIS PART
const messages2 = await thread.messages.fetch()
const messages1 = await thread.messages.fetch({
limit: 1,
after: thread.id,
})
//IDK WHAT THE FUCK IS THIS PART

const message = await thread.messages.fetch({
limit: 1,
after: 0,
})
console.log(message)
}
}
its the whole code since my event emitts , till i get to the part where i log it after the log is my if statement ok found the issue i guess after fetching the message i need to use this to get the first message fetched ( i also added the 20 second delay )
const messages = await thread.messages.fetch({
limit: 1,
after: 0,
})
const message = messages.first()
const messages = await thread.messages.fetch({
limit: 1,
after: 0,
})
const message = messages.first()
and when i use that constant message it works well