简体   繁体   中英

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

I'm working with a friend to add something to an existing Discord bot. There a number of working commands that use discord.js-commando , so we're tied to using Commando.

The guild we're doing this for has moved some resources from an old site to a new one, and we'd like to remind the guild members who link to the old site that they should use the new site instead:

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

The bot would only trigger if it sees www.example.com .

Here's the code...

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

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

and the file I'm working on

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

Trouble is, this doesn't use Commando, just regular discord.js. Is this even possible with Commando? Or do I need another approach?

If you intend on using Comando you'll need to work with the Commando.Client . The way your bot is set up is so you only need to add a command in the commands folder. Seems like Comando has a way to trigger on a regular expression.

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!');
    }
}

Here's an example of a Command . Here's the documentation of what can go into the CommandInfo (your command config).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM