Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send a message with Discord.js

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 ?

like image 353
Gabe Avatar asked Jul 15 '17 16:07

Gabe


People also ask

How do I mention a message in Discord Javascript?

Discord uses a special syntax to embed mentions in a message. For user mentions, it is the user's ID with <@ at the start and > at the end, like this: <@86890631690977280> . If they have a nickname, there will also be a ! after the @ .

Can you use Javascript in Discord?

For instance, Discord. js allows us to create a simple Discord bot using Javascript.

What is client Discordjs?

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 most other JS Discord libraries, making your bot's code significantly tidier and easier to comprehend.


1 Answers

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.

like image 152
Balasubramanian S Avatar answered Sep 20 '22 20:09

Balasubramanian S