Add a scaffold for a command
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
using Discord;
|
||||
using Discord.Interactions;
|
||||
using Discord.WebSocket;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Serilog;
|
||||
using Serilog.Events;
|
||||
|
||||
@@ -38,67 +37,71 @@ public class Program
|
||||
}
|
||||
|
||||
public static IHost BuildHost(string[] args) => new HostBuilder()
|
||||
.ConfigureAppConfiguration((context) => context
|
||||
.AddUserSecrets<Program>()
|
||||
.Build()
|
||||
)
|
||||
.ConfigureServices(services => services
|
||||
.AddSingleton<DiscordSocketClient>()
|
||||
.AddHostedService<GodReplacementBot>()
|
||||
)
|
||||
.UseSerilog((context, services, loggerConfiguration) => loggerConfiguration
|
||||
.ConfigureDefaults(args)
|
||||
.ConfigureAppConfiguration((context, builder) =>
|
||||
{
|
||||
builder
|
||||
.AddEnvironmentVariables(prefix: "DC_")
|
||||
.AddEnvironmentVariables("DOTNET_")
|
||||
.AddJsonFile("appsettings.json", optional: true)
|
||||
.AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true);
|
||||
|
||||
if (context.HostingEnvironment.IsDevelopment())
|
||||
{
|
||||
builder.AddUserSecrets<Program>();
|
||||
|
||||
}
|
||||
|
||||
builder.Build();
|
||||
})
|
||||
.ConfigureServices((context, services) =>
|
||||
{
|
||||
services
|
||||
.AddSingleton<GodReplacementLogger>()
|
||||
.AddSingleton(provider =>
|
||||
{
|
||||
var logger = provider.GetRequiredService<GodReplacementLogger>();
|
||||
var client = new DiscordSocketClient();
|
||||
client.Log += logger.LogAsync;
|
||||
|
||||
return client;
|
||||
})
|
||||
.AddSingleton(provider =>
|
||||
{
|
||||
var logger = provider.GetRequiredService<GodReplacementLogger>();
|
||||
var interactions = new InteractionService(provider.GetService<DiscordSocketClient>());
|
||||
interactions.Log += logger.LogAsync;
|
||||
|
||||
return interactions;
|
||||
})
|
||||
.AddHostedService<GodReplacementBot>()
|
||||
.AddHostedService<InteractionHandler>();
|
||||
|
||||
if (context.HostingEnvironment.IsDevelopment())
|
||||
{
|
||||
services.Configure<DevelopmentOptions>(context.Configuration.GetSection(DevelopmentOptions.Development));
|
||||
}
|
||||
})
|
||||
.UseSerilog((context, services, loggerConfiguration) =>
|
||||
{
|
||||
loggerConfiguration
|
||||
.MinimumLevel.Information()
|
||||
.MinimumLevel.Override("Microsoft", LogEventLevel.Information);
|
||||
if (context.HostingEnvironment.IsDevelopment()) {
|
||||
loggerConfiguration.MinimumLevel.Debug();
|
||||
}
|
||||
loggerConfiguration
|
||||
.ReadFrom.Configuration(context.Configuration)
|
||||
.Enrich.FromLogContext()
|
||||
.WriteTo.Console())
|
||||
.WriteTo.Console();
|
||||
|
||||
})
|
||||
.Build();
|
||||
}
|
||||
|
||||
public sealed class GodReplacementBot : IHostedService
|
||||
public class DevelopmentOptions
|
||||
{
|
||||
private readonly ILogger<GodReplacementBot> _logger;
|
||||
private readonly IConfiguration _config;
|
||||
private readonly DiscordSocketClient _client;
|
||||
public const string Development = "Development";
|
||||
|
||||
public GodReplacementBot(
|
||||
ILogger<GodReplacementBot> logger,
|
||||
IConfiguration config,
|
||||
DiscordSocketClient client
|
||||
)
|
||||
{
|
||||
_config = config;
|
||||
_client = client;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var token = _config.GetValue<string>("GodReplacementProject:DiscordToken");
|
||||
_client.Log += LogAsync;
|
||||
|
||||
await _client.LoginAsync(TokenType.Bot, token);
|
||||
await _client.StartAsync();
|
||||
}
|
||||
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _client.StopAsync();
|
||||
}
|
||||
|
||||
private Task LogAsync(LogMessage message) {
|
||||
var severity = message.Severity switch
|
||||
{
|
||||
LogSeverity.Critical => LogLevel.Critical,
|
||||
LogSeverity.Error => LogLevel.Error,
|
||||
LogSeverity.Warning => LogLevel.Warning,
|
||||
LogSeverity.Info => LogLevel.Information,
|
||||
LogSeverity.Verbose => LogLevel.Debug,
|
||||
LogSeverity.Debug => LogLevel.Trace,
|
||||
_ => LogLevel.Information,
|
||||
};
|
||||
|
||||
_logger.Log(severity, message.Exception, "[{Source}] {Message}", message.Source, message.Message);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
}
|
||||
public ulong TestGuildId { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user