subdirectories in utils function, split the attachement handler into a function
All checks were successful
PR Check / lint-and-format (pull_request) Successful in 19s

This commit is contained in:
Mylloon 2024-10-14 22:23:43 +02:00
parent ddcf488be5
commit 3a00874ce0
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
13 changed files with 64 additions and 55 deletions

View file

@ -8,7 +8,7 @@ import {
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
import { getLocale } from "../../utils/locales"; import { getLocale } from "../../utils/locales";
import { getFilename } from "../../utils/misc"; import { getFilename } from "../../utils/misc";
import { embedListReminders } from "../../utils/reminder"; import { embedListReminders } from "../../utils/commands/reminder";
import { collect } from "../loader"; import { collect } from "../loader";
export default { export default {

View file

@ -8,7 +8,7 @@ import {
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
import { getLocale } from "../../utils/locales"; import { getLocale } from "../../utils/locales";
import { getFilename } from "../../utils/misc"; import { getFilename } from "../../utils/misc";
import { embedListReminders } from "../../utils/reminder"; import { embedListReminders } from "../../utils/commands/reminder";
import { collect } from "../loader"; import { collect } from "../loader";
export default { export default {

View file

@ -10,8 +10,8 @@ import {
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
import { getLocale } from "../../utils/locales"; import { getLocale } from "../../utils/locales";
import { getFilename } from "../../utils/misc"; import { getFilename } from "../../utils/misc";
import { embedListQueue } from "../../utils/music";
import { collect } from "../loader"; import { collect } from "../loader";
import { embedListQueue } from "../../utils/commands/music";
export default { export default {
data: { data: {

View file

@ -10,8 +10,8 @@ import {
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
import { getLocale } from "../../utils/locales"; import { getLocale } from "../../utils/locales";
import { getFilename } from "../../utils/misc"; import { getFilename } from "../../utils/misc";
import { embedListQueue } from "../../utils/music";
import { collect } from "../loader"; import { collect } from "../loader";
import { embedListQueue } from "../../utils/commands/music";
export default { export default {
data: { data: {

View file

@ -19,7 +19,7 @@ import {
embedListReminders, embedListReminders,
getReminderInfo, getReminderInfo,
newReminder, newReminder,
} from "../../utils/reminder"; } from "../../utils/commands/reminder";
export default { export default {
scope: () => [], scope: () => [],

View file

@ -12,7 +12,7 @@ import { v4 as uuidv4 } from "uuid";
import { collect } from "../../buttons/loader"; import { collect } from "../../buttons/loader";
import { getLocale, getLocalizations } from "../../utils/locales"; import { getLocale, getLocalizations } from "../../utils/locales";
import { getFilename } from "../../utils/misc"; import { getFilename } from "../../utils/misc";
import { embedListQueue } from "../../utils/music"; import { embedListQueue } from "../../utils/commands/music";
export default { export default {
scope: () => [], scope: () => [],

View file

@ -8,7 +8,7 @@ import {
sendReminder, sendReminder,
setTimeoutReminder, setTimeoutReminder,
updateReminder, updateReminder,
} from "../../utils/reminder"; } from "../../utils/commands/reminder";
export const once = true; export const once = true;

View file

@ -1,8 +1,9 @@
import { APIEmbedField, Client, EmbedBuilder, Message, TextBasedChannel } from "discord.js"; import { Client, EmbedBuilder, Message, TextBasedChannel } from "discord.js";
import { getLocale } from "../../utils/locales"; import { getLocale } from "../../utils/locales";
import { isImage, userWithNickname } from "../../utils/misc"; import { userWithNickname } from "../../utils/misc";
import { showDate } from "../../utils/time"; import { showDate } from "../../utils/time";
import { RegexC, RegExpFlags } from "../../utils/regex"; import { RegexC, RegExpFlags } from "../../utils/regex";
import { handleAttachements } from "../../utils/events/citation";
/** https://discord.js.org/#/docs/discord.js/main/class/Client?scrollTo=e-messageCreate */ /** https://discord.js.org/#/docs/discord.js/main/class/Client?scrollTo=e-messageCreate */
export default async (message: Message, client: Client) => { export default async (message: Message, client: Client) => {
@ -109,44 +110,7 @@ export default async (message: Message, client: Client) => {
// Handle attachments // Handle attachments
if (quoted_post.attachments.size !== 0) { if (quoted_post.attachments.size !== 0) {
if (quoted_post.attachments.size === 1 && isImage(quoted_post.attachments.first()!.name)) { handleAttachements(loc, embed, quoted_post.attachments);
// Only contains one image
embed.setImage(quoted_post.attachments.first()!.url);
} else {
// Contains more than one image and/or other files
// We are currently losing a link to a file if the link is too long,
// but we can't do much about it
const maxFieldValueLength = 1024;
const files = quoted_post.attachments
.map((file) => `[${file.name}](${file.url})`)
.filter((link) => link.length <= maxFieldValueLength);
let currentField = "";
const fields: APIEmbedField[] = [];
let multiple = 0;
files.forEach((file, idx) => {
const fieldValue = currentField.length > 0 ? `${currentField}, ${file}` : file;
if (fieldValue.length > maxFieldValueLength || idx === files.length - 1) {
multiple = multiple === 0 && idx !== files.length - 1 ? 1 : multiple + 1;
fields.push({
name:
loc.get(
quoted_post.attachments.size > 1 && idx !== files.length - 1
? "e_attachements"
: "e_attachement",
) + (multiple ? ` (${multiple})` : ""),
value: currentField,
});
currentField = file;
} else {
currentField = fieldValue;
}
});
embed.addFields(fields);
}
} }
// Description as post content // Description as post content

View file

@ -1,6 +1,6 @@
import { Client, ModalSubmitInteraction } from "discord.js"; import { Client, ModalSubmitInteraction } from "discord.js";
import { getFilename } from "../../utils/misc"; import { getFilename } from "../../utils/misc";
import { newReminder } from "../../utils/reminder"; import { newReminder } from "../../utils/commands/reminder";
export default { export default {
data: { data: {

View file

@ -1,4 +1,4 @@
import { OptionReminder, splitTime } from "../../utils/reminder"; import { OptionReminder, splitTime } from "../../../utils/commands/reminder";
describe("Time splitter", () => { describe("Time splitter", () => {
{ {

View file

@ -1,8 +1,8 @@
import { EmbedBuilder } from "@discordjs/builders"; import { EmbedBuilder } from "@discordjs/builders";
import { GuildQueue, QueueRepeatMode } from "discord-player"; import { GuildQueue, QueueRepeatMode } from "discord-player";
import { Client } from "discord.js"; import { Client } from "discord.js";
import { getLocale } from "./locales"; import { getLocale } from "../locales";
import { blank } from "./misc"; import { blank } from "../misc";
export const embedListQueue = ( export const embedListQueue = (
client: Client, client: Client,

View file

@ -1,8 +1,8 @@
import { Client, Colors, EmbedBuilder, User } from "discord.js"; import { Client, Colors, EmbedBuilder, User } from "discord.js";
import { getLocale } from "./locales"; import { getLocale } from "../locales";
import { blank, cleanCodeBlock } from "./misc"; import { blank, cleanCodeBlock } from "../misc";
import { showDate, strToSeconds, timeDeltaToString } from "./time"; import { showDate, strToSeconds, timeDeltaToString } from "../time";
import { RegexC, RegExpFlags } from "./regex"; import { RegexC, RegExpFlags } from "../regex";
/** /**
* Option possible for reminders * Option possible for reminders

View file

@ -0,0 +1,45 @@
import { APIEmbedField, Attachment, Collection, EmbedBuilder } from "discord.js";
import { isImage } from "../misc";
export const handleAttachements = (
loc: Map<string, string>,
embed: EmbedBuilder,
attachments: Collection<string, Attachment>,
) => {
if (attachments.size === 1 && isImage(attachments.first()!.name)) {
// Only contains one image
embed.setImage(attachments.first()!.url);
} else {
// Contains more than one image and/or other files
// We are currently losing a link to a file if the link is too long,
// but we can't do much about it
const maxFieldValueLength = 1024;
const files = attachments
.map((file) => `[${file.name}](${file.url})`)
.filter((link) => link.length <= maxFieldValueLength);
let currentField = "";
const fields: APIEmbedField[] = [];
let multiple = 0;
files.forEach((file, idx) => {
const fieldValue = currentField.length > 0 ? `${currentField}, ${file}` : file;
if (fieldValue.length > maxFieldValueLength || idx === files.length - 1) {
multiple = multiple === 0 && idx !== files.length - 1 ? 1 : multiple + 1;
fields.push({
name:
loc.get(
attachments.size > 1 && idx !== files.length - 1 ? "e_attachements" : "e_attachement",
) + (multiple ? ` (${multiple})` : ""),
value: currentField,
});
currentField = file;
} else {
currentField = fieldValue;
}
});
embed.addFields(fields);
}
};