DiscordRPC-NativeAOT
1.0.2
dotnet add package DiscordRPC-NativeAOT --version 1.0.2
NuGet\Install-Package DiscordRPC-NativeAOT -Version 1.0.2
<PackageReference Include="DiscordRPC-NativeAOT" Version="1.0.2" />
<PackageVersion Include="DiscordRPC-NativeAOT" Version="1.0.2" />
<PackageReference Include="DiscordRPC-NativeAOT" />
paket add DiscordRPC-NativeAOT --version 1.0.2
#r "nuget: DiscordRPC-NativeAOT, 1.0.2"
#:package DiscordRPC-NativeAOT@1.0.2
#addin nuget:?package=DiscordRPC-NativeAOT&version=1.0.2
#tool nuget:?package=DiscordRPC-NativeAOT&version=1.0.2
DiscordRPC-NativeAOT
English
A high-performance, Native AOT compatible Discord Rich Presence client for .NET 9.0.
✨ Features
- ✅ Native AOT Compatible - Zero reflection, fully trimmable
- ✅ System.Text.Json Source Generation - Optimized JSON serialization
- ✅ Zero External Dependencies - No third-party libraries required
- ✅ Cross-Platform - Windows, Linux, macOS support
- ✅ Fixed Discord API Compatibility - Custom converters for Discord's string-to-number fields
📦 Installation
dotnet add package DiscordRPC-NativeAOT
🚀 Quick Start
using DiscordRPC;
using DiscordRPC.Logging;
var client = new DiscordRpcClient("YOUR_APPLICATION_ID")
{
Logger = new ConsoleLogger() { Level = LogLevel.Info }
};
client.OnReady += (sender, e) =>
{
Console.WriteLine($"Connected to Discord as {e.User.Username}");
};
client.Initialize();
client.SetPresence(new RichPresence()
{
Details = "Playing a game",
State = "In a match",
Timestamps = new Timestamps()
{
Start = DateTime.UtcNow
},
Assets = new Assets()
{
LargeImageKey = "large_image",
LargeImageText = "My Game",
SmallImageKey = "small_image",
SmallImageText = "Level 10"
}
});
// Keep application running
Console.ReadLine();
client.Dispose();
🔧 Changes from Original Repository
This fork is based on Lachee/discord-rpc-csharp with the following Native AOT compatibility fixes:
v1.0.1 - Native AOT JSON Deserialization Fixes
Problem: Discord API returns certain fields as strings (e.g., "id":"1164487009784569939", "discriminator":"0", "flags":0) but the library expects numeric types, causing System.Text.Json deserialization failures in Native AOT mode.
Solution: Added custom JSON converters:
UlongStringConverter- Handles Discord snowflake IDs// Discord returns: "id":"1164487009784569939" // C# expects: ulong IDIntStringConverter- Handles discriminator field// Discord returns: "discriminator":"0" // C# expects: int DiscriminatorEnumStringConverter<T>- Handles enum fields (flags, premium_type)// Discord returns: "flags":0 or "flags":"0" // C# expects: Flag Flags (enum)Named Pipe Timeout Fix - Added 5-second connection timeout to prevent infinite blocking:
_stream.Connect(5000); // Was: Connect(0)Enum Serialization - Added
JsonStringEnumConverter<T>toCommandandServerEventenums for proper Discord protocol compatibility.
Key Differences from Original
| Feature | Original | This Fork |
|---|---|---|
| Native AOT | ❌ Not supported | ✅ Fully compatible |
| JSON Library | Newtonsoft.Json | System.Text.Json (Source Gen) |
| User Field Types | Runtime conversion | Compile-time converters |
| Pipe Connection | No timeout | 5-second timeout |
| Trimming | Not trimmable | Fully trimmable |
📋 Requirements
- .NET 9.0 or later
- Discord Desktop Client running
🛠️ Native AOT Publishing
dotnet publish -c Release -r win-x64 --self-contained
📄 License
MIT License - Same as the original discord-rpc-csharp project.
🙏 Credits
- Original Library: Lachee/discord-rpc-csharp
- Native AOT Adaptation: samse1337
🐛 Issues
If you encounter any issues with Native AOT compatibility, please report them on GitHub Issues.
中文
一个高性能、Native AOT 兼容 的 Discord Rich Presence 客户端,适用于 .NET 9.0。
✨ 特性
- ✅ Native AOT 兼容 - 零反射,完全可裁剪
- ✅ System.Text.Json 源生成 - 优化的 JSON 序列化
- ✅ 零外部依赖 - 无需第三方库
- ✅ 跨平台 - 支持 Windows、Linux、macOS
- ✅ 修复 Discord API 兼容性 - 自定义转换器处理 Discord 的字符串转数字字段
📦 安装
dotnet add package DiscordRPC-NativeAOT
🚀 快速开始
using DiscordRPC;
using DiscordRPC.Logging;
var client = new DiscordRpcClient("你的应用ID")
{
Logger = new ConsoleLogger() { Level = LogLevel.Info }
};
client.OnReady += (sender, e) =>
{
Console.WriteLine($"已连接到 Discord:{e.User.Username}");
};
client.Initialize();
client.SetPresence(new RichPresence()
{
Details = "正在玩游戏",
State = "对战中",
Timestamps = new Timestamps()
{
Start = DateTime.UtcNow
},
Assets = new Assets()
{
LargeImageKey = "large_image",
LargeImageText = "我的游戏",
SmallImageKey = "small_image",
SmallImageText = "10 级"
}
});
// 保持程序运行
Console.ReadLine();
client.Dispose();
🔧 与源仓库的改动
此 Fork 基于 Lachee/discord-rpc-csharp,修复了以下 Native AOT 兼容性问题:
v1.0.1 - Native AOT JSON 反序列化修复
问题:Discord API 返回某些字段为字符串(如 "id":"1164487009784569939"、"discriminator":"0"、"flags":0),但库期望数值类型,导致在 Native AOT 模式下 System.Text.Json 反序列化失败。
解决方案:添加自定义 JSON 转换器:
UlongStringConverter- 处理 Discord 雪花 ID// Discord 返回:"id":"1164487009784569939" // C# 期望:ulong IDIntStringConverter- 处理 discriminator 字段// Discord 返回:"discriminator":"0" // C# 期望:int DiscriminatorEnumStringConverter<T>- 处理枚举字段(flags、premium_type)// Discord 返回:"flags":0 或 "flags":"0" // C# 期望:Flag Flags (枚举)命名管道超时修复 - 添加 5 秒连接超时防止无限阻塞:
_stream.Connect(5000); // 原为:Connect(0)枚举序列化 - 为
Command和ServerEvent枚举添加JsonStringEnumConverter<T>以支持 Discord 协议。
与原版的主要差异
| 特性 | 原版 | 此 Fork |
|---|---|---|
| Native AOT | ❌ 不支持 | ✅ 完全兼容 |
| JSON 库 | Newtonsoft.Json | System.Text.Json (源生成) |
| User 字段类型 | 运行时转换 | 编译时转换器 |
| 管道连接 | 无超时 | 5 秒超时 |
| 裁剪 | 不可裁剪 | 完全可裁剪 |
📋 要求
- .NET 9.0 或更高版本
- Discord 桌面客户端运行中
🛠️ Native AOT 发布
dotnet publish -c Release -r win-x64 --self-contained
📄 许可证
MIT 许可证 - 与原始 discord-rpc-csharp 项目相同。
🙏 致谢
- 原始库:Lachee/discord-rpc-csharp
- Native AOT 适配:samse1337
🐛 问题反馈
如果遇到 Native AOT 兼容性问题,请在 GitHub Issues 上报告。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
v1.0.2: Added bilingual (English/Chinese) README to NuGet package; Fixed author name to samse1337