Created
March 19, 2024 20:13
-
-
Save alula/2e9f6874bd7f12d4ca9201347979ac92 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { DiscordSDK } from "@discord/embedded-app-sdk"; | |
const SESSION_STORAGE_KEY = "__DISCORD_SDK_HACK__"; | |
interface DiscordSDKAuthResponse { | |
access_token: string; | |
user: { | |
username: string; | |
discriminator: string; | |
id: string; | |
public_flags: number; | |
avatar?: string | null | undefined; | |
global_name?: string | null | undefined; | |
}; | |
scopes: ( | |
| -1 | |
| "identify" | |
| "email" | |
| "connections" | |
| "guilds" | |
| "guilds.join" | |
| "guilds.members.read" | |
| "gdm.join" | |
| "rpc" | |
| "rpc.notifications.read" | |
| "rpc.voice.read" | |
| "rpc.voice.write" | |
| "rpc.video.read" | |
| "rpc.video.write" | |
| "rpc.screenshare.read" | |
| "rpc.screenshare.write" | |
| "rpc.activities.write" | |
| "bot" | |
| "webhook.incoming" | |
| "messages.read" | |
| "applications.builds.upload" | |
| "applications.builds.read" | |
| "applications.commands" | |
| "applications.commands.update" | |
| "applications.commands.permissions.update" | |
| "applications.store.update" | |
| "applications.entitlements" | |
| "activities.read" | |
| "activities.write" | |
| "relationships.read" | |
| "voice" | |
| "dm_channels.read" | |
| "role_connections.write" | |
)[]; | |
expires: string; | |
application: { | |
id: string; | |
description: string; | |
name: string; | |
icon?: string | null | undefined; | |
rpc_origins?: string[] | undefined; | |
}; | |
} | |
interface DiscordSDKHackData { | |
isInitialized: boolean; | |
cachedAuthData: DiscordSDKAuthResponse; | |
} | |
function getHackData(): DiscordSDKHackData | undefined { | |
const data = sessionStorage.getItem(SESSION_STORAGE_KEY); | |
if (data) { | |
return JSON.parse(data); | |
} | |
return undefined; | |
} | |
function setHackData(data: DiscordSDKHackData) { | |
sessionStorage.setItem(SESSION_STORAGE_KEY, JSON.stringify(data)); | |
} | |
class DiscordSDKManager { | |
discordSdk: DiscordSDK; | |
auth: DiscordSDKAuthResponse | undefined = undefined; | |
constructor() { | |
this.discordSdk = new DiscordSDK(import.meta.env.VITE_CLIENT_ID); | |
} | |
get clientId() { | |
return this.discordSdk.clientId; | |
} | |
get instanceId() { | |
return this.discordSdk.instanceId; | |
} | |
get platform() { | |
return this.discordSdk.platform; | |
} | |
get guildId() { | |
return this.discordSdk.guildId; | |
} | |
get channelId() { | |
return this.discordSdk.channelId; | |
} | |
get configuration() { | |
return this.discordSdk.configuration; | |
} | |
get commands() { | |
return this.discordSdk.commands; | |
} | |
async ready() { | |
// Hack for https://github.com/discord/embedded-app-sdk/issues/41 (only affects development environment) | |
if (import.meta.env.DEV) { | |
const data = getHackData(); | |
if (data) { | |
const discordSdkAny = this.discordSdk as any; | |
if (discordSdkAny.sourceOrigin.includes("discord.com")) { | |
sessionStorage.removeItem(SESSION_STORAGE_KEY); | |
} else { | |
discordSdkAny.sourceOrigin = "*"; | |
discordSdkAny.isReady = true; | |
this.auth = data.cachedAuthData; | |
} | |
} | |
} | |
await this.discordSdk.ready(); | |
} | |
async initialize() { | |
console.log("Loading Discord SDK..."); | |
await this.ready(); | |
if (this.auth) return; | |
console.log("Discord SDK loaded!"); | |
const { code } = await this.discordSdk.commands.authorize({ | |
client_id: this.discordSdk.clientId, | |
response_type: "code", | |
state: "", | |
prompt: "none", | |
scope: ["identify", "guilds"], | |
}); | |
console.log("Code:", code); | |
const response = await fetch("/api/token", { | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json", | |
}, | |
body: JSON.stringify({ | |
code, | |
}), | |
}); | |
const { accessToken } = await response.json(); | |
console.log("Access Token:", accessToken); | |
// Authenticate with Discord client (using the access_token) | |
this.auth = await this.discordSdk.commands.authenticate({ | |
access_token: accessToken, | |
}); | |
if (import.meta.env.DEV) { | |
setHackData({ | |
isInitialized: true, | |
cachedAuthData: this.auth, | |
}); | |
} | |
} | |
} | |
export const discordSdk = new DiscordSDKManager(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment