Skip to content

Commit

Permalink
v3.1.0 (#12)
Browse files Browse the repository at this point in the history
* Multi-target .NET 5

* Add build.yml

* Remove azure pipelines

* Update build pipeline

* Minor readme edit

* Add additional badges

* Formatting
  • Loading branch information
Hawxy authored Nov 15, 2020
1 parent cc65ef9 commit 63c8ca1
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 77 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: .NET Core Build

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: '5.0.x'
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
16 changes: 11 additions & 5 deletions Discord.Addons.Hosting/Discord.Addons.Hosting.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>netstandard2.1;net5</TargetFrameworks>
<PackageId>Discord.Addons.Hosting</PackageId>
<Version>3.0.0</Version>
<Version>3.1.0</Version>
<Authors>Hawxy</Authors>
<Description>Simplifying Discord.Net hosting with .NET Generic Host (Microsoft.Extensions.Hosting)</Description>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
Expand All @@ -12,16 +12,22 @@
<RepositoryUrl>https://github.com/Hawxy/Discord.Addons.Hosting</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageIcon>icon.png</PackageIcon>
<Copyright>Hawxy 2018-2020</Copyright>
<Copyright>Hawxy 2018-2021</Copyright>
<PackageTags>discord,discord.net,addon,hosting,microsoft.extensions.hosting</PackageTags>
<Nullable>Enable</Nullable>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Discord.Net.Commands" Version="2.2.0" />
<PackageReference Include="Discord.Net.WebSocket" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.5" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' ">
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.10" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net5' ">
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Discord.Addons.Hosting/DiscordHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static IHostBuilder ConfigureDiscordHost<T>(this IHostBuilder builder, Ac
if (config != null)
collection.Configure<DiscordHostConfiguration>(x => config(context, x));

collection.AddSingleton(x=> (T)Activator.CreateInstance(typeof(T), x.GetService<IOptions<DiscordHostConfiguration>>().Value.SocketConfig));
collection.AddSingleton(x=> (T)Activator.CreateInstance(typeof(T), x.GetRequiredService<IOptions<DiscordHostConfiguration>>().Value.SocketConfig)!);
if(typeof(T) != typeof(DiscordSocketClient))
collection.AddSingleton<DiscordSocketClient>(x => x.GetRequiredService<T>());

Expand Down Expand Up @@ -110,7 +110,7 @@ public static IHostBuilder UseCommandService(this IHostBuilder builder, Action<H

collection.Configure<CommandServiceConfig>(x => config(context, x));

collection.AddSingleton(x=> new CommandService(x.GetService<IOptions<CommandServiceConfig>>().Value));
collection.AddSingleton(x=> new CommandService(x.GetRequiredService<IOptions<CommandServiceConfig>>().Value));
collection.AddHostedService<CommandServiceInitializerHost>();
});

Expand Down
4 changes: 2 additions & 2 deletions Discord.Addons.Hosting/DiscordHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public DiscordHostedService(ILogger<DiscordHostedService> logger, IOptions<Disco

public async Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Discord.Net hosted service is starting");
_logger.LogInformation("Discord.NET hosted service is starting");

try
{
Expand All @@ -58,7 +58,7 @@ public async Task StartAsync(CancellationToken cancellationToken)

public async Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Discord.Net hosted service is stopping");
_logger.LogInformation("Discord.NET hosted service is stopping");
try
{
await _client.StopAsync().WithCancellation(cancellationToken);
Expand Down
4 changes: 2 additions & 2 deletions Discord.Addons.Hosting/Util/TaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public static async Task WithCancellation(this Task task, CancellationToken canc
{
var tcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);

using (cancellationToken.Register(state =>
await using (cancellationToken.Register(state =>
{
((TaskCompletionSource<object>)state).TrySetResult(null!);
((TaskCompletionSource<object>)state!).TrySetResult(null!);
},
tcs))
{
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Discord.Addons.Hosting
[![Build Status](https://dev.azure.com/GithubHawxy/Discord.Addons.Hosting/_apis/build/status/Hawxy.Discord.Addons.Hosting)](https://dev.azure.com/GithubHawxy/Discord.Addons.Hosting/_build/latest?definitionId=2)
![.NET Core Build](https://github.com/Hawxy/Discord.Addons.Hosting/workflows/.NET%20Core%20Build/badge.svg)
[![NuGet](https://img.shields.io/nuget/v/Discord.Addons.Hosting.svg?style=flat-square)](https://www.nuget.org/packages/Discord.Addons.Hosting)
![Nuget](https://img.shields.io/nuget/dt/Discord.Addons.Hosting?style=flat-square)

[Discord.Net](https://github.com/RogueException/Discord.Net) hosting with [Microsoft.Extensions.Hosting](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host).
This package provides extensions to a .NET Generic Host (IHostBuilder) that will run a Discord.Net socket/sharded client as a controllable IHostedService. This simplifies initial bot creation and moves the usual boilerplate to a convenient builder pattern.

Discord.Net 2.2.0+ & .NET Core 2.1+ is required.
Discord.Net 2.2.0+ & .NET Core 3.1+ is required.

```csharp
var builder = new HostBuilder()
Expand Down Expand Up @@ -58,6 +59,8 @@ If you want something more advanced, one of my bots CitizenEnforcer uses this ex

### Serilog

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](https://github.com/Hawxy/Discord.Addons.Hosting/tree/master/Samples/SampleBotSerilog) for usage.
Expand Down Expand Up @@ -106,7 +109,7 @@ public class CommandHandler : InitializedService

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](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-3.0#shutdowntimeout).
If you're finding that this isn't enough time, you can modify the shutdown timeout via the [ShutdownTimeout host setting](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-5.0#shutdowntimeout).
### IOptions

Expand Down
2 changes: 1 addition & 1 deletion Samples/SampleBotSerilog/CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public override async Task InitializeAsync(CancellationToken cancellationToken)

private async Task HandleMessage(SocketMessage incomingMessage)
{
if (!(incomingMessage is SocketUserMessage message)) return;
if (incomingMessage is not SocketUserMessage message) return;
if (message.Source != MessageSource.User) return;

int argPos = 0;
Expand Down
12 changes: 2 additions & 10 deletions Samples/SampleBotSerilog/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,11 @@ static async Task Main()
})
.UseConsoleLifetime();

//Stop just by hitting enter
//See https://github.com/aspnet/Hosting/blob/master/samples/GenericHostSample for other control patterns
var host = builder.Build();
using (host)
{
Log.Information("Starting!");
await host.StartAsync();
Log.Information("Started! Press <enter> to stop.");
Console.ReadLine();

Log.Information("Stopping!");
await host.StopAsync();
Log.Information("Stopped!");
//Fire and forget. Will run until console is closed or the service is stopped. Basically the same as normally running the bot.
await host.RunAsync();
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions Samples/SampleBotSerilog/PublicModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ public async Task PingAsync()
}

[Command("shutdown")]
public async Task Stop()
public Task Stop()
{
_ = _host.StopAsync();
return Task.CompletedTask;
}

[Command("log")]
public async Task TestLogs()
public Task TestLogs()
{
_logger.LogTrace("This is a trace log");
_logger.LogDebug("This is a debug log");
Expand All @@ -45,6 +46,8 @@ public async Task TestLogs()

_logger.Log(GetLogLevel(LogSeverity.Error), "Error logged from a Discord LogSeverity.Error");
_logger.Log(GetLogLevel(LogSeverity.Info), "Information logged from Discord LogSeverity.Info ");

return Task.CompletedTask;
}

private static LogLevel GetLogLevel(LogSeverity severity)
Expand Down
11 changes: 5 additions & 6 deletions Samples/SampleBotSerilog/Sample.Serilog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>latest</LangVersion>
<TargetFramework>net5</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Discord.Net.Commands" Version="2.2.0" />
<PackageReference Include="Discord.Net.WebSocket" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.5" />
<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Samples/SampleBotSimple/CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public override async Task InitializeAsync(CancellationToken cancellationToken)

private async Task HandleMessage(SocketMessage incomingMessage)
{
if (!(incomingMessage is SocketUserMessage message)) return;
if (incomingMessage is not SocketUserMessage message) return;
if (message.Source != MessageSource.User) return;

int argPos = 0;
Expand Down
2 changes: 2 additions & 0 deletions Samples/SampleBotSimple/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Logging.Console;

namespace Sample.Simple
{
Expand Down
7 changes: 5 additions & 2 deletions Samples/SampleBotSimple/PublicModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ public async Task PingAsync()
}

[Command("shutdown")]
public async Task Stop()
public Task Stop()
{
_ = _host.StopAsync();
return Task.CompletedTask;
}

[Command("log")]
public async Task TestLogs()
public Task TestLogs()
{
_logger.LogTrace("This is a trace log");
_logger.LogDebug("This is a debug log");
Expand All @@ -45,6 +46,8 @@ public async Task TestLogs()

_logger.Log(GetLogLevel(LogSeverity.Error), "Error logged from a Discord LogSeverity.Error");
_logger.Log(GetLogLevel(LogSeverity.Info), "Information logged from Discord LogSeverity.Info ");

return Task.CompletedTask;
}

private static LogLevel GetLogLevel(LogSeverity severity)
Expand Down
11 changes: 5 additions & 6 deletions Samples/SampleBotSimple/Sample.Simple.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>latest</LangVersion>
<TargetFramework>net5</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Discord.Net.Commands" Version="2.2.0" />
<PackageReference Include="Discord.Net.WebSocket" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
35 changes: 0 additions & 35 deletions azure-pipelines.yml

This file was deleted.

0 comments on commit 63c8ca1

Please sign in to comment.