tmijs / tmi.js

💬 Javascript library for the Twitch Messaging Interface. (Twitch.tv)
https://tmijs.com
MIT License
1.54k stars 216 forks source link

Bot is crashing if using API in chat events #36

Closed ghost closed 8 years ago

ghost commented 8 years ago

I'm trying to do command, that will get number of viewers and say that number into the chat. But when i trying to do this, i get:

G:\Program Files\nodejs\node_modules\tmi.js\lib\api.js:39
            callback(err, res, body);
            ^
TypeError: undefined is not a function
    at Request._callback (G:\Program Files\nodejs\node_modules\tmi.js\lib\api.js:39:13)
    at Request.self.callback (G:\Program Files\nodejs\node_modules\tmi.js\node_modules\request\request.js:198:22)
    at Request.emit (events.js:110:17)
    at Request.<anonymous> (G:\Program Files\nodejs\node_modules\tmi.js\node_modules\request\request.js:1057:14)
    at Request.emit (events.js:129:20)
    at IncomingMessage.<anonymous> (G:\Program Files\nodejs\node_modules\tmi.js\node_modules\request\request.js:1003:12)
    at IncomingMessage.emit (events.js:129:20)
    at _stream_readable.js:908:16
    at process._tickCallback (node.js:355:11)

The code is

client.on("chat", function (channel, user, message, self) {
    if (message.toLowerCase() == '!v') {
        client.api({
            url: "https://api.twitch.tv/kraken/streams/" + channel,
            methon: "GET",
            headers: {
                "Accept": "application/vnd.twitchtv.v3+json",
                "Authorization": "OAuth ",
                "Client-ID": ""
            }
        }), function (err, res, body) {
            console.log(body);
        }
    }
});

How can i fix this?

Schmoopiie commented 8 years ago

Move your callback inside the client.api function..

client.on("chat", function (channel, user, message, self) {
    if (message.toLowerCase() == '!v') {
        client.api({
            url: "https://api.twitch.tv/kraken/streams/" + channel.replace("#", ""),
            method: "GET"
        }, function (err, res, body) {
            console.log(body);
        });
    }
});

You also have a typo methon and because you are using the channel variable, use channel.replace("#", "") to remove the #.

AlcaDesign commented 8 years ago

:+1:

ghost commented 8 years ago

Thanks guys for help, now it's working. But i have one more question. How to get number of viewers and send it to the chat?

Decicus commented 8 years ago

You can use the /streams/:channel endpoint of the Twitch API.

AlcaDesign commented 8 years ago

If the stream is offline, stream will be null in that endpoint.

Edit: Oh, it was linked OpieOP Mobile Github's not amazing on a dark screen.

Decicus commented 8 years ago

Which is also specified on the page I linked.