2023-07-09 16:32:14 +03:00
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Remora.Discord.API.Abstractions.Gateway.Commands;
|
|
|
|
using Remora.Discord.API.Abstractions.Objects;
|
|
|
|
using Remora.Discord.Caching.Extensions;
|
|
|
|
using Remora.Discord.Caching.Services;
|
|
|
|
using Remora.Discord.Commands.Extensions;
|
|
|
|
using Remora.Discord.Commands.Services;
|
2023-12-20 20:48:32 +03:00
|
|
|
using Remora.Discord.Extensions.Extensions;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Discord.Gateway;
|
|
|
|
using Remora.Discord.Hosting.Extensions;
|
2023-08-02 16:25:41 +03:00
|
|
|
using Serilog.Extensions.Logging;
|
2024-05-16 18:34:26 +03:00
|
|
|
using TeamOctolings.Octobot.Commands.Events;
|
|
|
|
using TeamOctolings.Octobot.Services;
|
|
|
|
using TeamOctolings.Octobot.Services.Update;
|
2021-12-07 15:52:37 +03:00
|
|
|
|
2024-05-16 18:34:26 +03:00
|
|
|
namespace TeamOctolings.Octobot;
|
2021-12-07 15:52:37 +03:00
|
|
|
|
2024-05-16 18:34:26 +03:00
|
|
|
public sealed class Program
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
|
|
|
public static async Task Main(string[] args)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
var host = CreateHostBuilder(args).UseConsoleLifetime().Build();
|
|
|
|
var services = host.Services;
|
2024-05-16 18:34:26 +03:00
|
|
|
Utility.StaticLogger = services.GetRequiredService<ILogger<Program>>();
|
2021-12-07 15:52:37 +03:00
|
|
|
|
2023-07-09 16:32:14 +03:00
|
|
|
var slashService = services.GetRequiredService<SlashService>();
|
|
|
|
// Providing a guild ID to this call will result in command duplicates!
|
|
|
|
// To get rid of them, provide the ID of the guild containing duplicates,
|
|
|
|
// comment out calls to WithCommandGroup in CreateHostBuilder
|
|
|
|
// then launch the bot again and remove the guild ID
|
|
|
|
await slashService.UpdateSlashCommandsAsync();
|
2023-01-18 17:39:24 +03:00
|
|
|
|
2023-07-09 16:32:14 +03:00
|
|
|
await host.RunAsync();
|
2023-01-18 17:39:24 +03:00
|
|
|
}
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
private static IHostBuilder CreateHostBuilder(string[] args)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
return Host.CreateDefaultBuilder(args)
|
|
|
|
.AddDiscordService(
|
2023-08-02 23:51:16 +03:00
|
|
|
services =>
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
var configuration = services.GetRequiredService<IConfiguration>();
|
|
|
|
|
|
|
|
return configuration.GetValue<string?>("BOT_TOKEN")
|
|
|
|
?? throw new InvalidOperationException(
|
|
|
|
"No bot token has been provided. Set the "
|
|
|
|
+ "BOT_TOKEN environment variable to a valid token.");
|
|
|
|
}
|
|
|
|
).ConfigureServices(
|
2023-08-02 23:51:16 +03:00
|
|
|
(_, services) =>
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
services.Configure<DiscordGatewayClientOptions>(
|
2023-08-02 23:51:16 +03:00
|
|
|
options =>
|
|
|
|
{
|
|
|
|
options.Intents |= GatewayIntents.MessageContents
|
|
|
|
| GatewayIntents.GuildMembers
|
2023-08-04 16:52:54 +03:00
|
|
|
| GatewayIntents.GuildPresences
|
2023-08-02 23:51:16 +03:00
|
|
|
| GatewayIntents.GuildScheduledEvents;
|
|
|
|
});
|
2023-07-09 16:32:14 +03:00
|
|
|
services.Configure<CacheSettings>(
|
2023-08-02 23:51:16 +03:00
|
|
|
cSettings =>
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
cSettings.SetDefaultAbsoluteExpiration(TimeSpan.FromHours(1));
|
|
|
|
cSettings.SetDefaultSlidingExpiration(TimeSpan.FromMinutes(30));
|
|
|
|
cSettings.SetAbsoluteExpiration<IMessage>(TimeSpan.FromDays(7));
|
|
|
|
cSettings.SetSlidingExpiration<IMessage>(TimeSpan.FromDays(7));
|
2023-07-09 16:32:14 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
services.AddTransient<IConfigurationBuilder, ConfigurationBuilder>()
|
2023-07-18 15:25:02 +03:00
|
|
|
// Init
|
2023-07-09 16:32:14 +03:00
|
|
|
.AddDiscordCaching()
|
2023-10-30 07:37:16 +03:00
|
|
|
.AddDiscordCommands(true, false)
|
2024-05-16 18:34:26 +03:00
|
|
|
.AddRespondersFromAssembly(typeof(Program).Assembly)
|
|
|
|
.AddCommandGroupsFromAssembly(typeof(Program).Assembly)
|
2023-07-18 15:25:02 +03:00
|
|
|
// Slash command event handlers
|
2023-07-20 00:01:53 +03:00
|
|
|
.AddPreparationErrorEvent<LoggingPreparationErrorEvent>()
|
2023-07-18 15:25:02 +03:00
|
|
|
.AddPostExecutionEvent<ErrorLoggingPostExecutionEvent>()
|
|
|
|
// Services
|
2024-03-24 21:29:10 +03:00
|
|
|
.AddSingleton<AccessControlService>()
|
2023-12-20 20:08:56 +03:00
|
|
|
.AddSingleton<GuildDataService>()
|
2024-03-24 21:29:10 +03:00
|
|
|
.AddSingleton<Utility>()
|
2023-12-20 20:08:56 +03:00
|
|
|
.AddHostedService<GuildDataService>(provider => provider.GetRequiredService<GuildDataService>())
|
2023-08-05 21:02:40 +03:00
|
|
|
.AddHostedService<MemberUpdateService>()
|
|
|
|
.AddHostedService<ScheduledEventUpdateService>()
|
2023-12-20 20:48:32 +03:00
|
|
|
.AddHostedService<SongUpdateService>();
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
|
|
|
).ConfigureLogging(
|
|
|
|
c => c.AddConsole()
|
2023-09-30 16:58:32 +03:00
|
|
|
.AddFile("Logs/Octobot-{Date}.log",
|
2023-08-02 23:51:16 +03:00
|
|
|
outputTemplate: "{Timestamp:o} [{Level:u4}] {Message} {NewLine}{Exception}")
|
2023-07-09 16:32:14 +03:00
|
|
|
.AddFilter("System.Net.Http.HttpClient.*.LogicalHandler", LogLevel.Warning)
|
|
|
|
.AddFilter("System.Net.Http.HttpClient.*.ClientHandler", LogLevel.Warning)
|
2023-08-02 16:25:41 +03:00
|
|
|
.AddFilter<SerilogLoggerProvider>("System.Net.Http.HttpClient.*.LogicalHandler", LogLevel.Warning)
|
|
|
|
.AddFilter<SerilogLoggerProvider>("System.Net.Http.HttpClient.*.ClientHandler", LogLevel.Warning)
|
2023-07-09 16:32:14 +03:00
|
|
|
);
|
2022-05-14 16:12:24 +03:00
|
|
|
}
|
|
|
|
}
|