-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
129 lines (106 loc) · 3.44 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
/**
* Copyright 2021 yan-930521 All Rights Reserved.
*/
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => res.send('link start!'));
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));
const Discord = require('discord.js');
const client = new Discord.Client();
client.login(process.env.token);
const setting = require("./Data/setting.json"); // 系統上的設定
const data = require('./Data/data.json'); // 初始資料
const data_brain = require('./Data/brain.json'); // 初始資料
const grammar = require('./Data/grammar.json'); // 替代詞
const config = require('./config.js'); // 功能集
const ctl = require('./encapsulate.js')
const Control = new ctl();
var Mood;
var Event;
var Brain
var Msgs = {};
var isready = false;
client.on('ready', async () => {
console.clear();
console.log(client.user.tag + " link start!");
Mood = await Control.createNlp('mood.nlp');
Event = await Control.createNlp('event.nlp');
Brain = new Control.NeuralNet();
Brain.init(2, 5, 1)
Brain.train(data_brain, 500, 0.5, 0.1);
isready = true;
console.log("Control is ready!")
});
client.on('message', async msg => {
try {
if (!isready) return;
if (msg.content.startsWith(setting.prefix)) return;
if (msg.author.bot) return;
// 抓出重複的訊息
let auth = Msgs[msg.author.id];
if (auth) {
let msgData = Msgs[msg.author.id][msg.content];
if (msgData) {
let ans = config.repeat[random(conig.repeat.length)];
for (let t in config.tags) {
let r = new RegExp(t, "g");
ans = ans.replace(r, config.tags[t](msgData));
}
msg.channel.send(ans);
return;
}
} else {
Msgs[msg.author.id] = {};
}
Msgs[msg.author.id][msg.content] = new Control.Msg();
setTimeout(() => {
delete Msgs[msg.author.id][msg.content];
}, setting['time_to_forget']);
// 判別情感
let mood = await Control.moodExtractor(Mood, msg.content);
// 事件提取
let event = await Control.eventExtractor(Event, msg.content);
// 語法決定
//let action = Control.actionPredictor(Brain, data, mood, event);
//Control.act(Control, data, action, event, mood, msg.content);
} catch (err) {
console.error(err);
}
});
client.on('message', async msg => {
if (!isready) return;
if (msg.author.id != "823885929830940682") return;
if (msg.author.bot) return;
if (!msg.content.startsWith(setting.prefix)) return;
let cmd = msg.content.replace(setting.prefix, "").trim().split(' ')[0]
if (cmd.startsWith("train")) {
let str = cmd.replace('train', "");
let mood = str.split(' ')[0];
let event = str.split(' ')[1];
Mood = Control.trainNlp(Mood, data, "mood", mood, str);
await Mood.train();
Event = Control.trainNlp(Event, data, "event", event, str);
await Event.train();
return;
}
if (cmd.startsWith("list")) {
let str = "```\n";
for(let d in data){
str += `< ${d} >\n`;
for(let i in data[d]) {
str += ` ${data[d][i].num}. ${i}\n`;
}
}
str += "```";
msg.channel.send(str);
return;
}
if (cmd.startsWith("testMood")) {
await msg.react(setting.msg_to_react);
let str = msg.content.replace(setting.prefix, "").replace(cmd, "").trim();
let mood = await Control.moodExtractor(Mood, str);
msg.channel.send("```" + JSON.stringify(mood, null, 2) + "```");
return;
}
});