-
Notifications
You must be signed in to change notification settings - Fork 3
DiscordService, Configuration manager #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
64744a9
DiscordService, Configuration manager
Ingvarr100th 136a003
Removal of unnecessary defaults
Ingvarr100th a38ca31
Move discord client log event init into DiscordService constructor
Ingvarr100th 9ea60e9
Addition of appsettings
Ingvarr100th fb3987d
Review fixes
Ingvarr100th 830406d
Interface fix
Ingvarr100th 1e67c05
Removal of unuser var
Ingvarr100th 6a534a4
Add newline
Ingvarr100th File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
9 changes: 9 additions & 0 deletions
9
ArmaForces.Boderator/ArmaForces.Boderator.BotService/DTOs/DiscordServiceStatus.cs
This file contains hidden or 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,9 @@ | ||
| namespace ArmaForces.Boderator.BotService.DTOs | ||
| { | ||
| public class DiscordServiceStatus | ||
| { | ||
| public string ConnectionState { get; init; } | ||
| public string LoginState { get; init; } | ||
| public string ClientState { get; init; } | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
...ator/ArmaForces.Boderator.BotService/Discord/DependencyInjection/DiscordServiceBuilder.cs
This file contains hidden or 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,11 @@ | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace ArmaForces.Boderator.BotService.Discord.DependencyInjection | ||
| { | ||
| internal class DiscordServiceBuilder : IDiscordServiceBuilder | ||
| { | ||
| private IServiceCollection Services { get; } | ||
|
|
||
| public DiscordServiceBuilder(IServiceCollection services) => Services = services; | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
...tor/ArmaForces.Boderator.BotService/Discord/DependencyInjection/IDiscordServiceBuilder.cs
This file contains hidden or 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,6 @@ | ||
| namespace ArmaForces.Boderator.BotService.Discord.DependencyInjection | ||
| { | ||
| internal interface IDiscordServiceBuilder | ||
| { | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
ArmaForces.Boderator/ArmaForces.Boderator.BotService/Discord/DiscordService.cs
This file contains hidden or 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,67 @@ | ||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using ArmaForces.Boderator.BotService.DTOs; | ||
| using Discord; | ||
| using Discord.WebSocket; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace ArmaForces.Boderator.BotService.Discord | ||
| { | ||
| public sealed class DiscordService : IDiscordService, IHostedService | ||
| { | ||
| private readonly ILogger<DiscordService> _log; | ||
| private readonly DiscordSocketClient _discordClient; | ||
| private string _token; | ||
|
|
||
| public DiscordService(ILogger<DiscordService> logger, string token) | ||
| { | ||
| _log = logger; | ||
| _discordClient = new DiscordSocketClient(); | ||
| _discordClient.Log += message => Task.Run(() => _log.Log(MapSeverity(message.Severity), "[Discord.NET Log] " + message.Message)); | ||
| _discordClient.Connected += () => Task.Run(() => _log.LogInformation("Discord connected")); | ||
| _token = token; | ||
| } | ||
|
|
||
| public async Task StartAsync(CancellationToken cancellationToken) | ||
| { | ||
| _log.LogInformation("Discord Service started"); | ||
| await _discordClient.LoginAsync(TokenType.Bot, _token, true); | ||
| await _discordClient.StartAsync(); | ||
| } | ||
|
|
||
| public Task StopAsync(CancellationToken cancellationToken) => Task.Run(() => | ||
| { | ||
| _discordClient.Dispose(); | ||
| _log.LogInformation("Discord Service stopped"); | ||
| }, cancellationToken); | ||
|
|
||
| public DiscordServiceStatus GetDiscordClientStatus() | ||
| { | ||
| _log.LogInformation($"Current Discord Bot status: Login: {_discordClient.LoginState} | " + | ||
| $"Connection: {_discordClient.ConnectionState} | " + | ||
| $"Status: {_discordClient.Status}"); | ||
| return new DiscordServiceStatus | ||
| { | ||
| ConnectionState = _discordClient.ConnectionState.ToString(), | ||
| LoginState = _discordClient.LoginState.ToString(), | ||
| ClientState = _discordClient.Status.ToString() | ||
| }; | ||
| } | ||
|
|
||
| public async Task SetBotStatus(string newStatus, ActivityType statusType) => | ||
| await _discordClient.SetGameAsync(newStatus, type: statusType); | ||
|
|
||
| private static LogLevel MapSeverity(LogSeverity severity) => | ||
| severity switch | ||
| { | ||
| LogSeverity.Verbose => LogLevel.Trace, | ||
| LogSeverity.Debug => LogLevel.Debug, | ||
| LogSeverity.Info => LogLevel.Information, | ||
| LogSeverity.Warning => LogLevel.Warning, | ||
| LogSeverity.Error => LogLevel.Error, | ||
| LogSeverity.Critical => LogLevel.Critical | ||
| }; | ||
| } | ||
| } | ||
16 changes: 16 additions & 0 deletions
16
...s.Boderator/ArmaForces.Boderator.BotService/Discord/DiscordServiceCollectionExtentions.cs
This file contains hidden or 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,16 @@ | ||
| using ArmaForces.Boderator.BotService.Discord.DependencyInjection; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace ArmaForces.Boderator.BotService.Discord | ||
| { | ||
| internal static class DiscordServiceCollectionExtentions | ||
| { | ||
| public static IDiscordServiceBuilder AddDiscordService(this IServiceCollection serviceDescriptors, string token) | ||
| { | ||
| serviceDescriptors.AddSingleton<IDiscordService, DiscordService>(sP => new DiscordService(sP.GetService<ILogger<DiscordService>>(), token)); | ||
| serviceDescriptors.AddHostedService(sP => sP.GetRequiredService<IDiscordService>() as DiscordService); | ||
| return new DiscordServiceBuilder(serviceDescriptors); | ||
| } | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
ArmaForces.Boderator/ArmaForces.Boderator.BotService/Discord/Interfaces/IDiscordService.cs
This file contains hidden or 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,12 @@ | ||
| using System.Threading.Tasks; | ||
| using ArmaForces.Boderator.BotService.DTOs; | ||
| using Discord; | ||
|
|
||
| namespace ArmaForces.Boderator.BotService.Discord | ||
| { | ||
| public interface IDiscordService | ||
| { | ||
| DiscordServiceStatus GetDiscordClientStatus(); | ||
| Task SetBotStatus(string newStatus, ActivityType statusType); | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
ArmaForces.Boderator/ArmaForces.Boderator.BotService/Helpers/Configuration.cs
This file contains hidden or 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,19 @@ | ||
| using System; | ||
| using System.Collections; | ||
|
|
||
| namespace ArmaForces.Boderator.BotService.Helpers | ||
| { | ||
| public static class Configuration | ||
| { | ||
| public static string DiscordToken => GetParameter("DISCORD_TOKEN"); | ||
|
|
||
| private static IDictionary Parameters { get; } | ||
|
|
||
| static Configuration() | ||
| { | ||
| Parameters = Environment.GetEnvironmentVariables(); | ||
| } | ||
|
|
||
| public static string GetParameter(string key) => (string)Parameters[key]; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
34 changes: 34 additions & 0 deletions
34
ArmaForces.Boderator/ArmaForces.Boderator.BotService/appsettings.Development.json
This file contains hidden or 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,34 @@ | ||
| { | ||
| "Serilog": { | ||
| "WriteTo": [ | ||
| { | ||
| "Name": "Console", | ||
| "Args": { | ||
| "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console", | ||
| "outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level:u3}] [{ThreadId}] {Message:lj}{NewLine}{Exception}" | ||
| } | ||
| }, | ||
| { | ||
| "Name": "Debug", | ||
| "outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level:u3}] [{ThreadId}] {Message:lj}{NewLine}{Exception}" | ||
| }, | ||
| { | ||
| "Name": "File", | ||
| "Args": { | ||
| "path": "Logs/log.txt", | ||
| "rollingInterval": "Day", | ||
| "retainedFileCountLimit": 7, | ||
| "outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level:u3}] [{ThreadId}] {Message:lj}{NewLine}{Exception}" | ||
| } | ||
| } | ||
| ], | ||
| "Enrich": [ "WithThreadId" ] | ||
| }, | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft": "Warning", | ||
| "Microsoft.Hosting.Lifetime": "Information" | ||
| } | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
ArmaForces.Boderator/ArmaForces.Boderator.BotService/appsettings.json
This file contains hidden or 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,31 @@ | ||
| { | ||
| "Serilog": { | ||
| "WriteTo": [ | ||
| { | ||
| "Name": "Console", | ||
| "Args": { | ||
| "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console", | ||
| "outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level:u3}] [{ThreadId}] {Message:lj} {Properties}{NewLine}{Exception}" | ||
| } | ||
| }, | ||
| { | ||
| "Name": "File", | ||
| "Args": { | ||
| "path": "log.txt", | ||
| "rollingInterval": "Day", | ||
| "retainedFileCountLimit": 7, | ||
| "outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level:u3}] [{ThreadId}] {Message:lj} {Properties}{NewLine}{Exception}" | ||
| } | ||
| } | ||
| ], | ||
| "Enrich": [ "WithThreadId" ] | ||
| }, | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft": "Warning", | ||
| "Microsoft.Hosting.Lifetime": "Information" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*" | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.