-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
195 lines (158 loc) · 5.42 KB
/
server.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
183
184
185
186
187
188
189
190
191
192
193
194
195
require('dotenv').config();
var lame = require('lame');
var fs = require('fs');
var Discordie = require("discordie");
var express = require('express');
var bodyParser = require('body-parser');
var Events = Discordie.Events;
var app = express();
var client = new Discordie();
client.connect({
email: process.env.DISCORD_MAIL,
password: process.env.DISCORD_PASSWORD
});
client.Dispatcher.on(Events.GATEWAY_READY, function() {
console.log("Connected as: " + client.User.username);
say('hello, yes dis is bot!', 'general', 'BotTesting');
});
client.Dispatcher.on(Discordie.Events.DISCONNECTED, (e) => {
const delay = 5000;
const sdelay = Math.floor(delay/100)/10;
if (e.error.message.indexOf("gateway") >= 0) {
console.log("Disconnected from gw, resuming in " + sdelay + " seconds");
} else {
console.log("Failed to log in or get gateway, reconnecting in " + sdelay + " seconds");
}
setTimeout(connect, delay);
});
client.Dispatcher.on(Events.MESSAGE_CREATE, function(e) {
if (e.message.content == "ping")
e.message.channel.sendMessage("pong");
if(e.message.content == "vjoin")
voiceJoin('testing', 'BotTesting');
if(e.message.content == "vleave")
voiceLeave();
if(e.message.content == "play")
play();
if(e.message.content == "gtfo") {
client.disconnect();
process.exit();
}
});
var stopPlaying = true;
function play(filename, voiceConnectionInfo) {
stopPlaying = false;
var mp3decoder = new lame.Decoder();
mp3decoder.on('format', decode);
fs.createReadStream(filename).pipe(mp3decoder);
function decode(pcmfmt) {
var options = {
frameDuration: 60,
sampleRate: pcmfmt.sampleRate,
channels: pcmfmt.channels,
float: false,
multiThreadedVoice: true
};
const frameDuration = 60;
var readSize =
pcmfmt.sampleRate / 1000 *
options.frameDuration *
pcmfmt.bitDepth / 8 *
pcmfmt.channels;
mp3decoder.once('readable', function() {
if(!client.VoiceConnections.length) {
return console.log("Voice not connected");
}
if(!voiceConnectionInfo) {
voiceConnectionInfo = client.VoiceConnections[0];
}
var voiceConnection = voiceConnectionInfo.voiceConnection;
var encoder = voiceConnection.getEncoder(options);
const needBuffer = () => encoder.onNeedBuffer();
encoder.onNeedBuffer = function() {
var chunk = mp3decoder.read(readSize);
if (stopPlaying) return;
if (!chunk) return setTimeout(needBuffer, options.frameDuration);
var sampleCount = readSize / pcmfmt.channels / (pcmfmt.bitDepth / 8);
encoder.enqueue(chunk, sampleCount);
};
needBuffer();
});
//mp3decoder.once('end', () => setTimeout(play, 100, voiceConnectionInfo));
}
}
function stop() {
stopPlaying = true;
}
function say(text, textChannelName, guildName) {
var guild = client.Guilds.find(g => g.name == guildName);
var channel = guild.channels.find(c => c.name == textChannelName);
channel.sendMessage(text);
}
function voiceJoin(voiceChannelName, guildName) {
var guild = client.Guilds.find(g => g.name == guildName);
guild.voiceChannels
.forEach(channel => {
if(channel.name.toLowerCase().indexOf(voiceChannelName) >= 0)
channel.join();
});
}
function voiceLeave() {
client.Channels
.filter(channel => channel.type == 'voice')
.forEach(channel => {
if(channel.joined)
channel.leave();
});
}
app.listen(8080);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/api/sounds/play', function(req, res) {
var file = 'files/'+req.body.filename;
fs.access(file, function(err) {
if(err) {
res.send('file not found');
}
else {
play(file);
res.send('playing sound');
}
});
});
app.get('/api/sounds/stop', function(req, res) {
stop();
res.send('stopping sound');
});
app.get('/api/sounds/files', function(req, res) {
fs.readdir('files/', function(err, files) {
var newFiles = [];
files.forEach(function(element, index, array) {
newFiles.push({ id: index + 1, name: element });
});
res.send(newFiles);
});
});
app.post('/api/text/send', function(req, res) {
var guildName = req.body.guildName;
var channelName = req.body.channelName;
var text = req.body.text;
say(text, channelName, guildName);
res.send('sent message');
});
app.post('/api/voice/join', function(req, res) {
var guildName = req.body.guildName;
var channelName = req.body.channelName;
voiceJoin(channelName, guildName);
res.send('joined voice chat');
});
app.get('/api/voice/leave', function(req, res) {
voiceLeave();
res.send('left voice chat');
});
app.get('/', function(req, res) {
res.sendFile(__dirname+'/public/index.html');
});
app.get('/core.js', function(req, res) {
res.sendFile(__dirname+'/public/core.js');
});