user command support

This commit is contained in:
derpystuff 2024-03-19 17:28:37 +01:00
parent af5a7f4252
commit 86936dc100
14 changed files with 710 additions and 11 deletions

View file

@ -11,6 +11,14 @@ const urlr = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.
module.exports = {
name: 'Music Platforms',
type: ApplicationCommandTypes.MESSAGE,
contexts: [
0,
1,
2
],
integrationTypes: [
1
],
run: async (context, args) => {
try{
await context.respond({data: {}, type: InteractionCallbackTypes.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE})

View file

@ -11,9 +11,17 @@ const { STATICS } = require('../../../labscore/utils/statics');
module.exports = {
name: 'OCR',
type: ApplicationCommandTypes.MESSAGE,
contexts: [
0,
1,
2
],
integrationTypes: [
1
],
run: async (context, args) => {
try{
await context.respond({data: { flags: MessageFlags.EPHEMERAL }, type: InteractionCallbackTypes.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE})
await context.respond({data: {}, type: InteractionCallbackTypes.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE})
const { message } = args;
@ -23,11 +31,11 @@ module.exports = {
} else {
delete attachment;
}
if(!attachment) return context.editOrRespond({ embeds: [createEmbed("warning", context, "No images found.")], flags: MessageFlags.EPHEMERAL })
if(!attachment) return context.editOrRespond({ embeds: [createEmbed("warning", context, "No images found.")] })
let ocr = await googleVisionOcr(context, attachment)
if(ocr.response.body.status == 1) return context.editOrRespond({ embeds: [createEmbed("warning", context, ocr.response.body.text)], flags: MessageFlags.EPHEMERAL })
if(ocr.response.body.status == 1) return context.editOrRespond({ embeds: [createEmbed("warning", context, ocr.response.body.text)] })
await context.editOrRespond({
embeds: [createEmbed("default", context, {
@ -39,8 +47,7 @@ module.exports = {
iconUrl: STATICS.google,
text: `Google Cloud Vision • ${context.application.name} • Took ${ocr.timings}s`
}
})],
flags: MessageFlags.EPHEMERAL
})]
})
}catch(e){
console.log(e)

View file

@ -0,0 +1,71 @@
const { Constants } = require('detritus-client');
const { InteractionCallbackTypes, ApplicationCommandTypes, MessageFlags } = Constants;
const { googleVisionOcr, googleTranslate } = require('../../../labscore/api');
const { getMessageAttachment, validateAttachment } = require('../../../labscore/utils/attachment');
const { createEmbed } = require('../../../labscore/utils/embed');
const { codeblock, icon, pill } = require('../../../labscore/utils/markdown');
const { STATICS } = require('../../../labscore/utils/statics');
const { editOrReply } = require('../../../labscore/utils/message');
const { TRANSLATE_LANGUAGE_MAPPINGS, TRANSLATE_LANGUAGES } = require('../../../labscore/constants');
module.exports = {
name: 'OCR Translate',
type: ApplicationCommandTypes.MESSAGE,
contexts: [
0,
1,
2
],
integrationTypes: [
1
],
run: async (context, args) => {
try{
await context.respond({data: {}, type: InteractionCallbackTypes.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE})
const { message } = args;
let attachment = getMessageAttachment(message)
if(attachment && validateAttachment(attachment, "image")){
attachment = attachment.url
} else {
delete attachment;
}
if(!attachment) return editOrReply(context, createEmbed("warning", context, "No images found."))
let ocr = await googleVisionOcr(context, attachment)
if(ocr.response.body.status == 1) return editOrReply(context, createEmbed("warning", context, ocr.response.body.text))
try{
let translate = await googleTranslate(context, ocr.response.body.text, "en", "auto")
let fromFlag = TRANSLATE_LANGUAGE_MAPPINGS[translate.response.body.language.from || sourceLanguage] || ''
let toFlag = TRANSLATE_LANGUAGE_MAPPINGS[translate.response.body.language.to] || ''
return editOrReply(context, createEmbed("default", context, {
description: `${icon("locale")} ${fromFlag} ${pill(TRANSLATE_LANGUAGES[translate.response.body.language.from || sourceLanguage])} ${icon("arrow_right")} ${toFlag} ${pill(TRANSLATE_LANGUAGES[translate.response.body.language.to])}\n${codeblock("ansi", [translate.response.body.translation.substr(0,4000)])}`,
thumbnail: {
url: attachment
},
footer: {
iconUrl: STATICS.google,
text: `Google Cloud Vision • ${context.application.name}`
}
}))
}catch(e){
console.log(e)
if(e.response?.body?.status && e.response.body.status == 2) return editOrReply(context, createEmbed("error", context, `Unable to translate text.`))
return editOrReply(context, createEmbed("error", context, `Something went wrong.`))
}
}catch(e){
console.log(e)
await context.editOrRespond({
embeds: [createEmbed("error", context, "Unable to perform Optical Character Recognition.")],
flags: MessageFlags.EPHEMERAL
})
}
},
};