Skip to content

Instantly share code, notes, and snippets.

@januarionclx
Last active November 6, 2021 23:55
Show Gist options
  • Select an option

  • Save januarionclx/5d56ac39e1eea5630cd648ed7e0f381b to your computer and use it in GitHub Desktop.

Select an option

Save januarionclx/5d56ac39e1eea5630cd648ed7e0f381b to your computer and use it in GitHub Desktop.
Delete discord messages from server.
const getUserId = async () => {
return new Promise((resolve, reject) => {
const iframe = document.createElement('iframe');
iframe.onload = () => {
resolve(JSON.parse(iframe.contentWindow.localStorage.getItem('user_id_cache')));
}
iframe.src = 'about:blank';
document.body.appendChild(iframe);
});
};
const getMessages = async (guild, authorId) => {
try {
const response = await fetch(`https://discord.com/api/v9/guilds/${guild}/messages/search?author_id=${authorId}`, {
"headers": {
"authorization": AUTH_TOKEN,
},
});
const json = await response.json();
const messages = json.messages;
return messages;
} catch (err) {
console.error(err);
}
}
const deleteMessage = async (messageId, channel) => {
try {
const response = await fetch(`https://discord.com/api/v9/channels/${channel}/messages/${messageId}`, {
"headers": {
"authorization": AUTH_TOKEN,
},
"method": "DELETE",
});
} catch (err) {
console.error(err);
}
}
const deleteMessages = (messages) => {
return new Promise((resolve, reject) => {
for (let [index, message] of Object.entries(messages)) {
setTimeout(async (message) => {
console.log('Deleting ', message, '...');
await deleteMessage(message[0].id, message[0].channel_id);
if (parseInt(index) === messages.length - 1) {
console.log('Done deleting messages.');
resolve();
}
}, index * 1000, message);
}
})
}
const deleteAllMessages = async (userId) => {
while (true) {
const messages = await getMessages(SERVER_ID, userId);
if (messages.length !== 0) {
await deleteMessages(messages);
} else {
console.log('Done deleting ALL messages.');
return;
}
}
}
// Config;
let AUTH_TOKEN = '';
let SERVER_ID = '';
// Main
const myUserId = await getUserId();
await deleteAllMessages(myUserId);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment