如何让 Commando 机器人响应消息中任意位置包含特定子字符串的消息

How do I make a Commando bot respond to a message with a certain substring anywhere in the message

我正在与一位朋友一起为现有的 Discord 机器人添加一些东西。有许多使用 discord.js-commando 的工作命令,所以我们不得不使用 Commando。

我们为之服务的公会已将一些资源从旧站点转移到新站点,我们想提醒 link 到旧站点的公会成员他们应该使用改为新站点:

// User123 says...
Check out https://www.example.com/.
// bot responds:
Hey User123! You should use our new site! https://www.example2.com/

机器人只有在看到 www.example.com 时才会触发。

这是代码...

// file: index.js
const bot = new Commando.Client({
    commandPrefix: './'
});

bot.registry
    .registerGroup('autoresponses', 'AutoResponses')
    // other groups
    .registerDefaults()
    .registerCommandsIn(__dirname + '/commands')
    ;

以及我正在处理的文件

// file: commands/autoresponses/messages.js
const discord = require('discord.js');

client.on("message", function(message) {
    if (message.author.bot) {
        return;
    }
    if (message.content.includes("www.example.com")) {
        var responseString = "Hey " + message.author + "! That's the old site! Please use the new one: https://www.example2.com/";
        message.channel.send(responseString);
    }
};

问题是,这不使用 Commando,只是常规 discord.js。 Commando 甚至可以做到这一点吗?或者我需要另一种方法吗?

如果您打算使用 Comando,则需要使用 Commando.Client。您的机器人的设置方式是您只需要在 commands 文件夹中添加一个命令。好像 Comando 有办法触发正则表达式。

let Command = require('Comando').client;
module.exports = class OldWebsite extends Command {
    constructor(client){
        super(client, {/* Your command config here */, patterns: [/website\.com/] });
        //patterns is a prop that accepts regular expressions to match on a message
    }
    async run(msg){
        return msg.reply('Hey that\'s our old webiste!');
    }
}

这是一个 Command. Here's the documentation of what can go into the CommandInfo 的示例(您的命令配置)。