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 ?
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 @ .
For instance, Discord. js allows us to create a simple Discord bot using Javascript.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With