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();
worked perfectly, thank you very much indeed!
– Helder Junior