Send a message with Discord.js
Asked Answered
D

10

41

I am trying to make a discord bot, but I can't quite understand Discord.js. My code looks like this:

client.on('message', function(message) {
 if (message.content === 'ping') {
  client.message.send(author, 'pong');
 }
});

And the problem is that I can't quite understand how to send a message.

Can anybody help me ?

Duodiode answered 15/7, 2017 at 16:50 Comment(4)
Have you read through the documentation?Debora
is that if statement true at any point? Please try to use console.log("test") and put it inside the if statement and check the console for the outputBeside
I've runned through the documentationDuodiode
@MártonKissik Do not use that link as it contains outdated info. Use discord.js.org/#/docs/main/stable/general/welcome . You send a message using message.channel.send("Something")Brittanybritte
F
51

The send code has been changed again. Both the items in the question as well as in the answers are all outdated. For version 12, below will be the right code. The details about this code are available in this link.

To send a message to specific channel

const channel = <client>.channels.cache.get('<id>');
channel.send('<content>');

To send a message to a specific user in DM

const user = <client>.users.cache.get('<id>');
user.send('<content>');

If you want to DM a user, please note that the bot and the user should have at least one server in common.

Hope this answer helps people who come here after version 12.

Foulness answered 13/3, 2020 at 8:51 Comment(0)
E
41

You have an error in your .send() line. The current code that you have was used in an earlier version of the discord.js library, and the method to achieve this has been changed.

If you have a message object, such as in a message event handler, you can send a message to the channel of the message object like so:

message.channel.send("My Message");

An example of that from a message event handler:

client.on("message", function(message) {
  message.channel.send("My Message");
});

You can also send a message to a specific channel, which you can do by first getting the channel using its ID, then sending a message to it:

(using async/await)

const channel = await client.channels.fetch(channelID);
channel.send("My Message");

(using Promise callbacks)

client.channels.fetch(channelID).then(channel => {
  channel.send("My Message");
});

Works as of Discord.js version 12

Emmeram answered 17/7, 2017 at 8:58 Comment(0)
T
8

The top answer is outdated

New way is:

const channel = await client.channels.fetch(<id>);

await channel.send('hi')

To add a little context on getting the channel Id; The list of all the channels is stored in the client.channels property.

A simple console.log(client.channels) will reveal an array of all the channels on that server.

Twinned answered 26/2, 2021 at 7:7 Comment(0)
A
3

Below is the code to dm the user:

(In this case our message is not a response but a new message sent directly to the selected user.)

require('dotenv').config({ path: __dirname + '/.env.local' });

const Discord = require("discord.js");
const client = new Discord.Client();

client.on("ready", () => {
    console.log(client.users.get('ID_OF_USER').send("hello"));
});

client.login(process.env.DISCORD_BOT_TOKEN);

Further documentation:

https://github.com/AnIdiotsGuide/discordjs-bot-guide/blob/master/frequently-asked-questions.md#users-and-members

Alimentary answered 16/9, 2019 at 13:3 Comment(0)
E
3

There are four ways you could approach what you are trying to achieve, you can use message.reply("Pong") which mentions the user or use message.channel.send("Pong") which will not mention the user, additionally in discord.js you have the option to send embeds which you do through:

client.on("message", () => {
    var message = new Discord.MessageEmbed()
      .setDescription("Pong") // sets the body of it
      .setColor("somecolor")
      .setThumbnail("./image");
      .setAuthor("Random Person")
      .setTitle("This is an embed")
    msg.channel.send(message) // without mention
    msg.reply(message) // with mention
})

There is also the option to dm the user which can be achieved by:

client.on("message", (msg) => {
msg.author.send("This is a dm")

})

See the official documentation.

Elfish answered 9/3, 2021 at 10:8 Comment(1)
There are 3 ways. You can also DM the user.Twinned
E
1

You can only send a message to a channel

client.on('message', function(message) {
 if (message.content === 'ping') {
  message.channel.send('pong');
 }
});

If you want to DM the user, then you can use the User.send() function

client.on('message', function(message) {
 if (message.content === 'ping') {
  message.author.send('pong');
 }
});
Epileptoid answered 22/10, 2020 at 0:43 Comment(0)
M
1

Types of ways to send a message: DM'ing whoever ran the command:

client.on('message', function(message) {
 if (message.content === 'ping') {
  message.author.send('pong');
 }
});

Sends the message in the channel that the command was used in:

client.on('message', function(message) {
 if (message.content === 'ping') {
  message.channel.send('pong');
 }
});

Sends the message in a specific channel:

client.on('message', function(message) {
const channel = client.channels.get("<channel id>")
 if (message.content === 'ping') {
  channel.send("pong")
 }
});
Mohamed answered 26/7, 2021 at 3:52 Comment(0)
E
0

Discord.js v14.13.0 code:

// Import d.js library
const { Client, Events } = require('discord.js');

// Your client
const client = new Client({...})

// Sending message
client.on(Events.MessageCreate, (msg) => {
if(msg.content.startsWith('ping') {
 return msg.reply('pong')
}
});

client.login(...)
Egor answered 9/10, 2023 at 8:46 Comment(0)
B
0

To make it works with typescript, you should check if the channel exists and is text-based first:

const channel = await discordClient.channels.fetch("channel-id");
if (channel?.isTextBased()) {
   await channel.send("Hello, world!");
}
Bolden answered 5/5 at 15:4 Comment(0)
I
-1

It's message.channel.send("content"); since you're sending a message to the current channel.

Interoceptor answered 6/6, 2020 at 22:5 Comment(3)
Wrong link oops lol, discord.js.org/# is the link I meant to putInteroceptor
This is an almost 3 year old thread, but thanks nonetheless ;)Duodiode
Didn't realize that lmao, no problemInteroceptor

© 2022 - 2024 — McMap. All rights reserved.