Send a message with Discord.js

本小妞迷上赌 提交于 2019-12-18 04:34:15

问题


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 ?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/45120618/send-a-message-with-discord-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!