Discord URL Shortener

Enter a Discord message or channel URL to convert it into a shortened URL.


    
    
    

URL Format

The Discord URL can be in one of the following formats:

https://discord.com/channels/{channel_id}/{server_id}/{message_id}
https://discord.com/channels/{channel_id}/{server_id}

The shortened URL will be in one of the following formats:


    

    

Where each ID is base64url-encoded in little-endian format.

Code Examples

The following Python and JavaScript code blocks are implementations of a Discord URL shortener. Each function takes a Discord URL as input, splits it into channel ID, server ID, and message ID, encodes these IDs using base64url in little-endian format, and constructs the shortened URL.

Python

import base64

def to_url(s):
    parts = [int(n) for n in s.split('/channels/')[1].split('/')]
    channel_id, server_id = parts[:2]
    message_id = parts[2] if len(parts) > 2 else None
    encode = lambda n: base64.urlsafe_b64encode(n.to_bytes(length=8, byteorder="little")).rstrip(b'=').decode('utf-8')
    return f'{encode(channel_id)}/{encode(server_id)}' + (f'/{encode(message_id)}' if message_id else '')

JavaScript

function to_url(s) {
    let buffer = new ArrayBuffer(8);
    let view = new DataView(buffer);
    view.setBigUint64(0, BigInt(s), true);
    return btoa(String.fromCharCode.apply(null, new Uint8Array(buffer)))
        .replace(/+/g, '-')
        .replace(///g, '_')
        .replace(/=+$/, '');
}