music improvement

This commit is contained in:
ChomeNS 2023-02-28 14:17:49 +07:00
parent 4bf9a0a0ed
commit 33f03675fb
3 changed files with 27 additions and 11 deletions

5
bot.js
View file

@ -60,7 +60,10 @@ async function createBot (server, config, getBots, setNewBot, dcclient, rl) {
channel.send(
`Connecting to: \`${bot.server.host}:${bot.server.port}\``
)
bot._client.on('login', async function (data) {
bot._client.on('login', (data) => bot.emit('login', data))
bot.on('login', async function (data) {
bot.entityId = data.entityId
bot.uuid = bot._client.uuid
bot.username = bot._client.username

View file

@ -33,8 +33,7 @@ async function play (bot, values, discord, channeldc, selector, config) {
else absolutePath = await resolve(filepath)
song = await bot.music.load(await fs.readFile(absolutePath), path.basename(absolutePath))
bot.music.queue.push(song)
bot.music.play(song)
if (discord) {
const Embed = new EmbedBuilder()
.setColor(config.discord.embedsColors.normal)
@ -44,8 +43,11 @@ async function play (bot, values, discord, channeldc, selector, config) {
} else {
bot.tellraw(selector, [{ text: 'Added ', color: 'white' }, { text: song.name, color: 'gold' }, { text: ' to the song queue', color: 'white' }])
}
bot.music.queue.push(song)
bot.music.play(song)
} catch (e) {
console.log(e)
bot.console.error(e.stack)
if (discord) {
const Embed = new EmbedBuilder()
.setColor(config.discord.embedsColors.error)
@ -68,9 +70,7 @@ async function playUrl (bot, values, discord, channeldc, selector, config) {
},
responseType: 'arraybuffer'
})
song = await bot.music.load(response.data, getFilenameFromUrl(url))
bot.music.queue.push(song)
bot.music.play(song)
if (discord) {
const Embed = new EmbedBuilder()
.setColor(config.discord.embedsColors.normal)
@ -80,6 +80,10 @@ async function playUrl (bot, values, discord, channeldc, selector, config) {
} else {
bot.tellraw(selector, [{ text: 'Added ', color: 'white' }, { text: song.name, color: 'gold' }, { text: ' to the song queue', color: 'white' }])
}
song = await bot.music.load(response.data, getFilenameFromUrl(url))
bot.music.queue.push(song)
bot.music.play(song)
} catch (_err) {
const e = _err.toString().includes('Bad MIDI file. Expected \'MHdr\', got: ') ? response.data.toString() : _err
if (discord) {
@ -226,7 +230,7 @@ module.exports = {
case 'all':
bot.music.loop = 2
bot.tellraw(selector, {
text: 'Now looping every song in the queue'
text: 'Now looping every song'
})
break
default:
@ -321,7 +325,7 @@ module.exports = {
Embed = new EmbedBuilder()
.setColor(config.discord.embedsColors.normal)
.setTitle('Loop')
.setDescription('Now looping every song in the queue')
.setDescription('Now looping every song')
channeldc.send({ embeds: [Embed] })
break
}

View file

@ -67,6 +67,16 @@ function inject (bot) {
bot.core.run(`minecraft:execute as ${selector} at @s run playsound ${soundNames[note.instrument]} record @s ~ ~ ~ ${note.volume} ${floatingPitch}`)
noteIndex++
if (noteIndex >= bot.music.song.notes.length) {
bot.tellraw('@a', [
{
text: 'Finished playing '
},
{
text: bot.music.song.name,
color: 'gold'
}
])
if (bot.music.loop === 1) {
resetTime()
return
@ -79,7 +89,6 @@ function inject (bot) {
}
bot.music.queue.shift()
if (!bot.music.queue[0]) {
if (bot.music.queue.length === 0) bot.tellraw('@a', { text: 'Finished playing every song in the queue' })
bot.music.stop()
return
}
@ -119,7 +128,7 @@ function inject (bot) {
return song
}
bot.music.play = function () {
bot.music.play = function (song) {
if (bot.music.queue.length === 1) resetTime()
}