This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiasip.js
136 lines (121 loc) · 3.68 KB
/
iasip.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const Discord = require('discord.js');
const{
prefix,
token,
} = require('./config.json');
const client = new Discord.Client();
const info = require('./info.json');
var characters = [];
var audioFiles = new Map();
client.login(token);
client.once('ready', () => {
try{
proccessJSON();
}catch(err){
console.log(err);
return;
}
console.log('Ready!');
});
function proccessJSON(){
console.log('Processing audio info...');
characters = info.characters;
let audioInfo = info.audio;
for(let i = 0; i < characters.length; i++){
let char = characters[i];
audioFiles.set(char, audioInfo[char]);
}
return;
}
client.once('reconnecting', () => {
console.log('Reconnecting...');
});
client.once('disconnecting', () => {
console.log("Disconnecting :(");
});
client.on('message', async message => {
//Checking requirements
if(message.author.bot) return;
if(!message.content.startsWith(prefix)) return;
const voiceChannel = message.member.voice.channel;
if(!voiceChannel){
return message.channel.send("You need to be in a voice channel");
}
const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
return message.channel.send(
"Bot requires permissions to join and speak in your voice channel"
);
}
const messageArr = message.content.slice(prefix.length).trim().split(' ');
const command = messageArr.shift().toLowerCase();
var arg = '';
if(messageArr.length > 0){
arg = messageArr[0];
}
switch(command){
case "help":
generateHelp();
break;
default:
if(verifyRequest(command, arg)){
playQuote(command, arg);
return;
}
else{
return message.channel.send('Command not valid');
}
}
// Helper functions
function verifyRequest(command, arg){
if(!characters.includes(command)){
return false;
}
let charAudio = audioFiles.get(command);
if(arg && !charAudio.includes(arg)){
return false;
}
return true;
}
function playQuote(character, quote){
var quoteFile = 'audio/' + character;
if(arg){
quoteFile += '/' + quote + '.mp3';
}else{
let quoteFiles = audioFiles.get(character);
let selected = quoteFiles[Math.floor(Math.random() * quoteFiles.length)]
quoteFile += '/' + selected + '.mp3';
}
try{
voiceChannel.join().then(connection => {
connection.play(quoteFile).on('finish', () => {
voiceChannel.leave();
});
});
}catch(err){
return message.channel.send(err);
}
}
function generateHelp(){
var out = " 'It's Always Sunny in Philadelphia' Soundboard\n";
out += 'HOW TO USE:\n';
out += '--<character> - Play an audio\n';
out += '--<character> <file> - Play a specific file (extension not required)\n\n';
out += 'CURRENT COMMANDS\n';
for(let i in characters){
let char = characters[i];
let charArr = audioFiles.get(char);
out += '--' + char + '\n';
let flag = true;
for(let j in charArr){
if(!flag){
out += ', ';
}
out += charArr[j];
flag = false;
}
out += '\n\n'
}
message.channel.send(out);
}
});