Hello everyone.
Today, as a fun project, I tried making a Chatbot for the first time ever. I followed a tutorial, and i created it using Javascript and tmi.js. The bot worked fine at first, it would respond to 90% of my commands and would not crash. I tried hosting it on Heroku. Again, it worked fine at first, even if it seemed to crash sometimes, but i blamed that on me not knowing how to use Heroku properly. However, i tried launching the bot on my computer with NodeJs again (since it worked fine before). And here’s what i found :
- The bot, using a console.log line i added for debugging, seems to be able to read messages and tells me it responded
- In the chat, no message appears from the bot at all
- Eventually, the bot crash and i get this error :
C:\Users\natha\Desktop\MinouBot\twitch\oreminou-twitch-bot\server.js:47
const [raw, command, argument] = message.match(regexpCommand);
^
TypeError: object null is not iterable (cannot read property Symbol(Symbol.iterator))
at client.<anonymous> (C:\Users\natha\Desktop\MinouBot\twitch\oreminou-twitch-bot\server.js:47:38)
at client.EventEmitter.emit (C:\Users\natha\Desktop\MinouBot\twitch\oreminou-twitch-bot\node_modules\tmi.js\lib\events.js:94:44)
at client.emits (C:\Users\natha\Desktop\MinouBot\twitch\oreminou-twitch-bot\node_modules\tmi.js\lib\client.js:90:13)
at client.handleMessage (C:\Users\natha\Desktop\MinouBot\twitch\oreminou-twitch-bot\node_modules\tmi.js\lib\client.js:1117:13)
at C:\Users\natha\Desktop\MinouBot\twitch\oreminou-twitch-bot\node_modules\tmi.js\lib\client.js:1228:9
at Array.forEach (<anonymous>)
at client._onMessage (C:\Users\natha\Desktop\MinouBot\twitch\oreminou-twitch-bot\node_modules\tmi.js\lib\client.js:1225:8)
at WebSocket.onMessage (C:\Users\natha\Desktop\MinouBot\twitch\oreminou-twitch-bot\node_modules\ws\lib\event-target.js:199:18)
at WebSocket.emit (node:events:520:28)
at Receiver.receiverOnMessage (C:\Users\natha\Desktop\MinouBot\twitch\oreminou-twitch-bot\node_modules\ws\lib\websocket.js:1137:20)
I’m not quite sure what is happening. It seems like it is having trouble with checking if the message it read is a command ?
Other information : The bot was not a moderator on the channel when i tested it
Again, i’m quite new to developping twitch chatbots, so any help or tip would be amazing.
Thanks a lot !
Here’s my full code :
const tmi = require('tmi.js');
require('dotenv').config();
const regexpCommand = new RegExp(/^!([a-zA-Z0-9]+)(?:\W+)?(.*)?/);
const commands = {
* some commands *
}
const client = new tmi.Client({
connection: {
reconnect: true
},
channels: [
'my-twitch-channel'
],
identity: {
username: process.env.TWITCH_BOT_USERNAME,
password: process.env.TWITCH_OAUTH_TOKEN
}
});
client.connect();
client.on('message', async (channel, context, message) => {
client.on('message', async (channel, context, message) => {
const isNotBot = context.username.toLowerCase() !== process.env.TWITCH_BOT_USERNAME.toLowerCase();
if ( !isNotBot ) return;
const [raw, command, argument] = message.match(regexpCommand);
const { response } = commands[command] || {};
if ( typeof response === 'function' ) {
client.say(channel, response(argument));
} else if ( typeof response === 'string' ) {
client.say(channel, response);
}
console.log("Responded to a message in chat");
});
});