如何回复频道中发送的每条消息

How to reply to every message sent in a channel

我正在尝试创建一个 discord 机器人,它将使用
响应 #feedback 中发送的所有消息 'Thanks for your feedback, USERNAME! It has been sent to the admins.'

到目前为止,这是我的代码:

const Discord = require('discord.js');
const client = new Discord.Client();
const settings = require('./settings.json');

client.on('ready',() => {
    console.log('FBB Online!');
});

client.on('message', msg => {
    if (msg.channel != '#feedback') return;
    if (msg.author === client.user) return;
    client.channels.get('488795234705080320').send('Thanks for your feedback, '+msg.sender+'! It has been sent to the admins.');
});

client.login(settings.token);

但是,在测试机器人时,它不会响应任何频道中的消息。为什么会这样,我该如何解决?

我注意到您使用了错误的消息语法。这段代码应该可以工作:

if(message.author.equals(client.user)) return;
if(message.channel.name.equals("feedback")){
    message.channel.send(`Thank you for your feedback ${message.author.username}! It has been sent to the admins.`);
    // two `s allows you to add an object in a string. To add the object, put it inside this -> ${}
}

如果有帮助请告诉我。

编辑:自从我第一次将其写入仅适用于一台服务器的位置以来,我已将其修复为搜索频道名称,因为这就是我的用途。

要识别频道,我们可以使用 message.channel.name 或使用查找功能。使用 message.channel.name,我们可以查看或检查频道名称。我们可以用查找功能做同样的事情,为整个客户端或仅在公会中查找频道,如下所示:

let chan = message.guild.channels.cache.find(channel => channel.name === "feedback") (要搜索存在机器人的所有服务器,只需使用客户端而不是消息或消息)

完整代码,适用于您的代码,在 Discord.js v.12 中使用“msg”而不是“message”:

const Discord = require('discord.js');
const client = new Discord.Client();
const settings = require('./settings.json');

client.on('ready',() => {
    console.log('FBB Online!');
});

client.on('message', msg => {
    if (msg.channel.name != "feedback") return;
    if (msg.author === client.user) return;
    let chan = client.channels.cache.find(ch => ch.id == "488795234705080320")
    chan.send(`Thanks for your feedback, ${msg.author}! It has been sent to the admins.`);
});

client.login(settings.token);

代码很好,但我们可以改进它,连接 if:

const Discord = require('discord.js');
const client = new Discord.Client();
const settings = require('./settings.json');

client.on('ready',() => {
    console.log('FBB Online!');
});

client.on('message', msg => {
    if (msg.channel.name != "feedback" || msg.author === client.user) return;
    let chan = client.channels.cache.find(ch => ch.id == "488795234705080320");
    chan.send(`Thanks for your feedback, ${msg.author}! It has been sent to the admins.`);
});

client.login(settings.token);