【问题标题】:Banned words filter (tmi.js)禁用词过滤器 (tmi.js)
【发布时间】:2016-06-09 09:42:46
【问题描述】:

所以我已经尝试了大约 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)

【问题讨论】:

    标签: javascript node.js twitch


    【解决方案1】:

    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 聊天事件返回一个用户对象。下方有一个用户对象的描述,并附有关于它可以更改的通知...但是您现在当然可以这样做:将 user 替换为 user.username

    如果它不起作用,请检查用户对象以找到您应该使用的正确属性。

    【讨论】:

    • 谢谢,但现在我遇到的问题是用户只返回 [object Object] 而不是用户的实际名称。
    • 已编辑。顺便说一句,我从未使用过 tmijs 或 twitch... 只是阅读文档 :)
    • 感谢您的帮助!非常感谢:)
    【解决方案2】:

    这意味着您的 app.js 没有向 tmi.js 服务发送消息

    【讨论】:

      猜你喜欢
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 2017-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多