Discord bot improvements

This commit is contained in:
Jordan Eldredge 2019-06-06 10:12:49 -04:00
parent 0fded822f8
commit 17a2e96cae
13 changed files with 936112 additions and 24 deletions

View file

@ -1,2 +1,3 @@
config.js
node_modules/
node_modules/
uploads

View file

@ -2,6 +2,7 @@ const fs = require("fs");
const path = require("path");
const fetch = require("node-fetch");
const md5Buffer = require("md5");
const config = require("../config");
const { getInfo } = require("../info");
const Skin = require("./skin");
@ -28,9 +29,10 @@ async function handler(message) {
for (const file of files) {
const info = await getInfo(file.md5);
if (info == null) {
console.log(config);
console.log(file.filename);
fs.writeFileSync(
path.join(
"/Volumes/Mobile Backup/skins/skins/discord-bot",
path.join(config.uploadDir,
// TODO: Use a sub directory using md5 to avoid collision
// file.md5,
file.filename

View file

@ -12,11 +12,12 @@ async function reviewSkin(message) {
}
async function handler(message, args) {
const [arg1] = args;
const count = arg1 ? Math.min(arg1, 10) : 1;
if (count > 1) {
message.channel.send(`Going to show ${count} skins to review`);
let [count] = args;
if (count > 50) {
message.channel.send(`You can only review up to ${count} skins at a time.`);
count = 50
}
message.channel.send(`Going to show ${count} skins to review.`);
let i = Number(count);
while (i--) {
await reviewSkin(message);

View file

@ -0,0 +1,72 @@
const Discord = require("discord.js");
const temp = require('temp').track();
const fs = require("fs");
const path = require("path");
const fetch = require("node-fetch");
const md5Buffer = require("md5");
const config = require("../config");
const Shooter = require("../shooter");
const { getInfo } = require("../info");
const Skin = require("./skin");
async function handler(message, args) {
console.log("Trying to take a screenshot");
const { attachments } = message;
if (attachments.length < 1) {
await message.channel.send("Could not screenshot. No attachment found.");
return;
}
const files = await Promise.all(
attachments.map(async attachment => {
const { filename, url } = attachment;
const response = await fetch(url);
console.log("got response");
const buffer = await response.buffer();
console.log("got buffer");
const md5 = md5Buffer(buffer);
console.log("got md5", md5);
return { filename, buffer, md5 };
})
);
console.log("got files");
const shooter = new Shooter("https://webamp.org");
for (const file of files) {
const tempFile = temp.path({suffix: '.wsz'});
const tempScreenshotPath = temp.path({suffix: '.png'});
fs.writeFileSync(
tempFile,
file.buffer
);
try {
await shooter.takeScreenshot(tempFile, tempScreenshotPath, {
minify: true,
});
} catch(e) {
await message.channel.send(`Something went wrong taking the screenshot of ${file.filename}. Sorry.`);
continue;
}
const attachment = new Discord.Attachment(tempScreenshotPath, 'screenshot.png');
const embed = new Discord.RichEmbed()
.setTitle(`Here's a screenshot of ${file.filename}`)
.attachFile(attachment)
.setImage(`attachment://screenshot.png`);
await message.channel.send(embed);
}
await shooter.dispose();
}
module.exports = {
command: "screenshot",
usage: "",
description:
"Take a screenshot of the accompanying uploaded skin",
handler
};

View file

@ -1,5 +1,6 @@
const { getFilename } = require("../info");
const Utils = require("../utils");
async function handler(message, args) {
const [md5] = args;
const filename = getFilename(md5);

View file

@ -22,7 +22,7 @@ for (const command of commands) {
handlers[command.command] = command.handler;
}
async function handleHelp(message) {
async function handleHelp(message, args) {
const commandHelp = commands
.map(command => {
return `\`!${command.command} ${command.usage || ""}\` -- ${

View file

@ -1,13 +1,13 @@
const path = require("path");
const fs = require("fs");
const config = require("./config");
let cache = null;
function getCache() {
if (cache == null) {
cache = JSON.parse(
fs.readFileSync(
path.join("/Volumes/Mobile Backup/skins/cache", "info.json"),
fs.readFileSync(config.cachePath,
"utf8"
)
);

File diff suppressed because it is too large Load diff

View file

@ -6,9 +6,13 @@
"dependencies": {
"aws-sdk": "^2.429.0",
"discord.js": "^11.4.2",
"imagemin": "^6.1.0",
"imagemin-optipng": "^7.0.0",
"md5": "^2.2.1",
"node-fetch": "^2.3.0",
"rgb-hex": "^2.1.0"
"puppeteer": "^1.17.0",
"rgb-hex": "^2.1.0",
"temp": "^0.9.0"
},
"scripts": {
"start": "node index.js"

View file

@ -62,9 +62,9 @@ async function getStats() {
getFile("tweeted.txt")
]);
return {
approved: new Set(approved).size - new Set(tweeted).size,
rejected: new Set(rejected).size,
tweeted: new Set(tweeted).size
approved: new Set(getLines(approved)).size,
rejected: new Set(getLines(rejected)).size,
tweeted: new Set(getLines(tweeted)).size,
};
}

View file

@ -0,0 +1,88 @@
const path = require("path");
const puppeteer = require("puppeteer");
const imagemin = require("imagemin");
const imageminOptipng = require("imagemin-optipng");
function min(imgPath) {
return imagemin([imgPath], path.dirname(imgPath), {
use: [imageminOptipng()],
});
}
class Shooter {
constructor(url) {
this._initialized = false;
this._url = url;
}
async init() {
this._browser = await puppeteer.launch();
this._page = await this._browser.newPage();
this._page.setViewport({ width: 275, height: 116 * 3 });
this._page.on("console", (...args) => {
console.log("page log:", ...args);
});
this._page.on("error", e => {
console.log(`Page error: ${e.toString()}`);
});
this._page.on("dialog", async dialog => {
console.log(`Page dialog ${dialog.message()}`);
await dialog.dismiss();
});
const url = `${this._url}/?screenshot=1`;
await this._page.goto(url);
await this._page.waitForSelector("#main-window", { timeout: 2000 });
await this._page.evaluate(() => {
// Needed to allow for transparent screenshots
window.document.body.style.background = "none";
});
this._initialized = true;
}
async _ensureInitialized() {
if (!this._initialized) {
await this.init();
}
}
async takeScreenshot(skin, screenshotPath, { minify = false }) {
await this._ensureInitialized();
console.log("Going to try", screenshotPath, skin);
try {
console.log("geting input");
const handle = await this._page.$("#webamp-file-input");
console.log("uploading skin");
await handle.uploadFile(skin);
console.log("waiting for skin to load...");
await this._page.evaluate(() => {
return window.__webamp.skinIsLoaded();
});
console.log("waiting for screenshot");
await this._page.screenshot({
path: screenshotPath,
omitBackground: true, // Make screenshot transparent
// https://github.com/GoogleChrome/puppeteer/issues/703#issuecomment-366041479
clip: { x: 0, y: 0, width: 275, height: 116 * 3 },
});
console.log("Wrote screenshot to", screenshotPath);
if (minify) {
min(screenshotPath);
}
console.log("Minified", screenshotPath);
} catch (e) {
console.error("Something went wrong, restarting browser", e);
await this.dispose();
await this.init();
throw e;
}
}
async dispose() {
this._ensureInitialized();
await this._page.close();
await this._browser.close();
this._initialized = false;
}
}
module.exports = Shooter;

View file

@ -24,12 +24,16 @@ async function postSkin({ filename, md5, title, dest }) {
embed.addField("Warning", "Cached metadata not found", true);
} else {
if (info.averageColor) {
const color = rgbHex(info.averageColor);
if (String(color).length === 6) {
embed.setColor(`#${color}`);
} else {
console.log("Did not get a safe color from ", info.averageColor);
console.log("Got ", color);
try {
const color = rgbHex(info.averageColor);
if (String(color).length === 6) {
embed.setColor(`#${color}`);
} else {
console.log("Did not get a safe color from ", info.averageColor);
console.log("Got ", color);
}
} catch(e) {
console.error('could not use color', info.averageColor);
}
}
if (info.emails != null && info.emails.length) {

File diff suppressed because it is too large Load diff