#my discord bot does not answer dm messages. How can I handle direct messages sent to a bot?

49 messages · Page 1 of 1 (latest)

hollow oasis
#

Good evening,

I would like to get some help with my discord bot. I am trying to make my discord bot answer direct messages, but it does not react. I develop this bot using javaScript and Discord.js library. My code looks like this:

export const client = new Discord.Client({intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageReactions,
GatewayIntentBits.DirectMessageTyping,
]});

await client.login(process.env.BOT_TOKEN);

client.on( "messageCreate", async (message) => {

console.log("message created...");

}

I have added intents to see direct messages with the bot and I do not understand why the "messageCreate" event only works when the message is sent in the guild (server), but sending dm to the bot does not trigger this event. When I use commands in the channels of my server, it works well and I get the log in my console (I run my bot locally for testing). I have already done some commands like ban/unban and creating embeds with gif files and it all works. Also I have tried to use conditions like this

if(message.channel.type === 'dm'){
console.log("dm received")
}

This also does not work for me.
The reason why I want to make my bot answer to DMs is I want to develop a game where one person make up a word and the other tries to guess it. So I want to be able to save the word sent to a Bot's DM in the Database. Also I would like to make my bot create secret messages in the servers channel, which only can be read by certain persons. This message can be send to a Bot's DM and then the bot would create an Embed with a button to open the message.

Would be nice if someone could help me
Thank you!

thorny nova
# hollow oasis Good evening, I would like to get some help with my discord bot. I am trying to...

This is the eventemitter you would need to reply to DMS, I suggest you either A. Watch a youtube tutorial to learn how to write a bot or B. refer to the Discord.js docs to learn the methods and eventemitters you'd need for your bot. https://github.com/discordjs/discord.js/ The github has an entire folder dedicated to examples.

client.on('message', msg => {
  if (msg.channel.type == "dm") {
    msg.author.send("You are DMing me now!");
    return;
  }
});```
GitHub

A powerful JavaScript library for interacting with the Discord API - GitHub - discordjs/discord.js: A powerful JavaScript library for interacting with the Discord API

hollow oasis
thorny nova
hollow oasis
#

it is called messageCreate now...

#

but I could not find anything about answering bot's dm in that guide

thorny nova
#

Okay

#

so how would you fix that?

#

also why is your eventemitter async if you're not gonna use the await keyword

#
export const client = new Discord.Client({intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent,
        GatewayIntentBits.GuildVoiceStates,
        GatewayIntentBits.DirectMessages,
        GatewayIntentBits.DirectMessageReactions,
        GatewayIntentBits.DirectMessageTyping,
    ]});

await client.login(process.env.BOT_TOKEN);

client.on( "messageCreate", (message) => {

if(message.channel.type === 'dm'){
        console.log("dm received")
 }
}```
#

Here, this should work

hollow oasis
#

but, this is the same I did, only without the async operator

#

I can remove it

thorny nova
#

No it is not, You do realize the if statement needs to be inside the eventemitter for the If statement to event execute, Yes?

#

the message variable is in the scope of the messageCreate eventemitter

hollow oasis
#

this is the same I already did

#

I used the async operator bc I need it for other commands, I made. Webstorm says that methods on message return promise that's why I used async await, I think it could work without

thorny nova
#

in your original message the if statement was not inside the eventemitter

hollow oasis
#

I know, maybe I should have made my message better for understanding

#

but even without the condition the event should be triggered, right?

#

if the "messageCreate" event would be triggered by dm messages

#

then I should see "message created..." in my console

thorny nova
#

message.channel.DMChannel

Try this in your if statement

hollow oasis
#

it seem not to work. But I think I don't need the condition at all

thorny nova
#

no, you need the condition

hollow oasis
#

the problem is that dm's don't trigger the event

#

if "messageCreate" event would be triggered by dm's I would see my console.log

#

if the event is not triggered then I don't get to the condition

thorny nova
#
message.guild === null``` 
 try this it will check for all DMs (if the messages server is null that will return true if the message was not sent via a server, that then must be a dm.)
hollow oasis
#

but I think the problem is that the event is not triggered at all by dm's

thorny nova
#

oh then are those all your intents in your original msg?

hollow oasis
#

if I send dm's to my bot the "messageCreate" event is not triggered

thorny nova
#

export const client = new Discord.Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageReactions,
GatewayIntentBits.DirectMessageTyping,
], partials: [
'CHANNEL', // Required to receive DMs
]
}
);

hollow oasis
#

I don't have partials

thorny nova
#

and then for your if statement:

if (msg.partial) {
// Never triggers
console.log(Received partial message- ${msg.id});
return;
}

hollow oasis
hollow oasis
#

I had to import Partials Object

#

now the bot can see dm, thank you

thorny nova
hollow oasis
#

I think it is better