fix some dry code

This commit is contained in:
artie 2025-02-16 00:34:00 +01:00
parent 52b54aff28
commit 372746eba9
6 changed files with 36 additions and 56 deletions

View File

@ -11,7 +11,7 @@ import { yandexOcr } from "../../utils/yandex";
import sharp from "sharp"; import sharp from "sharp";
import { import {
capitalize, capitalize,
getImageFromAttachmentOrString, getImageUrlFromChatInteraction,
languageCodeToName, languageCodeToName,
run, run,
} from "../../utils/functions"; } from "../../utils/functions";
@ -76,7 +76,7 @@ export async function ocrImpl(url: string) {
if (!type?.mime.startsWith("image/")) { if (!type?.mime.startsWith("image/")) {
console.log(type, url); console.log(type, url);
abort("The file must be an image!"); abort("Not a valid image!");
} }
const compressed = await sharp(data) const compressed = await sharp(data)
@ -107,9 +107,7 @@ export default defineCommand({
), ),
async execute(interaction) { async execute(interaction) {
const attachment = interaction.options.getAttachment("image"); const imageUrl = getImageUrlFromChatInteraction(interaction);
const url = interaction.options.getString("url");
const imageUrl = getImageFromAttachmentOrString(attachment, url);
await interaction.deferReply(); await interaction.deferReply();

View File

@ -1,7 +1,7 @@
import { ApplicationCommandType, ContextMenuCommandBuilder } from "discord.js"; import { ApplicationCommandType, ContextMenuCommandBuilder } from "discord.js";
import { defineCommand } from ".."; import { defineCommand } from "..";
import { buildOcrPayload, ocrImpl } from "./ocr"; import { buildOcrPayload, ocrImpl } from "./ocr";
import { getImageFromAttachmentOrString } from "../../utils/functions"; import { getImageUrlFromMessage } from "../../utils/functions";
export default defineCommand({ export default defineCommand({
data: new ContextMenuCommandBuilder() data: new ContextMenuCommandBuilder()
@ -11,15 +11,7 @@ export default defineCommand({
async execute(interaction) { async execute(interaction) {
if (!interaction.isMessageContextMenuCommand()) return; if (!interaction.isMessageContextMenuCommand()) return;
const attachment = interaction.targetMessage.attachments.first(); const imageUrl = getImageUrlFromMessage(interaction.targetMessage);
const embed = interaction.targetMessage.embeds[0];
const imageUrl = getImageFromAttachmentOrString(
attachment,
embed?.image?.url ||
embed?.thumbnail?.url ||
interaction.targetMessage.content
);
await interaction.deferReply(); await interaction.deferReply();

View File

@ -7,7 +7,7 @@ import {
translateImpl, translateImpl,
} from "../language/translate"; } from "../language/translate";
import { ocrImpl } from "./ocr"; import { ocrImpl } from "./ocr";
import { getImageFromAttachmentOrString } from "../../utils/functions"; import { getImageUrlFromChatInteraction } from "../../utils/functions";
export default defineCommand({ export default defineCommand({
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
@ -37,13 +37,10 @@ export default defineCommand({
autocomplete: translateAutocompleteImpl, autocomplete: translateAutocompleteImpl,
async execute(interaction) { async execute(interaction) {
const attachment = interaction.options.getAttachment("image");
const url = interaction.options.getString("url");
const source = interaction.options.getString("source") ?? null; const source = interaction.options.getString("source") ?? null;
const target = interaction.options.getString("target") ?? "en-US"; const target = interaction.options.getString("target") ?? "en-US";
const imageUrl = getImageFromAttachmentOrString(attachment, url); const imageUrl = getImageUrlFromChatInteraction(interaction);
await interaction.deferReply(); await interaction.deferReply();

View File

@ -2,7 +2,7 @@ import { ApplicationCommandType, ContextMenuCommandBuilder } from "discord.js";
import { defineCommand } from ".."; import { defineCommand } from "..";
import { translateImpl } from "../language/translate"; import { translateImpl } from "../language/translate";
import { ocrImpl } from "./ocr"; import { ocrImpl } from "./ocr";
import { getImageFromAttachmentOrString } from "../../utils/functions"; import { getImageUrlFromMessage } from "../../utils/functions";
export default defineCommand({ export default defineCommand({
data: new ContextMenuCommandBuilder() data: new ContextMenuCommandBuilder()
@ -12,15 +12,7 @@ export default defineCommand({
async execute(interaction) { async execute(interaction) {
if (!interaction.isMessageContextMenuCommand()) return; if (!interaction.isMessageContextMenuCommand()) return;
const attachment = interaction.targetMessage.attachments.first(); const imageUrl = getImageUrlFromMessage(interaction.targetMessage);
const embed = interaction.targetMessage.embeds[0];
const imageUrl = getImageFromAttachmentOrString(
attachment,
embed?.image?.url ||
embed?.thumbnail?.url ||
interaction.targetMessage.content
);
await interaction.deferReply(); await interaction.deferReply();

View File

@ -2,10 +2,10 @@ import { ApplicationCommandType, ContextMenuCommandBuilder } from "discord.js";
import { defineCommand } from ".."; import { defineCommand } from "..";
import { translateImpl } from "../language/translate"; import { translateImpl } from "../language/translate";
import { ocrImpl } from "./ocr"; import { ocrImpl } from "./ocr";
import { getImageFromAttachmentOrString } from "../../utils/functions";
import { buildTranslateModal } from "../language/translateMenu"; import { buildTranslateModal } from "../language/translateMenu";
import { abort } from "../../utils/error"; import { abort } from "../../utils/error";
import { findFuzzyLanguage } from "../../utils/deepl"; import { findFuzzyLanguage } from "../../utils/deepl";
import { getImageUrlFromMessage } from "../../utils/functions";
export default defineCommand({ export default defineCommand({
data: new ContextMenuCommandBuilder() data: new ContextMenuCommandBuilder()
@ -15,15 +15,7 @@ export default defineCommand({
async execute(interaction) { async execute(interaction) {
if (!interaction.isMessageContextMenuCommand()) return; if (!interaction.isMessageContextMenuCommand()) return;
const attachment = interaction.targetMessage.attachments.first(); const imageUrl = getImageUrlFromMessage(interaction.targetMessage);
const embed = interaction.targetMessage.embeds[0];
const imageUrl = getImageFromAttachmentOrString(
attachment,
embed?.image?.url ||
embed?.thumbnail?.url ||
interaction.targetMessage.content
);
const modal = buildTranslateModal(); const modal = buildTranslateModal();
await interaction.showModal(modal); await interaction.showModal(modal);

View File

@ -2,7 +2,7 @@ import * as cheerio from "cheerio";
import { execa } from "execa"; import { execa } from "execa";
import { customAlphabet } from "nanoid"; import { customAlphabet } from "nanoid";
import { URL_REGEX } from "./constants"; import { URL_REGEX } from "./constants";
import type { Attachment } from "discord.js"; import { Message, type ChatInputCommandInteraction } from "discord.js";
import { abort } from "./error"; import { abort } from "./error";
export const nanoid = customAlphabet("1234567890abcdef"); export const nanoid = customAlphabet("1234567890abcdef");
@ -80,22 +80,31 @@ export function capitalize(str: string) {
return str.charAt(0).toUpperCase() + str.slice(1); return str.charAt(0).toUpperCase() + str.slice(1);
} }
export function getImageFromAttachmentOrString( export function getImageUrlFromChatInteraction(
attachment?: Attachment | null, interaction: ChatInputCommandInteraction,
str?: string | null attachmentName = "image",
urlName = "url"
) { ) {
if (attachment) { const attachment = interaction.options.getAttachment(attachmentName);
if (!attachment.contentType?.startsWith("image/")) { const url = interaction.options.getString(urlName);
abort("The file must be an image!");
} return (
return attachment.url; (attachment?.contentType?.startsWith("image/") && attachment.url) ||
} else if (str) { (url && findFirstUrl(url)) ||
const match = findFirstUrl(str); abort("You must provide a valid image or image URL!")
if (!match) abort("The URL is invalid!"); );
return match;
} else {
abort("You must provide an image or an image URL!");
} }
export function getImageUrlFromMessage(message: Message): string {
const attachment = message.attachments.first();
return (
(attachment?.contentType?.startsWith("image/") && attachment.url) ||
message.embeds[0]?.image?.url ||
message.embeds[0]?.thumbnail?.url ||
findFirstUrl(message.content) ||
abort("No valid image found!")
);
} }
export function languageCodeToName(code: string) { export function languageCodeToName(code: string) {