chipmunkbot3/commands/music.js
2024-02-12 21:54:29 -05:00

95 lines
2.6 KiB
JavaScript

const name = 'music'
const description = 'Plays music'
const usages = [
'play <song>',
'list',
'skip',
'stop'
]
const aliases = ['music']
const enabled = true
const permLevel = 0
const fs = require('fs')
const path = require('path')
const https = require('https')
function execute (bot, cmd, player, args, handler) {
const subCmd = args.shift()
let filepath, file, files, primary, msg, split
switch (subCmd) {
case 'play':
filepath = args.join(' ').replace(/\xa7.?/g, '')
/*
if (/https?:\/\//.test(filepath)) {
https.get(filepath, (res) => {
// Open file in local filesystem
tmpobj = tmp.fileSync()
file = fs.createWriteStream(tmpobj.name)
// Write data into local file
res.pipe(file)
// Close the file
file.on('finish', () => {
file.close()
bot.music.queue.push(tmpobj.name)
})
})// .on("error", (err) => {
// console.log("Error: ", err.message);
// });
return
}
*/
filepath = path.join('music', filepath)
// if (!filepath.endsWith('.mid')) { filepath += '.mid' }
split = filepath.split('/')
if (split[0] !== 'music') { throw new Error('geese') }
if (!fs.existsSync(filepath)) { throw new Error('Invalid song name.') }
if (!bot.music.playing) {
bot.music.play(filepath)
} else {
bot.music.queue.push(filepath)
bot.core.run('minecraft:tellraw @a ' + JSON.stringify([
{ text: 'Added ', color: bot.colors.primary },
{ text: filepath.replace(/.+\//g, ''), color: bot.colors.secondary },
' to the music queue.'
]))
}
break
case 'list':
files = fs.readdirSync('./music')
// files.filter((file) => file.endsWith('.mid'))
primary = false
msg = [{ text: 'Songs - ', color: 'gray' }]
files.forEach((file) => {
msg.push({
text: `${file} `,
color: (((primary = !primary)) ? bot.colors.primary : bot.colors.secondary),
clickEvent: { action: 'run_command', value: `${bot.prefix}${name} play ${file}` },
hoverEvent: { action: 'show_text', value: 'Click to play the song' }
})
})
bot.core.run(`/tellraw @a ${JSON.stringify(msg)}`)
break
case 'skip':
bot.music.skip()
break
case 'stop':
bot.music.stop()
break
case 'loop':
bot.music.looping = !bot.music.looping
break
default:
throw new Error('Invalid or missing argument.')
}
}
module.exports = { name, description, usages, aliases, enabled, execute, permLevel }