-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdiscord.js
225 lines (191 loc) · 6.05 KB
/
discord.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const Discord = require('discord.js');
const speech = require('@google-cloud/speech');
const text = require('@google-cloud/text-to-speech');
const dialogflow = require('@google-cloud/dialogflow-cx');
const { Readable } = require('stream');
const { Transform } = require('stream')
const fs = require('fs');
const util = require('util');
const token = 'Nzc0NzI1OTEyMzYwMjU1NTQw.X6b9uw.zVQSDMxApTTzl7DfmtBWZZGgusc';
const client = new Discord.Client();
const speechClient = new speech.SpeechClient();
const textClient = new text.TextToSpeechClient();
const sessionClient = new dialogflow.SessionsClient();
const sessionId = Math.random().toString(36).substring(7);
const servers = new Map();
function convertBufferTo1Channel(buffer) {
const convertedBuffer = Buffer.alloc(buffer.length / 2)
for (let i = 0; i < convertedBuffer.length / 2; i++) {
const uint16 = buffer.readUInt16LE(i * 4)
convertedBuffer.writeUInt16LE(uint16, i * 2)
}
return convertedBuffer
}
class ConvertTo1ChannelStream extends Transform {
constructor(source, options) {
super(options)
}
_transform(data, encoding, next) {
next(null, convertBufferTo1Channel(data))
}
}
class Silence extends Readable {
_read() {
this.push(Buffer.from([0xF8, 0xFF, 0xFE]));
}
}
client.once('ready', () => {
console.log('ready!');
client.user.setActivity("watching civilization burn");
});
client.login(token);
const encoding = 'LINEAR16';
const sampleRateHertz = 48000;
const languageCode = 'en-US';
const speechRequest = {
config: {
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode,
},
single_utterance: true,
interimResults: true, // If you want interim results, set this to true
};
client.on('message', async message => {
if(message.author.bot) return;
console.log(message.content);
if(message.content === '!echo') {
message.channel.send('echo');
}
else if(message.content === '!join') {
join(message);
}
else if(message.content === '!leave') {
leave(message);
}
else if(message.content === '!play') {
// message.guild.voice.
}
else if(message.content === '!kill') {
leave(message);
client.destroy()
}
});
async function join(message){
const voiceChannel = message.member.voice.channel
if(!voiceChannel) {
return message.channel.send('you need to be in a voice channel for me to join');
}
const permissions = voiceChannel.permissionsFor(message.client.user);
if(!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
return message.channel.send('i need perms bro');
}
const voiceConstruct = {
voiceChannel: voiceChannel,
connection: null,
stream: null,
};
servers.set(message.guild.id, voiceConstruct);
try {
const connection = await voiceChannel.join()
connection.play(new Silence(), { type: 'opus' });
connection.on('speaking', (user, speaking) => {
console.log(user + ' is ' + speaking);
if(speaking == 1) {
const audioStream = connection.receiver.createStream(user, {mode: 'pcm'})
let transcription = null;
voiceConstruct.stream = audioStream;
const recognizeStream = speechClient
.streamingRecognize(speechRequest)
.on('error', () =>{
console.log('recognizer error')
message.channel.send("i ain't got forever buddy");
leave(message);
})
.on('data', data => {
transcription = data.results
.map(result => result.alternatives[0].transcript)
.join(' ')
.replace(/ + +/," ")
.toLowerCase();
console.log(`Transcription: ${transcription}`)
});
const convert = new ConvertTo1ChannelStream();
audioStream.pipe(convert).pipe(recognizeStream);
audioStream.on('end', () => {
if(transcription) {
// message.channel.send(transcription);
dialog(message, transcription, connection);
// speak(message, transcription, connection);
}
// recognizeStream.destroy();
console.log('audio end')
})
.on('close', () => {
// if(transcription) message.channel.send(transcription);
// recognizeStream.destroy();
console.log('audio close')
})
.on('error',error => console.error(error));
}
});
// voiceConstruct.connection = connection
// const audio = connection.receiver.createStream(message, {end: 'manual'})
} catch (err) {
console.log(err);
servers.delete(message.guild.id);
return message.channel.send('oopsie');
}
}
async function dialog(message, text, connection) {
const sessionPath = sessionClient.projectLocationAgentSessionPath(
'hackrpidiscordbot',
'global',
'cf6e3571-68c4-4698-8413-44ff07229567',
sessionId
);
const dialogRequest = {
session: sessionPath,
queryInput: {
text: {
// The query to send to the dialogflow agent
text: text,
},
// The language used by the client (en-US)
languageCode: 'en',
},
};
const [response] = await sessionClient.detectIntent(dialogRequest);
console.log('Detected intent');
for (const m of response.queryResult.responseMessages) {
if (m.text) {
console.log(`Agent Response: ${m.text.text}`);
speak(message, m.text.text, connection);
}
}
if (response.queryResult.match.intent) {
console.log(`Matched Intent: ${response.queryResult.match.intent.displayName}`);
}
console.log(`Current Page: ${response.queryResult.currentPage.displayName}`);
}
async function speak(message, text, connection) {
console.log('speak')
const textRequest = {
input: {text: text},
// Select the language and SSML voice gender (optional)
voice: {languageCode: 'en-US', ssmlGender: 'NEUTRAL'},
// select the type of audio encoding
audioConfig: {audioEncoding: 'MP3'},
};
const [response] = await textClient.synthesizeSpeech(textRequest);
const writeFile = util.promisify(fs.writeFile);
await writeFile('output.mp3', response.audioContent, 'binary');
console.log('done writing')
connection.play('output.mp3');
}
async function leave(message) {
const voiceConstruct = servers.get(message.guild.id);
if(!voiceConstruct) return message.channel.send('oopsie whoopsie');
voiceConstruct.stream.destroy();
voiceConstruct.voiceChannel.leave();
}