-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
182 lines (164 loc) · 5.84 KB
/
index.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
"use strict";
const Discord = require('discord.js')
const ytdl = require('ytdl-core');
const client = new Discord.Client()
var connection;
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
var mysql = require('mysql');
connection = mysql.createConnection({
host: 'localhost',
user: 'SERVER USERNAME',
password: 'SERVER PASSWORD',
database: 'soundboy'
});
connection.connect((err) => {
if (err) {
return;
}
});
});
client.on('message', msg => {
if (msg.content.charAt(0) === ';' && msg.content.length > 1) {
const channel = client.channels.get(msg.member.voiceChannelID);
const args = msg.content.substring(1).split(' ').filter((a) => a != '');
switch (args.length) {
case 5:
if (timeToSeconds(args[3]) > 20) {
displayError(msg, "Too long, clips should be less than 20 seconds");
} else {
connection.query('SELECT * FROM sounds WHERE server_name = ? AND name = ?',
[msg.member.guild.id, args[4]],
(e, res, f) => {
if (res.length < 1) {
var exec = require('child_process').exec;
exec("ffmpeg -ss " + toTimeString(args[2]) + " -i $(youtube-dl -f 140 -g '" + args[1] + "') -t " + toTimeString(args[3]) + " -strict -2 ./soundbank/" + args[4] + msg.member.guild.id + ".mp4", (e, out, stErr) => {
console.log(e);
console.log(out);
console.log(stErr);
});
connection.query('INSERT INTO sounds(server_name, youtube_link, start_time, duration, name) VALUES(?, ?, ?, ?, ?)', [msg.member.guild.id, args[1], args[2], args[3], args[4]]);
sendMessage(msg, "We made it");
} else {
displayError(msg, "That's already a sound, pick a different name or delete the old one.");
}
}
);
}
break;
case 1:
if (args[0] == "sounds") {
displaySoundBoard(msg);
} else if (args[0] == "help") {
displayHelp(msg);
} else {
if (!channel) {
msg.reply("You aren't in a voice channel...");
} else {
connection.query('SELECT * FROM sounds WHERE server_name = ? AND name = ?',
[msg.member.guild.id, args[0]],
(e, res, f) => {
if (res.length < 1) {
displayError(msg, "That sound doesn't exist...");
} else {
channel.join().then(connection => {
const output = connection.playFile('./soundbank/' + args[0] + msg.member.guild.id + '.mp4');
output.on('end', () => {
channel.leave();
});
});
}
});
}
}
break;
case 2:
if (args[0] == "delete") {
connection.query('DELETE FROM sounds WHERE server_name = ? AND name = ?',
[msg.member.guild.id, args[1]], (e, res) => {
if (res.affectedRows > 0) {
var exec = require('child_process').exec;
exec('rm ./soundbank/' + args[1] + msg.member.guild.id + '.mp4');
sendMessage(msg, "Got him");
}
});
} else {
displayError(msg, "What's that? You need some ';help'?");
}
break;
default:
displayError(msg, "What's that? You need some ';help'?");
}
}
}
);
client.login('YOUR DISCORD BOT KEY');
function timeToSeconds(str) {
let arr = str.split(':'), retVal;
switch (arr.length) {
case 1:
retVal = Number(arr[0]);
break;
case 2:
retVal = Number(arr[0] * 60) + Number(arr[1]);
break;
case 3:
retVal = Number(arr[0] * 60 * 60) + Number(arr[1] * 60) + Number(arr[2]);
break;
}
return retVal;
}
function toTimeString(str) {
let arr = str.split(':'), retVal;
switch (arr.length) {
case 1:
retVal = Number(arr[0]);
break;
case 2:
retVal = Number(arr[0] * 60) + Number(arr[1]);
break;
case 3:
retVal = Number(arr[0] * 60 * 60) + Number(arr[1] * 60) + Number(arr[2]);
break;
}
let seconds = retVal % 60;
let minutes = Math.floor(retVal / 60);
let hours = Math.floor(minutes / 60);
minutes = minutes % 60;
retVal = String(hours).padStart(2, '0') + ":" + String(minutes).padStart(2, '0') + ":" + String(seconds).padStart(2, '0');
return retVal;
}
function displaySoundBoard(msg) {
let channel = client.channels.get(msg.member.lastMessage.channel.id);
connection.query('SELECT * FROM sounds WHERE server_name = ?',
[msg.member.guild.id], (e, res, f) => {
let str = "";
res.forEach(i => {
str += i.name + " -- ";
});
str = str.substring(0, str.length - 4);
channel.send(str);
});
}
function displayHelp(msg) {
let channel = client.channels.get(msg.member.lastMessage.channel.id);
channel.send("Tfw not general intelligence. Here are the only things I understand...\n\n" +
"1. To add a new sound:\n" +
";create <youtube link> <start time> <duration> <name to save as>\n\n" +
"2. To play a sound:\n" +
";<name>\n\n" +
"3. To show the saved sounds:\n" +
";sounds\n\n" +
"4. To delete a sound:\n" +
";delete <name>\n\n" +
"The command sequence below creates a new sound, plays it, then deletes it:\n" +
";create youtubelink 1:32 10 someKindaSound\n;someKindaSound\n;delete someKindaSound"
);
}
function displayError(msg, str) {
msg.reply(str);
}
function sendMessage(msg, str) {
let channel = client.channels.get(msg.member.lastMessage.channel.id);
channel.send(str);
}