Skip to content

Conversation

@imsyy
Copy link
Owner

@imsyy imsyy commented Dec 29, 2025

感觉是有点问题,还是放上来和原来的比较一下

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @imsyy, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求对 Discord Rich Presence (RPC) 的实现进行了重大重构,将其核心逻辑从 Rust 原生模块迁移到 Electron 主进程中的 TypeScript。这一改变旨在简化 Discord RPC 的管理,使其能够更灵活地直接从 JavaScript 端控制,同时原生模块现在专注于 Windows 上的系统媒体传输控件 (SMTC) 功能。此外,还引入了一个新的设置项,允许用户独立控制原生 SMTC 的启用状态。

Highlights

  • Discord RPC 实现重构: Discord Rich Presence (RPC) 的核心逻辑已从 Rust 原生模块迁移到 Electron 主进程中的 TypeScript 实现。这使得 Discord RPC 的管理更加灵活,并与原生系统媒体传输控件 (SMTC) 功能分离。
  • 新增 Discord RPC 管理器: 引入了一个新的 electron/main/utils/discord-rpc.ts 文件,其中包含 DiscordRpcManager 类,用于处理 Discord RPC 客户端的连接、活动更新和配置。所有 Discord RPC 相关的 IPC 事件现在都路由到这个新的管理器。
  • 原生 SMTC 模块精简: Rust 原生模块 smtc-for-splayer 不再直接处理 Discord RPC。其相关的函数、依赖和类型定义(如 Cargo.lockCargo.tomlindex.d.tssrc/discord.rs)已被移除或更新,使其专注于 SMTC 功能。
  • 新增原生 SMTC 开关: 在设置中新增了一个 enableNativeSmtc 选项,允许用户在 Windows 系统上选择是否启用原生 SMTC 支持。这使得用户可以根据需要独立控制原生 SMTC 和 Discord RPC 功能。
  • IPC 和播放器逻辑更新: IPC 通道和播放器核心逻辑(MediaSessionManager.tsPlayerController.ts)已更新,以区分并同时支持新的 JavaScript Discord RPC 实现和原有的原生 SMTC 功能。Discord RPC 更新现在通过新的 sendDiscord* IPC 函数发送。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

此次 Pull Request 成功地将 Discord RPC 功能从 Rust 重构到了 TypeScript,这简化了原生模块,并将相关逻辑集中到了 Electron 主进程中。新的 DiscordRpcManager 结构清晰,很好地处理了客户端生命周期和活动状态更新。

我发现了一些可以改进的地方:

  • MediaSessionManager 中存在一个高严重性缺陷,它导致在 Windows 上禁用原生 SMTC 后,无法回退使用标准的 navigator.mediaSession
  • ipc-smtc.ts 中存在一些重复代码,可以通过重构来提高可维护性。
  • 新的 Discord RPC 实现中有一个小逻辑问题,可能导致按钮链接不正确。
  • 通过为 Discord RPC 元数据定义一个专用类型,可以提高类型安全性。

总的来说,这是一个很棒的重构。解决这些问题将使实现更加健壮和易于维护。

* @returns 歌曲链接
*/
private processSongUrl(ncmId?: number): string | null {
if (!ncmId) return "https://music.163.com/";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

ncmId 不存在时,返回网易云音乐首页链接可能不是预期的行为。这会导致即使用户正在收听本地音乐或来自其他来源的音乐,Discord 上也会显示一个指向网易云音乐首页的按钮。更好的做法是在没有 ncmId 时返回 null,这样就不会显示“收听”按钮,从而提供更准确的用户体验。

Suggested change
if (!ncmId) return "https://music.163.com/";
if (!ncmId) return null;

Comment on lines 20 to 22
"discord-update-metadata": MetadataParam;
"discord-update-play-state": PlayStatePayload;
"discord-update-timeline": TimelinePayload;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

discord-update-metadata 等 Discord 相关事件重用来自原生模块的 MetadataParam 类型可能会引起混淆,因为这两个接口的用途和所需字段不同。例如,Discord RPC 实现不需要 coverData,但原生 SMTC 需要。

为了提高类型安全性和代码清晰度,建议为 Discord RPC 的元数据、播放状态和时间线定义专门的类型(例如 DiscordMetadataParam),并在 IpcChannelMap 中使用它们。这将使接口意图更加明确,并与刚刚分离的 Discord RPC 逻辑保持一致。

@imsyy
Copy link
Owner Author

imsyy commented Dec 29, 2025

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

这次的 PR 主要将 Discord RPC 功能从原生 Rust 模块重构到了 Electron 主进程的 TypeScript 模块中。这是一个很好的架构改进,它简化了原生插件,使得 Discord 集成功能更容易维护。相关的改动,包括 IPC 处理、状态管理和构建脚本的更新,都组织得很好。

我发现了一个与新的 Discord RPC 配置逻辑相关的严重 bug,它会导致类型不匹配和运行时错误。另外,我还提出了一个小的改进建议,以便在重构后清理原生模块中不再需要的数据。

总的来说,这是一个不错的 PR,提升了代码库的质量。在修复了那个严重问题后,应该就可以合并了。

@imsyy imsyy changed the title ✨ feat: Discord RPC JS 实现 ✨ feat: Discord RPC 插件分离 Dec 29, 2025
@imsyy
Copy link
Owner Author

imsyy commented Dec 29, 2025

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

这个 PR 很好地将 Discord RPC 功能从 SMTC 模块中分离出来,创建了一个新的原生模块。这对于代码的模块化和长期维护性来说是一个很棒的改进。代码结构更清晰,职责更分明。

我发现了一些可以改进的地方,主要集中在代码一致性和可读性上。例如,建议统一 IPC 通道命名,并对一些较长的函数进行重构。此外,我还发现了一个关于 Rust Edition 的配置问题,可能会导致编译失败,需要注意。

总的来说,这是一个高质量的重构,我的建议旨在让代码库更加健壮和易于维护。

@imsyy
Copy link
Owner Author

imsyy commented Dec 29, 2025

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

本次 PR 成功地将 Discord RPC 功能从 SMTC 模块中分离出来,创建了一个独立的 discord-rpc-for-splayer 原生模块。这次重构使得代码结构更清晰,权责更分明。同时,前端代码也进行了相应的适配,引入了更通用的 IPC 处理函数,并调整了相关设置项。

我发现了一些可以改进的地方,主要集中在新的 Rust 原生模块中,包括使用了不稳定的 Rust edition、一个潜在的逻辑 bug 和资源管理方面的问题。请查看具体的 review comments。

Comment on lines 338 to 342
if let Some(last_end) = last_sent_end_timestamp {
let diff = (*last_end - end).abs();
if diff < TIMESTAMP_UPDATE_THRESHOLD_MS {
return true;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

时间戳更新节流的逻辑似乎存在问题。diff 是以秒为单位计算的,但它与 TIMESTAMP_UPDATE_THRESHOLD_MS(值为100,且从命名上看单位应为毫秒)进行比较。这导致了一个100秒的阈值,太大了,会妨碍在拖动进度条后及时更新状态。

建议将阈值调整为一个较小的值(例如2秒),以正确检测跳转操作。

Suggested change
if let Some(last_end) = last_sent_end_timestamp {
let diff = (*last_end - end).abs();
if diff < TIMESTAMP_UPDATE_THRESHOLD_MS {
return true;
}
if let Some(last_end) = last_sent_end_timestamp {
let diff = (*last_end - end).abs();
// Using a smaller threshold (e.g., 2 seconds) to correctly detect seeks.
if diff < 2 {
return true;
}

Comment on lines 34 to 36
pub fn shutdown() {
discord_core::disable();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

shutdown 函数目前调用 discord_core::disable(),但这仅向后台线程发送一条消息,并不会终止它,可能导致资源泄漏。建议实现一个真正的关闭逻辑来停止线程。

这可以通过在 discord_core.rs 中添加一个 shutdown 函数来销毁 Sender 实现,这将导致后台线程的 recv() 调用失败并退出循环。

例如,在 discord_core.rs 中添加:

pub fn shutdown() {
    if let Ok(mut guard) = SENDER.lock() {
        if guard.take().is_some() {
            info!("Shutting down Discord RPC thread.");
        }
    }
}

然后在 lib.rs 中调用这个新函数。

Suggested change
pub fn shutdown() {
discord_core::disable();
}
pub fn shutdown() {
discord_core::shutdown();
}

}

fn build_base_activity(data: &ActivityData, display_mode: DiscordDisplayMode) -> Activity<'_> {
fn build_base_activity(data: &ActivityData, _display_mode: DiscordDisplayMode) -> Activity<'_> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

参数 _display_mode 被标记为未使用。由于 StatusDisplayType 在当前版本的 discord-rich-presence 中已不再是 Activity 的一部分,这个参数不再需要,可以从函数签名中移除,以提高代码的清晰度。

Suggested change
fn build_base_activity(data: &ActivityData, _display_mode: DiscordDisplayMode) -> Activity<'_> {
fn build_base_activity(data: &ActivityData) -> Activity<'_> {

@imsyy
Copy link
Owner Author

imsyy commented Dec 29, 2025

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the Discord Rich Presence (RPC) functionality into a new, dedicated native Rust module named discord-rpc-for-splayer, separating it from the existing smtc-for-splayer module. The Cargo.lock and Cargo.toml files are updated to reflect the new module and its dependencies, including a version downgrade for discord-rich-presence and the addition of numerous icu_* related packages. The electron/main/ipc/ipc-smtc.ts file is modified to load and manage both native modules independently, introducing a generic handler for IPC communication and distinct channels for Discord RPC. The new discord-rpc-for-splayer module includes its own NAPI bindings, build scripts, TypeScript definitions, and core Rust logic for managing Discord activity, with improvements to cover asset handling (specifically for Netease Cloud Music) and timestamp calculations. The smtc-for-splayer module is streamlined by removing its direct Discord RPC dependencies and related code. On the frontend, the PlaySetting.vue component's SMTC toggle is moved to ThirdSetting.vue, and a new setting enableNativeSmtc is introduced for Windows users to control native SMTC support. The MediaSessionManager.ts and PlayerController.ts files are updated to conditionally send Discord RPC updates via the new module, and the PlayerIpc.ts file now includes new functions for Discord RPC communication. The src/types/global.d.ts and src/types/smtc.ts files are adjusted to reflect the new Discord RPC types and enum changes. A review comment suggests changing the Rust edition from 2024 to 2021 in the new discord-rpc-for-splayer/Cargo.toml due to the instability of Rust 2024.

[package]
name = "discord-rpc-for-splayer"
version = "0.1.0"
edition = "2024"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

edition = "2024" 的使用存在风险。Rust 2024 版本目前还不稳定,只在 nightly 工具链中可用。这可能会导致使用 stable Rust 的用户在编译时遇到问题。建议在 2024 版本稳定之前,继续使用最新的稳定版本 "2021"

Suggested change
edition = "2024"
edition = "2021"

@imsyy imsyy marked this pull request as ready for review December 29, 2025 16:11
@imsyy imsyy merged commit e00dfa1 into dev Dec 29, 2025
4 checks passed
@imsyy imsyy deleted the dev-smtc-fix branch December 29, 2025 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants