mirror of
https://github.com/syuilo/ai.git
synced 2024-11-09 15:38:00 +00:00
記憶を永続化するなど
This commit is contained in:
parent
1dc0bf27e9
commit
d30a98bf2e
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
|||
config.json
|
||||
built
|
||||
node_modules
|
||||
memory.json
|
||||
|
|
40
src/ai.ts
40
src/ai.ts
|
@ -1,12 +1,12 @@
|
|||
// AI CORE
|
||||
|
||||
import * as loki from 'lokijs';
|
||||
import * as WebSocket from 'ws';
|
||||
import * as request from 'request-promise-native';
|
||||
import serifs from './serifs';
|
||||
import config from './config';
|
||||
import IModule from './module';
|
||||
import MessageLike from './message-like';
|
||||
import { contexts } from './memory';
|
||||
const ReconnectingWebSocket = require('../node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs.js');
|
||||
|
||||
/**
|
||||
|
@ -27,9 +27,40 @@ export default class 藍 {
|
|||
|
||||
private modules: IModule[] = [];
|
||||
|
||||
public db: loki;
|
||||
|
||||
private contexts: loki.Collection<{
|
||||
isMessage: boolean;
|
||||
noteId?: string;
|
||||
userId?: string;
|
||||
module: string;
|
||||
key: string;
|
||||
}>;
|
||||
|
||||
constructor(account: any) {
|
||||
this.account = account;
|
||||
|
||||
this.db = new loki('memory.json', {
|
||||
autoload: true,
|
||||
autosave: true,
|
||||
autosaveInterval: 1000,
|
||||
autoloadCallback: this.init
|
||||
});
|
||||
}
|
||||
|
||||
private init = () => {
|
||||
//#region Init DB
|
||||
this.contexts = this.db.getCollection('contexts');
|
||||
if (this.contexts === null) {
|
||||
this.contexts = this.db.addCollection('contexts', {
|
||||
indices: ['key']
|
||||
});
|
||||
}
|
||||
//#endregion
|
||||
|
||||
// Install modules
|
||||
this.modules.forEach(m => m.install(this));
|
||||
|
||||
//#region Home stream
|
||||
this.connection = new ReconnectingWebSocket(`${config.wsUrl}/?i=${config.i}`, [], {
|
||||
WebSocket: WebSocket
|
||||
|
@ -74,7 +105,6 @@ export default class 藍 {
|
|||
}
|
||||
|
||||
public install = (module: IModule) => {
|
||||
module.install(this);
|
||||
this.modules.push(module);
|
||||
}
|
||||
|
||||
|
@ -132,7 +162,7 @@ export default class 藍 {
|
|||
}
|
||||
}, 1000);
|
||||
|
||||
const context = !msg.isMessage && msg.replyId == null ? null : contexts.findOne(msg.isMessage ? {
|
||||
const context = !msg.isMessage && msg.replyId == null ? null : this.contexts.findOne(msg.isMessage ? {
|
||||
isMessage: true,
|
||||
userId: msg.userId
|
||||
} : {
|
||||
|
@ -170,7 +200,7 @@ export default class 藍 {
|
|||
};
|
||||
|
||||
public subscribeReply = (module: IModule, key: string, isMessage: boolean, id: string) => {
|
||||
contexts.insertOne(isMessage ? {
|
||||
this.contexts.insertOne(isMessage ? {
|
||||
isMessage: true,
|
||||
userId: id,
|
||||
module: module.name,
|
||||
|
@ -184,7 +214,7 @@ export default class 藍 {
|
|||
}
|
||||
|
||||
public unsubscribeReply = (module: IModule, key: string) => {
|
||||
contexts.findAndRemove({
|
||||
this.contexts.findAndRemove({
|
||||
key: key,
|
||||
module: module.name
|
||||
});
|
||||
|
|
|
@ -1,17 +0,0 @@
|
|||
// 藍の記憶
|
||||
|
||||
import * as loki from 'lokijs';
|
||||
|
||||
const db = new loki('ai');
|
||||
|
||||
export default db;
|
||||
|
||||
export const contexts = db.addCollection<{
|
||||
isMessage: boolean;
|
||||
noteId?: string;
|
||||
userId?: string;
|
||||
module: string;
|
||||
key: string;
|
||||
}>('contexts', {
|
||||
indices: ['key']
|
||||
});
|
|
@ -30,7 +30,7 @@ export default class FortuneModule implements IModule {
|
|||
public install = (ai: 藍) => { }
|
||||
|
||||
public onMention = (msg: MessageLike) => {
|
||||
if (msg.text && (msg.text.includes('占') || msg.text.includes('運勢'))) {
|
||||
if (msg.text && (msg.text.includes('占') || msg.text.includes('うらな') || msg.text.includes('運勢'))) {
|
||||
const date = new Date();
|
||||
const seed = `${date.getFullYear()}/${date.getMonth()}/${date.getDay()}@${msg.userId}`;
|
||||
const rng = seedrandom(seed);
|
||||
|
|
|
@ -1,31 +1,37 @@
|
|||
import * as loki from 'lokijs';
|
||||
import 藍 from '../../ai';
|
||||
import IModule from '../../module';
|
||||
import MessageLike from '../../message-like';
|
||||
import serifs from '../../serifs';
|
||||
import db from '../../memory';
|
||||
|
||||
export const guesses = db.addCollection<{
|
||||
userId: string;
|
||||
secret: number;
|
||||
tries: number[];
|
||||
isEnded: boolean;
|
||||
startedAt: number;
|
||||
endedAt: number;
|
||||
}>('guessingGame', {
|
||||
indices: ['userId']
|
||||
});
|
||||
|
||||
export default class GuessingGameModule implements IModule {
|
||||
public name = 'guessingGame';
|
||||
private ai: 藍;
|
||||
private guesses: loki.Collection<{
|
||||
userId: string;
|
||||
secret: number;
|
||||
tries: number[];
|
||||
isEnded: boolean;
|
||||
startedAt: number;
|
||||
endedAt: number;
|
||||
}>;
|
||||
|
||||
public install = (ai: 藍) => {
|
||||
this.ai = ai;
|
||||
|
||||
//#region Init DB
|
||||
this.guesses = this.ai.db.getCollection('guessingGame');
|
||||
if (this.guesses === null) {
|
||||
this.guesses = this.ai.db.addCollection('guessingGame', {
|
||||
indices: ['userId']
|
||||
});
|
||||
}
|
||||
//#endregion
|
||||
}
|
||||
|
||||
public onMention = (msg: MessageLike) => {
|
||||
if (msg.text && (msg.text.includes('数当て') || msg.text.includes('数あて'))) {
|
||||
const exist = guesses.findOne({
|
||||
const exist = this.guesses.findOne({
|
||||
userId: msg.userId,
|
||||
isEnded: false
|
||||
});
|
||||
|
@ -42,7 +48,7 @@ export default class GuessingGameModule implements IModule {
|
|||
|
||||
const secret = Math.floor(Math.random() * 100);
|
||||
|
||||
guesses.insertOne({
|
||||
this.guesses.insertOne({
|
||||
userId: msg.userId,
|
||||
secret: secret,
|
||||
tries: [],
|
||||
|
@ -64,7 +70,7 @@ export default class GuessingGameModule implements IModule {
|
|||
public onReplyThisModule = (msg: MessageLike) => {
|
||||
if (msg.text == null) return;
|
||||
|
||||
const exist = guesses.findOne({
|
||||
const exist = this.guesses.findOne({
|
||||
userId: msg.userId,
|
||||
isEnded: false
|
||||
});
|
||||
|
@ -73,7 +79,7 @@ export default class GuessingGameModule implements IModule {
|
|||
msg.reply(serifs.GUESSINGGAME_CANCEL);
|
||||
exist.isEnded = true;
|
||||
exist.endedAt = Date.now();
|
||||
guesses.update(exist);
|
||||
this.guesses.update(exist);
|
||||
this.ai.unsubscribeReply(this, msg.userId);
|
||||
return;
|
||||
}
|
||||
|
@ -115,7 +121,7 @@ export default class GuessingGameModule implements IModule {
|
|||
this.ai.unsubscribeReply(this, msg.userId);
|
||||
}
|
||||
|
||||
guesses.update(exist);
|
||||
this.guesses.update(exist);
|
||||
|
||||
msg.reply(text).then(reply => {
|
||||
if (!end) {
|
||||
|
|
Loading…
Reference in a new issue