chipmunkbot3/plugins/music.js

129 lines
3.5 KiB
JavaScript
Raw Normal View History

2024-02-11 21:23:41 -05:00
const fs = require('fs')
const convertMidi = require('../util/convert-midi.js')
const convertNBS = require('../util/nbs-converter.js')
function inject (bot) {
bot.music = {
playing: false,
queue: [],
nowPlaying: undefined,
looping: false,
_interval: null,
_playNextSong,
skip,
stop,
play
}
bot.music.nowPlaying = {
name: '',
tick: {
current: null,
total: null
// npt: null
}
}
setInterval(() => {
if (!bot.music.playing) return
const msg = [
2024-04-02 17:53:10 -04:00
{ text: 'Now Playing', ...bot.styles.primary },
2024-02-11 21:23:41 -05:00
{ text: ' | ', color: 'dark_gray' },
2024-04-02 17:53:10 -04:00
{ text: bot.music.nowPlaying.name, ...bot.styles.secondary, bold: true },
2024-02-11 21:23:41 -05:00
{ text: ' | ', color: 'dark_gray' },
format(bot.music.nowPlaying.time),
{ text: ' / ', color: 'gray' },
format(bot.music.nowPlaying.length)
// { text: ' (', color: 'dark_gray' },
// bot.music.nowPlaying.note.npt + ' npt',
// { text: ')', color: 'dark_gray' }
]
if (bot.music.looping) {
msg.push({ text: ' | ', color: 'dark_gray' })
2024-04-02 17:53:10 -04:00
msg.push({ text: 'Looping', ...bot.styles.secondary })
2024-02-11 21:23:41 -05:00
}
2024-04-05 20:54:10 -04:00
bot.core.run('title @a actionbar ' + JSON.stringify(msg))
2024-02-11 21:23:41 -05:00
}, 500)
function _playNextSong () {
const song = bot.music.queue.shift()
if (song != null) play(song)
}
function skip () {
clearInterval(bot.music._interval)
bot.music.playing = false
if (bot.music.queue.length !== 0) _playNextSong()
}
function stop () {
bot.music.queue = []
clearInterval(bot.music._interval)
bot.music.playing = false
}
function play (filepath) {
// set stuff up
let song
try {
if (filepath.endsWith('.nbs')) {
song = convertNBS(fs.readFileSync(filepath), filepath)
} else song = convertMidi(filepath)
song.time = 0
bot.music.nowPlaying = song
} catch (err) {
2024-04-05 20:54:10 -04:00
bot.tellraw({ text: err.message, ...bot.styles.error }, '@a')
2024-02-11 21:23:41 -05:00
return
}
// play the music lol
2024-04-05 20:54:10 -04:00
bot.tellraw([
2024-04-02 17:53:10 -04:00
{ text: 'Now playing ', ...bot.styles.primary },
2024-04-05 20:54:10 -04:00
{ text: song.name, ...bot.styles.secondary }
], '@a')
2024-02-11 21:23:41 -05:00
bot.music.playing = true
bot.music.looping = song.loop
let startTime = (new Date()).valueOf()
let notes = [ ...song.notes ]
bot.music._interval = setInterval(async () => {
const time = (new Date()).valueOf() - startTime
song.time = time
notes.forEach(async (note, i) => {
if (time >= note.time) {
const floatingpitch = Math.pow(2, (note.pitch - 12) / 12.0)
2024-04-05 20:57:08 -04:00
bot.core.run(`minecraft:execute at @a run playsound block.note_block.${note.instrument} record @p ~ ~ ~ ${note.volume} ${floatingpitch}`)
2024-02-11 21:23:41 -05:00
notes.splice(i, 1)
}
})
song.time++
if (time > song.length) {
if (bot.music.looping) {
notes = [ ...song.notes ]
startTime = (new Date()).valueOf() + song.loopPosition
return
}
clearInterval(bot.music._interval)
bot.music.playing = false
2024-04-05 20:54:10 -04:00
bot.tellraw([
2024-02-11 21:23:41 -05:00
{ text: 'Finished playing ', color: 'green' },
2024-04-05 20:54:10 -04:00
{ text: bot.music.nowPlaying.name, color: 'dark_green' }
], '@a')
2024-02-11 21:23:41 -05:00
if (bot.music.queue.length !== 0) _playNextSong()
}
}, 50)
}
}
function format (ms) {
const s = ms / 1000
const seconds = Math.floor(s / 60).toString()
const minutes = Math.floor(s % 60).toString()
return seconds + ':' + (minutes.length <= 1 ? '0' : '') + minutes
}
2024-02-29 20:39:21 -05:00
module.exports = inject