-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·163 lines (135 loc) · 6.15 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
// Smart Camel
// Testing machine learning in a discord bot
// yolo
console.log("\n"+
" jkcoxson \n"+
"\n\n"+
" ./#%#########%%(, \n"+
" .*////(#%#########%%%(////, \n"+
" ,/#%%%%%##########%%&&&&%%%%#(/(/, \n"+
" .*/#%%##############%%&&%%#######%%%#(*, \n"+
" *#%%##%%%#####%%#####%%%#######%%%##%%#* \n"+
" ,(###%&&%##%#(//(########%%%###%&&%###(* \n"+
" *#%%%%%%%###/*,*(%%%%%%#((//(##%&&%%%%#* \n"+
" .*(%&&%###(,...,*//////*,. .,/#%%&&&%(*. \n"+
" *(#((#%%%####((/,,*/(#####%%#((((* \n"+
" .,*,. ,#@@&%##%@@%**#@@&##%&@@%*. .,*,. \n"+
" *(%%#((#&@%. *%@&##%@&/. .(@@%(((%%#* \n"+
" ,(%&&%%&@@&(/(&@@@@@@@@@@&#/(%@@&%%&&&#* \n"+
" ,//*. .,(%&&&&%%%%#((#%%%%&&&&%(,. .*//, \n"+
" .***,,,,..,,...*((/,..,,..,,,,***, \n"+
" .*//*,,,**,**,**(#(/*,,*****,,,*//*. \n"+
" .,//*,,,,,,,,,/(#((*,,,,,,,,,,,*//*. \n"+
" .*//*,,,,,,***,,.,,,,,***,,,,,,*//*. \n"+
" .,//*,,,,,,/((/******/((/*,,,,,*//*. \n"+
" .*//,,,,,,,/(#((#(/*,,,,,,*/*. \n"+
" ......,/(((/*****,,,,,,,,*****/(((/,...... \n"+
" .. *#@@@@@@@@@@@@@@@@&(*,,,,*(&@@@@@@@@@@@@@@@@%*. .. \n"+
" .(&@@@@@&%#############%%&&&&&&&&&&&&%%#############%&@@@@@@#, \n"+
" .(@@@&&&&%#############%%&&&&&&&&&&&&%%#############%%&&&&@@#, \n"+
" ,(@@&&%&%%#############%%%%%%%%%%%%%%%%#############%%&%%&@@#, \n"+
" ,#@@@@@@@&&&&%%##########################################%%&&&&@@@@@@@%* \n"+
" ./##(///((#####################################################(///(##(, \n"+
" ./((/***/((((#%&%%####################################%%&%##(#(/***/((/, \n"+
" ./##(/*/((#(##%&&%##%#################################%&&%##(#((/**/(#/, \n"+
" ./((*,,,/(####%&&%####################################%%&%##(#(/*,,*((/, \n"+
" ./((*,.,*(####%&%%####################################%&&%####(/,..*((/, \n"+
" ./((*,.,/(####%&&&%%%#################################%%&%##(#(/,.,*((/, \n"+
" ./((*,.,*(####%&&&&%%##%%##########################%%%&&&%####(/,..*((/, \n")
const Discord = require('discord.js');
const client = new Discord.Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });
const config = require('./config.json');
const fs = require('fs')
const net = require('net');
const port = config.port
const host = config.host
let chatbot
let countDown = 60
let learnedMessages = []
const server = net.createServer();
server.listen(port, host, () => {
console.log('TCP Server is running on port ' + port +'.');
});
server.on("connection", (client)=>{
chatbot=client
});
client.on('message', async message=>{
if(message.author.bot==false&&message.content.length>1&&message.mentions.members.size<1&&!message.content.includes("@everyone")&&!message.content.includes("@here")){
if(message.content=="~chat"){
message.reply("I'll chat with people here now")
config.chatChannel=message.channel.id
fs.writeFileSync("./config.json",JSON.stringify(config, null, "\t"))
return;
}
if(message.content=="~learn"){
message.reply("I'll learn from chat here now")
config.learnChannel=message.channel.id
fs.writeFileSync("./config.json",JSON.stringify(config, null, "\t"))
return;
}
if(message.channel.id==config.chatChannel){
message.channel.startTyping();
let botResponse = await(getResponse(message.content))
if(!botResponse.startsWith("The current time is")){
message.channel.send(botResponse)
}
message.channel.stopTyping();
}else{
if(message.channel==config.learnChannel){
countDown=60
learnedMessages.push(message.content)
}
}
}
})
async function getResponse(message){
return new Promise(async(resolve,reject)=>{
chatbot.write(JSON.stringify({
"packet":"message",
"message":message
}))
let responded = false
while(!responded){
await(waitData()).then(response=>{
resolve(response)
}).catch(()=>{})
}
})
}
function waitData(){
return new Promise((resolve,reject)=>{
chatbot.once('data', (data)=>{
try{
packet = JSON.parse(data)
if(packet.packet=="response"){
chatbot.removeAllListeners()
resolve(packet.response)
}else{
reject("bad no")
}
}catch{
console.log("Bad packet: "+data)
reject();
}
})
})
}
setInterval(()=>{
countDown--;
if(countDown==0){
if(learnedMessages.length>4){
console.log("Sent new training data")
chatbot.write(JSON.stringify({
"packet":"train",
"conversation":learnedMessages
}))
}else{
console.log("Discarding messages, only had "+learnedMessages.length.toString()+" messages.")
}
learnedMessages=[]
if(learnedMessages.length>0){
client.channels.cache.get(config.learnChannel).send("The conversation has ended")
}
}
},1000)
client.login(config.token);