Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Fonctions utilitaires en rapport avec les messages Discord #217

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/commands/forum/[sub-commands]/resolve.cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { commands } from "#/configs/message/command";
import { ChannelType, ForumChannel } from "discord.js";
import { logger } from "#/utils/logger";
import { userWithId } from "#/utils/discord/user";
import { trustDiscordLinks } from "../forum.const";
import { containsDiscordLink } from "#/utils/message/message.util";

export const execute: CommandExecute = async(command) => {
const channel = command.channel;
Expand All @@ -21,7 +21,7 @@ export const execute: CommandExecute = async(command) => {

const answerLink = command.options.getString(commands.forum.subcmds.resolve.options.answer.name, true);

if (!answerLink.match(trustDiscordLinks)) {
if (!containsDiscordLink(answerLink)) {
void command.reply({
embeds: [simpleEmbed(commands.forum.exec.resolve.isntDiscordMessageLink, "error")],
ephemeral: true
Expand Down
32 changes: 32 additions & 0 deletions src/utils/message/_test/contains-discord-link.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, it } from "vitest";
import { containsDiscordLink } from "#/utils/message";

describe("isDiscordLink function", () => {
it("should return true when the link is a discord canary link", () => {
expect(
containsDiscordLink("Salut check : https://canary.discord.com/channels/732251741999071303/1113731433123565589/1113731587570413588")
).toBe(true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

En anglais les tests

});

it("should return true when the link is a discord ptb link", () => {
expect(
containsDiscordLink("Salut check : https://ptb.discord.com/channels/732251741999071303/786216771723198514/1075749993631191110")
).toBe(true);
});

it("should return true when the link is a discord link", () => {
expect(containsDiscordLink("Salut check : https://discord.com/channels/732251741999071303/786216771723198514/803532192793493544")).toBe(true);
});

it("should return false because is not valid", () => {
expect(containsDiscordLink("Salut check : https://discord.com/channels/732251741999071303")).toBe(false);
});

it("should return false because is litteraly not a discord link", () => {
expect(containsDiscordLink("Salut check la superbe vidéo: https://www.youtube.com/watch?v=dQw4w9WgXcQ")).toBe(false);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Soit plus sérieux dans les titres


it("should return false when the link is not a discord link", () => {
expect(containsDiscordLink("Ahah https://fakediscord.com/channels/732251741999071303/1113731433123565589/1113731587570413588")).toBe(false);
});
});
21 changes: 21 additions & 0 deletions src/utils/message/_test/extract-discord-link.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { describe, it, expect } from "vitest";
import { extractDiscordLink } from "#/utils/message";

describe("msgParams function", () => {
it("should correctly extract discord link", () => {
expect(
extractDiscordLink([
"Salut check : https://discord.com/channels/732251741999071303/786216771723198514/803532192793493544",
"et https://canary.discord.com/channels/732251741999071303/786216771723198514/803532192793493544"].join(" "))
).toEqual([
"https://discord.com/channels/732251741999071303/786216771723198514/803532192793493544",
"https://canary.discord.com/channels/732251741999071303/786216771723198514/803532192793493544"
]);
});

it("should return null when no discord link is found", () => {
expect(
extractDiscordLink("Salut check : https://www.youtube.com/watch?v=dQw4w9WgXcQ")
).toBe(null);
});
});
25 changes: 25 additions & 0 deletions src/utils/message/_test/get-message-instance.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, expect, it } from "vitest";
import { getMessageFromLink } from "#/utils/message";
import { Message } from "discord.js";

describe("isDiscordLink function", () => {
it("should return instance of Message", async() => {
const message = await getMessageFromLink("https://discord.com/channels/732251741999071303/786216771723198514/803532192793493544");
expect(message).toBeInstanceOf(Message);
});

it("should return instance of Message with PTB link", async() => {
const message = await getMessageFromLink("https://ptb.discord.com/channels/732251741999071303/786216771723198514/803532192793493544");
expect(message).toBeInstanceOf(Message);
});

it("should return instance of Message with Canary link", async() => {
const message = await getMessageFromLink("https://canary.discord.com/channels/732251741999071303/786216771723198514/803532192793493544");
expect(message).toBeInstanceOf(Message);
});

it("should return null when the link is not a discord link", async() => {
const message = await getMessageFromLink("https://www.youtube.com/watch?v=dQw4w9WgXcQ");
expect(message).toBe(null);
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from "vitest";
import { msgParams } from "./message.util";
import { msgParams } from "#/utils/message";

describe("msgParams function", () => {
it("should correctly replace placeholders with provided words", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/message/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { msgParams } from "./message.util";
export { msgParams, containsDiscordLink, extractDiscordLink, getMessageFromLink } from "./message.util";
1 change: 1 addition & 0 deletions src/utils/message/message.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const discordLinkRegex = /http(s?):\/\/(www\.|canary\.|ptb\.)?discord.com\/channels(\/\d*){3}/gi;
35 changes: 35 additions & 0 deletions src/utils/message/message.util.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,44 @@
import type { Message } from "discord.js";
import { discordLinkRegex } from "./message.type";
import { client } from "#/client";

export const msgParams = (message: string, params: (string | number)[]): string => {
const words = message.match(/{[^}]+}/g);

if (words) for (let i = 0; i < params.length; i++) {
message = message.replace(words[i], String(params[i]));
}

return message;
};

export const containsDiscordLink = (content: string): boolean => {
return content.match(discordLinkRegex) !== null;
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cette fonction est inutile. On peux s'en passer, juste la fonction extractDiscordLink() est suffisante, si elle retourne une array vide, ça veux dire qu'il n'y a pas de lien.


export const extractDiscordLink = (content: string): string | string[] | null => {
const discordLinks = content.match(discordLinkRegex);
if (discordLinks) return discordLinks;
return null;
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cette fonction devrait avoir cette valeur de retour :

type DiscordMessage = {
  guildID: Snowflake;
  channelID: Snowflake;
  messageID: Snowflake;
}

export function extractDiscordLink = (messageContent: string): DiscordMessage[] => {
  // TODO
}


export const getMessageFromLink = async(link: string): Promise<Message<true> | null> => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suite aux modifications de extractDiscordLink, cette fonction devrait être bien plus simple car elle n'a plus à gérer la logique d'extraction.

const ids = [...link.match(/(\d+)/g) ?? []];

if (ids.length !== 3) return null;

const guildId = ids[0];
const channelId = ids[1];
const messageId = ids[2];
Comment on lines +29 to +35
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

La manière de parse est pourrait être amélioré :

const content = `
	Hello World!
 	https://canary.discord.com/channels/732251741999071303/786216771723198514/803532192793493544
  	are you ok ?
   	https://canary.discord.com/channels/732251741999071303/786216771723198514/803532192793493544
`;

const matchs = [...content.matchAll(/\/\d+\/\d+\/\d+/g)].map(element => {
  const [guildID, channelID, messageID] = [...element[0].matchAll(/\d+/g)].map(e => e[0]);
  
  return { guildID, channelID, messageID };
});

console.log(matchs);


const guild = await client.guilds.fetch(guildId);
if (!guild) return null;

const channel = await guild.channels.fetch(channelId);
if (!channel || !channel.isTextBased()) return null;

const message = await channel.messages.fetch(messageId);
if (!message) return null;

return message;
};