fix music loop and use sleep-promise in index.js

bro this REALLY TOOK AGES
This commit is contained in:
ChomeNS 2022-10-31 16:46:23 +07:00
parent 26bc7f8fd8
commit 390852f890
3 changed files with 37 additions and 21 deletions

View file

@ -20,6 +20,7 @@ async function play(bot, values, discord, channeldc) {
const filepath = values.join(' '); const filepath = values.join(' ');
const absolutePath = await resolve(filepath); const absolutePath = await resolve(filepath);
song = bot.music.load(await fs.readFile(absolutePath), path.basename(absolutePath)); song = bot.music.load(await fs.readFile(absolutePath), path.basename(absolutePath));
bot.music.queue.push(song);
bot.music.play(song); bot.music.play(song);
if (discord) { if (discord) {
const Embed = new MessageEmbed() const Embed = new MessageEmbed()
@ -53,6 +54,7 @@ async function playUrl(bot, values, discord, channeldc) {
responseType: 'arraybuffer', responseType: 'arraybuffer',
}); });
song = bot.music.load(response.data, url); song = bot.music.load(response.data, url);
bot.music.queue.push(song);
bot.music.play(song); bot.music.play(song);
if (discord) { if (discord) {
const Embed = new MessageEmbed() const Embed = new MessageEmbed()
@ -140,7 +142,7 @@ module.exports = {
} }
if (args[0]==='stop') { if (args[0]==='stop') {
try { 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) { } catch (e) {
return; return;
} }
@ -157,7 +159,7 @@ module.exports = {
} }
if (args[1] === 'all') { if (args[1] === 'all') {
bot.music.loop = 2; 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') { if (args[0]==='list') {
@ -179,7 +181,7 @@ module.exports = {
const Embed = new MessageEmbed() const Embed = new MessageEmbed()
.setColor('#FFFF00') .setColor('#FFFF00')
.setTitle('Stop') .setTitle('Stop')
.setDescription(`Stopped playing ${bot.music.song.name}`); .setDescription('Cleared the song queue');
channeldc.send({embeds: [Embed]}); channeldc.send({embeds: [Embed]});
} catch (e) { } catch (e) {
return; return;
@ -187,13 +189,30 @@ module.exports = {
bot.music.stop(); bot.music.stop();
} }
if (args[0]==='loop') { if (args[0]==='loop') {
bot.music.loop = !bot.music.loop; if (args[1] === 'off') {
const loop = bot.music.loop ? 'enabled' : 'disabled'; bot.music.loop = 0;
const Embed = new MessageEmbed() const Embed = new MessageEmbed()
.setColor('#FFFF00') .setColor('#FFFF00')
.setTitle('Loop') .setTitle('Loop')
.setDescription(`Looping is now ${loop}`); .setDescription(`Looping is now disabled`);
channeldc.send({embeds: [Embed]}); 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') { if (args[0]==='list') {
list(bot, true, channeldc); list(bot, true, channeldc);

View file

@ -7,6 +7,7 @@ const mc = require('minecraft-protocol');
const config = require('./config'); const config = require('./config');
const crypto = require('crypto'); const crypto = require('crypto');
const colorConvert = require('color-convert'); const colorConvert = require('color-convert');
const sleep = require('sleep-promise');
const {containsIllegalCharacters} = require('./util/containsIllegalCharacters'); const {containsIllegalCharacters} = require('./util/containsIllegalCharacters');
const generateEaglerUsername = require('./util/generateEaglerUsername'); const generateEaglerUsername = require('./util/generateEaglerUsername');
const {EventEmitter} = require('events'); const {EventEmitter} = require('events');
@ -32,10 +33,6 @@ console.log = function() {
rl._refreshLine(); rl._refreshLine();
}; };
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
// Load discord.js // Load discord.js
const { const {
Client, Client,
@ -154,14 +151,13 @@ function main() {
bot.hash = ''; bot.hash = '';
sleep = (time) => new Promise((a)=>setTimeout(a, time)),
bot.chat = (message) => { bot.chat = (message) => {
bot.queue.push(String(message)); bot.queue.push(String(message));
}; };
discordQueue = setInterval(function() { discordQueue = setInterval(function() {
if (dcmsg.queue!='') { 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 = ''; dcmsg.queue = '';
} }
}, 1000); }, 1000);

View file

@ -48,14 +48,16 @@ function inject(bot) {
} }
if (bot.music.loop === 2) { if (bot.music.loop === 2) {
resetTime(); resetTime();
await sleep(500); await sleep(100);
bot.music.queue.push(song);
bot.music.play(bot.music.queue.shift()); bot.music.play(bot.music.queue.shift());
return; return;
} }
bot.music.queue.shift(); bot.music.queue.shift();
if (bot.music.queue[0]) { if (!bot.music.queue[0]) return;
resetTime(); if (bot.music.queue[0].notes.length > 0) {
await sleep(500); if (bot.music.queue.length !== 1) resetTime();
await sleep(100);
bot.music.play(bot.music.queue[0]); bot.music.play(bot.music.queue[0]);
return; return;
} }
@ -81,7 +83,6 @@ function inject(bot) {
}; };
bot.music.play = function(song) { bot.music.play = function(song) {
bot.music.queue.push(song);
loop = song.loop; loop = song.loop;
if (bot.music.queue.length === 1) resetTime(); if (bot.music.queue.length === 1) resetTime();
}; };