Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client can't say anything, results in error. #16

Closed
megadrive opened this issue Jul 12, 2015 · 15 comments
Closed

Client can't say anything, results in error. #16

megadrive opened this issue Jul 12, 2015 · 15 comments

Comments

@megadrive
Copy link

This is the error I get with the current latest codebase (d8c2e47) on Windows 8.1, running node 0.12.7 on npm 2.11.3

with this code

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

var clientOptions = {
    options: {
        'debug': true,
    },
    connection: {
        'random': 'chat'
    },
    identity: {
        'username': 'ozbt',
        'password': 'oauth:oauthtoken'
    },
    'channels': ['#ozbt']
};

// Calling a new instance..
var client = new irc.client(clientOptions);

// Connect the client to the server..
client.connect();

client.on('connected', function(channel, username, message){
    client.say(channel, 'hello');
});

I've tried fixing the code myself, but I'm awful at fixing other people's code, especially when I don't know what it's doing or trying to do. Any suggestions would lovely. Any more information needed, let me know.

@AlcaDesign
Copy link
Member

You cannot send a message before joining a channel.

The correct connected event is

client.addListener('connected', function(address, port) {

You're looking for join:

client.addListener('join', function(channel, username) {

@megadrive
Copy link
Author

Still doesn't work, even with the changes.

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

var _oauth = 'oauth:oauyth';
var _username = 'ozbt';


var clientOptions = {
    options: {
        'debug': true,
    },
    connection: {
        'random': 'chat'
    },
    identity: {
        'username': _username,
        'password': _oauth
    },
    'channels': ['#ozbt']
};

// Calling a new instance..
var client = new irc.client(clientOptions);

// Connect the client to the server..
client.connect();

client.addListener('join', function(channel, username){
    client.say(channel, 'hello');
});

Same error.

@AlcaDesign
Copy link
Member

If you put a timeout in the join for 1 millisecond, you do not get the error.

client.addListener('join', function(channel, username) {
        setTimeout(function() {
                client.say(channel, 'Hello');
            }, 1);
    });

Essentially, client.userstate needs to have information for that channel before client.say() works and that happens within that millisecond, I suppose. A longer time might be better.

Perhaps another function to handle this until something that needs fixing in the module is fixed?

function sendChat(channel, message) {
    if(client.userstate.hasOwnProperty(channel)) {
        client.say(channel, message);
    }
    else {
        setTimeout(sendChat, 10, channel, message); // Try again in 10ms
    }
}

@megadrive
Copy link
Author

The sendChat function you've provided works wonderfully. I wonder if tmi should have a timeout or something? Least the bug is known now.

@AlcaDesign
Copy link
Member

I didn't add any termination point that would account for the possibility of never joining the channel the message is intended for (including channel normalization), so be weary

@megadrive
Copy link
Author

Okay, I gotta take it back. I made another barebones file, and all that ever happens is it constantly waits, with client.userstate only ever equalling {"#jtv":{}}. It's a very strange error that I don't know how to fix or even workaround

edit: after calling client.join(), i manually added the userstate for the joined channel as an empty object and now it works?

client.join("ozbt");
client.userstate["#ozbt"] = {};

@Schmoopiie
Copy link
Member

This is something we can definitely improve. You cannot and should not alter the userstate object, it is something that we are doing automatically when joining and leaving a channel. We are adding #jtv in the userstate to prevent any bugs when sending whispers (whispers are sent via #jtv even if you are not on this channel)

@megadrive
Copy link
Author

Yeah, I figured. I try only to use functions given to me via documentation but after finding this bug I had to go digging and find out why. It's preventing me from migrating to tmi.js from twitch-irc currently, so hopefully it'll be fixed soon. Thanks for looking at it.

@systimotic
Copy link

Does that commit mean this issue is resolved and can be closed?

@Schmoopiie
Copy link
Member

Issue is solved but I'm not gonna close it until I bump the beta version.. I still have some changes and new stuff to implement. Expect a version bump soon though 😄

@Schmoopiie
Copy link
Member

Should be fixed now. Pushed update to NPM and CDN, please update to beta 0.0.18. Closing the issue.

@NathanEEvans
Copy link

just so you know, it doesn't play well with node-mumble

@TheHollidayInn
Copy link

I have this same issue with the latest version 1.1.1

@AlcaDesign
Copy link
Member

AlcaDesign commented Jul 21, 2016

@TheHollidayInn, you get the same TypeError? "Cannot set property of 'message-type' of undefined" ? Did you join the channel before sending the message?

@TheHollidayInn
Copy link

@AlcaDesign Sorry, I was about to edit to clarify.

After joining a channel, the client.userstate was not being set. But, it turns out the join event was be emitted even though the connection was unsuccessful.

I fixed some options to create a successful join.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants