Skip to content

Commit c753f65

Browse files
committed
Add Discord message link generator
1 parent eafc19f commit c753f65

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

discordAttachmentMessage.html

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Find Message URL of Discord Attachment</title>
6+
</head>
7+
<body>
8+
9+
<script type="text/javascript">
10+
function strictParseInt(num) {
11+
try {
12+
const result = JSON.parse(num);
13+
return Number.isInteger(result) ? result : NaN;
14+
} catch {
15+
return NaN;
16+
}
17+
}
18+
19+
function getMessageUrl(attachmentUrl, serverId) {
20+
const attachPrefix = "https://cdn.discordapp.com/attachments/";
21+
const messagePrefix = "https://discord.com/channels/";
22+
if (!attachmentUrl.startsWith(attachPrefix)) {
23+
throw new Error("Invalid attachment URL");
24+
}
25+
if (!strictParseInt(serverId)) {
26+
throw new Error("Invalid server ID");
27+
}
28+
attachmentUrl = new URL(attachmentUrl);
29+
let [channelId, messageId] =
30+
attachmentUrl.pathname.split("/").slice(2, 4);
31+
let messagePath = [serverId, channelId, messageId].join("/");
32+
return messagePrefix + messagePath;
33+
}
34+
35+
function generate() {
36+
let attachmentUrl = document.getElementById("url-input").value;
37+
let serverId = document.getElementById("id-input").value;
38+
let linkElem = document.getElementById("message-link");
39+
let errorElem = document.getElementById("error");
40+
let messageUrl = "";
41+
try {
42+
messageUrl = getMessageUrl(attachmentUrl, serverId);
43+
errorElem.innerText = "";
44+
} catch(e) {
45+
errorElem.innerText = e.message;
46+
}
47+
linkElem.href = messageUrl;
48+
linkElem.innerText = messageUrl;
49+
}
50+
</script>
51+
52+
<h1>Find Message URL of Discord Attachment</h1>
53+
<p>
54+
This tool generates the message URL associated with the given
55+
Discord attachment URL and server ID.
56+
</p>
57+
<div id="box1">
58+
<label for="url-input">Attachment URL:</label><br>
59+
<input type="text" id="url-input" value="" style="width:60em" />
60+
</div>
61+
<br>
62+
<div id="box2">
63+
<label for="id-input">Server ID:</label><br>
64+
<input type="text" id="id-input" value="" style="width:10em" />
65+
</div>
66+
<br>
67+
<button onclick="generate();">Generate Message URL</button>
68+
<br><br>
69+
<div id="link-area">
70+
<a id="message-link" href=""></a>
71+
<div id="error" style="color: red"></div>
72+
</div>
73+
74+
</body>
75+
</html>

0 commit comments

Comments
 (0)