Skip to content

Instantly share code, notes, and snippets.

@BIRTAX38
Last active May 2, 2024 02:35
Show Gist options
  • Save BIRTAX38/1cbf2232f9a4adc26777ae02e62bca19 to your computer and use it in GitHub Desktop.
Save BIRTAX38/1cbf2232f9a4adc26777ae02e62bca19 to your computer and use it in GitHub Desktop.

Downloading friends list from Discord account

Note

If you can't or don't know how to enable the developer console in the application, please use a web browser.

Note

The date in "Friend since" refers to the date when the friend request was sent by either party.

How to use this script:

  1. Go to the page https://discord.com/channels/@me
  2. Log in
  3. Press Ctrl + Shift + I to open Developer Tools
  4. Go to the Console tab
  5. Paste the script
  6. If you can't paste the script type: allow pasting and try pasting the script again. If you can paste script skip this step
  7. Press Enter and your friend list will be downloaded as a text file
var token = (webpackChunkdiscord_app.push([[''],{},e=>{m=[];for(let c in e.c)m.push(e.c[c])}]),m).find(m=>m?.exports?.default?.getToken!==void 0).exports.default.getToken();

fetch('https://discord.com/api/v9/users/@me/relationships', {
  method: 'GET',
  headers: {
    'Authorization': token
  }
})
  .then(response => {
    if (!response.ok) {
      throw new Error('Network response was not ok');
    }
    return response.json();
  })
  .then(data => {
    const jsonData = data;
    const userTimeZoneOffset = new Date().getTimezoneOffset() * 60000;
    const filteredData = jsonData.filter(item => item.type === 1);

    let text = '';
    filteredData.forEach(item => {
      const username = item.user.username;
      const globalName = item.user.global_name ? unicodeToChar(item.user.global_name) : username;
      const nickname = item.nickname ? `Friend Nickname: ${unicodeToChar(item.nickname)}\n` : '';

      const sinceUTC = new Date(item.since);
      const sinceLocal = new Date(sinceUTC.getTime() - userTimeZoneOffset);
      const since = sinceLocal.toISOString().replace(/T/, ' ').replace(/\..+/, '');

      const id = item.id;

      text += `Username: ${username}\n`;
      text += `Display name: ${globalName}\n`;
      text += `${nickname}`;
      text += `Id: ${id}\n`;
      text += `Friends since: ${since}\n\n\n`;
    });

    fetch('https://discord.com/api/v9/users/@me', {
  method: 'GET',
  headers: {
    'Authorization': token
  }
})
  .then(response => response.json())
  .then(data => {
    const discordUserData = data;
    const username = discordUserData.username;
    const globalName = discordUserData.global_name ? unicodeToChar(discordUserData.global_name) : username;
    const id = discordUserData.id;

    text += `\n\n\nInformation about the account from which the friends list was retrieved:\n`
    text += `Username: ${username}\n`
    text += `Display name: ${globalName}\n`
    text += `Id: ${id}\n`

    const today = new Date();
    const day = String(today.getDate()).padStart(2, '0');
    const month = String(today.getMonth() + 1).padStart(2, '0');
    const year = today.getFullYear();

    const formattedDate = `${day}-${month}-${year}`;
    //console.log(formattedDate);


    const blob = new Blob([text], { type: 'text/plain' });
    const fileName = `Discord Friend List ${username} ${formattedDate}.txt`;

    const link = document.createElement('a');
    link.href = window.URL.createObjectURL(blob);
    link.download = fileName;

    link.click();
      })
  .catch(error => console.error('Error:', error));
  })
  .catch(error => console.error('Error:', error));

function unicodeToChar(text) {
  return text.replace(/\\u[\dA-F]{4}/gi, 
      function (match) {
          return String.fromCharCode(parseInt(match.replace(/\\u/g, ''), 16));
      });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment