-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundboard.js
46 lines (43 loc) · 1.35 KB
/
soundboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//UI system used to play sound files through the bot
const fs = require('fs'),
path = require('path'),
readline = require('readline');
//Path to audio files.
const soundPath = '.\\data\\sounds';
//Creates terminal interface
const interface = readline.createInterface({
input: process.stdin,
output: process.stdout
});
//Voice connection from discord client.
var voiceConnection = null;
exports.connect = function setConnection(connection) {
voiceConnection = connection;
console.log('Voice channel connected!');
interface.prompt();
}
function playSound(filename) {
//Makes sure there is a connection to play into first.
if (!voiceConnection) {
console.log('Voice connection not established!');
interface.prompt();
return null;
}
var filepath = path.join(soundPath, filename + '.wav');
//Dispatches the audio stream if the file exists.
var dispatch = fs.existsSync(filepath) ? voiceConnection.playFile(filepath) : null;
if (!dispatch) {
console.log(`File ${filepath} not found!`);
interface.prompt();
return null;
} else {
dispatch.on('end', (reason) => console.log(`Played ${filename}`));
interface.prompt();
return filepath;
}
}
exports.play = playSound;
//Simple interface catch
interface.on('line', (input) => {
playSound(input);
});