clean up and simplify sfx command
This commit is contained in:
85
main.js
85
main.js
@@ -1,23 +1,31 @@
|
|||||||
|
// Import modules
|
||||||
const { Client } = require('discord.js');
|
const { Client } = require('discord.js');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const tokens = require('./tokens.json');
|
const tokens = require('./tokens.json');
|
||||||
|
|
||||||
|
// Set up Discord client
|
||||||
const client = new Client();
|
const client = new Client();
|
||||||
const sfxFilePath = path.join(__dirname, 'sfx');
|
|
||||||
const allowedChannels = /bot|alttp-alerts/;
|
|
||||||
|
|
||||||
let playOptions = {volume: 0.25, passes: tokens.passes};
|
// Set up SFX
|
||||||
|
const sfxFilePath = path.join(__dirname, 'sfx');
|
||||||
|
const allowedSfxChannels = new RegExp(tokens.allowedSfxChannels);
|
||||||
|
let playOptions = {volume: tokens.sfxVolume, passes: tokens.passes};
|
||||||
let playing = false;
|
let playing = false;
|
||||||
|
|
||||||
// read in sfx directory, filenames are the commands
|
// Read in sfx directory, filenames are the commands
|
||||||
let sfxList = fs.readdirSync(sfxFilePath);
|
let sfxList = readSfxDirectory(sfxFilePath);
|
||||||
sfxList.forEach(function(el, index, a) {
|
// Watch directory for changes and update the list
|
||||||
a[index] = el.split('.')[0];
|
fs.watch(sfxFilePath, (eventType, filename) => {
|
||||||
|
if (eventType === 'rename') {
|
||||||
|
sfxList = readSfxDirectory(sfxFilePath);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Set up the native commands to handle
|
||||||
const commands = {
|
const commands = {
|
||||||
'x': (msg) => {
|
'sfx': (msg) => {
|
||||||
|
if (!allowedSfxChannels.test(msg.channel.name)) return;
|
||||||
let sfx = msg.content.split(' ')[1];
|
let sfx = msg.content.split(' ')[1];
|
||||||
if (sfx == '' || sfx === undefined) return msg.channel.send('```'+sfxList.join(', ')+'```');
|
if (sfx == '' || sfx === undefined) return msg.channel.send('```'+sfxList.join(', ')+'```');
|
||||||
|
|
||||||
@@ -30,51 +38,56 @@ const commands = {
|
|||||||
} else if (fs.existsSync(path.join(sfxFilePath, sfx + '.wav'))) {
|
} else if (fs.existsSync(path.join(sfxFilePath, sfx + '.wav'))) {
|
||||||
sfxPath = path.join(sfxFilePath, sfx + '.wav');
|
sfxPath = path.join(sfxFilePath, sfx + '.wav');
|
||||||
} else {
|
} else {
|
||||||
return msg.channel.send('This sound effect does not exist!');
|
return msg.reply('This sound effect does not exist!');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!msg.guild.voiceConnection) return commands.xjoin(msg).then(() => commands.x(msg));
|
if (!msg.guild.voiceConnection) return joinVoiceChannel(msg).then(() => commands.sfx(msg));
|
||||||
|
|
||||||
playing = true;
|
playing = true;
|
||||||
(function play(song) {
|
(function play(sfxFile) {
|
||||||
console.log(song);
|
const dispatcher = msg.guild.voiceConnection.playFile(sfxFile, playOptions);
|
||||||
const dispatcher = msg.guild.voiceConnection.playFile(song, playOptions);
|
|
||||||
dispatcher.on('end', reason => {
|
dispatcher.on('end', reason => {
|
||||||
console.log('end: ' + reason)
|
//console.log('end: ' + reason)
|
||||||
playing = false;
|
playing = false;
|
||||||
msg.guild.voiceConnection.disconnect();
|
msg.guild.voiceConnection.disconnect();
|
||||||
})
|
})
|
||||||
.on('error', error => {
|
.on('error', error => {
|
||||||
console.log('error: ' + error);
|
//console.log('error: ' + error);
|
||||||
playing = false;
|
playing = false;
|
||||||
msg.guild.voiceConnection.disconnect();
|
msg.guild.voiceConnection.disconnect();
|
||||||
})
|
})
|
||||||
.on('start', () => {console.log('started');});
|
.on('start', () => {});
|
||||||
})(sfxPath);
|
})(sfxPath);
|
||||||
},
|
},
|
||||||
'xjoin': (msg) => {
|
'reboot': (msg) => {
|
||||||
|
if (msg.author.id == tokens.adminID) process.exit(); //Requires a node module like Forever to work.
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Wait for discord to be ready, handle messages
|
||||||
|
client.on('ready', () => {
|
||||||
|
console.log(`${tokens.botName} is connected and ready`);
|
||||||
|
}).on('message', msg => {
|
||||||
|
if (!msg.content.startsWith(tokens.prefix)) return;
|
||||||
|
let cmd = msg.content.toLowerCase().slice(tokens.prefix.length).split(' ')[0];
|
||||||
|
if (commands.hasOwnProperty(cmd)) commands[cmd](msg);
|
||||||
|
});
|
||||||
|
client.login(tokens.d_token);
|
||||||
|
|
||||||
|
function readSfxDirectory(path)
|
||||||
|
{
|
||||||
|
let sfxList = fs.readdirSync(sfxFilePath);
|
||||||
|
sfxList.forEach(function(el, index, a) {
|
||||||
|
a[index] = el.split('.')[0];
|
||||||
|
});
|
||||||
|
return sfxList;
|
||||||
|
}
|
||||||
|
|
||||||
|
function joinVoiceChannel(msg)
|
||||||
|
{
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const voiceChannel = msg.member.voiceChannel;
|
const voiceChannel = msg.member.voiceChannel;
|
||||||
if (!voiceChannel || voiceChannel.type !== 'voice') return msg.reply('I couldn\'t connect to your voice channel...');
|
if (!voiceChannel || voiceChannel.type !== 'voice') return msg.reply('I couldn\'t connect to your voice channel...');
|
||||||
voiceChannel.join().then(connection => resolve(connection)).catch(err => reject(err));
|
voiceChannel.join().then(connection => resolve(connection)).catch(err => reject(err));
|
||||||
});
|
});
|
||||||
},
|
|
||||||
'xhelp': (msg) => {
|
|
||||||
let tosend = ['```xl', tokens.prefix + 'x {sfx}: "Plays the requested sound effect in your current voice channel"', '```'];
|
|
||||||
msg.channel.sendMessage(tosend.join('\n'));
|
|
||||||
},
|
|
||||||
'xreboot': (msg) => {
|
|
||||||
if (msg.author.id == tokens.adminID) process.exit(); //Requires a node module like Forever to work.
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
client.on('ready', () => {
|
|
||||||
console.log('ready!');
|
|
||||||
});
|
|
||||||
|
|
||||||
client.on('message', msg => {
|
|
||||||
if (!allowedChannels.test(msg.channel.name) || !msg.content.startsWith(tokens.prefix)) return;
|
|
||||||
let cmd = msg.content.toLowerCase().slice(tokens.prefix.length).split(' ')[0];
|
|
||||||
if (commands.hasOwnProperty(cmd)) commands[cmd](msg);
|
|
||||||
});
|
|
||||||
client.login(tokens.d_token);
|
|
||||||
BIN
sfx/eheh.mp3
Executable file
BIN
sfx/eheh.mp3
Executable file
Binary file not shown.
BIN
sfx/fine.mp3
Executable file
BIN
sfx/fine.mp3
Executable file
Binary file not shown.
Reference in New Issue
Block a user