Send a message with Discord.js

So. 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 ?


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.

discord.js, About. discord.js is a powerful node.js module that allows you to interact with the Discord API very easily. It takes a much more object-oriented approach than  There are many ways to get a unicode character of an emoji, but the easiest way would be through Discord itself. If you send a message with a unicode emoji (such as :smile:, for example) and put a \ before it, it will "escape" the emoji and will display the unicode character instead of the normal emoji image.


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

To send a message, use this line:

message.channel.send('My Message')

If you get an error saying that message is not defined, make sure that you have put the line in your message event handler.

client.on("message", function(message) {
  //message sending goes here
});

You can also send a message to a specific channel, which you can do using the line below.

client.channels.get(channelID).send('My Message');

Or if you prefer, a guild's default channel (the #general channel that was made when the guild was created)

guildObj.defaultChannel.send('My Message');

Hope this helped,

- Spy

A short discord.js tutorial with examples · GitHub, author will send a dm to the user. That's how you send private messages. Congrats another time! You successfully made your first command. Editing a message. Your code is returning the error, because Discord.Client () doesn't have a method called sendMessage () as can be seen in the docs. If you would like to send a message, you should do it in the following way; var Discord = require('discord.js'); var bot = new Discord.Client() bot.on('ready', function() { console.log(bot.user.username); }); bot.on('message', function() { if (message.content === "$loop") { var interval = setInterval (function () { message.channel.send("123") }, 1 * 1000); } });


Below I presenting script that send direct message to user.

In this case our message is not a response but new message send directly to 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

DiscordJS Tutorial Ep. 3, If you want to DM the user who sent the message, you can use <message>.​author.send() . # How do I tag a certain user in a message? const user  I am making a discord bot using node.js and discord.js, and I am currently trying to make it so that when a user joins the discord server, a custom welcome message is sent. Here is my code: bot.on("guildMemberAdd" ,(message, member) => { message.channel.send("Welcome") });


Discord.JS MessageCollector, Bots receive webhook messages in a text channel normally. You can detect if the message was sent by a webhook by simply checking if the  To install the discord.js package, just run npm i discord.js (or npm i discord.js --save if you want to write it automatically in your package.json file) in your bots' directory. Important for windows users: Normally cmd opens in system32, so navigate into your directory using cd (otherwise it'll install that package in system32).


Common questions, How to send messages to an specific channel. Discord.js. I want to do a command that you can write for example in #commands , and the bot answers in another  Discord.js features the utility class MessageEmbed for easy construction and manipulation of embeds. WARNING In version 12 the receiving and outgoing embed classes have been unified; you will need to use Discord.MessageEmbed() as constructor instead.


Webhooks, You can also send the bot a direct/private message and it will respond in the same const Discord = require('discord.js') If you want to tag the user who sent the message, you can use <message>.reply(). For example: <message>.reply('hi.') would result in @User, hi.. If you want to insert the tag elsewhere, you can store <message>.author as your user variable and use the original example.