From 6160d0f8257addd6da28c74fc9ec3f7cd3e78765 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 11 Aug 2018 15:26:25 +0900 Subject: [PATCH] :v: --- package-lock.json | 32 +++++++++ package.json | 4 +- src/ai.ts | 122 +++++++++++++++++++++++++++++++ src/config.ts | 1 - src/index.ts | 136 ++++------------------------------- src/message-like.ts | 2 +- src/module.ts | 2 +- src/modules/ping/index.ts | 16 +++++ src/modules/reversi/index.ts | 2 +- src/modules/server/index.ts | 2 +- 10 files changed, 192 insertions(+), 127 deletions(-) create mode 100644 src/ai.ts create mode 100644 src/modules/ping/index.ts diff --git a/package-lock.json b/package-lock.json index e619121..bf38284 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,19 @@ "resolved": "https://registry.npmjs.org/@types/node/-/node-10.0.5.tgz", "integrity": "sha512-he3QlF+xnGlmsnL1H8/CiM6r25kk0STky6U5yIqNh4Nnp9KlJBSdMMIiCzDYtAFLw2rWnJ4XKc1xB2/u/anYow==" }, + "@types/promise-retry": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/@types/promise-retry/-/promise-retry-1.1.2.tgz", + "integrity": "sha512-rnfMx3T3mJBd1T4Jd4EouFTSXivbOIDmamTKPXx9KuwEzYxqPW7SuPAnQ7KmHq52GjKYzQFWz++ICuWa092deQ==", + "requires": { + "@types/retry": "*" + } + }, + "@types/retry": { + "version": "0.10.2", + "resolved": "https://registry.npmjs.org/@types/retry/-/retry-0.10.2.tgz", + "integrity": "sha512-LqJkY4VQ7S09XhI7kA3ON71AxauROhSv74639VsNXC9ish4IWHnIi98if+nP1MxQV3RMPqXSCYgpPsDHjlg9UQ==" + }, "@types/ws": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/@types/ws/-/ws-5.1.2.tgz", @@ -167,6 +180,11 @@ "safer-buffer": "^2.1.0" } }, + "err-code": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/err-code/-/err-code-1.1.2.tgz", + "integrity": "sha1-BuARbTAo9q70gGhJ6w6mp0iuaWA=" + }, "escape-string-regexp": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", @@ -344,6 +362,15 @@ "resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz", "integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=" }, + "promise-retry": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/promise-retry/-/promise-retry-1.1.1.tgz", + "integrity": "sha1-ZznpaOMFHaIM5kl/srUPaRHfPW0=", + "requires": { + "err-code": "^1.0.0", + "retry": "^0.10.0" + } + }, "psl": { "version": "1.1.29", "resolved": "https://registry.npmjs.org/psl/-/psl-1.1.29.tgz", @@ -419,6 +446,11 @@ "tough-cookie": ">=2.3.3" } }, + "retry": { + "version": "0.10.1", + "resolved": "https://registry.npmjs.org/retry/-/retry-0.10.1.tgz", + "integrity": "sha1-52OI0heZLCUnUCQdPTlW/tmNj/Q=" + }, "safe-buffer": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", diff --git a/package.json b/package.json index e539d8f..02d2954 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,15 @@ { "name": "ai", - "main": "./built/front.js", + "main": "./built/index.js", "scripts": { "build": "tsc" }, "dependencies": { "@types/node": "10.0.5", + "@types/promise-retry": "1.1.2", "@types/ws": "5.1.2", "misskey-reversi": "0.0.5", + "promise-retry": "1.1.1", "reconnecting-websocket": "4.0.0-rc5", "request": "2.87.0", "request-promise-native": "1.0.5", diff --git a/src/ai.ts b/src/ai.ts new file mode 100644 index 0000000..9d78903 --- /dev/null +++ b/src/ai.ts @@ -0,0 +1,122 @@ +// AI CORE + +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'; +const ReconnectingWebSocket = require('../node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs.js'); + +/** + * 藍 + */ +export default class 藍 { + private account: any; + + /** + * ホームストリーム + */ + private connection: any; + + private modules: IModule[] = []; + + constructor(account: any) { + this.account = account; + + this.connection = new ReconnectingWebSocket(`${config.wsUrl}/?i=${config.i}`, [], { + WebSocket: WebSocket + }); + + this.connection.addEventListener('open', () => { + console.log('home stream opened'); + }); + + this.connection.addEventListener('close', () => { + console.log('home stream closed'); + }); + + this.connection.addEventListener('message', message => { + const msg = JSON.parse(message.data); + + this.onMessage(msg); + }); + + if (config.reversiEnabled) { + } + } + + public install = (module: IModule) => { + module.install(this); + this.modules.push(module); + } + + private onMessage = (msg: any) => { + switch (msg.type) { + // メンションされたとき + case 'mention': { + if (msg.body.userId == this.account.id) return; // 自分は弾く + if (msg.body.text.startsWith('@' + this.account.username)) { + this.onMention(new MessageLike(this, msg.body, false)); + } + break; + } + + // 返信されたとき + case 'reply': { + if (msg.body.userId == this.account.id) return; // 自分は弾く + this.onMention(new MessageLike(this, msg.body, false)); + break; + } + + // メッセージ + case 'messaging_message': { + if (msg.body.userId == this.account.id) return; // 自分は弾く + this.onMention(new MessageLike(this, msg.body, true)); + break; + } + + default: + break; + } + } + + private onMention = (msg: MessageLike) => { + console.log(`mention received: ${msg.id}`); + + // リアクションする + if (!msg.isMessage) { + setTimeout(() => { + request.post(`${config.apiUrl}/notes/reactions/create`, { + json: { + i: config.i, + noteId: msg.id, + reaction: 'love' + } + }); + }, 1000); + } + + this.modules.filter(m => m.hasOwnProperty('onMention')).some(m => { + return m.onMention(msg); + }); + } + + public post = (param: any) => { + this.api('notes/create', param); + } + + public sendMessage = (userId: any, param: any) => { + this.api('messaging/messages/create', Object.assign({ + userId: userId, + }, param)); + } + + public api = (endpoint: string, param) => { + return request.post(`${config.apiUrl}/${endpoint}`, { + json: Object.assign({ + i: config.i + }, param) + }); + }; +} diff --git a/src/config.ts b/src/config.ts index 0636b42..43edba5 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,7 +1,6 @@ type Config = { host: string; i: string; - id: string; wsUrl: string; apiUrl: string; reversiEnabled: boolean; diff --git a/src/index.ts b/src/index.ts index 552229a..a6e02fc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,127 +1,21 @@ -// AI CORE - -import * as WebSocket from 'ws'; -import * as request from 'request-promise-native'; -import serifs from './serifs'; +import 藍 from './ai'; import config from './config'; -import IModule from './module'; -import MessageLike from './message-like'; import ReversiModule from './modules/reversi'; import ServerModule from './modules/server'; -const ReconnectingWebSocket = require('../node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs.js'); +import PingModule from './modules/ping'; +import * as request from 'request-promise-native'; +const promiseRetry = require('promise-retry'); -/** - * 藍 - */ -export default class 藍 { - - /** - * ホームストリーム - */ - private connection: any; - - private modules: IModule[] = []; - - constructor() { - this.connection = new ReconnectingWebSocket(`${config.wsUrl}/?i=${config.i}`, [], { - WebSocket: WebSocket - }); - - this.connection.addEventListener('open', () => { - console.log('home stream opened'); - }); - - this.connection.addEventListener('close', () => { - console.log('home stream closed'); - }); - - this.connection.addEventListener('message', message => { - const msg = JSON.parse(message.data); - - this.onMessage(msg); - }); - - if (config.reversiEnabled) { +promiseRetry(retry => { + return request.post(`${config.apiUrl}/i`, { + json: { + i: config.i } - } + }).catch(retry); +}).then(account => { + const ai = new 藍(account); - public install = (module: IModule) => { - module.install(this); - this.modules.push(module); - } - - private onMessage = (msg: any) => { - switch (msg.type) { - // メンションされたとき - case 'mention': { - if (msg.body.userId == config.id) return; // 自分は弾く - this.onMention(new MessageLike(this, msg.body, false)); - break; - } - - // 返信されたとき - case 'reply': { - if (msg.body.userId == config.id) return; // 自分は弾く - this.onMention(new MessageLike(this, msg.body, false)); - break; - } - - // メッセージ - case 'messaging_message': { - if (msg.body.userId == config.id) return; // 自分は弾く - this.onMention(new MessageLike(this, msg.body, true)); - break; - } - - default: - break; - } - } - - private onMention = (msg: MessageLike) => { - console.log(`mention received: ${msg.id}`); - - // リアクションする - if (!msg.isMessage) { - setTimeout(() => { - request.post(`${config.apiUrl}/notes/reactions/create`, { - json: { - i: config.i, - noteId: msg.id, - reaction: 'love' - } - }); - }, 1000); - } - - this.modules.filter(m => m.hasOwnProperty('onMention')).some(m => { - return m.onMention(msg); - }); - } - - public post = (param: any) => { - this.api('notes/create', param); - } - - public sendMessage = (userId: any, param: any) => { - this.api('messaging/messages/create', Object.assign({ - userId: userId, - }, param)); - } - - public api = (endpoint: string, param) => { - return request.post(`${config.apiUrl}/${endpoint}`, { - json: Object.assign({ - i: config.i - }, param) - }); - }; -} - -const ai = new 藍(); - -const serverModule = new ServerModule(); -ai.install(serverModule); - -const reversiModule = new ReversiModule(); -ai.install(reversiModule); + ai.install(new PingModule()); + ai.install(new ServerModule()); + ai.install(new ReversiModule()); +}); diff --git a/src/message-like.ts b/src/message-like.ts index b80ec60..413e90e 100644 --- a/src/message-like.ts +++ b/src/message-like.ts @@ -1,4 +1,4 @@ -import 藍 from '.'; +import 藍 from './ai'; export default class MessageLike { private ai: 藍; diff --git a/src/module.ts b/src/module.ts index a2285f3..2a88636 100644 --- a/src/module.ts +++ b/src/module.ts @@ -1,4 +1,4 @@ -import 藍 from '.'; +import 藍 from './ai'; import MessageLike from './message-like'; export default interface IModule { diff --git a/src/modules/ping/index.ts b/src/modules/ping/index.ts new file mode 100644 index 0000000..0dee2c4 --- /dev/null +++ b/src/modules/ping/index.ts @@ -0,0 +1,16 @@ +import 藍 from '../../ai'; +import IModule from '../../module'; +import MessageLike from '../../message-like'; + +export default class PingModule implements IModule { + public install = (ai: 藍) => { } + + public onMention = (msg: MessageLike) => { + if (msg.text && msg.text.indexOf('ping') > -1) { + msg.reply('PONG!'); + return true; + } else { + return false; + } + } +} diff --git a/src/modules/reversi/index.ts b/src/modules/reversi/index.ts index 4bf7821..ae8c00b 100644 --- a/src/modules/reversi/index.ts +++ b/src/modules/reversi/index.ts @@ -1,6 +1,6 @@ import * as childProcess from 'child_process'; const ReconnectingWebSocket = require('../../../node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs.js'); -import 藍 from '../..'; +import 藍 from '../../ai'; import IModule from '../../module'; import serifs from '../../serifs'; import config from '../../config'; diff --git a/src/modules/server/index.ts b/src/modules/server/index.ts index 93ff02d..8a67fc5 100644 --- a/src/modules/server/index.ts +++ b/src/modules/server/index.ts @@ -1,6 +1,6 @@ import * as childProcess from 'child_process'; import * as WebSocket from 'ws'; -import 藍 from '../..'; +import 藍 from '../../ai'; import IModule from '../../module'; import serifs from '../../serifs'; import config from '../../config';