From 7255aed2c1f52ffbcaed64e60cd5672504893274 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 4 Oct 2020 19:15:34 -0600 Subject: [PATCH 1/4] working on queue --- events/message.js | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/events/message.js b/events/message.js index c8eef78..9a41121 100644 --- a/events/message.js +++ b/events/message.js @@ -3,6 +3,15 @@ const { prefix, colors } = require('../config.json') const reactMessage = require('../utils/reactMessage') +var wrapFunction = function(fn, context, params) { + return function() { + fn.apply(context, params); + }; +} + +cmdQueue = [] +handlingCommand = false + module.exports = async (bot, webhook, message) => { if (message.author.bot) return // Ignore all bots if (message.author.id === bot.user.id) return // Ignore bot itself @@ -14,7 +23,6 @@ module.exports = async (bot, webhook, message) => { // Our standard argument/command name definition. const args = message.content.slice(prefix.length).trim().split(/ +/g) const command = args.shift().toLowerCase() - // Grab the command data from the client Collection const cmd = bot.commands.get(command) || bot.commands.get(bot.aliases.get(command)) @@ -30,7 +38,6 @@ module.exports = async (bot, webhook, message) => { webhook.send(privateEmbed) // -------------------- Command execution -------------------- - message.delete() if (!message.member.hasPermission(cmd.config.permissionNeeded)) { message.reply('Only your server administrator can do this!').then((m) => { @@ -40,10 +47,24 @@ module.exports = async (bot, webhook, message) => { }) return } - cmd.run(bot, message, args) // Run the command + // ----- Push to queue and wait for other jobs to finish ----- + cmdQueue.push({cmd: cmd, bot: bot, message: message, args: args}) + console.log(handlingCommand) + console.log(cmdQueue.length) + // while (handlingCommand) { + // setTimeout(() => {}, 2000); + // } + handlingCommand = true + console.log('running commands') + if (cmdQueue.length > 0) { + const cmdFunc = cmdQueue.shift() + cmdFunc.cmd.run(cmdFunc.bot, cmdFunc.message, cmdFunc.args) + console.log('after command') + } + + // handlingCommand = false } else { // -------------------- Reaction system -------------------- - // console.log(reactMessage) reactMessage(message.guild.id, message) } } From d444dbdcda040454beaf56dcee634d1826e373f0 Mon Sep 17 00:00:00 2001 From: amerrill Date: Sun, 4 Oct 2020 20:35:26 -0600 Subject: [PATCH 2/4] queue system working with each thread waiting until it can go --- commands/Sound/playsound.js | 3 ++- events/message.js | 39 ++++++++++++++++++------------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/commands/Sound/playsound.js b/commands/Sound/playsound.js index a2d40ed..a78417a 100644 --- a/commands/Sound/playsound.js +++ b/commands/Sound/playsound.js @@ -12,7 +12,7 @@ module.exports = { displayHelp: false, }, - run: async (bot, message, args) => { + run: async (bot, message, args, callback) => { if (message.member.voice.channel) { let soundPath = __dirname + `/../../sounds/` const connection = await message.member.voice.channel.join() @@ -25,6 +25,7 @@ module.exports = { dispatcher.on('finish', () => { connection.voice.channel.leave() + callback() }) } else { message.reply('How can I scare your companions without you beeing present in the voice channel?') diff --git a/events/message.js b/events/message.js index 9a41121..4b60a4c 100644 --- a/events/message.js +++ b/events/message.js @@ -3,15 +3,27 @@ const { prefix, colors } = require('../config.json') const reactMessage = require('../utils/reactMessage') -var wrapFunction = function(fn, context, params) { - return function() { - fn.apply(context, params); - }; -} - cmdQueue = [] handlingCommand = false +var done = function() { + handlingCommand = false +} + +var waitToRunCommand = function() { + if (handlingCommand) { + setTimeout(waitToRunCommand, 300) + } else { + setTimeout(runCommand, 300) + } +} + +var runCommand = function() { + handlingCommand = true + const cmdFunc = cmdQueue.shift() + cmdFunc.cmd.run(cmdFunc.bot, cmdFunc.message, cmdFunc.args, done) +} + module.exports = async (bot, webhook, message) => { if (message.author.bot) return // Ignore all bots if (message.author.id === bot.user.id) return // Ignore bot itself @@ -49,20 +61,7 @@ module.exports = async (bot, webhook, message) => { } // ----- Push to queue and wait for other jobs to finish ----- cmdQueue.push({cmd: cmd, bot: bot, message: message, args: args}) - console.log(handlingCommand) - console.log(cmdQueue.length) - // while (handlingCommand) { - // setTimeout(() => {}, 2000); - // } - handlingCommand = true - console.log('running commands') - if (cmdQueue.length > 0) { - const cmdFunc = cmdQueue.shift() - cmdFunc.cmd.run(cmdFunc.bot, cmdFunc.message, cmdFunc.args) - console.log('after command') - } - - // handlingCommand = false + waitToRunCommand() } else { // -------------------- Reaction system -------------------- reactMessage(message.guild.id, message) From 15a748d92559a2de818d128bc79f05c9664beb27 Mon Sep 17 00:00:00 2001 From: amerrill Date: Sun, 4 Oct 2020 21:14:07 -0600 Subject: [PATCH 3/4] fixed issue when queue was larger than 2 --- events/message.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/events/message.js b/events/message.js index 4b60a4c..8b9ee8e 100644 --- a/events/message.js +++ b/events/message.js @@ -6,16 +6,8 @@ const reactMessage = require('../utils/reactMessage') cmdQueue = [] handlingCommand = false -var done = function() { - handlingCommand = false -} - var waitToRunCommand = function() { - if (handlingCommand) { - setTimeout(waitToRunCommand, 300) - } else { - setTimeout(runCommand, 300) - } + setTimeout(runCommand, 300) } var runCommand = function() { @@ -24,6 +16,11 @@ var runCommand = function() { cmdFunc.cmd.run(cmdFunc.bot, cmdFunc.message, cmdFunc.args, done) } +var done = function() { + handlingCommand = false + if (cmdQueue.length > 0) waitToRunCommand() +} + module.exports = async (bot, webhook, message) => { if (message.author.bot) return // Ignore all bots if (message.author.id === bot.user.id) return // Ignore bot itself @@ -61,7 +58,9 @@ module.exports = async (bot, webhook, message) => { } // ----- Push to queue and wait for other jobs to finish ----- cmdQueue.push({cmd: cmd, bot: bot, message: message, args: args}) - waitToRunCommand() + if (!handlingCommand) { + waitToRunCommand() + } } else { // -------------------- Reaction system -------------------- reactMessage(message.guild.id, message) From eb4b3d92e6c9f807bedbbb72d6dd8d4f56579969 Mon Sep 17 00:00:00 2001 From: amerrill Date: Sun, 4 Oct 2020 21:16:27 -0600 Subject: [PATCH 4/4] fixed formatting --- events/message.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/events/message.js b/events/message.js index 8b9ee8e..f0496a2 100644 --- a/events/message.js +++ b/events/message.js @@ -32,6 +32,7 @@ module.exports = async (bot, webhook, message) => { // Our standard argument/command name definition. const args = message.content.slice(prefix.length).trim().split(/ +/g) const command = args.shift().toLowerCase() + // Grab the command data from the client Collection const cmd = bot.commands.get(command) || bot.commands.get(bot.aliases.get(command)) @@ -47,6 +48,7 @@ module.exports = async (bot, webhook, message) => { webhook.send(privateEmbed) // -------------------- Command execution -------------------- + message.delete() if (!message.member.hasPermission(cmd.config.permissionNeeded)) { message.reply('Only your server administrator can do this!').then((m) => {