Skip to content

Instantly share code, notes, and snippets.

@nullableVoidPtr
Last active November 8, 2017 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nullableVoidPtr/700c5ae07e6edd4d20008d44c73f6a13 to your computer and use it in GitHub Desktop.
Save nullableVoidPtr/700c5ae07e6edd4d20008d44c73f6a13 to your computer and use it in GitHub Desktop.
var authToken;
var archive = {};
let delay = 100;
function processMentions(message) {
for (mention of message.mentions) message.content = message.content.replace(RegExp("<@!?" + mention['id'] + ">"), mention.username);
}
function renderIRC(messages) {
var array = [];
for (message of messages) {
processMentions(message);
array.push(`[${message.timestamp}] ${message.author.username}: ${message.content} ${message.attachments.map(x => x.url).join(" ")}`);
}
return array.join("\n")
}
function renderHTML(messages) {
function renderMessageHTML(message) {
var messageDiv = document.createElement("div");
var avatar = document.createElement("div");
var username = document.createElement("strong");
messageDiv.appendChild(avatar);
messageDiv.appendChild(username);
messageDiv.style.marginLeft = "20px";
messageDiv.style.marginRight = "6px";
avatar.classList.add("avatar");
avatar.style.width = avatar.style.height = "40px";
avatar.style.backgroundSize = "40px 40px"
avatar.style.marginTop = "-2px";
avatar.style.marginRight = "20px";
avatar.style.borderRadius = "50%";
if (message.author.avatar != undefined) {
avatar.style.backgroundImage = `url("https://cdn.discordapp.com/avatars/${message.author.id}/${message.author.avatar}.webp?size=128"`
if (message.author.avatar.startsWith("a_")) {
avatar.setAttribute("onmouseover", `this.style.backgroundImage = 'url("https://cdn.discordapp.com/avatars/${message.author.id}/${message.author.avatar}.gif?size=128")'`);
avatar.setAttribute("onmouseout", `this.style.backgroundImage = 'url("https://cdn.discordapp.com/avatars/${message.author.id}/${message.author.avatar}.webp?size=128")'`);
}
} else {
avatar.style.backgroundImage = 'url("https://discordapp.com/assets/0e291f67c9274a1abdddeb3fd919cbaa.png")';
}
username.style.color = "white";
username.classList.add("username");
username.innerHTML = `${message.author.username}<sub>#${message.author.discriminator}</sub>`;
}
var wrapper = document.createElement("div");
wrapper.style.backgroundColor = "#36393e";
var template = initMessageTemplate();
for (message of messages) {
processMentions(message);
wrapper.appendChild(renderMessageHTML(message));
}
}
function archiveMessages(channel, before, after) {
if (archive[channel] == undefined) archive[channel] = [];
fetch(`https://discordapp.com/api/channels/${channel}/messages?before=${before}&limit=100`, {headers: {"Authorization": authToken}})
.then(resp => resp.json())
.then(messages => {
var end = false;
if (messages.length != 0) {
before = messages[messages.length - 1].id;
} else {
end = true
}
for (message of messages) {
if (message.id == after) {
end = true;
} else {
archive[channel].unshift(message);
}
}
return end;
}).then((end) => {if (!end) {
setTimeout(archiveMessages, delay, channel, before, after)
} else {
console.log("Done!")
}});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment