manual maze request

This commit is contained in:
syuilo 2019-05-11 12:05:04 +09:00
parent 5ac502d401
commit ace5bf8f8f
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69
4 changed files with 66 additions and 15 deletions

View file

@ -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 @autobind
public includes(words: string[]): boolean { public includes(words: string[]): boolean {
return includes(this.text, words); return includes(this.text, words);

View file

@ -5,6 +5,7 @@ import serifs from '../../serifs';
import * as tmp from 'tmp'; import * as tmp from 'tmp';
import { genMaze } from './gen-maze'; import { genMaze } from './gen-maze';
import { renderMaze } from './render-maze'; import { renderMaze } from './render-maze';
import Message from '../../message';
export default class extends Module { export default class extends Module {
public readonly name = 'maze'; public readonly name = 'maze';
@ -14,7 +15,9 @@ export default class extends Module {
this.post(); this.post();
setInterval(this.post, 1000 * 60 * 3); setInterval(this.post, 1000 * 60 * 3);
return {}; return {
mentionHook: this.mentionHook
};
} }
@autobind @autobind
@ -27,17 +30,8 @@ export default class extends Module {
data.lastPosted = date; data.lastPosted = date;
this.setData(data); this.setData(data);
const seed = date; this.log('Time to maze');
const file = await this.genMazeFile(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('Posting...'); this.log('Posting...');
this.ai.post({ this.ai.post({
@ -55,4 +49,36 @@ export default class extends Module {
}); });
}); });
} }
@autobind
private async genMazeFile(seed): Promise<any> {
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;
}
}
} }

View file

@ -6,8 +6,8 @@ import * as Line from 'pureimage/src/Line.js';
import { CellType } from './maze'; import { CellType } from './maze';
import { themes } from './themes'; import { themes } from './themes';
const imageSize = 2048; // px const imageSize = 1024; // px
const margin = 192; const margin = 96;
const mazeAreaSize = imageSize - (margin * 2); const mazeAreaSize = imageSize - (margin * 2);
export function renderMaze(seed, maze: CellType[][], stream: fs.WriteStream): Promise<void> { export function renderMaze(seed, maze: CellType[][], stream: fs.WriteStream): Promise<void> {

View file

@ -313,7 +313,8 @@ export default {
}, },
maze: { maze: {
post: '今日の迷路です! #AiMaze' post: '今日の迷路です! #AiMaze',
foryou: '描きました!'
}, },
}; };