diff --git a/src/modules/color/index.ts b/src/modules/color/index.ts index ecd8904..a7e13be 100644 --- a/src/modules/color/index.ts +++ b/src/modules/color/index.ts @@ -1,6 +1,7 @@ import autobind from 'autobind-decorator'; import Module from '@/module'; import Message from '@/message'; +import { generateColorSample } from './render'; export default class extends Module { public readonly name = 'color'; @@ -21,13 +22,31 @@ export default class extends Module { const b = Math.floor(Math.random() * 256); // rgbをhexに変換する const hex = `${r.toString(16)}${g.toString(16)}${b.toString(16)}`; - const message = `RGB: ${r}, ${g}, ${b} (#${hex})とかどう? [参考](https://www.colorhexa.com/${hex})` - msg.reply(message, { - immediate: true - }); - return true; + const message = `RGB: ${r}, ${g}, ${b} (# ${hex})とかどう?` + + setTimeout(async () => { + const file = await this.getColorSampleFile(r,g,b); + this.log('Replying...'); + msg.reply(message, { file }); + }, 500); + return { + reaction: 'like' + }; } else { return false; } } + + @autobind + private async getColorSampleFile(r,g,b): Promise { + const colorSample = generateColorSample(r,g,b); + + this.log('Image uploading...'); + const file = await this.ai.upload(colorSample, { + filename: 'color.png', + contentType: 'image/png' + }); + + return file; + } } diff --git a/src/modules/color/render.ts b/src/modules/color/render.ts new file mode 100644 index 0000000..05aa6ca --- /dev/null +++ b/src/modules/color/render.ts @@ -0,0 +1,17 @@ +import { createCanvas } from 'canvas'; + +const imageSize = 512; //px + +export function generateColorSample(r: string, g: string, b: string) { + const canvas = createCanvas(imageSize, imageSize); + const ctx = canvas.getContext('2d'); + ctx.antialias = 'none'; + + // 引数で渡されたrgb値を基準に、色を塗りつぶす + ctx.fillStyle = `rgb(${r},${g},${b})`; + ctx.beginPath(); + ctx.fillRect(0, 0, imageSize, imageSize); + + // canvas.toBuffer()をreturn + return canvas.toBuffer(); +} \ No newline at end of file