sdfksdjfk
This commit is contained in:
parent
dcf116767b
commit
e4d23c193b
5 changed files with 71 additions and 77 deletions
|
@ -1,20 +1,20 @@
|
|||
<script lang="ts">
|
||||
import { authorize } from "./lib/discord";
|
||||
import { logs } from "./lib/stores";
|
||||
import { authorize } from "./lib/discord.js";
|
||||
import { user } from "./lib/stores.js";
|
||||
|
||||
async function main() {
|
||||
const auth = await authorize();
|
||||
$logs = [...$logs, { type: "Auth", content: JSON.stringify(auth.user) }];
|
||||
user.set(auth.user);
|
||||
}
|
||||
|
||||
main();
|
||||
</script>
|
||||
|
||||
<main>
|
||||
<p>garf 2: eletric boogaloo</p>
|
||||
{#each $logs as log}
|
||||
<p><strong>{log.type}</strong></p>
|
||||
<tt>{log.content}</tt>
|
||||
<br />
|
||||
{/each}
|
||||
{#if $user.avatar}
|
||||
<img src={`https://cdn.discordapp.com/avatars/${$user.id}/${$user.avatar}.png?size=256`} alt="" />
|
||||
{:else}
|
||||
<img src={`https://cdn.discordapp.com/embed/avatars/${Math.abs(Number($user.id) >> 22) % 6}.png`} alt="" />
|
||||
{/if}
|
||||
<p>Hello, <strong>{$user.username}</strong></p>
|
||||
</main>
|
||||
|
|
|
@ -1,63 +0,0 @@
|
|||
const CLIENT_ID = "869016244613951539";
|
||||
import { DiscordSDK } from "@discord/embedded-app-sdk";
|
||||
export const discordSdk = new DiscordSDK(CLIENT_ID);
|
||||
|
||||
import { logs } from "./stores";
|
||||
|
||||
export const ACTIVITY_STARTED = Date.now();
|
||||
|
||||
/**
|
||||
* @param {any} activity
|
||||
*/
|
||||
async function setActivity(activity) {
|
||||
const result = await discordSdk.commands.setActivity({ activity });
|
||||
logs.update((value) => {
|
||||
return [...value, { type: "Activity", content: JSON.stringify(result) }];
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async function activityUpdateLoop() {
|
||||
const { participants } = await discordSdk.commands.getInstanceConnectedParticipants();
|
||||
logs.update((value) => {
|
||||
return [...value, { type: "Peers", content: JSON.stringify(participants) }];
|
||||
});
|
||||
|
||||
setActivity({
|
||||
type: 3,
|
||||
details: "garf",
|
||||
state: "garfing rn",
|
||||
assets: {
|
||||
large_image: "embedded_cover",
|
||||
},
|
||||
party: {
|
||||
size: [participants.length, 5],
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function authorize() {
|
||||
await discordSdk.ready();
|
||||
|
||||
const { code } = await discordSdk.commands.authorize({
|
||||
client_id: CLIENT_ID,
|
||||
response_type: "code",
|
||||
state: "",
|
||||
prompt: "none",
|
||||
scope: ["identify", "guilds", "rpc.activities.write"],
|
||||
});
|
||||
|
||||
const response = await fetch("/api/token", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ code }),
|
||||
});
|
||||
|
||||
const { access_token } = await response.json();
|
||||
const auth = await discordSdk.commands.authenticate({ access_token });
|
||||
|
||||
setInterval(activityUpdateLoop, 10 * 1000);
|
||||
|
||||
return auth;
|
||||
}
|
48
src/lib/discord.ts
Normal file
48
src/lib/discord.ts
Normal file
|
@ -0,0 +1,48 @@
|
|||
const CLIENT_ID = "869016244613951539";
|
||||
import { DiscordSDK, Events, type Types } from "@discord/embedded-app-sdk";
|
||||
export const discordSdk = new DiscordSDK(CLIENT_ID);
|
||||
import { participants as partz } from "./stores";
|
||||
|
||||
export const ACTIVITY_STARTED = Date.now();
|
||||
|
||||
export async function authorize() {
|
||||
await discordSdk.ready();
|
||||
|
||||
const { code } = await discordSdk.commands.authorize({
|
||||
client_id: CLIENT_ID,
|
||||
response_type: "code",
|
||||
state: "",
|
||||
prompt: "none",
|
||||
scope: ["identify", "guilds", "rpc.activities.write", "guilds.members.read"],
|
||||
});
|
||||
|
||||
const response = await fetch("/api/token", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ code }),
|
||||
});
|
||||
|
||||
const { access_token } = await response.json();
|
||||
const auth = await discordSdk.commands.authenticate({ access_token });
|
||||
|
||||
return auth;
|
||||
}
|
||||
|
||||
discordSdk.subscribe(Events.ACTIVITY_INSTANCE_PARTICIPANTS_UPDATE, (data) => {
|
||||
partz.set(data);
|
||||
|
||||
discordSdk.commands.setActivity({
|
||||
activity: {
|
||||
type: 3,
|
||||
details: "garf",
|
||||
state: "garfmaxxing",
|
||||
assets: {
|
||||
large_image: "embedded_cover",
|
||||
large_text: "garf",
|
||||
},
|
||||
party: {
|
||||
size: [data.participants.length, 5],
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
|
@ -1,5 +0,0 @@
|
|||
import { writable } from "svelte/store";
|
||||
|
||||
export const user = writable({});
|
||||
|
||||
export const logs = writable([{ type: "Init", content: "Begin logs" }]);
|
14
src/lib/stores.ts
Normal file
14
src/lib/stores.ts
Normal file
|
@ -0,0 +1,14 @@
|
|||
import type { Types } from "@discord/embedded-app-sdk";
|
||||
import { writable, type Writable } from "svelte/store";
|
||||
|
||||
interface AuthenticatedUser {
|
||||
username: string;
|
||||
discriminator: string;
|
||||
id: string;
|
||||
public_flags: number;
|
||||
avatar?: string | null | undefined;
|
||||
global_name?: string | null | undefined;
|
||||
}
|
||||
|
||||
export const user: Writable<AuthenticatedUser> = writable();
|
||||
export const participants: Writable<Types.GetActivityInstanceConnectedParticipantsResponse> = writable();
|
Loading…
Reference in a new issue