So, here is the simplest way I have to make a MessageCollector that collects messages from DMs using DiscordJS v13
First off, lets start off with the simplest of bots:
const { Client } = require("discord.js"); //We only need to destructure Client from the library require('dotenv').config(); //We use environment variables: process.env.VARIABLE_NAME //We're creating a new Client with 4 intents and 3 partials. The partials aren't needed for this to work, but I like adding them anyways. //The important intents are: GUILDS, GUILD_MESSAGES and DIRECT_MESSAGES, particularly this last one, because we're working with DMs. const client = new Client({ intents: ["GUILDS", 'GUILD_MESSAGES', 'GUILD_MEMBERS', 'DIRECT_MESSAGES'], partials: ['CHANNEL', 'MESSAGE', 'REACTION'], }); //This event listener is triggered on the Event READY and only run once. client.once('ready', () => { console.log(`Bot is online and ready!`); //Console logging to show it's online } //The old message event listener. Now called messageCreate. client.on('messageCreate', async (message) => { const prefix = process.env.PREFIX; //In the .env file you need: PREFIX=! - I'm using ! as my prefix, you can use anything you want. if (message.author.bot) return; //If the author of the message sent is a bot, then do nothing. if (!message.content.startsWith(prefix)) return; //If the message doesn't start with prefix (!), do nothing. if (message.content.startsWith(prefix + 'start')) { //If the message starts with prefix (!) and directly followed by start, continue. const filter = m => m.author.id === message.author.id; //Filter for the collector. Only messages where the new messages author ID matched the one that started the collector. const DM = await message.member.send({content: `The collector is started! Type something:`}); //We're creating a DM channel with the user that ran the command. const collector = DM.channel.createMessageCollector({filter, max: 5, time: 30000}); //We're creating the collector, allowing for a max of 5 messages or 30 seconds runtime. collector.on('collect', m => { //Triggered when the collector is receiving a new message console.log(m.content); //Console log the content of the message } collector.on('end', (collected, reason) => { //Triggered when the collector ends. collected is a <Collection> of all messages collected and reason is the reason the collector ended. DM.channel.send({content: `Collector ended for reason: ${reason} reached! I collected ${collected.size} messages.`}); //Sending a message to the user informing the collector ended, and why. } } } client.login(process.env.TOKEN); //Log in with the bots token. You need a line in the .env file: TOKEN=KUAHSFGAJSDHFGKJHGSDF%%$%)"
This is the simplest way to create a MessageCollector, that collects messages via DMs.
I have commented on all the important bits, but if you have questions feel free to ask me.
You can join the Discord server which I’ve linked in another post.