Skip to content

Commit

Permalink
patch mock sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
matthova committed Nov 20, 2024
1 parent efbe592 commit 78083bf
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions sdk-playground/packages/client/src/discordSdk.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
import {DiscordSDK, DiscordSDKMock, IDiscordSDK, patchUrlMappings} from '@discord/embedded-app-sdk';
import {
DiscordSDK,
DiscordSDKMock,
IDiscordSDK,
patchUrlMappings,
} from "@discord/embedded-app-sdk";

const queryParams = new URLSearchParams(window.location.search);
const isEmbedded = queryParams.get('frame_id') != null;
const isEmbedded = queryParams.get("frame_id") != null;

let discordSdk: IDiscordSDK;

if (isEmbedded) {
discordSdk = new DiscordSDK(import.meta.env.VITE_CLIENT_ID, {disableConsoleLogOverride: true});
discordSdk = new DiscordSDK(import.meta.env.VITE_CLIENT_ID, {
disableConsoleLogOverride: true,
});
patchUrlMappings([]);
} else {
discordSdk = new DiscordSDKMock(import.meta.env.VITE_CLIENT_ID, null, null);
discordSdk = new DiscordSDKMock(
import.meta.env.VITE_CLIENT_ID,
null,
null,
null
);
// @ts-expect-error
discordSdk.channelId = 'test_channel_id';
let storedUserId = sessionStorage.getItem('user_id');
discordSdk.channelId = "test_channel_id";
let storedUserId = sessionStorage.getItem("user_id");
if (storedUserId == null) {
// Set user_id to a random 8-character string, this gives us a consistent user id
storedUserId = Math.random().toString(36).slice(2, 10);
sessionStorage.setItem('user_id', storedUserId);
sessionStorage.setItem("user_id", storedUserId);
}
const queryParamsUserId = new URLSearchParams(window.location.search).get('user_id');
const queryParamsUserId = new URLSearchParams(window.location.search).get(
"user_id"
);

const userId = queryParamsUserId ?? storedUserId ?? '';
const userId = queryParamsUserId ?? storedUserId ?? "";
const discriminator = String(userId.charCodeAt(0) % 5);

(discordSdk as DiscordSDKMock)._updateCommandMocks({
authenticate: () =>
Promise.resolve({
access_token: 'mock_token',
access_token: "mock_token",
user: {
username: userId,
discriminator,
Expand All @@ -37,10 +51,10 @@ if (isEmbedded) {
scopes: [],
expires: new Date(2112, 1, 1).toString(),
application: {
description: 'mock_app_description',
icon: 'mock_app_icon',
id: 'mock_app_id',
name: 'mock_app_name',
description: "mock_app_description",
icon: "mock_app_icon",
id: "mock_app_id",
name: "mock_app_name",
},
}),
});
Expand Down

0 comments on commit 78083bf

Please sign in to comment.