Improve AI

This commit is contained in:
syuilo
2018-08-27 06:59:18 +09:00
parent d30a98bf2e
commit 805b25bb51
6 changed files with 147 additions and 7 deletions

100
src/modules/core/index.ts Normal file
View File

@ -0,0 +1,100 @@
import * as loki from 'lokijs';
import from '../../ai';
import IModule from '../../module';
import MessageLike from '../../message-like';
import serifs from '../../serifs';
export default class CoreModule implements IModule {
public name = 'core';
private ai: ;
public install = (ai: ) => {
this.ai = ai;
}
public onMention = (msg: MessageLike) => {
if (msg.text && msg.text.includes('って呼んで') && !msg.text.startsWith('って呼んで')) {
let friend = this.ai.friends.findOne({
userId: msg.userId
});
if (friend == null) {
friend = this.ai.friends.insertOne({
userId: msg.userId
});
}
const name = msg.text.match(/^(.+?)って呼んで/)[1];
const withSan =
name.endsWith('さん') ||
name.endsWith('くん') ||
name.endsWith('君') ||
name.endsWith('ちゃん') ||
name.endsWith('様');
if (withSan) {
friend.name = name;
this.ai.friends.update(friend);
msg.reply(serifs.core.setNameOk.replace('{name}', name));
} else {
msg.reply(serifs.core.san).then(reply => {
this.ai.subscribeReply(this, msg.userId, msg.isMessage, msg.isMessage ? msg.userId : reply.id, {
friend: friend,
name: name
});
});
}
return true;
} else if (msg.text && msg.text.includes('おはよう')) {
const friend = this.ai.friends.findOne({
userId: msg.userId
});
if (friend && friend.name) {
msg.reply(serifs.core.goodMorningWithName.replace('{name}', friend.name));
} else {
msg.reply(serifs.core.goodMorning);
}
return true;
} else if (msg.text && msg.text.includes('おやすみ')) {
const friend = this.ai.friends.findOne({
userId: msg.userId
});
if (friend && friend.name) {
msg.reply(serifs.core.goodNightWithName.replace('{name}', friend.name));
} else {
msg.reply(serifs.core.goodNight);
}
return true;
} else {
return false;
}
}
public onReplyThisModule = (msg: MessageLike, data: any) => {
if (msg.text == null) return;
const done = () => {
this.ai.friends.update(data.friend);
msg.reply(serifs.core.setNameOk.replace('{name}', data.friend.name));
this.ai.unsubscribeReply(this, msg.userId);
};
if (msg.text.includes('はい')) {
data.friend.name = data.name + 'さん';
done();
} else if (msg.text.includes('いいえ')) {
data.friend.name = data.name;
done();
} else {
msg.reply(serifs.core.yesOrNo).then(reply => {
this.ai.subscribeReply(this, msg.userId, msg.isMessage, reply.id, data);
});
}
}
}

View File

@ -35,8 +35,9 @@ export default class TimerModule implements IModule {
const str = `${hours ? hoursQuery[0] : ''}${minutes ? minutesQuery[0] : ''}${seconds ? secondsQuery[0] : ''}`;
setTimeout(() => {
const name = this.ai.getName(msg.userId);
this.ai.sendMessage(msg.userId, {
text: serifs.timer.notify.replace('{time}', str)
text: name ? serifs.timer.notifyWithName.replace('{time}', str).replace('{name}', name) : serifs.timer.notify.replace('{time}', str)
});
}, time);