diff --git a/package.json b/package.json index c1add63..bd1a166 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "autobind-decorator": "2.4.0", "canvas": "2.6.1", "chalk": "4.1.0", + "emoji-regex": "9.0.0", "lokijs": "1.5.11", "mecab-async": "0.1.2", "misskey-reversi": "0.0.5", diff --git a/src/index.ts b/src/index.ts index 05f835b..7faa7cf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,7 @@ import BirthdayModule from './modules/birthday'; import ReversiModule from './modules/reversi'; import PingModule from './modules/ping'; import EmojiModule from './modules/emoji'; +import EmojiReactModule from './modules/emoji-react'; import FortuneModule from './modules/fortune'; import GuessingGameModule from './modules/guessing-game'; import KazutoriModule from './modules/kazutori'; @@ -57,6 +58,7 @@ promiseRetry(retry => { // 藍起動 new 藍(account, [ new EmojiModule(), + new EmojiReactModule(), new FortuneModule(), new GuessingGameModule(), new KazutoriModule(), diff --git a/src/misskey/note.ts b/src/misskey/note.ts new file mode 100644 index 0000000..0c1f718 --- /dev/null +++ b/src/misskey/note.ts @@ -0,0 +1,4 @@ +export type Note = { + id: string; + text: string | null; +}; diff --git a/src/modules/emoji-react/index.ts b/src/modules/emoji-react/index.ts new file mode 100644 index 0000000..8eba70a --- /dev/null +++ b/src/modules/emoji-react/index.ts @@ -0,0 +1,57 @@ +import autobind from 'autobind-decorator'; +const emojiRegex = require('emoji-regex'); + +import { Note } from '../../misskey/note'; +import Module from '../../module'; +import Stream from '../../stream'; + +export default class extends Module { + public readonly name = 'emoji-react'; + + private htl: ReturnType; + + @autobind + public install() { + this.htl = this.ai.connection.useSharedConnection('homeTimeline'); + this.htl.on('note', this.onNote); + + return {}; + } + + @autobind + private async onNote(note: Note) { + if (note.text == null) return; + + const customEmojis = note.text.match(/:([^\n:]+?):/g); + if (customEmojis) { + // カスタム絵文字が複数種類ある場合はキャンセル + if (!customEmojis.every((val, i, arr) => val === arr[0])) return; + + this.log(`Custom emoji detected - ${customEmojis[0]}`); + + setTimeout(() => { + this.ai.api('notes/reactions/create', { + noteId: note.id, + reaction: customEmojis[0] + }); + }, 2000); + return; + } + + const emojis = note.text.match(emojiRegex()); + if (emojis) { + // 絵文字が複数種類ある場合はキャンセル + if (!emojis.every((val, i, arr) => val === arr[0])) return; + + this.log(`Emoji detected - ${emojis[0]}`); + + setTimeout(() => { + this.ai.api('notes/reactions/create', { + noteId: note.id, + reaction: emojis[0] + }); + }, 2000); + return; + } + } +} diff --git a/tsconfig.json b/tsconfig.json index b070af7..07dbb1a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,6 +5,7 @@ "noImplicitReturns": true, "noImplicitThis": true, "noFallthroughCasesInSwitch": true, + "strictNullChecks": false, "experimentalDecorators": true, "sourceMap": false, "target": "es2017",