Migration from 4.x to 5.x
Note
The API surface of DSharpPlus 5.x is not stable yet. This migration guide may be incomplete or outdated. It is recommended to cross-reference with the tracking issue when migrating.
The first change you will likely encounter is a rewrite to how bots are set up. We now support two approaches instead of the old approach:
The simplest way to get a bot running is to use DSharpPlus.DiscordClientBuilder
. To get started, create a new builder as follows:
DiscordClientBuilder builder = DiscordClientBuilder.CreateDefault(string token, DiscordIntents intents);
Instead, if you are sharding, create it as follows:
DiscordClientBuilder builder = DiscordClientBuilder.CreateSharded(string token, DiscordIntents intents, uint? shardCount);
You may omit the shard count to instead defer to Discord's recommended shard count.
Then, migrate your configuration options. Rest-related settings from your old DiscordConfiguration are covered by DiscordClientBuilder.ConfigureRestClient
, gateway-related settings are covered by DiscordClientBuilder.ConfigureGatewayClient
.
LogLevel
has been migrated to DiscordClientBuilder.SetLogLevel
, and configuring the gateway client is now done through either overriding or decorating the default client via DiscordClientBuilder.ConfigureServices
. It is comprised of two parts, ITransportService
and IGatewayClient
Then, we will need to update event handling. For more information, see the dedicated article, but in short, events have also been migrated to DiscordClientBuilder: events are now handled through DiscordClientBuilder.ConfigureEventHandlers
. You can register handlers on the configuration delegate as follows:
builder.ConfigureEventHandlers
(
b => b.HandleMessageCreated(async (s, e) =>
{
if (e.Message.Content.ToLower().StartsWith("spiderman"))
{
await e.Message.RespondAsync("I want pictures of Spiderman!");
}
})
.HandleGuildMemberAdded(OnGuildMemberAdded)
);
private Task OnGuildMemberAdded(DiscordClient sender, GuildMemberAddedEventArgs args)
{
// non-asynchronous code here
return Task.CompletedTask;
}
Lastly, we'll need to migrate our extensions. Our extensions have an UseExtensionName
method for DiscordClientBuilder, similar to how they previously had such extensions for DiscordClient. Some extensions take an additional Action<ExtensionType>
parameter you can use to configure the extension, like so:
builder.UseCommandsExtension
(
extension =>
{
extension.AddCommands([typeof(MyCommands)]);
extension.AddParameterCheck(typeof(MyCheck))
}
);