I am currently working with node.js to create an IRC bot. The bot allows users to add links to links in the database. Each time someone submits a song, it is added to the new line "shuffle.txt" as such:
user1,The Beatles,Yesterday,(youtube link) user2,The Rolling Stones,Angie,(youtube link) user1,The Bealtes,Yellow Sumbarine,(youtube link)
Please note that user1 did not accept any information in his last addition. I am trying to make a UNDO command so that the user can delete their last entered line. I plan to do this by finding the last occurrence of my name in shuffle.txt and deleting the entire string found. Here is my message listener:
bot.addListener('message', function(from, to, message) { if (message.indexOf(config.prefix) == 0) { message = message.slice(1); var token = message.split(" "); if (token[0] == 'undo') {
the user entering the command is saved as from
I assume that I need to do something according to this:
var songList = fs.readFileSync('shuffle.txt', 'utf8'); var position = songList.indexOf(from); if (position != -1) { //if 'from' found //find LAST occurrence of 'from' //get length from here to next occurrence of '\n' //substr(length + 1) fs.writeFile('shuffle.txt', songList, function(err) { if (err) { console.log (err); } }
I am new to JavaScript and this is my first time using node.js so I can use any help I can get! Thanks to everyone.
EDIT: I should also point out that I do not need help in recognizing commands. I need help only with the search / delete part. Hooray!