-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from j4asper/54-refactor-to-use-generic-host
54 refactor to use generic host
- Loading branch information
Showing
24 changed files
with
331 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System.Reflection; | ||
using DSharpPlus; | ||
using DSharpPlus.Entities; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace KanbanCord.Bot.BackgroundServices; | ||
|
||
public class BotBackgroundService : IHostedService | ||
{ | ||
private readonly ILogger<BotBackgroundService> logger; | ||
private readonly IHostApplicationLifetime applicationLifetime; | ||
private readonly DiscordClient discordClient; | ||
|
||
public BotBackgroundService(ILogger<BotBackgroundService> logger, IHostApplicationLifetime applicationLifetime, DiscordClient discordClient) | ||
{ | ||
this.logger = logger; | ||
this.applicationLifetime = applicationLifetime; | ||
this.discordClient = discordClient; | ||
} | ||
|
||
public async Task StartAsync(CancellationToken token) | ||
{ | ||
DiscordActivity status = new("out for work", DiscordActivityType.Watching); | ||
|
||
await discordClient.ConnectAsync(status, DiscordUserStatus.Online); | ||
|
||
logger.LogInformation("Bot User: {username} ({userId})", discordClient.CurrentUser.Username, discordClient.CurrentUser.Id); | ||
logger.LogInformation("Application Version: {version}", Assembly.GetExecutingAssembly().GetName().Version!.ToString()); | ||
} | ||
|
||
public async Task StopAsync(CancellationToken token) | ||
{ | ||
logger.LogInformation("Shutting down bot..."); | ||
await discordClient.DisconnectAsync(); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
KanbanCord.Bot/BackgroundServices/DatabaseSetupBackgroundService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using KanbanCord.Core.Models; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using MongoDB.Driver; | ||
|
||
namespace KanbanCord.Bot.BackgroundServices; | ||
|
||
public class DatabaseSetupBackgroundService : IHostedService | ||
{ | ||
private readonly IMongoDatabase database; | ||
private readonly ILogger<DatabaseSetupBackgroundService> logger; | ||
|
||
public DatabaseSetupBackgroundService(IMongoDatabase database, ILogger<DatabaseSetupBackgroundService> logger) | ||
{ | ||
this.database = database; | ||
this.logger = logger; | ||
} | ||
|
||
public async Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
var requiredCollections = Enum.GetValues<RequiredCollections>() | ||
.Select(x => x.ToString()) | ||
.ToArray(); | ||
|
||
var cursor = await database.ListCollectionNamesAsync(cancellationToken: cancellationToken); | ||
|
||
var collectionList = await cursor.ToListAsync<string>(cancellationToken: cancellationToken); | ||
|
||
List<string> createdCollections = []; | ||
|
||
foreach (var collectionName in requiredCollections) | ||
{ | ||
if (collectionList.Contains(collectionName)) | ||
continue; | ||
|
||
await database.CreateCollectionAsync(collectionName, cancellationToken: cancellationToken); | ||
|
||
createdCollections.Add(collectionName); | ||
} | ||
|
||
if (createdCollections.Count != 0) | ||
logger.LogInformation("Created missing mongodb collection(s): {collections}", string.Join(", ", createdCollections)); | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using DSharpPlus; | ||
using DSharpPlus.EventArgs; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace KanbanCord.Bot.EventHandlers; | ||
|
||
public class GuildCreatedEventHandler : IEventHandler<GuildCreatedEventArgs> | ||
{ | ||
private readonly ILogger<GuildCreatedEventHandler> logger; | ||
|
||
public GuildCreatedEventHandler(ILogger<GuildCreatedEventHandler> logger) | ||
{ | ||
this.logger = logger; | ||
} | ||
|
||
|
||
public Task HandleEventAsync(DiscordClient sender, GuildCreatedEventArgs eventArgs) | ||
{ | ||
logger.LogInformation("Joined Guild: {guildName} ({guildId})", eventArgs.Guild.Name, eventArgs.Guild.Id); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using Serilog; | ||
|
||
namespace KanbanCord.Bot.Extensions; | ||
|
||
public static class HostBuilderExtensions | ||
{ | ||
public static IHostBuilder AddHostDependencies(this IHostBuilder hostBuilder) | ||
{ | ||
hostBuilder.ConfigureServices((hostContext, services) => | ||
{ | ||
services.AddServices(hostContext.Configuration); | ||
services.AddDiscordConfiguration(hostContext.Configuration); | ||
}); | ||
|
||
return hostBuilder; | ||
} | ||
|
||
public static IHostBuilder UseSerilog(this IHostBuilder hostBuilder) | ||
{ | ||
hostBuilder.ConfigureLogging((hostContext, logging) => | ||
{ | ||
logging.ClearProviders(); | ||
|
||
Log.Logger = new LoggerConfiguration() | ||
.ReadFrom.Configuration(hostContext.Configuration) | ||
.CreateLogger(); | ||
|
||
logging.AddSerilog(); | ||
}); | ||
|
||
return hostBuilder; | ||
} | ||
|
||
public static IHostBuilder UseDefaultServiceProvider(this IHostBuilder hostBuilder) | ||
{ | ||
hostBuilder.UseDefaultServiceProvider(options => | ||
{ | ||
options.ValidateScopes = true; | ||
options.ValidateOnBuild = true; | ||
}); | ||
|
||
return hostBuilder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.