Compare commits
No commits in common. "4ac6ec0a9d4ff532844da89b31595a9c3b94a8e9" and "f47d62faadb8292d7c3d5d96a4909ff5ed4b6e41" have entirely different histories.
4ac6ec0a9d
...
f47d62faad
2 changed files with 6 additions and 75 deletions
|
@ -1,66 +0,0 @@
|
||||||
import { Attachment, Collection, EmbedBuilder } from "discord.js";
|
|
||||||
import { handleAttachments } from "../../../utils/events/citation";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate a new random string
|
|
||||||
* @returns random string
|
|
||||||
*/
|
|
||||||
const newKey = () => Math.random().toString(36).substring(2);
|
|
||||||
|
|
||||||
describe("Attachements Handler", () => {
|
|
||||||
const map = new Map([
|
|
||||||
["e_attachements", "yes_s"],
|
|
||||||
["e_attachement", "no_s"],
|
|
||||||
]);
|
|
||||||
{
|
|
||||||
const name = "One image";
|
|
||||||
test(name, () => {
|
|
||||||
const embedExpected = new EmbedBuilder();
|
|
||||||
embedExpected.setImage("http://url");
|
|
||||||
|
|
||||||
const embedTest = new EmbedBuilder();
|
|
||||||
handleAttachments(
|
|
||||||
map,
|
|
||||||
embedTest,
|
|
||||||
new Collection([[newKey(), { name: "image.png", url: "http://url" } as Attachment]]),
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(embedTest).toStrictEqual(embedExpected);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
{
|
|
||||||
const name = "One link";
|
|
||||||
test(name, () => {
|
|
||||||
const embedExpected = new EmbedBuilder();
|
|
||||||
embedExpected.addFields({ name: "no_s", value: "[f](url)" });
|
|
||||||
|
|
||||||
const embedTest = new EmbedBuilder();
|
|
||||||
handleAttachments(
|
|
||||||
map,
|
|
||||||
embedTest,
|
|
||||||
new Collection([[newKey(), { name: "f", url: "url" } as Attachment]]),
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(embedTest).toStrictEqual(embedExpected);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
{
|
|
||||||
const name = "Two files";
|
|
||||||
test(name, () => {
|
|
||||||
const embedExpected = new EmbedBuilder();
|
|
||||||
embedExpected.addFields({ name: "yes_s", value: "[f](url), [f](url)" });
|
|
||||||
|
|
||||||
const embedTest = new EmbedBuilder();
|
|
||||||
handleAttachments(
|
|
||||||
map,
|
|
||||||
embedTest,
|
|
||||||
new Collection([
|
|
||||||
[newKey(), { name: "f", url: "url" } as Attachment],
|
|
||||||
[newKey(), { name: "f", url: "url" } as Attachment],
|
|
||||||
]),
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(embedTest).toStrictEqual(embedExpected);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
|
@ -21,23 +21,20 @@ export const handleAttachments = (
|
||||||
|
|
||||||
let currentField = "";
|
let currentField = "";
|
||||||
const fields: APIEmbedField[] = [];
|
const fields: APIEmbedField[] = [];
|
||||||
let multipleFields = 0;
|
let multiple = 0;
|
||||||
let numberOfLinks = 0;
|
|
||||||
files.forEach((file, idx) => {
|
files.forEach((file, idx) => {
|
||||||
numberOfLinks++;
|
|
||||||
const fieldValue = currentField.length > 0 ? `${currentField}, ${file}` : file;
|
const fieldValue = currentField.length > 0 ? `${currentField}, ${file}` : file;
|
||||||
|
|
||||||
if (fieldValue.length > maxFieldValueLength) {
|
if (fieldValue.length > maxFieldValueLength) {
|
||||||
multipleFields = multipleFields === 0 && idx !== files.length - 1 ? 1 : multipleFields + 1;
|
multiple = multiple === 0 && idx !== files.length - 1 ? 1 : multiple + 1;
|
||||||
fields.push({
|
fields.push({
|
||||||
name:
|
name:
|
||||||
loc.get(
|
loc.get(
|
||||||
attachments.size > 1 && numberOfLinks > 1 ? "e_attachements" : "e_attachement",
|
attachments.size > 1 && idx !== files.length - 1 ? "e_attachements" : "e_attachement",
|
||||||
) + (multipleFields ? ` (${multipleFields})` : ""),
|
) + (multiple ? ` (${multiple})` : ""),
|
||||||
value: currentField,
|
value: currentField,
|
||||||
});
|
});
|
||||||
currentField = file;
|
currentField = file;
|
||||||
numberOfLinks = 0;
|
|
||||||
} else {
|
} else {
|
||||||
currentField = fieldValue;
|
currentField = fieldValue;
|
||||||
}
|
}
|
||||||
|
@ -46,8 +43,8 @@ export const handleAttachments = (
|
||||||
if (currentField.length > 0) {
|
if (currentField.length > 0) {
|
||||||
fields.push({
|
fields.push({
|
||||||
name:
|
name:
|
||||||
loc.get(attachments.size > 1 && numberOfLinks > 1 ? "e_attachements" : "e_attachement") +
|
loc.get(attachments.size > 1 && multiple ? "e_attachements" : "e_attachement") +
|
||||||
(multipleFields ? ` (${multipleFields + 1})` : ""),
|
(multiple ? ` (${multiple + 1})` : ""),
|
||||||
value: currentField,
|
value: currentField,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue