Mongodb not recognizing command when someone writes something in front

Asked

Viewed 27 times

2

I have a bot and saved its commands in the Mongodb database, but when I write, for example, "! command and other things on a line" it does not answer, how could it ignore these other words?

My code is:

client.on('chat',(channel, user, message, self) => {
    if(message.substr(0,1)==='!' && !message.startsWith('!add') && !message.startsWith('!deletecommand') && !message.startsWith('!comandos')) {
        const MongoClient = mongodb.MongoClient;
        const url = "my-url";
        MongoClient.connect(url,(err,dbclient) => {
            if(err) {
                console.log(err);
            } else {
                console.log('DB foi conectado com sucesso');
                const database = dbclient.db('twitchbot');
                const collection = database.collection("commands");
                collection.findOne({command:message}).then(result => {
                    if(result) {
                        client.say(ch, result.message );
                    }
                });
            }
            dbclient.close();
        });
    } else if (message.startsWith('!add') && (user['user-type']==='mod' || user['display-name'].toLowerCase()==='helderjunior')) {
        const newcommand = '!'+message.split(" ")[1].replace(/[^\w\s]/gi,'')
        const newmessage = message.split(" ").slice(2).join(" ");
        const newitem = {command: newcommand, message: newmessage};
        const MongoClient = mongodb.MongoClient;
        const url = "my-url";
        MongoClient.connect(url,(err,dbclient) => {
            if(err) {
                console.log(err);
            } else {
                console.log("DB foi conectado com sucesso");
                const database = dbclient.db('twitchbot');
                const collection = database.collection('commands');
                collection.insertOne(newitem).then(result => {
                    client.say(ch,'O comando foi criado com susseso: '+newcommand);
                    console.log(result);
                }).catch(err => {
                    client.say(ch,'Não consegui criar o comando, helderjunior Panela');
                    console.log(err);
                });
            }
            dbclient.close();


        

o que acontece

1 answer

2


One way to solve it would be by doing the split of the message before making the query in mongodb.

The split() method divides a String into a list of substrings, place these substrings in an array and return the array. A split is done looking for a pattern, where the pattern is provided as the first parameter in the method call.

The pattern we should use is the character espaço em branco (U+0020)

Repair the use of the function split in the variable message, using as a separator the espaço em branco.

Using your code would be something like this:

if(message.substr(0,1)==='!' && !message.startsWith('!add') && !message.startsWith('!deletecommand') && !message.startsWith('!comandos')) {
        const MongoClient = mongodb.MongoClient;
        const url = "my-url";
        MongoClient.connect(url,(err,dbclient) => {
            if(err) {
                console.log(err);
            } else {
                console.log('DB foi conectado com sucesso');
                const database = dbclient.db('twitchbot');
                const arrcmd = message.split(' ')
                if (arrcmd.length > 0) {
                    const collection = database.collection("commands");
                    collection.findOne({command:arrcmd[0]}).then(result => {
                        if(result) {
                            client.say(ch, result.message );
                        }
                    });
                }
            }
            dbclient.close();
        });
    }
  • worked perfectly, thank you very much indeed!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.