Created
February 24, 2022 23:50
-
-
Save exsersewo/11f2cac6637711d5282cdcfe5b267ae6 to your computer and use it in GitHub Desktop.
Light-ish weight M.A.H/M.A.L/M.A.C Discord host - Gist is MIT
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
using Discord; | |
using Discord.WebSocket; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.Hosting; | |
using Microsoft.Extensions.Logging; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using IConfiguration = Microsoft.Extensions.Configuration.IConfiguration; | |
public class DiscordHostedClient : IHostedService | |
{ | |
public BaseSocketClient Client; | |
public ILogger Logger; | |
public IConfiguration HostConfiguration; | |
public DiscordHostedClient(IConfiguration hostConfiguration, ILogger<DiscordHostedClient> logger) | |
{ | |
Logger = logger; | |
HostConfiguration = hostConfiguration; | |
} | |
LogSeverity GetLogSeverity(string logLevel) | |
=> logLevel switch | |
{ | |
"Warning" => LogSeverity.Warning, | |
"Debug" => LogSeverity.Debug, | |
"Error" => LogSeverity.Error, | |
"Verbose" => LogSeverity.Verbose, | |
"Critical" => LogSeverity.Critical, | |
_ => LogSeverity.Info | |
}; | |
public async Task StartAsync(CancellationToken cancellationToken) | |
{ | |
Logger.LogInformation($"Discord|App Lifetime|Starting up"); | |
var discordSection = HostConfiguration.GetRequiredSection("Discord"); | |
if (discordSection is not null) | |
{ | |
var logSection = HostConfiguration.GetRequiredSection("Logging").GetRequiredSection("LogLevel"); | |
var discordLogLevel = logSection.GetValue<string>("Discord"); | |
if (string.IsNullOrEmpty(discordLogLevel)) | |
{ | |
discordLogLevel = logSection.GetValue<string>("Default"); | |
} | |
LogSeverity logLevel = GetLogSeverity(discordLogLevel); | |
Client = new DiscordSocketClient(new DiscordSocketConfig | |
{ | |
LogLevel = logLevel, | |
GatewayIntents = GatewayIntents.AllUnprivileged, | |
}); | |
var token = discordSection.GetValue<string>("Token"); | |
await Client.LoginAsync(TokenType.Bot, token).WaitAsync(cancellationToken).ConfigureAwait(false); | |
await Client.StartAsync().WaitAsync(cancellationToken).ConfigureAwait(false); | |
} | |
} | |
public async Task StopAsync(CancellationToken cancellationToken) | |
{ | |
Logger.LogInformation($"Discord|App Lifetime|Shutting down"); | |
if (Client is not null) | |
{ | |
await Client.SetStatusAsync(UserStatus.Offline).WaitAsync(cancellationToken).ConfigureAwait(false); | |
await Client.StopAsync().WaitAsync(cancellationToken).ConfigureAwait(false); | |
if (Client.LoginState != LoginState.LoggedOut) | |
{ | |
await Client.LogoutAsync().WaitAsync(cancellationToken).ConfigureAwait(false); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment