Added initial support for detecting videos, prevent music messages from showing on soundboard commands, fixed(?) permission checking

This commit is contained in:
TheEssem 2021-04-19 09:31:39 -05:00
parent 8aa0cc2163
commit c67499af9d
6 changed files with 26 additions and 22 deletions

View file

@ -8,7 +8,7 @@ const path = require("path");
const { EventEmitter } = require("events");
const logger = require("./logger.js");
const formats = ["image/jpeg", "image/png", "image/webp", "image/gif"];
const formats = ["image/jpeg", "image/png", "image/webp", "image/gif", "video/mp4", "video/webm", "video/mov"];
const jobs = {};

View file

@ -1,5 +1,4 @@
const fetch = require("node-fetch");
const url = require("url");
const { getType } = require("./image.js");
const execPromise = require("util").promisify(require("child_process").exec);
@ -23,17 +22,18 @@ const gfycatURLs = [
"giant.gfycat.com"
];
const formats = ["image/jpeg", "image/png", "image/webp", "image/gif"];
const imageFormats = ["image/jpeg", "image/png", "image/webp", "image/gif"];
const videoFormats = ["video/mp4", "video/webm", "video/mov"];
// gets the proper image paths
const getImage = async (image, image2, gifv = false) => {
const getImage = async (image, image2, video, gifv = false) => {
try {
const payload = {
url: image2,
path: image
};
if (gifv) {
const host = url.parse(image2).host;
const host = new URL(image2).host;
if (tenorURLs.includes(host)) {
if (process.env.TENOR !== "") {
const data = await fetch(`https://api.tenor.com/v1/gifs?ids=${image2.split("-").pop()}&key=${process.env.TENOR}`);
@ -51,9 +51,12 @@ const getImage = async (image, image2, gifv = false) => {
payload.path = `https://thumbs.gfycat.com/${image.split("/").pop().split(".mp4")[0]}-size_restricted.gif`;
}
payload.type = "image/gif";
} else if (video) {
payload.type = await getType(payload.path);
if (!payload.type || !videoFormats.includes(payload.type)) return;
} else {
payload.type = await getType(payload.path);
if (!payload.type || !formats.includes(payload.type)) return;
if (!payload.type || !imageFormats.includes(payload.type)) return;
}
return payload;
} catch (error) {
@ -65,42 +68,42 @@ const getImage = async (image, image2, gifv = false) => {
}
};
const checkImages = async (message) => {
const checkImages = async (message, video) => {
let type;
// first check the embeds
if (message.embeds.length !== 0) {
// embeds can vary in types, we check for tenor gifs first
if (message.embeds[0].type === "gifv") {
type = await getImage(message.embeds[0].video.url, message.embeds[0].url, true);
type = await getImage(message.embeds[0].video.url, message.embeds[0].url, video, true);
// then we check for other image types
} else if ((message.embeds[0].type === "video" || message.embeds[0].type === "image") && message.embeds[0].thumbnail) {
type = await getImage(message.embeds[0].thumbnail.proxy_url, message.embeds[0].thumbnail.url);
type = await getImage(message.embeds[0].thumbnail.proxy_url, message.embeds[0].thumbnail.url, video);
// finally we check both possible image fields for "generic" embeds
} else if (message.embeds[0].type === "rich") {
if (message.embeds[0].thumbnail) {
type = await getImage(message.embeds[0].thumbnail.proxy_url, message.embeds[0].thumbnail.url);
type = await getImage(message.embeds[0].thumbnail.proxy_url, message.embeds[0].thumbnail.url, video);
} else if (message.embeds[0].image) {
type = await getImage(message.embeds[0].image.proxy_url, message.embeds[0].image.url);
type = await getImage(message.embeds[0].image.proxy_url, message.embeds[0].image.url, video);
}
}
// then check the attachments
} else if (message.attachments.length !== 0 && message.attachments[0].width) {
type = await getImage(message.attachments[0].proxy_url, message.attachments[0].url);
type = await getImage(message.attachments[0].proxy_url, message.attachments[0].url, video);
}
// if the file is an image then return it
return type ? type : false;
};
// this checks for the latest message containing an image and returns the url of the image
module.exports = async (client, cmdMessage) => {
module.exports = async (client, cmdMessage, video = false) => {
// we start by checking the current message for images
const result = await checkImages(cmdMessage);
const result = await checkImages(cmdMessage, video);
if (result !== false) return result;
// if there aren't any in the current message then check if there's a reply
if (cmdMessage.messageReference) {
const replyMessage = await client.getMessage(cmdMessage.messageReference.channelID, cmdMessage.messageReference.messageID);
if (replyMessage) {
const replyResult = await checkImages(replyMessage);
const replyResult = await checkImages(replyMessage, video);
if (replyResult !== false) return replyResult;
}
}
@ -108,7 +111,7 @@ module.exports = async (client, cmdMessage) => {
const messages = await cmdMessage.channel.getMessages();
// iterate over each message
for (const message of messages) {
const result = await checkImages(message);
const result = await checkImages(message, video);
if (result === false) {
continue;
} else {