禁用词过滤器 (tmi.js)
Banned words filter (tmi.js)
所以我已经尝试完成这项工作大约 3 个小时了,我几乎要放弃了。
我当前的代码是:
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);
}
}
});
但是每当我在聊天中输入数组 linksDisallowed 中的一个词时,机器人就会立即崩溃并出现以下错误:
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)
tmi.js 命令 say
有 2 个参数,你只传递了一个。
https://docs.tmijs.org/v0.0.29/Commands.html#say
将client.say("Hello, " + user);
更改为client.say(channel,"Hello, " + user);
编辑以帮助评论:
根据文档,https://docs.tmijs.org/v0.0.29/Events.html#chat 聊天事件 returns 一个用户对象。下面有一个用户对象的描述,上面有一个关于它可以改变的通知......但你现在当然可以这样做:将 user
替换为 user.username
.
如果它不起作用,请检查用户对象以找到您应该使用的正确 属性。
这意味着您的 app.js
没有向 tmi.js 服务发送消息
所以我已经尝试完成这项工作大约 3 个小时了,我几乎要放弃了。
我当前的代码是:
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);
}
}
});
但是每当我在聊天中输入数组 linksDisallowed 中的一个词时,机器人就会立即崩溃并出现以下错误:
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)
tmi.js 命令 say
有 2 个参数,你只传递了一个。
https://docs.tmijs.org/v0.0.29/Commands.html#say
将client.say("Hello, " + user);
更改为client.say(channel,"Hello, " + user);
编辑以帮助评论:
根据文档,https://docs.tmijs.org/v0.0.29/Events.html#chat 聊天事件 returns 一个用户对象。下面有一个用户对象的描述,上面有一个关于它可以改变的通知......但你现在当然可以这样做:将 user
替换为 user.username
.
如果它不起作用,请检查用户对象以找到您应该使用的正确 属性。
这意味着您的 app.js
没有向 tmi.js 服务发送消息