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

标签 javascript discord.js commando

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

我们这样做的公会已经将一些资源从旧站点转移到新站点,我们想提醒链接到旧站点的公会成员,他们应该改用新站点:

// 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 的例子.这是可以进入 CommandInfo 的文档(您的命令配置)。

关于javascript - 如何让 Commando 机器人响应消息中任意位置包含特定子字符串的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57445034/

相关文章:

javascript - 测试错误 : Component is not part of any NgModule or the module has not been imported into your module

javascript 多语言元描述

discord.js - 获取对旧消息的旧 react

javascript - 命令分组时未定义的模块

javascript - Discord.js - 允许在特定 channel 中执行命令

javascript - 广播所有命令不自动删除广播 - Discord.js-Commando

javascript - 如何修复响应未知命令的 discord.js-commando bot

javascript - ReactJS 应用认证 : Firebase+FirebaseUI Uncaught Error: Firebase App named '[DEFAULT]-firebaseui-temp' already exists

javascript - 使用 jquery 旋转后其他元素的不当行为