Skip to content

Commit

Permalink
Update to latest discord host & packages (#74)
Browse files Browse the repository at this point in the history
# Motivations
Keeping up-to-date

# Modifications
- Upgrade to 6.x of the Discord host extensions package
- Upgrade all other packages to latest
  • Loading branch information
Twinki14 authored Jul 1, 2024
1 parent 9581d01 commit 79631f5
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 62 deletions.
6 changes: 3 additions & 3 deletions src/Miha.Discord/Miha.Discord.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

<ItemGroup>
<PackageReference Include="Cronos" Version="0.8.4" />
<PackageReference Include="Discord.Addons.Hosting" Version="5.2.0" />
<PackageReference Include="Discord.Net" Version="3.14.1" />
<PackageReference Include="Discord.Addons.Hosting" Version="6.1.0" />
<PackageReference Include="Discord.Net" Version="3.15.2" />
<PackageReference Include="Humanizer" Version="2.14.1" />
<PackageReference Include="SlimMessageBus" Version="2.0.2" />
<PackageReference Include="SlimMessageBus.Host.Memory" Version="2.2.3" />
<PackageReference Include="SlimMessageBus.Host.Memory" Version="2.3.5" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Miha.Logic/Miha.Logic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Discord.Addons.Hosting" Version="5.2.0" />
<PackageReference Include="Discord.Net" Version="3.14.1" />
<PackageReference Include="Discord.Addons.Hosting" Version="6.1.0" />
<PackageReference Include="Discord.Net" Version="3.15.2" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/Miha.Redis/Miha.Redis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentResults" Version="3.15.2" />
<PackageReference Include="FluentResults.Extensions.FluentAssertions" Version="2.1.1" />
<PackageReference Include="Redis.OM" Version="0.6.1" />
<PackageReference Include="FluentResults" Version="3.16.0" />
<PackageReference Include="FluentResults.Extensions.FluentAssertions" Version="2.1.2" />
<PackageReference Include="Redis.OM" Version="0.7.1" />
</ItemGroup>

<ItemGroup>
Expand Down
16 changes: 8 additions & 8 deletions src/Miha.Shared/Miha.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="GitInfo" Version="3.3.3">
<PackageReference Include="GitInfo" Version="3.3.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.1" />
<PackageReference Include="NodaTime" Version="3.1.10" />
<PackageReference Include="NodaTime.Serialization.SystemTextJson" Version="1.1.2" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageReference Include="NodaTime" Version="3.1.11" />
<PackageReference Include="NodaTime.Serialization.SystemTextJson" Version="1.2.0" />
<PackageReference Include="Serilog" Version="4.0.0" />
<PackageReference Include="Serilog.Exceptions" Version="8.4.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Serilog.Formatting.Compact" Version="2.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Formatting.Compact" Version="3.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="TimeZoneConverter" Version="6.1.0" />
</ItemGroup>

Expand Down
50 changes: 5 additions & 45 deletions src/Miha/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Discord;
using Discord.Addons.Hosting;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -30,7 +27,7 @@
builder.AddJsonFile("appsettings.edge.json", optional: true, reloadOnChange: false);
}

// Disable ReloadOnChange for all sources, we don't intend to support this
// Disable ReloadOnChange for all sources, we don't intend to support this,
// and it creates a lot of inotify issues on docker hosts running on linux
foreach (var s in builder.Sources)
{
Expand All @@ -45,53 +42,16 @@
.Enrich.FromLogContext()
.Enrich.WithExceptionDetails()
.WriteTo.Console(new CompactJsonFormatter()))
.ConfigureDiscordHost((context, config) =>
{
var discordOptions = context.Configuration.GetSection(DiscordOptions.Section).Get<DiscordOptions>();

if (string.IsNullOrEmpty(discordOptions?.Token))
{
throw new ArgumentNullException(nameof(discordOptions.Token), "Discord token cannot be empty or null");
}

if (discordOptions.Guild is null)
{
throw new ArgumentNullException(nameof(discordOptions.Guild), "Need a target guild id, Miha does not support multi-guilds yet");
}

config.SocketConfig = new DiscordSocketConfig
{
LogLevel = LogSeverity.Verbose,
LogGatewayIntentWarnings = true,
AlwaysDownloadUsers = true,
GatewayIntents = GatewayIntents.GuildScheduledEvents
| GatewayIntents.DirectMessageTyping
| GatewayIntents.DirectMessageReactions
| GatewayIntents.DirectMessages
| GatewayIntents.GuildMessageTyping
| GatewayIntents.GuildMessageReactions
| GatewayIntents.GuildMessages
| GatewayIntents.GuildVoiceStates
| GatewayIntents.Guilds
| GatewayIntents.GuildMembers
};

config.Token = discordOptions.Token;
})
.UseInteractionService((_, config) =>
{
config.LogLevel = LogSeverity.Verbose;
config.UseCompiledLambda = true;
})
.ConfigureServices(Startup.ConfigureServices)
.Build();

var discordOptions = host.Services.GetRequiredService<IOptions<DiscordOptions>>();
var seedService = host.Services.GetRequiredService<RedisSeedService>();

await seedService.SeedGuildAsync(discordOptions.Value.Guild);

await host.RunAsync();

return 0;
}
catch (Exception ex)
Expand Down
50 changes: 49 additions & 1 deletion src/Miha/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using Microsoft.Extensions.Configuration;
using Discord;
using Discord.Addons.Hosting;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Miha.Logic;
using Miha.Redis;
using Miha.Discord;
Expand Down Expand Up @@ -31,6 +35,50 @@ public static void ConfigureServices(HostBuilderContext context, IServiceCollect
.AddDiscordHostedServices()
.AddDiscordMessageBus();

services
.AddDiscordHost((config, provider) =>
{
var discordOptions = provider.GetRequiredService<IOptions<DiscordOptions>>().Value;

if (string.IsNullOrEmpty(discordOptions.Token))
{
throw new ArgumentNullException(nameof(discordOptions.Token),
"Discord token cannot be empty or null");
}

if (discordOptions.Guild is null)
{
throw new ArgumentNullException(nameof(discordOptions.Guild),
"Need a target guild id, Miha does not support multi-guilds yet");
}

config.SocketConfig = new DiscordSocketConfig
{
LogLevel = LogSeverity.Verbose,
LogGatewayIntentWarnings = true,
AlwaysDownloadUsers = true,
GatewayIntents = GatewayIntents.GuildScheduledEvents
| GatewayIntents.DirectMessageTyping
| GatewayIntents.DirectMessageReactions
| GatewayIntents.DirectMessages
| GatewayIntents.GuildMessageTyping
| GatewayIntents.GuildMessageReactions
| GatewayIntents.GuildMessages
| GatewayIntents.GuildVoiceStates
| GatewayIntents.Guilds
| GatewayIntents.GuildMembers
};

config.Token = discordOptions.Token;
});

services
.AddInteractionService((config, _) =>
{
config.LogLevel = LogSeverity.Verbose;
config.UseCompiledLambda = true;
});

services
.AddLogicServices()
.AddBackgroundServices();
Expand Down

0 comments on commit 79631f5

Please sign in to comment.