Skip to content
This repository has been archived by the owner on Sep 28, 2021. It is now read-only.

Feature/scare queue #46

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion commands/Sound/playsound.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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?')
Expand Down
25 changes: 23 additions & 2 deletions events/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@ const { prefix, colors } = require('../config.json')

const reactMessage = require('../utils/reactMessage')

cmdQueue = []
handlingCommand = false

var waitToRunCommand = function() {
setTimeout(runCommand, 300)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there a timeout in the queue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was an error where the bot would leave the voice channel and then reenter it immediately, which would cause the command to stop running and the bot got stuck in the voice channel, so I buffered it with that.

}

var runCommand = function() {
handlingCommand = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this not be handled by Promises / Observables / Callbacks to prevent having to manage a flag?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It probably could, I'm not super familiar with how to do that. You're welcome to make changes if you'd like. Or you could try to walk me through that

Copy link
Contributor

@tmttn tmttn Oct 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started a PR to your feature branch in your fork.

const cmdFunc = cmdQueue.shift()
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
Expand Down Expand Up @@ -40,10 +58,13 @@ 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})
if (!handlingCommand) {
waitToRunCommand()
}
} else {
// -------------------- Reaction system --------------------
// console.log(reactMessage)
reactMessage(message.guild.id, message)
}
}