Rewrite website as a Hyprland desktop
This commit is contained in:
1 parent
7c52779cf6
commit
3065ad6092
197 files changed
+5609
-17779
No files matched your search
@@ -0,0 +1,71 @@
|
||||
import { ActivityType, Client, GatewayIntentBits, Partials, type Activity, type GuildMember } from 'discord.js';
|
||||
import { DISCORD_GUILD_ID, DISCORD_TOKEN, DISCORD_USER_ID } from '$app/env/private';
|
||||
import { offlinePresence, type DiscordActivity, type DiscordPresence, type DiscordStatus } from '#lib/discord/types';
|
||||
|
||||
const customStatusType = ActivityType.Custom;
|
||||
|
||||
let client: Client | null = null;
|
||||
|
||||
export function getDiscordClient(): Client | null {
|
||||
if (!DISCORD_TOKEN) return null;
|
||||
if (client) return client;
|
||||
|
||||
client = new Client({
|
||||
partials: [Partials.User],
|
||||
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildPresences]
|
||||
});
|
||||
client.login(DISCORD_TOKEN).catch((error) => {
|
||||
console.error('Discord login failed:', error);
|
||||
client = null;
|
||||
});
|
||||
return client;
|
||||
}
|
||||
|
||||
function assetUrl(asset: string | null | undefined, applicationId: string | null): string | null {
|
||||
if (!asset) return null;
|
||||
if (asset.startsWith('mp:external/')) return `https://media.discordapp.net/${asset.slice(3)}`;
|
||||
if (/^https?:\/\//.test(asset)) return asset;
|
||||
return applicationId ? `https://cdn.discordapp.com/app-assets/${applicationId}/${asset}.webp` : null;
|
||||
}
|
||||
|
||||
function toActivity(activity: Activity): DiscordActivity {
|
||||
return {
|
||||
name: activity.name,
|
||||
type: activity.type,
|
||||
details: activity.details ?? activity.state ?? null,
|
||||
largeImage: assetUrl(activity.assets?.largeImage, activity.applicationId),
|
||||
smallImage: assetUrl(activity.assets?.smallImage, activity.applicationId)
|
||||
};
|
||||
}
|
||||
|
||||
function toPresence(member: GuildMember): DiscordPresence {
|
||||
const user = member.user;
|
||||
const primaryGuild = user.primaryGuild;
|
||||
const badgeGuildId = primaryGuild?.identityGuildId;
|
||||
const badge = primaryGuild?.badge;
|
||||
|
||||
return {
|
||||
status: (member.presence?.status ?? 'offline') as DiscordStatus,
|
||||
displayName: member.displayName,
|
||||
avatarUrl: user.displayAvatarURL({ size: 128 }),
|
||||
guildTag: primaryGuild?.tag ?? null,
|
||||
guildTagBadgeUrl:
|
||||
badgeGuildId && badge ? `https://cdn.discordapp.com/clan-badges/${badgeGuildId}/${badge}.png?size=32` : null,
|
||||
activities: (member.presence?.activities ?? [])
|
||||
.filter((activity) => activity.type !== customStatusType)
|
||||
.map(toActivity)
|
||||
};
|
||||
}
|
||||
|
||||
export async function getDiscordPresence(): Promise<DiscordPresence> {
|
||||
const discordClient = getDiscordClient();
|
||||
if (!discordClient?.isReady() || !DISCORD_GUILD_ID || !DISCORD_USER_ID) return offlinePresence;
|
||||
|
||||
try {
|
||||
const guild = await discordClient.guilds.fetch(DISCORD_GUILD_ID);
|
||||
const member = await guild.members.fetch(DISCORD_USER_ID);
|
||||
return toPresence(member);
|
||||
} catch {
|
||||
return offlinePresence;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user