Context Menu OCR

This commit is contained in:
derpystuff 2022-06-22 22:43:22 +02:00
parent e37de9e68b
commit ec2ec39e25
3 changed files with 94 additions and 33 deletions

View file

@ -2,6 +2,22 @@ const attachmentTypes = Object.freeze({
image: ["image/png", "image/jpeg", "image/gif"]
})
module.exports.attachmentTypes = attachmentTypes
// Returns the first attachment from a message (if it exists)
function getMessageAttachment(message) {
if (message.attachments.first()) {
return message.attachments.first()
} else if (message.embeds.length > 0 && message.embeds.toArray()[0].image) {
return message.embeds.toArray()[0].image
} else if (message.embeds.length > 0 && message.embeds.toArray()[0].thumbnail) {
return message.embeds.toArray()[0].thumbnail
}
return;
}
module.exports.getMessageAttachment = getMessageAttachment
async function getRecentMedia(context, limit) {
if (!context.message.channel) {
return undefined;
@ -27,20 +43,8 @@ async function getRecentMedia(context, limit) {
let attachments = [];
for (const m of messages) {
let message = m[1]
if ( // First the attachment on the message
message.attachments.first()
) {
attachments.push(message.attachments.first())
} else if ( // Then the embed image
message.embeds.length > 0 &&
message.embeds.toArray()[0].image
) {
attachments.push(message.embeds.toArray()[0].image)
} else if (
message.embeds.length > 0 &&
message.embeds.toArray()[0].thumbnail
) { attachments.push(message.embeds.toArray()[0].thumbnail) }
let a = getMessageAttachment(m[1])
if(a) attachments.push(a)
}
return attachments;
}
@ -81,16 +85,24 @@ module.exports.getRecentVideo = async function(context, limit) {
return attachments;
}
module.exports.getRecentImage= async function(context, limit){
function validateAttachment(attachment){
if (attachment.contentType && attachmentTypes.image.includes(attachment.contentType)) { // discord attachment
return true
} else if (!attachment.content_type) { // other form of media
return true
} else {
return false
}
}
module.exports.validateAttachment = validateAttachment
module.exports.getRecentImage = async function(context, limit){
let attachments = await getRecentMedia(context, limit)
let at;
let validImages = attachmentTypes.image
for (const a of attachments) {
if (a.contentType && validImages.includes(a.contentType) && at === undefined) { // discord attachment
at = a.url
} else if (!a.content_type && at === undefined) { // other form of media
at = a.url
}
if(validateAttachment(a) && at === undefined) at = a.url
}
return at;
}