From ace5bf8f8f42df1fe3a41be3d41bf5a2bd3c4297 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 11 May 2019 12:05:04 +0900 Subject: [PATCH] manual maze request --- src/message.ts | 24 ++++++++++++++++ src/modules/maze/index.ts | 50 +++++++++++++++++++++++++-------- src/modules/maze/render-maze.ts | 4 +-- src/serifs.ts | 3 +- 4 files changed, 66 insertions(+), 15 deletions(-) diff --git a/src/message.ts b/src/message.ts index ca0859b..cc1f900 100644 --- a/src/message.ts +++ b/src/message.ts @@ -80,6 +80,30 @@ export default class Message { } } + @autobind + public async replyWithFile(text: string, file: any, cw?: string, renote?: string) { + if (text == null) return; + + this.ai.log(`>>> Sending reply to ${chalk.underline(this.id)}`); + + await delay(2000); + + if (this.isDm) { + return await this.ai.sendMessage(this.messageOrNote.userId, { + text: text, + fileId: file.id + }); + } else { + return await this.ai.post({ + replyId: this.messageOrNote.id, + text: text, + fileIds: [file.id], + cw: cw, + renoteId: renote + }); + } + } + @autobind public includes(words: string[]): boolean { return includes(this.text, words); diff --git a/src/modules/maze/index.ts b/src/modules/maze/index.ts index 2f343c6..db7e978 100644 --- a/src/modules/maze/index.ts +++ b/src/modules/maze/index.ts @@ -5,6 +5,7 @@ import serifs from '../../serifs'; import * as tmp from 'tmp'; import { genMaze } from './gen-maze'; import { renderMaze } from './render-maze'; +import Message from '../../message'; export default class extends Module { public readonly name = 'maze'; @@ -14,7 +15,9 @@ export default class extends Module { this.post(); setInterval(this.post, 1000 * 60 * 3); - return {}; + return { + mentionHook: this.mentionHook + }; } @autobind @@ -27,17 +30,8 @@ export default class extends Module { data.lastPosted = date; this.setData(data); - const seed = date; - - this.log('Maze generating...'); - const maze = genMaze(seed); - - this.log('Maze rendering...'); - const [temp] = await this.createTemp(); - await renderMaze(seed, maze, fs.createWriteStream(temp)); - - this.log('Image uploading...'); - const file = await this.ai.upload(fs.createReadStream(temp)); + this.log('Time to maze'); + const file = await this.genMazeFile(date); this.log('Posting...'); this.ai.post({ @@ -55,4 +49,36 @@ export default class extends Module { }); }); } + + @autobind + private async genMazeFile(seed): Promise { + this.log('Maze generating...'); + const maze = genMaze(seed); + + this.log('Maze rendering...'); + const [temp] = await this.createTemp(); + await renderMaze(seed, maze, fs.createWriteStream(temp)); + + this.log('Image uploading...'); + const file = await this.ai.upload(fs.createReadStream(temp)); + + return file; + } + + @autobind + private async mentionHook(msg: Message) { + if (msg.includes(['迷路'])) { + this.log('Maze requested'); + setTimeout(async () => { + const file = await this.genMazeFile(Date.now()); + this.log('Replying...'); + msg.replyWithFile(serifs.maze.foryou, file); + }, 3000); + return { + reaction: 'like' + }; + } else { + return false; + } + } } diff --git a/src/modules/maze/render-maze.ts b/src/modules/maze/render-maze.ts index b44453d..ca4392a 100644 --- a/src/modules/maze/render-maze.ts +++ b/src/modules/maze/render-maze.ts @@ -6,8 +6,8 @@ import * as Line from 'pureimage/src/Line.js'; import { CellType } from './maze'; import { themes } from './themes'; -const imageSize = 2048; // px -const margin = 192; +const imageSize = 1024; // px +const margin = 96; const mazeAreaSize = imageSize - (margin * 2); export function renderMaze(seed, maze: CellType[][], stream: fs.WriteStream): Promise { diff --git a/src/serifs.ts b/src/serifs.ts index 92fd508..b430724 100644 --- a/src/serifs.ts +++ b/src/serifs.ts @@ -313,7 +313,8 @@ export default { }, maze: { - post: '今日の迷路です! #AiMaze' + post: '今日の迷路です! #AiMaze', + foryou: '描きました!' }, };