Waiting for user message in DM.

I've been trying to get the bot to wait for a message but it keeps logging this error
TypeError: Cannot read properties of null (reading 'awaitMessages')
at Object.execute (/Users/kirakenjiro/Desktop/Coding/Active/AkedasBot/commands/verify.js:31:61)
at Client.<anonymous> (/Users/kirakenjiro/Desktop/Coding/Active/AkedasBot/index.js:80:19)
at Client.emit (node:events:514:28)
at InteractionCreateAction.handle (/Users/kirakenjiro/node_modules/discord.js/src/client/actions/InteractionCreate.js:97:12)
TypeError: Cannot read properties of null (reading 'awaitMessages')
at Object.execute (/Users/kirakenjiro/Desktop/Coding/Active/AkedasBot/commands/verify.js:31:61)
at Client.<anonymous> (/Users/kirakenjiro/Desktop/Coding/Active/AkedasBot/index.js:80:19)
at Client.emit (node:events:514:28)
at InteractionCreateAction.handle (/Users/kirakenjiro/node_modules/discord.js/src/client/actions/InteractionCreate.js:97:12)
Here is my code.
const { EmbedBuilder } = require("discord.js");
const handlers = require("../handlerLoader.js");

module.exports = {
name: "verify",
description: "Sends your ID to the moderators to be verified for the 18+ channels",

async execute(interaction) {
// Check if the command was sent in a DM channel
if (interaction.guildId) {
// Send an embed indicating that the command must be executed in a DM
const warningEmbed = new EmbedBuilder()
.setColor(0x2b2d31)
.setAuthor({
name: "Verification Error",
})
.setDescription(
'This command must be executed in a DM (Direct Message) channel.\nYour legal ID was not processed.'
);

await interaction.reply({
embeds: [warningEmbed],
ephemeral: true,
});
} else {
// Wait for 30 seconds in the DM
const filter = (msg) => msg.author.id === interaction.user.id;
const options = { max: 1, time: 30000, errors: ["time"] };

try {
const collected = await interaction.channel.awaitMessages(filter, options);
const message = collected.first();

// Check if the message contains an image
if (message && message.attachments.size > 0) {
const imageUrl = message.attachments.first().url;
console.log(`Image URL: ${imageUrl}`);
}
} catch (error) {
console.error(error);
}
}
}
};
const { EmbedBuilder } = require("discord.js");
const handlers = require("../handlerLoader.js");

module.exports = {
name: "verify",
description: "Sends your ID to the moderators to be verified for the 18+ channels",

async execute(interaction) {
// Check if the command was sent in a DM channel
if (interaction.guildId) {
// Send an embed indicating that the command must be executed in a DM
const warningEmbed = new EmbedBuilder()
.setColor(0x2b2d31)
.setAuthor({
name: "Verification Error",
})
.setDescription(
'This command must be executed in a DM (Direct Message) channel.\nYour legal ID was not processed.'
);

await interaction.reply({
embeds: [warningEmbed],
ephemeral: true,
});
} else {
// Wait for 30 seconds in the DM
const filter = (msg) => msg.author.id === interaction.user.id;
const options = { max: 1, time: 30000, errors: ["time"] };

try {
const collected = await interaction.channel.awaitMessages(filter, options);
const message = collected.first();

// Check if the message contains an image
if (message && message.attachments.size > 0) {
const imageUrl = message.attachments.first().url;
console.log(`Image URL: ${imageUrl}`);
}
} catch (error) {
console.error(error);
}
}
}
};
I'd love a few pointers of where im going wrong <3
9 Replies
d.js toolkit
d.js toolkit14mo 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! - Marked as resolved by OP
Kiɾʌ Kenjiɾø
Kiɾʌ KenjiɾøOP14mo ago
Oh also! Feel free to ping me, It's the best way to get my attention noeviiCozy
kernelxion
kernelxion14mo ago
@Kira Kenjiro Your attempt to utilize awaitMessages on interaction.channel is problematic because awaitMessages is not a method directly accessible in this context. You can try this awaitMessageComponent
Kiɾʌ Kenjiɾø
Kiɾʌ KenjiɾøOP14mo ago
ohh i see :O okay okay i thought it was thanks kern, I'll take alook at the component version
Syjalo
Syjalo14mo ago
DM channels aren't cached by default. You have to fetch it. interaction.user.createDM() He need to collect messages, not message component interactions
kernelxion
kernelxion14mo ago
oh you are right
Kiɾʌ Kenjiɾø
Kiɾʌ KenjiɾøOP14mo ago
Oh yeah! ok got it reddragonLove i thought they we're also cached like normal channels I am now a little lost..
// Import necessary modules
const { EmbedBuilder } = require("discord.js");
const handlers = require("../handlerLoader.js");

module.exports = {
name: "verify",
description: "Sends your ID to the moderators to be verified for the 18+ channels",

async execute(interaction) {
// Check if the command was sent in a DM channel
if (interaction.guildId) {
// Send an embed indicating that the command must be executed in a DM
const warningEmbed = new EmbedBuilder()
.setColor(0x2b2d31)
.setAuthor({
name: "Verification Error",
})
.setDescription(
'This command must be executed in a DM (Direct Message) channel.\nYour legal ID was not processed.'
);

await interaction.reply({
embeds: [warningEmbed],
ephemeral: true,
});
} else {
// Fetch the user's DMs and cache them
const userDM = await interaction.user.createDM();

// Send an embed indicating that the command must be executed in a DM
const messageEmbed = new EmbedBuilder()
.setColor(0x2b2d31)
.setAuthor({
name: "Verification",
})
.setDescription(
'Please send a message within 30 seconds.'
);

await interaction.reply({
embeds: [messageEmbed],
ephemeral: true,
});

// Set up a filter to collect messages from the user
const filter = (message) => message.author.id === interaction.user.id;

// Set up options for awaitMessages
const options = { max: 1, time: 30000, errors: ['time'] };

// ? What the hecc do i do now o-o

}
}
};
// Import necessary modules
const { EmbedBuilder } = require("discord.js");
const handlers = require("../handlerLoader.js");

module.exports = {
name: "verify",
description: "Sends your ID to the moderators to be verified for the 18+ channels",

async execute(interaction) {
// Check if the command was sent in a DM channel
if (interaction.guildId) {
// Send an embed indicating that the command must be executed in a DM
const warningEmbed = new EmbedBuilder()
.setColor(0x2b2d31)
.setAuthor({
name: "Verification Error",
})
.setDescription(
'This command must be executed in a DM (Direct Message) channel.\nYour legal ID was not processed.'
);

await interaction.reply({
embeds: [warningEmbed],
ephemeral: true,
});
} else {
// Fetch the user's DMs and cache them
const userDM = await interaction.user.createDM();

// Send an embed indicating that the command must be executed in a DM
const messageEmbed = new EmbedBuilder()
.setColor(0x2b2d31)
.setAuthor({
name: "Verification",
})
.setDescription(
'Please send a message within 30 seconds.'
);

await interaction.reply({
embeds: [messageEmbed],
ephemeral: true,
});

// Set up a filter to collect messages from the user
const filter = (message) => message.author.id === interaction.user.id;

// Set up options for awaitMessages
const options = { max: 1, time: 30000, errors: ['time'] };

// ? What the hecc do i do now o-o

}
}
};
I've gotten here, I've got the filter and options setup but eahm would i do something along the lines of userDM.interaction.awaitMessages()
// Wait for the user's message
const collected = await userDM.awaitMessages(filter, options);

// Check if a message was collected
if (collected.size) {
// Access the content of the collected message
const userMessageContent = collected.first().content;

console.log(userMessageContent)
}
// Wait for the user's message
const collected = await userDM.awaitMessages(filter, options);

// Check if a message was collected
if (collected.size) {
// Access the content of the collected message
const userMessageContent = collected.first().content;

console.log(userMessageContent)
}
I'm using this but, I don't think thats how i should be doing it as it won't return the content! But.. it also doesn’t error which, isn’t helpful
d.js docs
d.js docs14mo ago
Tag suggestion for @Kira Kenjiro: The filter for collectors has moved into the options:
- const collector = message.createReactionCollector(collectorFilter, { ...options });
+ const collector = message.createReactionCollector({ filter: collectorFilter, ...options });
- const collector = message.createReactionCollector(collectorFilter, { ...options });
+ const collector = message.createReactionCollector({ filter: collectorFilter, ...options });
Kiɾʌ Kenjiɾø
Kiɾʌ KenjiɾøOP14mo ago
Ohh! okay okay i got it, Thanks Sy <3 Yeh!! it's working now <a:pats_neko:1096725152114429962> Thanks for the help I really appreciate it
Want results from more Discord servers?
Add your server