Skip to content

Instantly share code, notes, and snippets.

@nxmad
Created July 24, 2023 16:58
Show Gist options
  • Save nxmad/739fc6d4fa1f0fad8c649a9657403b75 to your computer and use it in GitHub Desktop.
Save nxmad/739fc6d4fa1f0fad8c649a9657403b75 to your computer and use it in GitHub Desktop.
Simple discord.com script to leave all group DMs after some inactivity time
{
// personal discord token (can be found in cookies)
const TOKEN = ''
// inactivity threshold in days
const LEAVE_THRESHOLD = 30
const wait = (ms) => new Promise(resolve => setTimeout(resolve, ms))
const getChannels = async () => {
const res = await fetch('https://discord.com/api/v9/users/@me/channels', {
headers: {
Authorization: TOKEN,
}
})
return res.json()
}
const getLastMessageTimestamp = async (id) => {
const res = await fetch(`https://discord.com/api/v9/channels/${id}/messages`, {
headers: {
Authorization: TOKEN,
}
})
// filter out all system messages
const messages = (await res.json()).filter(m => m.type === 0)
if (!messages.length) return 0
return (new Date(messages[0].timestamp)).getTime()
}
const leave = async (id) => {
const res = await fetch(`https://discord.com/api/v9/channels/${id}`, {
method: 'DELETE',
headers: {
Authorization: TOKEN,
}
})
return res.json()
}
const channels = await getChannels()
const groups = channels.filter(ch => ch.type === 3)
const deleteUpTo = (new Date).getTime() - (LEAVE_THRESHOLD * 86400 * 1e3)
for (const g of groups) {
const ts = await getLastMessageTimestamp(g.id)
if (ts < deleteUpTo) {
console.log(`leaving ${g.id}...`)
await leave(g.id)
}
await wait(100)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment