A high-performance, Native AOT compatible Discord Rich Presence client for .NET 9.0.
- ✅ 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
dotnet add package DiscordRPC-NativeAOTusing 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();This fork is based on Lachee/discord-rpc-csharp with the following Native AOT compatibility 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 ID
-
IntStringConverter- Handles discriminator field// Discord returns: "discriminator":"0" // C# expects: int Discriminator
-
EnumStringConverter<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.
| 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 |
- .NET 9.0 or later
- Discord Desktop Client running
dotnet publish -c Release -r win-x64 --self-containedMIT License - Same as the original discord-rpc-csharp project.
- Original Library: Lachee/discord-rpc-csharp
- Native AOT Adaptation: samse1337
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-NativeAOTusing 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 兼容性问题:
问题:Discord API 返回某些字段为字符串(如 "id":"1164487009784569939"、"discriminator":"0"、"flags":0),但库期望数值类型,导致在 Native AOT 模式下 System.Text.Json 反序列化失败。
解决方案:添加自定义 JSON 转换器:
-
UlongStringConverter- 处理 Discord 雪花 ID// Discord 返回:"id":"1164487009784569939" // C# 期望:ulong ID
-
IntStringConverter- 处理 discriminator 字段// Discord 返回:"discriminator":"0" // C# 期望:int Discriminator
-
EnumStringConverter<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 桌面客户端运行中
dotnet publish -c Release -r win-x64 --self-containedMIT 许可证 - 与原始 discord-rpc-csharp 项目相同。
- 原始库:Lachee/discord-rpc-csharp
- Native AOT 适配:samse1337
如果遇到 Native AOT 兼容性问题,请在 GitHub Issues 上报告。