From 465b4312dc7dcc461cc024979ca78adfb72ad823 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=BE=E3=81=A3=E3=81=A1=E3=82=83=E3=81=A8=E3=83=BC?= =?UTF-8?q?=E3=81=AB=E3=82=85?= <17376330+u1-liquid@users.noreply.github.com> Date: Thu, 25 Jan 2024 05:22:43 +0900 Subject: [PATCH] :v: (#131) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(process): 何か失敗してもクラッシュしないように * fix(upload): ファイルのアップロードができない問題を修正 * fix(docker): ビルドできるように --- Dockerfile | 6 +++--- package.json | 2 +- src/ai.ts | 18 ++++++++---------- src/friend.ts | 2 +- src/index.ts | 8 ++++++++ src/message.ts | 2 +- src/modules/emoji-react/index.ts | 4 ++-- src/modules/kazutori/index.ts | 2 +- src/modules/poll/index.ts | 2 +- src/modules/reversi/back.ts | 4 ++-- 10 files changed, 28 insertions(+), 22 deletions(-) diff --git a/Dockerfile b/Dockerfile index bed4350..5a5fcf1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ -FROM node:lts-bullseye +FROM node:lts -RUN apt-get update && apt-get install -y tini +RUN apt-get update && apt-get install tini --no-install-recommends -y && apt-get clean && rm -rf /var/lib/apt-get/lists/* ARG enable_mecab=1 @@ -19,7 +19,7 @@ RUN if [ $enable_mecab -ne 0 ]; then apt-get update \ COPY . /ai WORKDIR /ai -RUN npm install && npm run build +RUN npm install && npm run build || test -f ./built/index.js ENTRYPOINT ["/usr/bin/tini", "--"] CMD npm start diff --git a/package.json b/package.json index 1d79894..21025da 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "@types/ws": "8.5.10", "canvas": "2.11.2", "chalk": "5.3.0", + "formdata-node": "6.0.3", "got": "14.0.0", "lokijs": "1.5.12", "memory-streams": "0.1.3", @@ -28,7 +29,6 @@ "promise-retry": "2.0.1", "random-seed": "0.3.0", "reconnecting-websocket": "4.4.0", - "request": "2.88.2", "seedrandom": "3.0.5", "ts-patch": "3.1.2", "twemoji-parser": "14.0.0", diff --git a/src/ai.ts b/src/ai.ts index c44bfba..701b97e 100644 --- a/src/ai.ts +++ b/src/ai.ts @@ -4,6 +4,7 @@ import * as fs from 'fs'; import { bindThis } from '@/decorators.js'; import loki from 'lokijs'; import got from 'got'; +import { FormData, File } from 'formdata-node'; import chalk from 'chalk'; import { v4 as uuid } from 'uuid'; @@ -11,7 +12,7 @@ import config from '@/config.js'; import Module from '@/module.js'; import Message from '@/message.js'; import Friend, { FriendDoc } from '@/friend.js'; -import { User } from '@/misskey/user.js'; +import type { User } from '@/misskey/user.js'; import Stream from '@/stream.js'; import log from '@/utils/log.js'; import { sleep } from './utils/sleep.js'; @@ -343,17 +344,14 @@ export default class 藍 { * ファイルをドライブにアップロードします */ @bindThis - public async upload(file: Buffer | fs.ReadStream, meta: any) { + public async upload(file: Buffer | fs.ReadStream, meta: { filename: string, contentType: string }) { + const form = new FormData(); + form.set('i', config.i); + form.set('file', new File([file], meta.filename, { type: meta.contentType })); + const res = await got.post({ url: `${config.apiUrl}/drive/files/create`, - formData: { - i: config.i, - file: { - value: file, - options: meta - } - }, - json: true + body: form }).json(); return res; } diff --git a/src/friend.ts b/src/friend.ts index 1ac199e..5fe2c3e 100644 --- a/src/friend.ts +++ b/src/friend.ts @@ -2,7 +2,7 @@ import { bindThis } from '@/decorators.js'; import 藍 from '@/ai.js'; import IModule from '@/module.js'; import getDate from '@/utils/get-date.js'; -import { User } from '@/misskey/user.js'; +import type { User } from '@/misskey/user.js'; import { genItem } from '@/vocabulary.js'; export type FriendDoc = { diff --git a/src/index.ts b/src/index.ts index e94b9a3..2f85f93 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ // AiOS bootstrapper +import process from 'node:process'; import chalk from 'chalk'; import got from 'got'; import promiseRetry from 'promise-retry'; @@ -45,6 +46,13 @@ function log(msg: string): void { log(chalk.bold(`Ai v${pkg._v}`)); +process.on('uncaughtException', err => { + try { + console.error(`Uncaught exception: ${err.message}`); + console.dir(err, { colors: true, depth: 2 }); + } catch { } +}); + promiseRetry(retry => { log(`Account fetching... ${chalk.gray(config.host)}`); diff --git a/src/message.ts b/src/message.ts index c662e8a..104ca18 100644 --- a/src/message.ts +++ b/src/message.ts @@ -3,7 +3,7 @@ import chalk from 'chalk'; import 藍 from '@/ai.js'; import Friend from '@/friend.js'; -import { User } from '@/misskey/user.js'; +import type { User } from '@/misskey/user.js'; import includes from '@/utils/includes.js'; import or from '@/utils/or.js'; import config from '@/config.js'; diff --git a/src/modules/emoji-react/index.ts b/src/modules/emoji-react/index.ts index 145ec65..b131fa1 100644 --- a/src/modules/emoji-react/index.ts +++ b/src/modules/emoji-react/index.ts @@ -1,11 +1,11 @@ import { bindThis } from '@/decorators.js'; import { parse } from 'twemoji-parser'; -import { Note } from '@/misskey/note.js'; +import type { Note } from '@/misskey/note.js'; import Module from '@/module.js'; import Stream from '@/stream.js'; import includes from '@/utils/includes.js'; -import { sleep } from '../../utils/sleep.js'; +import { sleep } from '@/utils/sleep.js'; export default class extends Module { public readonly name = 'emoji-react'; diff --git a/src/modules/kazutori/index.ts b/src/modules/kazutori/index.ts index a5a3caa..0df52a2 100644 --- a/src/modules/kazutori/index.ts +++ b/src/modules/kazutori/index.ts @@ -3,7 +3,7 @@ import loki from 'lokijs'; import Module from '@/module.js'; import Message from '@/message.js'; import serifs from '@/serifs.js'; -import { User } from '@/misskey/user.js'; +import type { User } from '@/misskey/user.js'; import { acct } from '@/utils/acct.js'; type Game = { diff --git a/src/modules/poll/index.ts b/src/modules/poll/index.ts index c3bbe6e..2af3cc0 100644 --- a/src/modules/poll/index.ts +++ b/src/modules/poll/index.ts @@ -4,7 +4,7 @@ import Module from '@/module.js'; import serifs from '@/serifs.js'; import { genItem } from '@/vocabulary.js'; import config from '@/config.js'; -import { Note } from '@/misskey/note.js'; +import type { Note } from '@/misskey/note.js'; export default class extends Module { public readonly name = 'poll'; diff --git a/src/modules/reversi/back.ts b/src/modules/reversi/back.ts index 0f0cd6d..d3769af 100644 --- a/src/modules/reversi/back.ts +++ b/src/modules/reversi/back.ts @@ -10,7 +10,7 @@ import got from 'got'; import * as Reversi from './engine.js'; import config from '@/config.js'; import serifs from '@/serifs.js'; -import { User } from '@/misskey/user.js'; +import type { User } from '@/misskey/user.js'; function getUserName(user) { return user.name || user.username; @@ -263,7 +263,7 @@ class Session { } break; } - + default: break; }