This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Fonctions utilitaires en rapport avec les messages Discord #217
Closed
+119
−11
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0e8c272
test(message.test.ts): add tests for isDiscordLink function
Steellgold 0a6cdcd
refactor(resolve.cmd.ts): replace trustDiscordLinks with isDiscordLin…
Steellgold 03e7d8d
feat(utils/message): add isDiscordLink and getMessageContentFromLink …
Steellgold 71e4e57
Merge branch 'main' into fetch-message
Bluzzi 0d2820a
refactor(utils/message): rename isDiscordLink to containsDiscordLink …
Steellgold 4f1d142
refactor(resolve.cmd.ts): rename isDiscordLink to containsDiscordLink…
Steellgold 3f4a580
feat(utils): add extractDiscordLink function to message.util.ts
Steellgold 00364d8
feat(message.util.ts): add discordLinkRegex to message.type.ts and im…
Steellgold 940241f
refactor(message.util.ts): use match instead of test for containsDisc…
Steellgold 0ebb1af
feat(utils): add getMessageFromLink function to get a message instanc…
Steellgold 19721b4
test(get-message-instance.test.ts): add tests for PTB and Canary link…
Steellgold 0099e2d
test(contains-discord-link.test.ts): update test descriptions and lin…
Steellgold 82394b3
chore(contains-discord-link.test.ts): remove contains-discord-link.te…
Steellgold cbdc681
fix(resolve.cmd.ts): change function name from containsDiscordLink to…
Steellgold File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { extractDiscordLink } from "#/utils/message"; | ||
|
||
describe("extractDiscordLink function", () => { | ||
it("should correctly extract discord canary link", () => { | ||
expect(extractDiscordLink("https://canary.discord.com/channels/732251741999071303/973634573936246834/1128283771691601942")).toEqual({ | ||
guildID: "732251741999071303", | ||
channelID: "973634573936246834", | ||
messageID: "1128283771691601942" | ||
}); | ||
}); | ||
|
||
it("should correctly extract discord ptb link", () => { | ||
expect(extractDiscordLink("https://canary.discord.com/channels/732251741999071303/977731788002697326/1119643273695346820")).toEqual({ | ||
guildID: "732251741999071303", | ||
channelID: "977731788002697326", | ||
messageID: "1119643273695346820" | ||
}); | ||
}); | ||
|
||
it("should correctly extract discord link", () => { | ||
expect(extractDiscordLink("https://discord.com/channels/732251741999071303/786216771723198514/803532192793493544")).toEqual({ | ||
guildID: "732251741999071303", | ||
channelID: "786216771723198514", | ||
messageID: "803532192793493544" | ||
}); | ||
}); | ||
|
||
it("should return empty array when no discord link is found", () => { | ||
expect(extractDiscordLink("https://www.youtube.com/watch?v=dQw4w9WgXcQ")).toStrictEqual([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
2 changes: 1 addition & 1 deletion
2
src/utils/message/message.test.ts → src/utils/message/_test/msg-params.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { msgParams } from "./message.util"; | ||
export { msgParams, extractDiscordLink, getMessageFromLink } from "./message.util"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { Snowflake } from "discord.js"; | ||
|
||
export const discordLinkRegex = /http(s?):\/\/(www\.|canary\.|ptb\.)?discord.com\/channels(\/\d*){3}/gi; | ||
|
||
export type DiscordMessage = { | ||
guildID: Snowflake; | ||
channelID: Snowflake; | ||
messageID: Snowflake; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,47 @@ | ||
import type { Message } from "discord.js"; | ||
import type { DiscordMessage } from "./message.type"; | ||
import { client } from "#/client"; | ||
import { messageUrlRegex } from "#/events/message-link-reaction/message-link-reaction.const"; | ||
|
||
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 extractDiscordLink = (content: string): DiscordMessage | null => { | ||
if (!content.match(messageUrlRegex)) return null; | ||
const [guildID, channelID, messageID] = [...content.match(/(\d+)/g) ?? []]; | ||
if (!guildID || !channelID || !messageID) return null; | ||
|
||
return { | ||
guildID, | ||
channelID, | ||
messageID | ||
}; | ||
}; | ||
|
||
export const getMessageFromLink = async(link: string): Promise<Message<true> | null> => { | ||
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; | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Suite aux modifications de
extractDiscordLink
, cette fonction devrait être bien plus simple car elle n'a plus à gérer la logique d'extraction.