一键导入
discord-net-worker-service
Pattern for hosting Discord.NET bot inside a .NET generic host BackgroundService
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Pattern for hosting Discord.NET bot inside a .NET generic host BackgroundService
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Full-stack acceptance testing with Aspire.Hosting.Testing — launching entire AppHost including containers
How to wire up .NET Aspire 13.1 AppHost with containers, databases, and service projects
Patterns for integration testing Aspire-hosted .NET services with WebApplicationFactory, Testcontainers, and SQLite
Patterns for setting up a .NET CI pipeline with GitHub Actions and xUnit test projects
| name | discord-net-worker-service |
| description | Pattern for hosting Discord.NET bot inside a .NET generic host BackgroundService |
| domain | integration |
| confidence | low |
| source | earned |
When building a Discord bot as part of a larger .NET service architecture (Aspire, microservices), the bot needs to integrate with the generic host lifecycle rather than running standalone.
DiscordSocketConfig and DiscordSocketClient as singletons in DIBackgroundService that receives the client via constructor injectionExecuteAsync: wire events → LoginAsync → StartAsync → Task.Delay(Infinite, token) → StopAsyncLogSeverity to Microsoft.Extensions.Logging.LogLevel via the client.Log eventclient.Ready event (gateway is fully connected at that point)client.SlashCommandExecuted eventIConfiguration["Discord:BotToken"] — works with user secrets and env varsclient.ChannelCreated, client.ChannelDestroyed, client.ChannelUpdated — detect category vs text channel with is ICategoryChannelIConnectionMultiplexer (from Aspire's AddRedisClient) to get ISubscriber for Redis pub/subRedisChannel.Literal(channelName) for publish — avoid pattern-matching overheadChannelUpdated events to only publish when meaningful properties change (name, position) — Discord fires this event for many property changes// Program.cs
builder.Services.AddSingleton(new DiscordSocketConfig
{
GatewayIntents = GatewayIntents.Guilds,
LogLevel = LogSeverity.Info
});
builder.Services.AddSingleton<DiscordSocketClient>();
builder.Services.AddHostedService<DiscordBotWorker>();
// Graceful shutdown in ExecuteAsync
try { await Task.Delay(Timeout.Infinite, stoppingToken); }
catch (OperationCanceledException) { }
await client.StopAsync();
DiscordSocketClient per-request or as transient — it manages a persistent WebSocketReady event — the client isn't connected yetwhile (!token.IsCancellationRequested) { await Task.Delay(...) } — Task.Delay(Infinite) is cleaner and avoids unnecessary wake-ups