Dunkstormen Dunkstormen - 6 months ago 25
Node.js Question

Banned words filter (tmi.js)

So I've been trying to make this work for about 3 hours now, and I' almost giving up.

My current code is:



var tmi = require('tmi.js');

var linksDisallowed = ["http://", "https://", ".dk"];

var options = {
options: {
debug: true
},
connection: {
cluster: "aws",
reconnect: true
},
identity: {
username: "botstormen",
password: "**CENSORED FOR SECURITY REASONS**"
},
channels: ["dunkstormen"]
};

var client = new tmi.client(options);
client.connect();

client.on('connected', function(adress, port) {
client.action("dunkstormen", "joinede chatten og er klar til at hjælpe! :3");
});

client.on('chat', function(channel, user, message, self) {
if(message === "!social" || message === "!Social") {
client.action("dunkstormen", "Twitter: twitter.com/dunkstormen Facebook: facebook.com/dunkstormen");
}
});

client.on('chat', function(channel, user, message, self) {
for (var i = 0; i < 3; i++) {

if(message.indexOf(linksDisallowed[i]) >= 0) {
client.say("Hello, " + user);
}

}
});





But whenever I type one of the word in the array linksDisallowed into the chat the bot just instantly crashes with the following error:



C:\Users\Benjamin Jørgensen\Desktop\botstormen\node_modules\tmi.js\lib\commands.js:207
if (message.toLowerCase().startsWith("/me ") || message.toLowerCase().startsWith("\\me ")) {
^

TypeError: Cannot read property 'toLowerCase' of undefined
at client.say (C:\Users\Benjamin Jørgensen\Desktop\botstormen\node_modules\tmi.js\lib\commands.js:207:20)
at client.<anonymous> (C:\Users\Benjamin Jørgensen\Desktop\botstormen\app.js:37:11)
at client.EventEmitter.emit (C:\Users\Benjamin Jørgensen\Desktop\botstormen\node_modules\tmi.js\lib\events.js:99:50)
at client.handleMessage (C:\Users\Benjamin Jørgensen\Desktop\botstormen\node_modules\tmi.js\lib\client.js:792:34)
at C:\Users\Benjamin Jørgensen\Desktop\botstormen\node_modules\tmi.js\lib\client.js:919:18
at Array.forEach (native)
at client._onMessage (C:\Users\Benjamin Jørgensen\Desktop\botstormen\node_modules\tmi.js\lib\client.js:917:11)
at WebSocket.onMessage (C:\Users\Benjamin Jørgensen\Desktop\botstormen\node_modules\tmi.js\node_modules\ws\lib\WebSocket.js:442:14)
at emitTwo (events.js:87:13)
at WebSocket.emit (events.js:172:7)




Answer

tmi.js command say takes 2 arguments, you are passing only one.

https://docs.tmijs.org/v0.0.29/Commands.html#say

Change client.say("Hello, " + user); to client.say(channel,"Hello, " + user);

EDIT to help about comment:

According to docs, https://docs.tmijs.org/v0.0.29/Events.html#chat the chat event returns a user object. There is the description of a user object just below with a notice about it can change... But you can certainly do this for now: replace user by user.username .

If it does not work, inspect the user object to find the right property you should use.