update sfx code to match new discord API

This commit is contained in:
greenham
2020-05-10 17:25:06 -07:00
parent fb7aaa5c84
commit 34b86fff09

View File

@@ -14,7 +14,6 @@ function init(config) {
// Set up SFX // Set up SFX
const sfxFilePath = path.join(__dirname, "sfx"); const sfxFilePath = path.join(__dirname, "sfx");
let playing = false;
// Read in sfx directory, filenames are the commands // Read in sfx directory, filenames are the commands
let sfxList = readSfxDirectory(sfxFilePath); let sfxList = readSfxDirectory(sfxFilePath);
@@ -47,7 +46,7 @@ function init(config) {
// Set up the native commands to handle // Set up the native commands to handle
const commands = { const commands = {
sfx: (msg, guildConfig) => { sfx: async (msg, guildConfig) => {
let allowedSfxChannels = new RegExp(guildConfig.allowedSfxChannels); let allowedSfxChannels = new RegExp(guildConfig.allowedSfxChannels);
if (!allowedSfxChannels.test(msg.channel.name)) return; if (!allowedSfxChannels.test(msg.channel.name)) return;
let sfx = msg.content.split(" ")[1]; let sfx = msg.content.split(" ")[1];
@@ -73,9 +72,6 @@ function init(config) {
return true; return true;
} }
if (playing === true)
return msg.channel.send("Already playing, please wait.");
// make sure this file exists either as an mp3 or wav // make sure this file exists either as an mp3 or wav
let sfxPath; let sfxPath;
if (fs.existsSync(path.join(sfxFilePath, sfx + ".mp3"))) { if (fs.existsSync(path.join(sfxFilePath, sfx + ".mp3"))) {
@@ -86,23 +82,23 @@ function init(config) {
return msg.reply("This sound effect does not exist!"); return msg.reply("This sound effect does not exist!");
} }
if (!msg.guild.voiceConnection) // Join the same voice channel of the author of the message
return joinVoiceChannel(msg).then(() => commands.sfx(msg, guildConfig)); const connection = await joinVoiceChannel(msg);
if (connection === false) {
return msg.reply("I couldn't connect to your voice channel...");
}
playing = true;
(function play(sfxFile) { (function play(sfxFile) {
const dispatcher = msg.guild.voiceConnection.playFile(sfxFile, { const dispatcher = connection.play(sfxFile, {
volume: guildConfig.sfxVolume, volume: guildConfig.sfxVolume,
passes: guildConfig.passes passes: guildConfig.passes
}); });
dispatcher dispatcher
.on("end", (reason) => { .on("finish", (reason) => {
playing = false; connection.disconnect();
msg.guild.voiceConnection.disconnect();
}) })
.on("error", (error) => { .on("error", (error) => {
playing = false; connection.disconnect();
msg.guild.voiceConnection.disconnect();
console.error("Error playing sfx: " + error); console.error("Error playing sfx: " + error);
}) })
.on("start", () => {}); .on("start", () => {});
@@ -233,6 +229,9 @@ function init(config) {
.on("ready", () => { .on("ready", () => {
console.log(`${config.botName} is connected and ready`); console.log(`${config.botName} is connected and ready`);
client.setRandomActivity(); client.setRandomActivity();
setInterval(() => {
client.setRandomActivity();
}, 3600 * 1000);
}) })
// Listen for commands for the bot to respond to across all channels // Listen for commands for the bot to respond to across all channels
.on("message", (msg) => { .on("message", (msg) => {
@@ -347,16 +346,13 @@ function readSfxDirectory(path) {
return sfxList; return sfxList;
} }
function joinVoiceChannel(msg) { async function joinVoiceChannel(message) {
return new Promise((resolve, reject) => { // Join the same voice channel of the author of the message
const voiceChannel = msg.member.voiceChannel; if (message.member.voice.channel) {
if (!voiceChannel || voiceChannel.type !== "voice") return await message.member.voice.channel.join();
return msg.reply("I couldn't connect to your voice channel..."); } else {
voiceChannel return false;
.join() }
.then((connection) => resolve(connection))
.catch((err) => reject(err));
});
} }
// Read/parse text lines from a file // Read/parse text lines from a file