mirror of
https://github.com/artiemis/artemis.js.git
synced 2026-02-14 10:21:54 +00:00
fix some dry code
This commit is contained in:
parent
52b54aff28
commit
372746eba9
@ -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();
|
||||
|
||||
|
||||
@ -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();
|
||||
|
||||
|
||||
@ -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();
|
||||
|
||||
|
||||
@ -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();
|
||||
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user