kritzware/twitch-bot

Display currentsong from text file, how?

Closed this issue ยท 3 comments

Hello all.

First of all, thank you for the bot. I got the bot running and the !test command is working ๐Ÿ‘
Is it possible to display the current song playing with a command? Since i have no programming knowledge i just tried my best and came up with this:

var fs = require('fs');
var myReadStream = fs.createReadStream(__dirname + '/trackinfo.txt', 'utf8');

myReadStream.on('data', function(chunk){
	
	console.log(chunk);
});

^ No problem here, it shows the current song playing in the console.

After that i tried combine the !test command with my code but that didn't work, hehe.

var fs = require('fs');
var myReadStream = fs.createReadStream(_dirname + '/trackinfo.txt', 'utf8');

Bot.on('join', () => {

  Bot.on('message', chatter => {
    if(chatter.message === '!currentsong'){ 
	myReadStream.on('data', function(chunk){
	
	console.log(chunk);
});

It keeps popping out syntax errors... Could you point me out how to fix this and is this even possible to do? Thanks in advance

Hi, I'm glad you like it ๐Ÿ˜„

There are a couple of syntax errors with your provided code, such as missing brackets and the _dirname variable being undefined (it should have two underscores e.g. __dirname). The correct version looks like this:

var myReadStream = fs.createReadStream(__dirname + '/trackinfo.txt', 'utf8')

Bot.on('join', () => {
    Bot.on('message', chatter => {
        if(chatter.message === '!currentsong') { 
	        myReadStream.on('data', chunk => {
                console.log(chunk)
            })
        }
    })
})

I would probably do what you want this way though, making use of the readFileSync method.

const FILEPATH = __dirname + '/trackinfo.txt'

Bot.on('join', () => {

  Bot.on('message', chatter => {
    if(chatter.message === '!test') {
        const track = fs.readFileSync(FILEPATH).toString()
        if(track) {
            Bot.say(`Current track playing is ${track}`)
        }
    }
  })
})

If you're familiar with the concepts of Asynchronous code and Promises in Javascript, it would be better to wrap the file read in a promise function that resolves when the file has been read, e.g.

Bot.on('join', () => {

  Bot.on('message', async chatter => {
    if(chatter.message === '!test') {
        try {
            const track = await getTrack()
            Bot.say(`Current song playing is ${track}`)
        } catch(err) {
            console.log('error retrieving track name!')
        }
    }
  })
})

function getTrack() {
    return new Promise((resolve, reject) => {
        fs.readFile(FILEPATH, (err, data) => {
            if(err) reject(err)
            else resolve(data)
        })
    })
}

Note: I am currently using Node v8.4.0 for these examples

It is working! Thank you so much. Is Codecademy a good website to learn how to code?

Codecademy is a good place to start and learn the basics. The best approach I found for learning to code was by just working on my own projects and googling/reading about stuff I didn't understand, or know how to do ๐Ÿ‘