-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
67 lines (61 loc) · 2 KB
/
bot.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
require('dotenv').config()
const Discord = require('discord.js');
const synthesize = require('./synthesizeText');
const Duplex = require('stream').Duplex;
const client = new Discord.Client();
async function tts(connection, text) {
const buffer = await synthesize(text);
const stream = new Duplex();
stream.push(buffer);
stream.push(null);
connection.play(stream);
}
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', async msg => {
const command = msg.content.substr(0,msg.content.indexOf(' ')) || msg.content;
const content = msg.content.substr(msg.content.indexOf(' ')+1);
const voiceChannel = msg.member.voice.channel;
switch (command) {
case ',join':
if(voiceChannel) {
if( client.voice.connections.find(vc => vc.channel.id === voiceChannel.id)) {
msg.reply('Already in your voice channel.');
} else {
await voiceChannel.join()
msg.channel.send(`Joined voice channel ${voiceChannel}`);
}
} else {
msg.reply('You must be in a voice channel.');
}
break;
case ',say':
let connection;
if(voiceChannel) {
if(! (connection = client.voice.connections.find(vc => vc.channel.id === voiceChannel.id))) {
connection = await voiceChannel.join();
msg.channel.send(`Joined voice channel ${voiceChannel}`);
}
tts(connection, content);
} else {
msg.reply('You must be in a voice channel.');
}
break;
case ',leave':
if(voiceChannel) {
let connection;
if(! (connection = client.voice.connections.find(vc => vc.channel.id === voiceChannel.id))) {
msg.reply('You must be in a voice channel.');
} else {
await connection.channel.leave();
msg.channel.send(`Left voice channel ${voiceChannel}.`);
}
} else {
msg.reply('You must be in a voice channel.');
}
default:
break;
}
});
client.login(process.env.TOKEN);