Home > Software design >  How can I make a Discord bot listen to my DMs instead of simple channel messages?
How can I make a Discord bot listen to my DMs instead of simple channel messages?

Time:07-17

I'm messing around creating a simple quiz bot. So far it is working well in channels, but I would like to modify it into a DM-only quiz bot.

I listen to commands from Discord users like this:

client.on('messageCreate', async message => {

And it works well but ONLY IN channels, not in DMs.

I've searched for 'directMessageCreate' or anything else but I haven't found the solution yet.

I also tried to search for tutorials and everybody starts the bot DM with a public command in a public channel, I would like to make a DM only bot.

How could I make my public bot a DM only bot?

E D I T:

These are my intents, which have DIRECT_MESSAGES listed on. Is something still missing?

const client = new discord.Client({
    intents: ["GUILDS", "GUILD_MESSAGES", "DIRECT_MESSAGES"]
});

CodePudding user response:

You'll need to enable the required intents and partials to be able to listen to direct messages. As you've already added DIRECT_MESSAGES, you must have missed the CHANNEL partial. It's required for DMs. Although server channels will always be available, DM channels can be uncached and that's why you'll need to add it like this:

const client = new discord.Client({
    intents: ["GUILDS", "GUILD_MESSAGES", "DIRECT_MESSAGES"],
    partials: ['CHANNEL']
});

If you want to make a DM only bot, you'll need to check the channel.type and ignore anything that's not a DM:

client.on('messageCreate', (message) => {
    if (message.author.bot || message.channel.type !== 'DM') return;

    // handle the command sent in a DM
});

  • Related