-
Notifications
You must be signed in to change notification settings - Fork 1
Fonctions utilitaires en rapport avec les messages Discord #217
Changes from 11 commits
0e8c272
0a6cdcd
03e7d8d
71e4e57
0d2820a
4f1d142
3f4a580
00364d8
940241f
0ebb1af
19721b4
0099e2d
82394b3
cbdc681
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
}); | ||
|
||
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); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
}); | ||
}); |
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); | ||
}); | ||
}); |
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 +1 @@ | ||
export { msgParams } from "./message.util"; | ||
export { msgParams, containsDiscordLink, extractDiscordLink, getMessageFromLink } from "./message.util"; |
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; |
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; | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cette fonction est inutile. On peux s'en passer, juste la fonction |
||
|
||
export const extractDiscordLink = (content: string): string | string[] | null => { | ||
const discordLinks = content.match(discordLinkRegex); | ||
if (discordLinks) return discordLinks; | ||
return null; | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suite aux modifications de |
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
En anglais les tests