From c74c709ae9cb83fd797fa2ed2492290cce497cb4 Mon Sep 17 00:00:00 2001 From: na2na <49822810+na2na-p@users.noreply.github.com> Date: Fri, 25 Feb 2022 14:13:10 +0900 Subject: [PATCH] =?UTF-8?q?=E3=81=AD=E3=81=93=E5=8F=AC=E5=96=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 2 ++ src/modules/emoji-react/index.ts | 8 ++--- src/modules/summonCat/index.ts | 58 ++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 src/modules/summonCat/index.ts diff --git a/src/index.ts b/src/index.ts index 3a5f0de..353db8b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,6 +15,7 @@ import CoreModule from './modules/core'; import TalkModule from './modules/talk'; import BirthdayModule from './modules/birthday'; import ReversiModule from './modules/reversi'; +import summonCat from './modules/summonCat'; import PingModule from './modules/ping'; import EmojiModule from './modules/emoji'; import EmojiReactModule from './modules/emoji-react'; @@ -69,6 +70,7 @@ promiseRetry(retry => { // 藍起動 new 藍(account, [ new CoreModule(), + new summonCat(), new EmojiModule(), new EmojiReactModule(), new FortuneModule(), diff --git a/src/modules/emoji-react/index.ts b/src/modules/emoji-react/index.ts index 7e037a8..9a21677 100644 --- a/src/modules/emoji-react/index.ts +++ b/src/modules/emoji-react/index.ts @@ -38,19 +38,19 @@ export default class extends Module { // /う[〜|ー]*んこ/g]にマッチしたときの処理 if (note.text.match(/う[〜|ー]*んこ/g) || includes(note.text, ['unko'])) { - await react(':anataima_unkotte_iimashitane:', true); + await react(':anataima_unkotte_iimashitane:'); } if (note.text.match(/う[〜|ー]*んち/g)) { - await react(':erait:', true); + await react(':erait:'); } if (includes(note.text, ['いい']) && (includes(note.text, ["?"]) || includes(note.text, ["?"]))) { // 50%の確率で":dame:"または":yattare:"を返す if (Math.random() < 0.5) { - return react(':dame:', true); + return react(':dame:'); } else { - return react(':yattare:', true); + return react(':yattare:'); } } diff --git a/src/modules/summonCat/index.ts b/src/modules/summonCat/index.ts new file mode 100644 index 0000000..9ffce4d --- /dev/null +++ b/src/modules/summonCat/index.ts @@ -0,0 +1,58 @@ +import autobind from 'autobind-decorator'; +import Module from '@/module'; +import Message from '@/message'; +import fetch from 'node-fetch'; +import { ReadStream } from 'fs'; + +export default class extends Module { + public readonly name = 'summonCat'; + + @autobind + public install() { + return { + mentionHook: this.mentionHook + }; + } + + @autobind + private async mentionHook(msg: Message) { + // cat/Cat/ねこ/ネコ/にゃん + console.log(msg.text) + if (msg.text && (msg.text.match(/(cat|Cat|ねこ|ネコ|にゃ[〜|ー]*ん)/g))) { + const message = "にゃ~ん!"; + + setTimeout(async () => { + const file = await this.getCatImage(); + this.log(file); + this.log('Replying...'); + msg.reply(message, { file }); + }, 500); + + return { + reaction: ':blobcatmeltnomblobcatmelt:' + }; + } else { + return false; + } + } + + @autobind + private async getCatImage(): Promise { + // https://aws.random.cat/meowにGETリクエストを送る + // fileに画像URLが返ってくる + const res = await fetch('https://aws.random.cat/meow'); + const json = await res.json(); + console.table(json); + const fileUri = json.file; + // 拡張子を取り除く + const fileName = fileUri.split('/').pop().split('.')[0]; + const rawFile = await fetch(fileUri); + const imgBuffer = await rawFile.buffer(); + // 拡張子とcontentTypeを判断する + const ext = fileUri.split('.').pop(); + const file = await this.ai.upload(imgBuffer, { + filename: `${fileName}.${ext}`, + }); + return file; + } +}