From 390852f890e659db68920ae4d9a814fda641ae65 Mon Sep 17 00:00:00 2001 From: ChomeNS Date: Mon, 31 Oct 2022 16:46:23 +0700 Subject: [PATCH] fix music loop and use sleep-promise in index.js bro this REALLY TOOK AGES --- commands/music.js | 39 +++++++++++++++++++++++++++++---------- index.js | 8 ++------ plugins/music.js | 11 ++++++----- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/commands/music.js b/commands/music.js index 92d29d1..1bca7fa 100644 --- a/commands/music.js +++ b/commands/music.js @@ -20,6 +20,7 @@ async function play(bot, values, discord, channeldc) { const filepath = values.join(' '); const absolutePath = await resolve(filepath); song = bot.music.load(await fs.readFile(absolutePath), path.basename(absolutePath)); + bot.music.queue.push(song); bot.music.play(song); if (discord) { const Embed = new MessageEmbed() @@ -53,6 +54,7 @@ async function playUrl(bot, values, discord, channeldc) { responseType: 'arraybuffer', }); song = bot.music.load(response.data, url); + bot.music.queue.push(song); bot.music.play(song); if (discord) { const Embed = new MessageEmbed() @@ -140,7 +142,7 @@ module.exports = { } if (args[0]==='stop') { try { - bot.core.run('minecraft:tellraw @a ' + JSON.stringify([{text: 'Stopped playing '}, {text: bot.music.song.name, color: 'gold'}])); + bot.core.run('minecraft:tellraw @a ' + JSON.stringify({text: 'Cleared the song queue'})); } catch (e) { return; } @@ -157,7 +159,7 @@ module.exports = { } if (args[1] === 'all') { bot.music.loop = 2; - bot.core.run('minecraft:tellraw @a ' + JSON.stringify({text: 'Now Looping every song in the queue'})); + bot.core.run('minecraft:tellraw @a ' + JSON.stringify({text: 'Now looping every song in the queue'})); } } if (args[0]==='list') { @@ -179,7 +181,7 @@ module.exports = { const Embed = new MessageEmbed() .setColor('#FFFF00') .setTitle('Stop') - .setDescription(`Stopped playing ${bot.music.song.name}`); + .setDescription('Cleared the song queue'); channeldc.send({embeds: [Embed]}); } catch (e) { return; @@ -187,13 +189,30 @@ module.exports = { bot.music.stop(); } if (args[0]==='loop') { - bot.music.loop = !bot.music.loop; - const loop = bot.music.loop ? 'enabled' : 'disabled'; - const Embed = new MessageEmbed() - .setColor('#FFFF00') - .setTitle('Loop') - .setDescription(`Looping is now ${loop}`); - channeldc.send({embeds: [Embed]}); + if (args[1] === 'off') { + bot.music.loop = 0; + const Embed = new MessageEmbed() + .setColor('#FFFF00') + .setTitle('Loop') + .setDescription(`Looping is now disabled`); + channeldc.send({embeds: [Embed]}); + } + if (args[1] === 'current') { + bot.music.loop = 1; + const Embed = new MessageEmbed() + .setColor('#FFFF00') + .setTitle('Loop') + .setDescription(`Now looping ${song.name}`); + channeldc.send({embeds: [Embed]}); + } + if (args[1] === 'all') { + bot.music.loop = 2; + const Embed = new MessageEmbed() + .setColor('#FFFF00') + .setTitle('Loop') + .setDescription('Now looping every song in the queue'); + channeldc.send({embeds: [Embed]}); + } } if (args[0]==='list') { list(bot, true, channeldc); diff --git a/index.js b/index.js index 495b89f..467a439 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ const mc = require('minecraft-protocol'); const config = require('./config'); const crypto = require('crypto'); const colorConvert = require('color-convert'); +const sleep = require('sleep-promise'); const {containsIllegalCharacters} = require('./util/containsIllegalCharacters'); const generateEaglerUsername = require('./util/generateEaglerUsername'); const {EventEmitter} = require('events'); @@ -32,10 +33,6 @@ console.log = function() { rl._refreshLine(); }; -function sleep(ms) { - return new Promise((resolve) => setTimeout(resolve, ms)); -} - // Load discord.js const { Client, @@ -154,14 +151,13 @@ function main() { bot.hash = ''; - sleep = (time) => new Promise((a)=>setTimeout(a, time)), bot.chat = (message) => { bot.queue.push(String(message)); }; discordQueue = setInterval(function() { if (dcmsg.queue!='') { - channel.send('```ansi\n' + dcmsg.queue.substring(0, 1986) + '\n```'); + channel.send({content: '```ansi\n' + dcmsg.queue.substring(0, 1986) + '\n```', allowedMentions: {parse: []}}); dcmsg.queue = ''; } }, 1000); diff --git a/plugins/music.js b/plugins/music.js index 4435dfa..e6ecdd5 100644 --- a/plugins/music.js +++ b/plugins/music.js @@ -48,14 +48,16 @@ function inject(bot) { } if (bot.music.loop === 2) { resetTime(); - await sleep(500); + await sleep(100); + bot.music.queue.push(song); bot.music.play(bot.music.queue.shift()); return; } bot.music.queue.shift(); - if (bot.music.queue[0]) { - resetTime(); - await sleep(500); + if (!bot.music.queue[0]) return; + if (bot.music.queue[0].notes.length > 0) { + if (bot.music.queue.length !== 1) resetTime(); + await sleep(100); bot.music.play(bot.music.queue[0]); return; } @@ -81,7 +83,6 @@ function inject(bot) { }; bot.music.play = function(song) { - bot.music.queue.push(song); loop = song.loop; if (bot.music.queue.length === 1) resetTime(); };