chipmunkbot3/commands/music.js

94 lines
2.5 KiB
JavaScript
Raw Normal View History

2024-04-02 17:53:10 -04:00
const { literal, argument, DynamicCommandExceptionType } = require('brigadier-commands')
const { location, path, isUrl } = require('../util/command/argument/location')
const TextMessage = require('../util/command/text_message')
const fs = require('fs')
2024-02-11 21:23:41 -05:00
2024-04-02 17:53:10 -04:00
const FILE_DOES_NOT_EXIST_ERROR = new DynamicCommandExceptionType(filename => new TextMessage(['File ', filename, ' does not exist']))
2024-02-11 21:23:41 -05:00
2024-04-02 17:53:10 -04:00
module.exports = {
2024-04-05 20:54:10 -04:00
register (dispatcher, { bot }) {
2024-04-02 17:53:10 -04:00
const node = dispatcher.register(
literal('music')
.then(
literal('play')
.then(
2024-04-05 20:54:10 -04:00
argument('location', location(bot.paths.music))
2024-04-02 17:53:10 -04:00
.executes(this.playCommand)
)
)
.then(
literal('skip')
.executes(this.skipCommand)
)
.then(
literal('stop')
.executes(this.stopCommand)
)
.then(
literal('loop')
.executes(this.loopCommand)
)
.then(
literal('list')
.executes(this.listCommand.bind(this))
.then(
2024-04-05 20:54:10 -04:00
argument('location', path(bot.paths.music))
2024-04-02 17:53:10 -04:00
.executes(this.locationListCommand.bind(this))
)
)
)
node.description = 'Plays songs using note block sounds'
node.permissionLevel = 0
},
playCommand (context) {
const source = context.source
const bot = source.bot
2024-02-11 21:23:41 -05:00
2024-04-02 17:53:10 -04:00
const filepath = context.getArgument('location')
2024-02-11 21:23:41 -05:00
2024-04-02 17:53:10 -04:00
if (!isUrl(filepath) && !fs.existsSync(filepath)) throw FILE_DOES_NOT_EXIST_ERROR.create(filepath)
2024-02-11 21:23:41 -05:00
2024-04-05 20:54:10 -04:00
if (!bot.music.queue.length) bot.music.play(filepath)
else bot.music.queue.push(filepath)
2024-04-02 17:53:10 -04:00
source.sendFeedback([
{ text: 'Added ', ...bot.styles.primary },
{ text: filepath.replace(/.+\//g, ''), ...bot.styles.secondary },
2024-04-05 20:54:10 -04:00
' to the music queue'
2024-04-02 17:53:10 -04:00
], false)
},
2024-02-11 21:23:41 -05:00
2024-04-02 17:53:10 -04:00
skipCommand (context) {
2024-04-05 20:54:10 -04:00
const bot = context.source.bot
2024-04-02 17:53:10 -04:00
bot.music.skip()
},
2024-02-11 21:23:41 -05:00
2024-04-02 17:53:10 -04:00
stopCommand (context) {
2024-04-05 20:54:10 -04:00
const bot = context.source.bot
2024-04-02 17:53:10 -04:00
bot.music.stop()
},
2024-02-11 21:23:41 -05:00
2024-04-02 17:53:10 -04:00
loopCommand (context) {
2024-04-05 20:54:10 -04:00
const bot = context.source.bot
2024-04-02 17:53:10 -04:00
bot.music.looping = !bot.music.looping
},
2024-02-11 21:23:41 -05:00
async listCommand (context) {
await this.listSongs(context, context.source.bot.paths.music)
2024-04-02 17:53:10 -04:00
},
2024-02-11 21:23:41 -05:00
async locationListCommand (context) {
await this.listSongs(context, context.getArgument('location'))
2024-04-02 17:53:10 -04:00
},
async listSongs (context, path) {
const source = context.source
const bot = source.bot
const list = await bot.listFiles(path)
source.sendFeedback(['', { text: 'Songs - ', ...bot.styles.primary }, ...list], false)
2024-02-11 21:23:41 -05:00
}
}