-
i can connect and recieve messages but i can only respond when i recieve a message i made a very simple function to send a message to a channel but i keep getting this error: Uncaught (in promise) Not connected to server. this is the function: function sendMessage(message) {
client.join(settings.TWITCH.CHANNEL_NAME);
client.say(settings.TWITCH.CHANNEL_NAME, message);
} i just want to know how to send a simple message. thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
i dont know if this is the correct way but this is what i came up with: const tmi = require('tmi.js');
let message = ''
const client = new tmi.Client({
options: {
skipUpdatingEmotesets: true,
},
identity: {
username: config.settings.TWITCH.USERNAME,
password: `oauth:${config.settings.TWITCH.OAUTH_TOKEN}`,
},
channels: [config.settings.TWITCH.CHANNEL_NAME],
});
client.on('connected', () => {
console.log(`${message}`);
if(message !== ''){
client.say(config.settings.TWITCH.CHANNEL_NAME, message);
}
message = '';
});
function sendMessage(text) {
client.connect().catch(console.error);
message = text
}
client.connect().catch(console.error);
sendMessage("test"); i dont know if there is a more effective way to do this but this is all i got. |
Beta Was this translation helpful? Give feedback.
-
It takes time for things to happen on the network. (Decide to connect, lookup address in DNS, begin establishing a websocket connection (http request, then request upgrade from http to websocket, handshake), begin the IRC handshake including authorization, now it's ready) JavaScript will not wait on each line or code unless you tell it to with async/await syntax (or you can utilize Promise callbacks ("then" and "catch", etc.)). In order to send a message you must first fully connect just like the error says. You don't have to join a channel in order to send a message to that channel (but that also does take time to join a channel because of network and rate limiting). If you just want to send a message to announce that the bot is ready, use the "connected" event or by using the "connect" Promise: client.connect()
.then(() => client.say(config.settings.TWITCH.CHANNEL_NAME, 'Connected'))
.catch(console.error); client.on('connected', () => {
client.say(config.settings.TWITCH.CHANNEL_NAME, 'Connected');
});
client.connect().catch(console.error); You don't need to add "oauth:" to your token as tmi.js will do that for you. |
Beta Was this translation helpful? Give feedback.
i dont know if this is the correct way but this is what i came up with:
i dont kn…