Move some functions to Util, Bug Fixes, new stuff

This commit is contained in:
Dragon Fire
2020-03-13 22:59:22 -04:00
parent bf7579c4ac
commit 0edac3e86d
14 changed files with 99 additions and 92 deletions
+2 -30
View File
@@ -3,6 +3,7 @@ const { createCanvas, loadImage } = require('canvas');
const GIFEncoder = require('gifencoder');
const request = require('node-superfetch');
const path = require('path');
const { streamToArray } = require('../../util/Util');
const { drawImageWithTint } = require('../../util/Canvas');
const coord1 = [-25, -33, -42, -14];
const coord2 = [-25, -13, -34, -10];
@@ -62,39 +63,10 @@ module.exports = class TriggeredCommand extends Command {
encoder.addFrame(ctx);
}
encoder.finish();
const buffer = await this.streamToArray(stream);
const buffer = await streamToArray(stream);
return msg.say({ files: [{ attachment: Buffer.concat(buffer), name: 'triggered.gif' }] });
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
streamToArray(stream) {
if (!stream.readable) return Promise.resolve([]);
return new Promise((resolve, reject) => {
const array = [];
function onData(data) {
array.push(data);
}
function onEnd(error) {
if (error) reject(error);
else resolve(array);
cleanup();
}
function onClose() {
resolve(array);
cleanup();
}
function cleanup() {
stream.removeListener('data', onData);
stream.removeListener('end', onEnd);
stream.removeListener('error', onEnd);
stream.removeListener('close', onClose);
}
stream.on('data', onData);
stream.on('end', onEnd);
stream.on('error', onEnd);
stream.on('close', onClose);
});
}
};
+2 -16
View File
@@ -2,6 +2,7 @@ const Command = require('../../structures/Command');
const { createCanvas, loadImage } = require('canvas');
const request = require('node-superfetch');
const path = require('path');
const { centerImage } = require('../../util/Canvas');
module.exports = class ApprovedCommand extends Command {
constructor(client) {
@@ -43,22 +44,7 @@ module.exports = class ApprovedCommand extends Command {
const canvas = createCanvas(data.width, data.height);
const ctx = canvas.getContext('2d');
ctx.drawImage(data, 0, 0);
const dataRatio = data.width / data.height;
const baseRatio = base.width / base.height;
let { width, height } = data;
let x = 0;
let y = 0;
if (baseRatio < dataRatio) {
height = data.height;
width = base.width * (height / base.height);
x = (data.width - width) / 2;
y = 0;
} else if (baseRatio > dataRatio) {
width = data.width;
height = base.height * (width / base.width);
x = 0;
y = (data.height - height) / 2;
}
const { x, y, width, height } = centerImage(base, data);
ctx.drawImage(base, x, y, width, height);
const attachment = canvas.toBuffer();
if (Buffer.byteLength(attachment) > 8e+6) return msg.reply('Resulting image was above 8 MB.');
+2 -16
View File
@@ -2,6 +2,7 @@ const Command = require('../../structures/Command');
const { createCanvas, loadImage } = require('canvas');
const request = require('node-superfetch');
const path = require('path');
const { centerImage } = require('../../util/Canvas');
module.exports = class RejctedCommand extends Command {
constructor(client) {
@@ -43,22 +44,7 @@ module.exports = class RejctedCommand extends Command {
const canvas = createCanvas(data.width, data.height);
const ctx = canvas.getContext('2d');
ctx.drawImage(data, 0, 0);
const dataRatio = data.width / data.height;
const baseRatio = base.width / base.height;
let { width, height } = data;
let x = 0;
let y = 0;
if (baseRatio < dataRatio) {
height = data.height;
width = base.width * (height / base.height);
x = (data.width - width) / 2;
y = 0;
} else if (baseRatio > dataRatio) {
width = data.width;
height = base.height * (width / base.width);
x = 0;
y = (data.height - height) / 2;
}
const { x, y, width, height } = centerImage(base, data);
ctx.drawImage(base, x, y, width, height);
const attachment = canvas.toBuffer();
if (Buffer.byteLength(attachment) > 8e+6) return msg.reply('Resulting image was above 8 MB.');
+1 -1
View File
@@ -9,7 +9,7 @@ module.exports = class FirstMessageCommand extends Command {
group: 'info',
memberName: 'first-message',
description: 'Responds with the first message ever sent to a channel.',
clientPermissions: ['EMBED_LINKS', 'READ_MESSAGE_HISTORY'],
clientPermissions: ['EMBED_LINKS'],
args: [
{
key: 'channel',
-4
View File
@@ -43,7 +43,6 @@ module.exports = class SosCommand extends Command {
}
async run(msg, { message }) {
try {
const base = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'sos.png'));
const canvas = createCanvas(base.width, base.height);
const ctx = canvas.getContext('2d');
@@ -61,8 +60,5 @@ module.exports = class SosCommand extends Command {
ctx.fillText(message.toUpperCase(), 362, 522);
ctx.rotate(-15 * (Math.PI / 180));
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'sos.png' }] });
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
};
+1
View File
@@ -110,6 +110,7 @@ module.exports = class WordChainCommand extends Command {
}
async verifyWord(word) {
if (startWords.includes(word.toLowerCase())) return true;
try {
const { body } = await request
.get(`https://www.dictionaryapi.com/api/v3/references/collegiate/json/${word}`)
+15 -2
View File
@@ -5,7 +5,19 @@ module.exports = class KissMarryKillCommand extends Command {
constructor(client) {
super(client, {
name: 'kiss-marry-kill',
aliases: ['kiss-kill-marry', 'kill-kiss-marry', 'kill-marry-kiss', 'marry-kiss-kill', 'marry-kill-kiss'],
aliases: [
'kiss-kill-marry',
'kill-kiss-marry',
'kill-marry-kiss',
'marry-kiss-kill',
'marry-kill-kiss',
'fuck-marry-kill',
'fuck-kiss-marry',
'kill-fuck-marry',
'kill-marry-fuck',
'marry-fuck-kill',
'marry-kill-fuck'
],
group: 'random',
memberName: 'kiss-marry-kill',
description: 'Determines who to kiss, who to marry, and who to kill.',
@@ -36,7 +48,8 @@ module.exports = class KissMarryKillCommand extends Command {
}
run(msg, { first, second, third }) {
const kissFuck = msg.channel.nsfw ? 'fuck' : 'kiss';
const things = shuffle([first, second, third]);
return msg.say(`I'd kiss ${things[0]}, marry ${things[1]}, and kill ${things[2]}.`);
return msg.say(`I'd ${kissFuck} ${things[0]}, marry ${things[1]}, and kill ${things[2]}.`);
}
};
+2 -1
View File
@@ -47,8 +47,9 @@ module.exports = class FrinkiacCommand extends Command {
}
}
url += `?b64lines=${base64(wrapped.join('\n'))}`;
const seasonEpisode = `S${data.Episode.Season}E${data.Episode.EpisodeNumber}`;
return msg.say(
`This is from **Season ${data.Episode.Season} Episode ${data.Episode.EpisodeNumber} @ ${time}**.`,
`This is from **${seasonEpisode} ("${data.Episode.Title}") @ ${time}**.`,
{ files: [url] }
);
} catch (err) {
+2 -1
View File
@@ -1,9 +1,10 @@
const Command = require('../../structures/Command');
const { stripIndents } = require('common-tags');
const { list } = require('../../util/Util');
const difficulties = ['easy', 'medium', 'hard', 'extreme', 'impossible'];
const difficulties = ['baby', 'easy', 'medium', 'hard', 'extreme', 'impossible'];
const operations = ['+', '-', '*'];
const maxValues = {
baby: 5,
easy: 10,
medium: 100,
hard: 500,
+4 -2
View File
@@ -2,8 +2,9 @@ const Command = require('../../structures/Command');
const { stripIndents } = require('common-tags');
const { list } = require('../../util/Util');
const sentences = require('../../assets/json/typing-test');
const difficulties = ['easy', 'medium', 'hard', 'extreme', 'impossible'];
const difficulties = ['baby', 'easy', 'medium', 'hard', 'extreme', 'impossible'];
const times = {
baby: 60000,
easy: 25000,
medium: 20000,
hard: 15000,
@@ -43,7 +44,8 @@ module.exports = class TypingTestCommand extends Command {
max: 1,
time
});
if (!msgs.size || msgs.first().content !== sentence) return msg.reply('Sorry! You lose!');
if (!msgs.size) return msg.reply('Sorry! You lose!');
if (msgs.first().content !== sentence) return msg.reply('Sorry! You made a typo, so you lose!');
return msg.reply(`Nice job! 10/10! You deserve some cake! (Took ${(Date.now() - now) / 1000} seconds)`);
}
};
+2 -2
View File
@@ -9,7 +9,6 @@ module.exports = class MockingCommand extends Command {
group: 'text-edit',
memberName: 'mocking',
description: 'SenDs TexT lIkE ThiS.',
clientPermissions: ['USE_EXTERNAL_EMOJIS'],
args: [
{
key: 'text',
@@ -23,11 +22,12 @@ module.exports = class MockingCommand extends Command {
}
run(msg, { text }) {
const canEmoji = msg.channel.permissionsFor(this.client.user).has('USE_EXTERNAL_EMOJIS');
const letters = text.split('');
for (let i = 0; i < letters.length; i += Math.floor(Math.random() * 4)) {
letters[i] = letters[i].toUpperCase();
}
return msg.say(`${letters.join('')}${this.mockingEmoji}`);
return msg.say(`${letters.join('')}${canEmoji ? this.mockingEmoji : ''}`);
}
get mockingEmoji() {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "112.5.2",
"version": "112.6.0",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {
+20
View File
@@ -126,4 +126,24 @@ module.exports = class CanvasUtil {
return resolve(lines);
});
}
static centerImage(base, data) {
const dataRatio = data.width / data.height;
const baseRatio = base.width / base.height;
let { width, height } = data;
let x = 0;
let y = 0;
if (baseRatio < dataRatio) {
height = data.height;
width = base.width * (height / base.height);
x = (data.width - width) / 2;
y = 0;
} else if (baseRatio > dataRatio) {
width = data.width;
height = base.height * (width / base.width);
x = 0;
y = (data.height - height) / 2;
}
return { x, y, width, height };
}
};
+29
View File
@@ -75,6 +75,35 @@ module.exports = class Util {
return crypto.createHash(algorithm).update(text).digest('hex');
}
static streamToArray(stream) {
if (!stream.readable) return Promise.resolve([]);
return new Promise((resolve, reject) => {
const array = [];
function onData(data) {
array.push(data);
}
function onEnd(error) {
if (error) reject(error);
else resolve(array);
cleanup();
}
function onClose() {
resolve(array);
cleanup();
}
function cleanup() {
stream.removeListener('data', onData);
stream.removeListener('end', onEnd);
stream.removeListener('error', onEnd);
stream.removeListener('close', onClose);
}
stream.on('data', onData);
stream.on('end', onEnd);
stream.on('error', onEnd);
stream.on('close', onClose);
});
}
static today(timeZone) {
const now = new Date();
if (timeZone) now.setUTCHours(timeZone);