【问题标题】:How to reply to every message sent in a channel如何回复频道中发送的每条消息
【发布时间】:2019-06-06 19:41:13
【问题描述】:

我正在尝试创建一个 Discord 机器人,该机器人将响应在#feedback 发送的所有消息
'感谢您的反馈,USERNAME!它已发送给管理员。'

这是我目前的代码:

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);

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

【问题讨论】:

    标签: discord discord.js


    【解决方案1】:

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

    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 -> ${}
    }
    

    如果这有帮助,请告诉我。

    编辑:我修复了这个以搜索频道的名称,因为我第一次将它写到它只适用于一个服务器的地方,因为这就是我的用途。

    【讨论】:

    • 也许这里的 Lebster 或访问此问题的任何其他人希望他们的机器人位于多个服务器上并响应任何名为 #feedback 的频道。也许您也可以添加一个方法来做到这一点?
    • 是的,我很抱歉,我没有想到这一点。生病写它,以便它可以在其他服务器上工作
    【解决方案2】:

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

    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);
    

    【讨论】:

      猜你喜欢
      • 2012-06-18
      • 2020-05-24
      • 2019-02-12
      • 2021-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-14
      相关资源
      最近更新 更多