ship.ts (4222B)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | import { createHash } from 'crypto';
import { FORMATS } from '../extensions';
import { help_info } from '../utils';
import { MessageAttachment, MessageEmbed } from 'discord.js';
import Jimp from 'jimp';
const TEMPLATE = "./lib/resources/templates/ship.png";
const RESPONSES = [
{ range: [0, 9], message: "Fate has decided you two aren't made for each other. :fearful:" },
{ range: [9, 24], message: "You should never cross paths, ever. :no_good:" },
{ range: [24, 49], message: "Not good. :confounded: :broken_heart:" },
{ range: [49, 59], message: "Nothing is impossible, but... :slight_frown:" },
{ range: [59, 64], message: "Friends, but maybe... :smirk:" },
{ range: [64, 69], message: "We have some chemistry going on here... :heart_eyes:" },
{ range: [69, 74], message: "A match for sure! :relaxed:" },
{ range: [74, 79], message: "I approve this couple! :kissing_heart:" },
{ range: [79, 84], message: "They like each other very much!! :blush:" },
{ range: [84, 89], message: "If both aren't already dating I'd be surprised! :kissing_closed_eyes:" },
{ range: [89, 93], message: "Both are made for each other! :relaxed:" },
{ range: [93, 97], message: "They deeply love each other! :blush:" },
{ range: [97, 100], message: "Lovey-dovey couple!! :kissing_heart: :heart: :two_hearts:" },
];
const BIG_ZERO = BigInt(0);
const read512bitsBigIntBigEndian = (buffer : Buffer) : bigint => {
let val = BIG_ZERO;
for (let i = 512 + 64; i -= 64;)
val += buffer.readBigUInt64BE(i / 8 - 8) * BigInt(Math.pow(2, i));
return val;
};
export default (homescope : HomeScope) => {
const { message, args, CONFIG } = homescope;
if (args.length === 0 || args[0] === 'help'
|| message.mentions.users.size === 0
|| message.mentions.users.size > 2) {
message.channel.send(help_info('ship', CONFIG.commands.prefix));
return;
}
const users = [message.mentions.users.size === 1
? message.author
: message.mentions.users.first(), message.mentions.users.last()];
const user_avatars = {
first: users[0].avatarURL({ 'format': 'png' }),
second: users[1].avatarURL({ 'format': 'png' })
};
const in_range = ([min, max]: number[], num: number) =>
num >= min && num < max;
const get_response = (num: number) => RESPONSES
.filter(res => in_range(res.range, num))[0]?.message
|| RESPONSES[0].message;
const get_percentage = (n: number) => {
const bar = {
size: 10,
knob: '█',
empty: '.',
get filling(): number {
return Math.round(n / this.size);
},
get space(): number {
return Math.abs(this.filling - this.size);
}
}!;
const percentage = `${n.toString().format(FORMATS.bold)}%`;
const progress_bar =
(`[${ bar.knob.repeat(bar.filling) }` +
`${ bar.empty.repeat(bar.space) }]`)
.format(FORMATS.block);
return `${percentage} ${progress_bar}`;
};
const die = Number(read512bitsBigIntBigEndian(createHash('sha512')
.update(users.reduce((a, c) => a + BigInt(c.id), BigInt(0))
.toString()).digest()) % BigInt(101)); // ^ < 80 cols
const response = `${get_percentage(die)} ${get_response(die)}`;
const error_msg = (e: Error) =>
message.reply("Unable to calculate the love grade :("
+ `:\n${e.message}`.format('```'));
const compose_images = async ({ first, second }) => {
const padding = 4;
const size = 128;
const filename = "ship-result.png";
const template = await Jimp.read(TEMPLATE);
const fst = await Jimp.read(first);
const composed = template.blit(fst.resize(Jimp.AUTO, size), 0, 0);
const snd = await Jimp.read(second);
const image = await composed.blit(
snd.resize(Jimp.AUTO, size),
snd.bitmap.width * 2 + padding, 0);
image.getBuffer('image/png', (e : Error, buffer : Buffer) => {
if (e && e.message)
return error_msg(e);
const attachment = new MessageAttachment(buffer, filename);
const embed = new MessageEmbed()
.setColor('#b943e8')
.setTitle(`Love grade between \
${users[0].username} & \
${users[1].username}`.squeeze())
.setDescription(response)
.attachFiles([attachment])
.setImage(`attachment://${filename}`);
message.channel.send(embed);
});
};
try {
compose_images(user_avatars);
} catch (e) { error_msg(e); }
};
|