-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
54 lines (41 loc) · 1.38 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
// Instantiate a DialogFlow client.
const dialogflow = require('dialogflow');
const dialogflowClient = new dialogflow.SessionsClient();
// Define session path
const sessionPath = dialogflowClient.sessionPath(process.env.PROJECT_ID, 'discordbot');
const Discord = require('discord.js');
const discordClient = new Discord.Client();
discordClient.on('ready', () => {
console.log('Ready!');
});
discordClient.on('message', m => {
if (!shouldBeInvoked(m)) {
return;
}
const message = remove(discordClient.user.username, m.cleanContent);
if (message === 'help') {
return m.channel.send(process.env.DISCORD_HELP_MESSAGE);
}
const dialogflowRequest = {
session: sessionPath,
queryInput: {
text: {
text: message,
languageCode: 'en-US'
}
}
};
dialogflowClient.detectIntent(dialogflowRequest).then(responses => {
m.channel.send(responses[0].queryResult.fulfillmentText);
});
});
function shouldBeInvoked(message) {
return (message.content.startsWith(process.env.DISCORD_PREFIX) ||
message.content.startsWith('@' + discordClient.user.username) ||
message.channel.type === 'dm') &&
discordClient.user.id !== message.author.id;
}
function remove(username, text) {
return text.replace('@' + username + ' ', '').replace(process.env.DISCORD_PREFIX + ' ', '');
}
discordClient.login(process.env.DISCORD_TOKEN);