diff --git a/package.json b/package.json index 071a63b..c7ba8f1 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,6 @@ "memory-streams": "0.1.3", "misskey-reversi": "0.0.5", "module-alias": "2.2.2", - "node-fetch": "2.6.7", "promise-retry": "2.0.1", "random-seed": "0.3.0", "reconnecting-websocket": "4.4.0", diff --git a/src/modules/summonCat/index.ts b/src/modules/summonCat/index.ts index d29c702..426441a 100644 --- a/src/modules/summonCat/index.ts +++ b/src/modules/summonCat/index.ts @@ -1,7 +1,13 @@ import autobind from 'autobind-decorator'; import Module from '@/module'; import Message from '@/message'; -import fetch from 'node-fetch'; + +type theCatApiReturnType = Readonly<{ + id: string, + url: string, + width: number, + height: number +}> export default class extends Module { public readonly name = 'summonCat'; @@ -20,10 +26,16 @@ export default class extends Module { if (msg.text && (msg.text.match(/(cat|Cat|ねこ|ネコ|にゃ[〜|ー]*ん)/g))) { const message = 'にゃ~ん!'; + try { const file = await this.getCatImage(); - this.log(file); - this.log('Replying...'); - msg.reply(message, {file}); + // this.log(file); + this.log('Replying...'); + msg.reply(message, {file}); + } catch (e) { + console.log(e); + msg.reply('にゃ~ん?'); + } + return { reaction: ':blobcatmeltnomblobcatmelt:', @@ -34,21 +46,15 @@ export default class extends Module { } @autobind - private async getCatImage(): Promise { - // https://aws.random.cat/meowにGETリクエストを送る - // fileに画像URLが返ってくる + private async getCatImage() { + console.warn('attempt'); const res = await fetch('https://api.thecatapi.com/v1/images/search'); - const json = await res.json() as any; - console.table(json); - const fileUri = json[0].url; - // 拡張子を取り除く - 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}`, + const theCatApi: theCatApiReturnType = (await res.json())[0]; + const rawFile = await fetch(theCatApi.url); + const ext = theCatApi.url.split('.').pop(); + const buffer = await rawFile.arrayBuffer(); + const file = await this.ai.upload(Buffer.from(buffer), { + filename: `${theCatApi.id}.${ext}`, }); return file; } diff --git a/yarn.lock b/yarn.lock index be68fbf..6ad8bfd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3461,7 +3461,7 @@ next-tick@^1.1.0: resolved "https://registry.yarnpkg.com/next-tick/-/next-tick-1.1.0.tgz#1836ee30ad56d67ef281b22bd199f709449b35eb" integrity sha512-CXdUiJembsNjuToQvxayPZF9Vqht7hewsvy2sOWafLvi2awflj9mOC6bHIg50orX8IJvWKY9wYQ/zB2kogPslQ== -node-fetch@2.6.7, node-fetch@^2.6.7: +node-fetch@^2.6.7: version "2.6.7" resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad" integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==