From ac1a0230cecfa21f3bb74ebbdf7c6e3236203be5 Mon Sep 17 00:00:00 2001 From: Tony Date: Sun, 18 Jan 2026 08:58:27 +0800 Subject: [PATCH] feat(route): discord quests (#20911) * feat(route): discord quests * fix(route): handle missing Discord authorization config --- lib/routes/discord/discord-api.ts | 38 +++++ lib/routes/discord/quest.ts | 61 ++++++++ lib/routes/discord/types.d.ts | 236 ++++++++++++++++++++++++++++++ 3 files changed, 335 insertions(+) create mode 100644 lib/routes/discord/quest.ts create mode 100644 lib/routes/discord/types.d.ts diff --git a/lib/routes/discord/discord-api.ts b/lib/routes/discord/discord-api.ts index d94c5508a..13187d95a 100644 --- a/lib/routes/discord/discord-api.ts +++ b/lib/routes/discord/discord-api.ts @@ -1,3 +1,5 @@ +import crypto from 'node:crypto'; + import type { RESTGetAPIChannelMessagesQuery, RESTGetAPIChannelMessagesResult, RESTGetAPIChannelResult, RESTGetAPIGuildChannelsResult, RESTGetAPIGuildResult } from 'discord-api-types/rest/v10'; import type { APIMessage } from 'discord-api-types/v10'; @@ -5,6 +7,8 @@ import { config } from '@/config'; import cache from '@/utils/cache'; import ofetch from '@/utils/ofetch'; +import type { QuestResponse } from './types'; + export const baseUrl = 'https://discord.com'; const apiUrl = `${baseUrl}/api/v10`; @@ -90,3 +94,37 @@ export const searchGuildMessages = (guildId: string, authorization: string, para config.cache.routeExpire, false ) as Promise; + +export const getQuests = (authorization: string) => + cache.tryGet( + 'discord:quests', + () => + ofetch(`${apiUrl}/quests/@me`, { + headers: { + authorization, + 'X-Super-Properties': Buffer.from( + JSON.stringify({ + os: 'Windows', + browser: 'Chrome', + device: '', + system_locale: 'en-GB', + has_client_mods: false, + browser_user_agent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', + browser_version: '120.0', + os_version: '10', + referrer: '', + referring_domain: '', + referrer_current: '', + referring_domain_current: '', + release_channel: 'stable', + client_build_number: Math.floor(Math.random() * (500000 - 400000 + 1)) + 400000, + client_event_source: null, + client_launch_id: crypto.randomUUID(), + client_app_state: 'unfocused', + }) + ).toString('base64'), + }, + }), + config.cache.routeExpire, + false + ) as Promise; diff --git a/lib/routes/discord/quest.ts b/lib/routes/discord/quest.ts new file mode 100644 index 000000000..0cbaa6e62 --- /dev/null +++ b/lib/routes/discord/quest.ts @@ -0,0 +1,61 @@ +import { config } from '@/config'; +import ConfigNotFoundError from '@/errors/types/config-not-found'; +import type { DataItem, Route } from '@/types'; +import { parseDate } from '@/utils/parse-date'; + +import { baseUrl, getQuests } from './discord-api'; + +export const route: Route = { + path: '/quests', + categories: ['social-media'], + example: '/discord/quests', + features: { + requireConfig: [ + { + name: 'DISCORD_AUTHORIZATION', + description: 'Discord authorization header', + }, + ], + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['discord.com/quest-home'], + }, + ], + name: 'Quests', + maintainers: ['TonyRL'], + handler, +}; + +async function handler() { + const { authorization } = config.discord || {}; + if (!authorization) { + throw new ConfigNotFoundError('Discord RSS is disabled due to the lack of authorization config'); + } + + const questData = await getQuests(authorization); + + const items = questData.quests.map((quest) => { + const tasks = Object.values(quest.config.task_config.tasks).map((task) => task.event_name); + return { + title: `${quest.config.messages.quest_name} - Claim ${quest.config.rewards_config.rewards[0].messages.name}`, + description: tasks.join(', '), + author: quest.config.messages.game_publisher, + pubDate: parseDate(quest.config.starts_at), + category: tasks, + link: quest.config.application.link.split('?')[0], + guid: quest.id, + }; + }); + + return { + title: 'Available Quests - Discord', + link: `${baseUrl}/quest-home`, + item: items satisfies DataItem[], + }; +} diff --git a/lib/routes/discord/types.d.ts b/lib/routes/discord/types.d.ts new file mode 100644 index 000000000..589c9c734 --- /dev/null +++ b/lib/routes/discord/types.d.ts @@ -0,0 +1,236 @@ +interface Application { + link: string; + id: string; + name: string; +} + +interface Assets { + hero: string; + hero_video: string | null; + quest_bar_hero: string; + quest_bar_hero_video: string | null; + game_tile: string; + logotype: string; + game_tile_light: string; + game_tile_dark: string; + logotype_light: string; + logotype_dark: string; +} + +interface Colors { + primary: string; + secondary: string; +} + +interface Messages { + quest_name: string; + game_title: string; + game_publisher: string; +} + +interface TaskEvent { + event_name: string; + target: number; + external_ids: string[]; +} + +interface TaskEventNoExternalIds { + event_name: string; + target: number; + external_ids: never[]; +} + +interface TaskConfig { + type: number; + join_operator: string; + tasks: { + PLAY_ON_XBOX?: TaskEvent; + PLAY_ON_DESKTOP?: TaskEventNoExternalIds; + PLAY_ON_PLAYSTATION?: TaskEvent; + WATCH_VIDEO_ON_MOBILE?: TaskEventNoExternalIds; + WATCH_VIDEO?: TaskEventNoExternalIds; + }; +} + +interface ApplicationRef { + id: string; +} + +interface TaskV2Base { + type: string; + target: number; +} + +interface PlayTask extends TaskV2Base { + applications: ApplicationRef[]; + external_ids?: string[]; +} + +interface VideoAsset { + url: string; + width: number; + height: number; + thumbnail: string; + caption?: string; + transcript?: string; +} + +interface VideoTaskAssets { + video: VideoAsset; + video_low_res: VideoAsset; + video_hls: VideoAsset; +} + +interface VideoTaskMessages { + video_title: string; + video_end_cta_title?: string; + video_end_cta_subtitle?: string; + video_end_cta_button_label?: string; +} + +interface WatchVideoTask extends TaskV2Base { + assets: VideoTaskAssets; + messages: VideoTaskMessages; +} + +interface TaskConfigV2 { + tasks: { + PLAY_ON_XBOX?: PlayTask; + PLAY_ON_DESKTOP?: PlayTask; + PLAY_ON_PLAYSTATION?: PlayTask; + WATCH_VIDEO_ON_MOBILE?: WatchVideoTask; + WATCH_VIDEO?: WatchVideoTask; + }; + join_operator: string; +} + +interface RewardMessages { + name: string; + name_with_article: string; + redemption_instructions_by_platform: { + '0': string; + }; +} + +interface Reward { + type: number; + sku_id: string; + messages: RewardMessages; + orb_quantity?: number; + asset?: string; + asset_video?: null; + approximate_count?: null; + redemption_link?: null; + expires_at?: string; + expires_at_premium?: null; + expiration_mode?: number; +} + +interface RewardsConfig { + assignment_method: number; + rewards: Reward[]; + rewards_expire_at: string; + platforms: number[]; +} + +interface CTAConfig { + link: string; + button_label: string; + android?: { + android_app_id: string; + }; + ios?: { + ios_app_id: string; + }; + subtitle?: string; +} + +interface VideoMetadataMessages { + video_title: string; + video_end_cta_button_label: string; + video_end_cta_title?: string; + video_end_cta_subtitle?: string; +} + +interface VideoMetadataAssets { + video_player_video_hls: string; + video_player_video: string; + video_player_thumbnail: string; + video_player_video_low_res: string; + video_player_caption: string | null; + video_player_transcript: string | null; + quest_bar_preview_video: null; + quest_bar_preview_thumbnail: null; + quest_home_video: null; +} + +interface VideoMetadata { + messages: VideoMetadataMessages; + assets: VideoMetadataAssets; +} + +interface QuestConfig { + id: string; + config_version: number; + starts_at: string; + expires_at: string; + features: number[]; + application: Application; + assets: Assets; + colors: Colors; + messages: Messages; + task_config: TaskConfig; + task_config_v2: TaskConfigV2; + rewards_config: RewardsConfig; + share_policy: string; + cta_config: CTAConfig; + video_metadata?: VideoMetadata; +} + +interface ProgressHeartbeat { + last_beat_at: string; + expires_at: null; +} + +interface ProgressTask { + value: number; + event_name: string; + updated_at: string; + completed_at: string; + heartbeat: ProgressHeartbeat | null; +} + +interface UserProgress { + PLAY_ON_DESKTOP?: ProgressTask; + PLAY_ON_XBOX?: ProgressTask; + PLAY_ON_PLAYSTATION?: ProgressTask; + WATCH_VIDEO_ON_MOBILE?: ProgressTask; + WATCH_VIDEO?: ProgressTask; +} + +interface UserStatus { + user_id: string; + quest_id: string; + enrolled_at: string; + completed_at: string; + claimed_at: string; + claimed_tier: null; + last_stream_heartbeat_at: null; + stream_progress_seconds: number; + dismissed_quest_content: number; + progress: UserProgress; +} + +interface Quest { + id: string; + config: QuestConfig; + user_status: UserStatus | null; + targeted_content: never[]; + preview: boolean; + traffic_metadata_raw: string; + traffic_metadata_sealed: string; +} + +export interface QuestResponse { + quests: Quest[]; +}