Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

javascript - 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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...