![]() |
VOOZH | about |
dotnet add package Discord.Addons.Hosting --version 6.1.0
NuGet\Install-Package Discord.Addons.Hosting -Version 6.1.0
<PackageReference Include="Discord.Addons.Hosting" Version="6.1.0" />
<PackageVersion Include="Discord.Addons.Hosting" Version="6.1.0" />Directory.Packages.props
<PackageReference Include="Discord.Addons.Hosting" />Project file
paket add Discord.Addons.Hosting --version 6.1.0
#r "nuget: Discord.Addons.Hosting, 6.1.0"
#:package Discord.Addons.Hosting@6.1.0
#addin nuget:?package=Discord.Addons.Hosting&version=6.1.0Install as a Cake Addin
#tool nuget:?package=Discord.Addons.Hosting&version=6.1.0Install as a Cake Tool
Discord.NET hosting with Microsoft.Extensions.Hosting.
This package provides extensions that will run a Discord.NET socket/sharded client as an IHostedService, featuring:
✅ Simplified, best practice bot creation with a reduction in boilerplate.
✅ Instant wire-up of Logging and Dependency Injection support.
✅ Extensions to easily run startup & background tasks involving the Discord Client.
✅ Easy integration with other generic host consumers, such as ASP.NET Core.
.NET 6.0+ is required.
// CreateApplicationBuilder configures a lot of stuff for us automatically
// See: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host
var builder = Host.CreateApplicationBuilder(args);
// Configure Discord.NET
builder.Services.AddDiscordHost((config, _) =>
{
config.SocketConfig = new DiscordSocketConfig
{
LogLevel = LogSeverity.Verbose,
AlwaysDownloadUsers = true,
MessageCacheSize = 200,
GatewayIntents = GatewayIntents.All
};
config.Token = builder.Configuration["Token"]!;
});
// Optionally wire up the command service
builder.Services.AddCommandService((config, _) =>
{
config.DefaultRunMode = RunMode.Async;
config.CaseSensitiveCommands = false;
});
// Optionally wire up the interaction service
builder.Services.AddInteractionService((config, _) =>
{
config.LogLevel = LogSeverity.Info;
config.UseCompiledLambda = true;
});
// Add any other services here
builder.Services.AddHostedService<CommandHandler>();
builder.Services.AddHostedService<InteractionHandler>();
builder.Services.AddHostedService<BotStatusService>();
builder.Services.AddHostedService<LongRunningService>();
var host = builder.Build();
await host.RunAsync();
dotnet new worker -o MyWorkerService)Discord.Addons.Hosting to your project.dotnet user-secrets set "token" "your-token-here"appsettings.jsonbuilder.Services.AddDiscordHost.CommandService and/or the InteractionService with builder.Services.AddCommandService and builder.Services.AddInteractionServiceFully working examples are available here
To use the sharded client instead of the socket client, simply replace ConfigureDiscordHost with ConfigureDiscordShardedHost:
.AddDiscordShardedHost((config, _) =>
{
config.SocketConfig = new DiscordSocketConfig
{
// Manually set the required shards, or leave empty for the recommended count
TotalShards = 4
};
config.Token = context.Configuration["token"];
})
Microsoft's default logging has an unfortunate default output format, so I highly recommend using Serilog instead of the standard Microsoft logging.
Serilog should be added to the host with Serilog.Extensions.Hosting.
See the Serilog example for usage.
This section assumes some prior knowledge of Dependency Injection within the .NET ecosystem. Take a read of this if you have no idea what any of this means.
During bot development, it's highly like you'll require the ability to execute code immediately after startup, such as setting the bot's status, reaching out to a web server, registering an event, or kicking off the continuous execution of code in the background. Given we're using the generic host and have its IHostedService & BackgroundService capabilities in our toolbelt, this is easily achievable in a clean and concise way.
This package ships with the DiscordClientService and DiscordShardedClientService base classes for the socket client and sharded client respectively. For convenience, both of them expose the Client and Logger. Simply inherit from the given type, implement the required constructor, place your execution requirements within ExecuteAsync and register the service with your service collection via services.AddHostedService.
public class CommandHandler : DiscordClientService
{
private readonly IServiceProvider _provider;
private readonly CommandService _commandService;
private readonly IConfiguration _config;
public CommandHandler(DiscordSocketClient client, ILogger<CommandHandler> logger, IServiceProvider provider, CommandService commandService, IConfiguration config) : base(client, logger)
{
_provider = provider;
_commandService = commandService;
_config = config;
}
// This'll be executed during startup.
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
Client.MessageReceived += HandleMessage;
_commandService.CommandExecuted += CommandExecutedAsync;
await _commandService.AddModulesAsync(Assembly.GetEntryAssembly(), _provider);
}
//.....
}
builder.Services.AddHostedService<CommandHandler>();
The WaitForReadyAsync extension method is also available for both client types to await execution of your service until the client has reached a Ready state:
public class BotStatusService : DiscordClientService
{
public BotStatusService(DiscordSocketClient client, ILogger<DiscordClientService> logger) : base(client, logger)
{
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
// Wait for the client to be ready before setting the status
await Client.WaitForReadyAsync(stoppingToken);
Logger.LogInformation("Client is ready!");
await Client.SetActivityAsync(new Game("Set my status!"));
}
}
Services that do not require access to the Discord Client should use an implementation of BackgroundService.
Services with complex startup & shutdown activities should implement IHostedService directly.
When shutdown is requested, the host will wait a maximum of 5 seconds for services to stop before timing out.
If you're finding that this isn't enough time, you can modify the shutdown timeout via the ShutdownTimeout host setting.
This package uses Microsoft.Extensions.Options internally, so both the DiscordHostConfiguration and CommandServiceConfig can be configured within the services registration instead of within the HostBuilder extensions if it better suits your scenario.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
Showing the top 2 NuGet packages that depend on Discord.Addons.Hosting:
| Package | Downloads |
|---|---|
|
Discord.Addons.ChainHandlers
Simple Discord.Net chain handler implementation |
|
|
FoundryBot
Package Description |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 6.1.0 | 8,426 | 6/10/2024 | |
| 6.0.0 | 3,727 | 1/14/2024 | |
| 5.2.0 | 8,889 | 11/6/2022 | |
| 5.1.0 | 6,832 | 1/30/2022 | |
| 5.1.0-labs | 1,023 | 1/30/2022 | 5.1.0-labs is deprecated. |
| 5.0.0 | 1,987 | 12/19/2021 | |
| 4.0.3-labs | 1,064 | 11/16/2021 | 4.0.3-labs is deprecated. |
| 4.0.2 | 5,054 | 6/19/2021 | 4.0.2 is deprecated. |
| 4.0.2-labs | 1,362 | 8/3/2021 | 4.0.2-labs is deprecated. |
| 4.0.1 | 1,620 | 6/14/2021 | 4.0.1 is deprecated. |
| 3.1.1 | 4,583 | 11/20/2020 | 3.1.1 is deprecated. |
| 3.1.0 | 1,821 | 11/15/2020 | 3.1.0 is deprecated. |
| 3.0.0 | 4,107 | 6/14/2020 | 3.0.0 is deprecated. |
| 2.1.0 | 2,161 | 11/14/2019 | 2.1.0 is deprecated. |
| 2.0.0 | 1,821 | 10/10/2019 | 2.0.0 is deprecated. |
| 1.3.0 | 1,966 | 2/10/2019 | 1.3.0 is deprecated. |
| 1.2.1 | 1,909 | 1/6/2019 | 1.2.1 is deprecated. |