added ability to reset playlists

- only owners can run it 
- iterates over all playlists in foler /playlists (gitignored) 
- shuffles mpc playlist and then plays
This commit is contained in:
notnull 2019-12-22 02:04:03 -05:00
parent 1418299ff0
commit 8f5510993c
4 changed files with 85 additions and 4 deletions

View File

@ -9,6 +9,7 @@
}
},
"env": {
"node": true
"node": true,
"es6": true
}
}

View File

@ -4,7 +4,8 @@
"description": "a bot",
"main": "index.js",
"scripts": {
"start": "node ./src"
"start": "node ./src",
"dev": "nodemon ./src"
},
"author": "notnull",
"license": "ISC",

View File

@ -1,7 +1,12 @@
const IRC = require('irc-framework')
const bot = new IRC.Client()
const { getPlaylist, getCurrentTrack, skipTrack } = require('./mpc-commands')
const {
getPlaylist,
getCurrentTrack,
skipTrack,
reset,
} = require('./mpc-commands')
const { searchTrack, requestTrack } = require('./spotify')
const chalk = require('chalk')
@ -26,6 +31,7 @@ bot.on('socket close', () => console.log('bot has disconnected.'))
bot.matchMessage(/^!hello/, event => event.reply('Hi!'))
/** ADMIN COMMANDS **/
bot.matchMessage(/^!reset/, event => handleReset(event))
bot.matchMessage(/^!join/, event => handleJoin(event))
bot.matchMessage(/^!part/, event => handlePart(event))
bot.matchMessage(/^!quit/, event => handleQuit(event))
@ -53,6 +59,10 @@ const handleQuit = event => {
bot.quit(["You'll cowards, don't even smoke crack."])
}
const handleReset = event => {
if (!owners.includes(event.nick)) return
reset(event)
}
const sendPlaylist = event => {
const { error, playlist } = getPlaylist()
if (error) return event.reply('Something went wrong.')

View File

@ -1,4 +1,5 @@
const { spawnSync } = require('child_process')
const fs = require('fs')
const getPlaylist = () => {
const { stderr, stdout } = spawnSync('mpc', ['playlist'])
@ -17,8 +18,76 @@ const skipTrack = () => {
}
const insertTrack = spotifyURI => {
console.log('inserting track:', JSON.stringify(spotifyURI))
const { stderr, stdout } = spawnSync('mpc', ['insert', spotifyURI])
console.log(stderr.toString(), stdout.toString())
return { error: stderr.toString(), insert: stdout.toString() }
}
module.exports = { getPlaylist, getCurrentTrack, skipTrack, insertTrack }
const insertTrackAsync = spotifyURI => {
console.log('inserting track:', spotifyURI)
const promise = new Promise((resolve, reject) => {
const { stdout, error } = spawnSync('mpc', ['insert', spotifyURI])
if (error) reject(error.message)
resolve({ track: stdout.toString() })
})
return promise
}
const reset = event => {
clearAllPlaylists(event)
addAllPlaylists(event)
}
const clearAllPlaylists = event => {
const { error } = spawnSync('mpc', ['clear'])
if (error) return handleError(error, event)
}
const addAllPlaylists = async event => {
const files = fs.readdirSync('./playlists')
const tracks = readPlaylists(files)
try {
await Promise.all(
tracks.map(t => insertTrackAsync(t.split(' # ')[0]))
).then(() => {
shuffleAllPlaylists()
play()
})
} catch (e) {
return handleError(e, event)
}
}
const readPlaylists = files =>
files
.map(f =>
fs
.readFileSync('./playlists/' + f)
.toString()
.split('\n')
)
.flat()
const handleError = (err, event) => {
console.log(err)
return event.reply('Something went wrong.')
}
const shuffleAllPlaylists = event => {
const { error } = spawnSync('mpc', ['shuffle'])
if (error) return handleError(error, event)
}
const play = event => {
const { error } = spawnSync('mpc', ['play'])
if (error) return handleError(error, event)
}
module.exports = {
getPlaylist,
getCurrentTrack,
skipTrack,
insertTrack,
reset,
}