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

View File

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

View File

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

View File

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

View File

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

View File

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