Skip to content

Commit

Permalink
fix: only play sound if the bot is not busy
Browse files Browse the repository at this point in the history
This closes #23.
  • Loading branch information
sondr3 committed Oct 18, 2020
1 parent cd7cc68 commit b5dc917
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/commands/sound/sound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from "path";
import { soundSamples } from "../../utils";
import logger from "../../utils/logger";
import { ALLOWED_VOICE_CHANNEL } from "../../constants";
import { getState, setPlayingState } from "../../state";

class SoundCommand extends Command {
constructor(client: CommandoClient) {
Expand Down Expand Up @@ -72,6 +73,15 @@ class SoundCommand extends Command {
return await message.say(`You can only annoy others in <#${ALLOWED_VOICE_CHANNEL[0]}>, behave.`);
}

if (getState().playingSound) {
logger.info({
message: "Attempting to play audio multiple times, aborting",
userId: message.author.id,
});

return await message.say(`I can only do so much at a time, please wait for me to finish...`);
}

logger.info({
message: "Sound played and users annoyed",
userId: message.author.id,
Expand All @@ -81,11 +91,15 @@ class SoundCommand extends Command {
const connection = await voiceChannel.join();
const dispatcher = connection.play(fs.createReadStream(path.resolve(process.cwd(), `assets/${file}.mp3`)));
dispatcher.on("error", console.error);
dispatcher.on("start", () => console.log(`${file}.mp3 is now playing`));
dispatcher.on("start", () => {
console.log(`${file}.mp3 is now playing`);
setPlayingState(true);
});
dispatcher.on("finish", () => {
console.log(`${file}.mp3 is done playing`);
dispatcher.destroy();
voiceChannel.leave();
setPlayingState(false);
});

return await message.direct("I hope you're happy now...");
Expand Down

0 comments on commit b5dc917

Please sign in to comment.