How do I connect my app to chat?

I want to know how I would put the stuff to log in through identity.
I have already gotten the access code through the client secret

Here’s my code:

require('dotenv/config')
require('./app')

console.log(`App Started`)

const tmi = require("tmi.js")
const channel = "deadpeng1"
const prefix = "!"

const config = {
    options: {
        debug: true
    },
    connection: {
        cluster: "aws",
        reconnect: true
    },
    identity: {
        username: process.env.CLIENT_ID,
        password: process.env.ACCESS_TOKEN
    },
    channels: [channel]
}
const client = new tmi.client(config)

client.connect()
    .then(() => {
        console.log("Connected to Chat")
    })
    .catch(() => {
        "Failed to connect to Twitch Chat"
    })

client.on("connected", (address, port) => {
    client.action(channel, `Connected to ${address}: ${port}`)
})

client.on("chat", (target, ctx, message, self) => {
    if (self) return;

    console.log(target)
    console.log(ctx)

    // command handler

    const args = message.slice(prefix.length).trim().split(' ');
    const cmd = args.shift().toLowerCase();

    try {
        let commandFile = require(`./Commands/${cmd}.js`)
        commandFile.run(client, target, ctx, message, self, args)
    } catch (error) {
        return
    }
});

client.on('cheer', (target, userstate, message) => {
    console.log(`${userstate.username} cheered ${userstate.bits} bits with message: "${message}"`)
    client.say(`${userstate.username} cheered ${userstate.bits} bits with message: "${message}"`)
});

client.on('follow', (target, userstate, message) => {
    console.log(`${userstate.username} cheered ${userstate.bits} bits with message: "${message}"`)
    client.say(`${userstate.username} cheered ${userstate.bits} bits with message: "${message}"`)
});

Since you are using a library, you may want to refer to the libraries documentation

And/or the connection example

Hey Barry, I am not as experienced as your run-of-the-mill js devs. I am having trouble understanding the docs. Do you know how to do it with the config dictionary, or should I come back to this when I am more experienced? Anyways, thank you for your help!

Your code appears to be correct, since you are using .env and your config object looks similar to the example on the tmi.js quick start.

You did preceed your password with oauth:

Are you getting errors?

const client = new tmi.Client({
	options: { debug: true, messagesLogLevel: "info" },
	connection: {
		reconnect: true,
		secure: true
	},
	identity: {
		username: 'bot-name',
		password: 'oauth:my-bot-token'
	},
	channels: [ 'my-channel' ]
});

Is the example from tmi.js

With and without “oauth:” preceding my bot token, this is the output:

App Started
[15:58] info: Connecting to irc-ws.chat.twitch.tv on port 443…
[15:58] info: Sending authentication to server…
[15:58] error: Login unsuccessful

[Done] exited with code=0 in 1.408 seconds

Then that would success the generated user access token is not valid

That is peculiar. Just clarifying, I get the oauth from the redirect, right?

http://localhost/#access_token=[access token]&scope=channel_editor&token_type=bearer

You need a few more scopes than that to login to chat.

chat:edit and chat:read

to write and read from chat

channel_editor is useless on it’s own

I have a feeling that I might just have to use a real account dedicated for the bot. Thank you for all the help.

After doing this again, results:

Link:
http://localhost/?code={code}&scope=chat%3Aread+chat%3Aedit+channel_editor

Code (config):

const config = {
    options: {
        debug: true
    },
    connection: {
        cluster: "aws",
        reconnect: true
    },
    identity: {
        username: process.env.CLIENT_ID,
        password: "oauth:"+process.env.ACCESS_TOKEN
    },
    channels: [channel]
}

Output:

App Started

[16:50] info: Connecting to irc-ws.chat.twitch.tv on port 443..

[16:50] info: Sending authentication to server..

[16:50] error: Login authentication failed

[Done] exited with code=0 in 1.16 seconds

Are you completing the OAuth process? Once you’ve got the code from the querystring of your callback URL you need to exchange that for an Access Token and Refresh Token.


I did not know that lol. It is probably the reason for it not working

https://dev.twitch.tv/docs/authentication/getting-tokens-oauth#oauth-authorization-code-flow Steps 3 and 4 explain what to do with the code and how to exchange it for OAuth tokens.

Looks like you’re using your app’s client id as username here instead of the bot’s name.

Yeah it has to be a real account

here you generated an implicit oAuth token

Here you generate a regular oAuth token but only got to step 2

As Dist pointed out, you need to take the code and exchange it for a token

And yeah you tried to use your ClientID as the username instead of using the username for the user you generate the token for.