mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-20 05:51:35 +02:00
Seperate canvas util functions
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
const { Command } = require('discord.js-commando');
|
const { Command } = require('discord.js-commando');
|
||||||
const { createCanvas, loadImage } = require('canvas');
|
const { createCanvas, loadImage } = require('canvas');
|
||||||
const snekfetch = require('snekfetch');
|
const snekfetch = require('snekfetch');
|
||||||
const { distort } = require('../../util/Util');
|
const { distort } = require('../../util/Canvas');
|
||||||
|
|
||||||
module.exports = class DistortCommand extends Command {
|
module.exports = class DistortCommand extends Command {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
const { Command } = require('discord.js-commando');
|
const { Command } = require('discord.js-commando');
|
||||||
const { createCanvas, loadImage } = require('canvas');
|
const { createCanvas, loadImage } = require('canvas');
|
||||||
const snekfetch = require('snekfetch');
|
const snekfetch = require('snekfetch');
|
||||||
const { distort } = require('../../util/Util');
|
const { distort } = require('../../util/Canvas');
|
||||||
|
|
||||||
module.exports = class GlitchCommand extends Command {
|
module.exports = class GlitchCommand extends Command {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
const { Command } = require('discord.js-commando');
|
const { Command } = require('discord.js-commando');
|
||||||
const { createCanvas, loadImage } = require('canvas');
|
const { createCanvas, loadImage } = require('canvas');
|
||||||
const snekfetch = require('snekfetch');
|
const snekfetch = require('snekfetch');
|
||||||
const { greyscale } = require('../../util/Util');
|
const { greyscale } = require('../../util/Canvas');
|
||||||
|
|
||||||
module.exports = class GreyscaleCommand extends Command {
|
module.exports = class GreyscaleCommand extends Command {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
const { Command } = require('discord.js-commando');
|
const { Command } = require('discord.js-commando');
|
||||||
const { createCanvas, loadImage } = require('canvas');
|
const { createCanvas, loadImage } = require('canvas');
|
||||||
const snekfetch = require('snekfetch');
|
const snekfetch = require('snekfetch');
|
||||||
const { invert } = require('../../util/Util');
|
const { invert } = require('../../util/Canvas');
|
||||||
|
|
||||||
module.exports = class InvertCommand extends Command {
|
module.exports = class InvertCommand extends Command {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ const { Command } = require('discord.js-commando');
|
|||||||
const { createCanvas, loadImage } = require('canvas');
|
const { createCanvas, loadImage } = require('canvas');
|
||||||
const snekfetch = require('snekfetch');
|
const snekfetch = require('snekfetch');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const { greyscale } = require('../../util/Util');
|
const { greyscale } = require('../../util/Canvas');
|
||||||
|
|
||||||
module.exports = class RIPCommand extends Command {
|
module.exports = class RIPCommand extends Command {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
const { Command } = require('discord.js-commando');
|
const { Command } = require('discord.js-commando');
|
||||||
const { createCanvas, loadImage } = require('canvas');
|
const { createCanvas, loadImage } = require('canvas');
|
||||||
const snekfetch = require('snekfetch');
|
const snekfetch = require('snekfetch');
|
||||||
const { sepia } = require('../../util/Util');
|
const { sepia } = require('../../util/Canvas');
|
||||||
|
|
||||||
module.exports = class SepiaCommand extends Command {
|
module.exports = class SepiaCommand extends Command {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
const { Command } = require('discord.js-commando');
|
const { Command } = require('discord.js-commando');
|
||||||
const { createCanvas, loadImage } = require('canvas');
|
const { createCanvas, loadImage } = require('canvas');
|
||||||
const snekfetch = require('snekfetch');
|
const snekfetch = require('snekfetch');
|
||||||
const { silhouette } = require('../../util/Util');
|
const { silhouette } = require('../../util/Canvas');
|
||||||
|
|
||||||
module.exports = class SilhouetteCommand extends Command {
|
module.exports = class SilhouetteCommand extends Command {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ const { Command } = require('discord.js-commando');
|
|||||||
const { createCanvas, loadImage } = require('canvas');
|
const { createCanvas, loadImage } = require('canvas');
|
||||||
const snekfetch = require('snekfetch');
|
const snekfetch = require('snekfetch');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const { greyscale } = require('../../util/Util');
|
const { greyscale } = require('../../util/Canvas');
|
||||||
|
|
||||||
module.exports = class ThugLifeCommand extends Command {
|
module.exports = class ThugLifeCommand extends Command {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ const { Command } = require('discord.js-commando');
|
|||||||
const { createCanvas, loadImage } = require('canvas');
|
const { createCanvas, loadImage } = require('canvas');
|
||||||
const snekfetch = require('snekfetch');
|
const snekfetch = require('snekfetch');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const { drawImageWithTint } = require('../../util/Util');
|
const { drawImageWithTint } = require('../../util/Canvas');
|
||||||
|
|
||||||
module.exports = class TriggeredCommand extends Command {
|
module.exports = class TriggeredCommand extends Command {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ const { Command } = require('discord.js-commando');
|
|||||||
const { createCanvas, loadImage } = require('canvas');
|
const { createCanvas, loadImage } = require('canvas');
|
||||||
const snekfetch = require('snekfetch');
|
const snekfetch = require('snekfetch');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const { sepia } = require('../../util/Util');
|
const { sepia } = require('../../util/Canvas');
|
||||||
|
|
||||||
module.exports = class WantedCommand extends Command {
|
module.exports = class WantedCommand extends Command {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
const { Command } = require('discord.js-commando');
|
const { Command } = require('discord.js-commando');
|
||||||
const { createCanvas, loadImage } = require('canvas');
|
const { createCanvas, loadImage } = require('canvas');
|
||||||
const snekfetch = require('snekfetch');
|
const snekfetch = require('snekfetch');
|
||||||
const { filterPkmn, pad, silhouette } = require('../../util/Util');
|
const { filterPkmn, pad } = require('../../util/Util');
|
||||||
|
const { silhouette } = require('../../util/Canvas');
|
||||||
|
|
||||||
module.exports = class WhosThatPokemonCommand extends Command {
|
module.exports = class WhosThatPokemonCommand extends Command {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "xiaobot",
|
"name": "xiaobot",
|
||||||
"version": "51.4.1",
|
"version": "51.4.2",
|
||||||
"description": "Your personal server companion.",
|
"description": "Your personal server companion.",
|
||||||
"main": "XiaoBot.js",
|
"main": "XiaoBot.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
class CanvasUtil {
|
||||||
|
static greyscale(ctx, x, y, width, height) {
|
||||||
|
const data = ctx.getImageData(x, y, width, height);
|
||||||
|
for (let i = 0; i < data.data.length; i += 4) {
|
||||||
|
const brightness = (0.34 * data.data[i]) + (0.5 * data.data[i + 1]) + (0.16 * data.data[i + 2]);
|
||||||
|
data.data[i] = brightness;
|
||||||
|
data.data[i + 1] = brightness;
|
||||||
|
data.data[i + 2] = brightness;
|
||||||
|
}
|
||||||
|
ctx.putImageData(data, x, y);
|
||||||
|
return ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
static invert(ctx, x, y, width, height) {
|
||||||
|
const data = ctx.getImageData(x, y, width, height);
|
||||||
|
for (let i = 0; i < data.data.length; i += 4) {
|
||||||
|
data.data[i] = 255 - data.data[i];
|
||||||
|
data.data[i + 1] = 255 - data.data[i + 1];
|
||||||
|
data.data[i + 2] = 255 - data.data[i + 2];
|
||||||
|
}
|
||||||
|
ctx.putImageData(data, x, y);
|
||||||
|
return ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
static silhouette(ctx, x, y, width, height) {
|
||||||
|
const data = ctx.getImageData(x, y, width, height);
|
||||||
|
for (let i = 0; i < data.data.length; i += 4) {
|
||||||
|
data.data[i] = 0;
|
||||||
|
data.data[i + 1] = 0;
|
||||||
|
data.data[i + 2] = 0;
|
||||||
|
}
|
||||||
|
ctx.putImageData(data, x, y);
|
||||||
|
return ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
static sepia(ctx, x, y, width, height) {
|
||||||
|
const data = ctx.getImageData(x, y, width, height);
|
||||||
|
for (let i = 0; i < data.data.length; i += 4) {
|
||||||
|
const brightness = (0.34 * data.data[i]) + (0.5 * data.data[i + 1]) + (0.16 * data.data[i + 2]);
|
||||||
|
data.data[i] = brightness + 100;
|
||||||
|
data.data[i + 1] = brightness + 50;
|
||||||
|
data.data[i + 2] = brightness;
|
||||||
|
}
|
||||||
|
ctx.putImageData(data, x, y);
|
||||||
|
return ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
static distort(ctx, x, y, width, height, amplitude, strideLevel = 4) {
|
||||||
|
const data = ctx.getImageData(x, y, width, height);
|
||||||
|
const temp = ctx.getImageData(x, y, width, height);
|
||||||
|
const stride = width * strideLevel;
|
||||||
|
for (let i = 0; i < width; i++) {
|
||||||
|
for (let j = 0; j < height; j++) {
|
||||||
|
const xs = Math.round(amplitude * Math.sin(2 * Math.PI * 3 * (j / height)));
|
||||||
|
const ys = Math.round(amplitude * Math.cos(2 * Math.PI * 3 * (i / width)));
|
||||||
|
const dest = (j * stride) + (i * strideLevel);
|
||||||
|
const src = ((j + ys) * stride) + ((i + xs) * strideLevel);
|
||||||
|
data.data[dest] = temp.data[src];
|
||||||
|
data.data[dest + 1] = temp.data[src + 1];
|
||||||
|
data.data[dest + 2] = temp.data[src + 2];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ctx.putImageData(data, x, y);
|
||||||
|
return ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
static drawImageWithTint(ctx, image, color, x, y, width, height) {
|
||||||
|
ctx.globalAlpha = 0.5;
|
||||||
|
ctx.fillStyle = color;
|
||||||
|
ctx.fillRect(x, y, width, height);
|
||||||
|
ctx.drawImage(image, x, y, width, height);
|
||||||
|
ctx.globalAlpha = 1;
|
||||||
|
ctx.fillStyle = '#000000';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = CanvasUtil;
|
||||||
@@ -82,80 +82,6 @@ class Util {
|
|||||||
.replace(/\[i\]|\[\/i\]/g, '*');
|
.replace(/\[i\]|\[\/i\]/g, '*');
|
||||||
}
|
}
|
||||||
|
|
||||||
static greyscale(ctx, x, y, width, height) {
|
|
||||||
const data = ctx.getImageData(x, y, width, height);
|
|
||||||
for (let i = 0; i < data.data.length; i += 4) {
|
|
||||||
const brightness = (0.34 * data.data[i]) + (0.5 * data.data[i + 1]) + (0.16 * data.data[i + 2]);
|
|
||||||
data.data[i] = brightness;
|
|
||||||
data.data[i + 1] = brightness;
|
|
||||||
data.data[i + 2] = brightness;
|
|
||||||
}
|
|
||||||
ctx.putImageData(data, x, y);
|
|
||||||
return ctx;
|
|
||||||
}
|
|
||||||
|
|
||||||
static invert(ctx, x, y, width, height) {
|
|
||||||
const data = ctx.getImageData(x, y, width, height);
|
|
||||||
for (let i = 0; i < data.data.length; i += 4) {
|
|
||||||
data.data[i] = 255 - data.data[i];
|
|
||||||
data.data[i + 1] = 255 - data.data[i + 1];
|
|
||||||
data.data[i + 2] = 255 - data.data[i + 2];
|
|
||||||
}
|
|
||||||
ctx.putImageData(data, x, y);
|
|
||||||
return ctx;
|
|
||||||
}
|
|
||||||
|
|
||||||
static silhouette(ctx, x, y, width, height) {
|
|
||||||
const data = ctx.getImageData(x, y, width, height);
|
|
||||||
for (let i = 0; i < data.data.length; i += 4) {
|
|
||||||
data.data[i] = 0;
|
|
||||||
data.data[i + 1] = 0;
|
|
||||||
data.data[i + 2] = 0;
|
|
||||||
}
|
|
||||||
ctx.putImageData(data, x, y);
|
|
||||||
return ctx;
|
|
||||||
}
|
|
||||||
|
|
||||||
static sepia(ctx, x, y, width, height) {
|
|
||||||
const data = ctx.getImageData(x, y, width, height);
|
|
||||||
for (let i = 0; i < data.data.length; i += 4) {
|
|
||||||
const brightness = (0.34 * data.data[i]) + (0.5 * data.data[i + 1]) + (0.16 * data.data[i + 2]);
|
|
||||||
data.data[i] = brightness + 100;
|
|
||||||
data.data[i + 1] = brightness + 50;
|
|
||||||
data.data[i + 2] = brightness;
|
|
||||||
}
|
|
||||||
ctx.putImageData(data, x, y);
|
|
||||||
return ctx;
|
|
||||||
}
|
|
||||||
|
|
||||||
static distort(ctx, x, y, width, height, amplitude, strideLevel = 4) {
|
|
||||||
const data = ctx.getImageData(x, y, width, height);
|
|
||||||
const temp = ctx.getImageData(x, y, width, height);
|
|
||||||
const stride = width * strideLevel;
|
|
||||||
for (let i = 0; i < width; i++) {
|
|
||||||
for (let j = 0; j < height; j++) {
|
|
||||||
const xs = Math.round(amplitude * Math.sin(2 * Math.PI * 3 * (j / height)));
|
|
||||||
const ys = Math.round(amplitude * Math.cos(2 * Math.PI * 3 * (i / width)));
|
|
||||||
const dest = (j * stride) + (i * strideLevel);
|
|
||||||
const src = ((j + ys) * stride) + ((i + xs) * strideLevel);
|
|
||||||
data.data[dest] = temp.data[src];
|
|
||||||
data.data[dest + 1] = temp.data[src + 1];
|
|
||||||
data.data[dest + 2] = temp.data[src + 2];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ctx.putImageData(data, x, y);
|
|
||||||
return ctx;
|
|
||||||
}
|
|
||||||
|
|
||||||
static drawImageWithTint(ctx, image, color, x, y, width, height) {
|
|
||||||
ctx.globalAlpha = 0.5;
|
|
||||||
ctx.fillStyle = color;
|
|
||||||
ctx.fillRect(x, y, width, height);
|
|
||||||
ctx.drawImage(image, x, y, width, height);
|
|
||||||
ctx.globalAlpha = 1;
|
|
||||||
ctx.fillStyle = '#000000';
|
|
||||||
}
|
|
||||||
|
|
||||||
static async verify(channel, user, time = 30000) {
|
static async verify(channel, user, time = 30000) {
|
||||||
const filter = res => {
|
const filter = res => {
|
||||||
const value = res.content.toLowerCase();
|
const value = res.content.toLowerCase();
|
||||||
|
|||||||
Reference in New Issue
Block a user