2023-07-09 16:32:14 +03:00
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-09-30 16:58:32 +03:00
|
|
|
using Octobot.Commands;
|
|
|
|
using Octobot.Commands.Events;
|
|
|
|
using Octobot.Services;
|
|
|
|
using Octobot.Services.Update;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Commands.Extensions;
|
|
|
|
using Remora.Discord.API.Abstractions.Gateway.Commands;
|
|
|
|
using Remora.Discord.API.Abstractions.Objects;
|
|
|
|
using Remora.Discord.API.Objects;
|
|
|
|
using Remora.Discord.Caching.Extensions;
|
|
|
|
using Remora.Discord.Caching.Services;
|
|
|
|
using Remora.Discord.Commands.Extensions;
|
|
|
|
using Remora.Discord.Commands.Services;
|
|
|
|
using Remora.Discord.Gateway;
|
|
|
|
using Remora.Discord.Gateway.Extensions;
|
|
|
|
using Remora.Discord.Hosting.Extensions;
|
|
|
|
using Remora.Rest.Core;
|
2023-08-02 16:25:41 +03:00
|
|
|
using Serilog.Extensions.Logging;
|
2021-12-07 15:52:37 +03:00
|
|
|
|
2023-09-30 16:58:32 +03:00
|
|
|
namespace Octobot;
|
2021-12-07 15:52:37 +03:00
|
|
|
|
2023-09-30 16:58:32 +03:00
|
|
|
public sealed class Octobot
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
public static readonly AllowedMentions NoMentions = new(
|
|
|
|
Array.Empty<MentionType>(), Array.Empty<Snowflake>(), Array.Empty<Snowflake>());
|
2021-12-07 15:52:37 +03:00
|
|
|
|
2023-12-17 21:35:09 +03:00
|
|
|
public const string RepositoryUrl = "https://github.com/LabsDevelopment/Octobot";
|
|
|
|
public const string IssuesUrl = $"{RepositoryUrl}/issues";
|
|
|
|
|
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;
|
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)
|
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
|
2023-07-09 16:32:14 +03:00
|
|
|
.AddSingleton<GuildDataService>()
|
2023-12-20 19:33:52 +03:00
|
|
|
.AddSingleton<Utility>()
|
2023-08-05 21:02:40 +03:00
|
|
|
.AddHostedService<MemberUpdateService>()
|
|
|
|
.AddHostedService<ScheduledEventUpdateService>()
|
|
|
|
.AddHostedService<SongUpdateService>()
|
2023-08-22 10:44:05 +03:00
|
|
|
.AddHostedService<BackgroundGuildDataSaverService>()
|
2023-07-18 15:25:02 +03:00
|
|
|
// Slash commands
|
2023-07-09 16:32:14 +03:00
|
|
|
.AddCommandTree()
|
|
|
|
.WithCommandGroup<AboutCommandGroup>()
|
|
|
|
.WithCommandGroup<BanCommandGroup>()
|
|
|
|
.WithCommandGroup<ClearCommandGroup>()
|
|
|
|
.WithCommandGroup<KickCommandGroup>()
|
|
|
|
.WithCommandGroup<MuteCommandGroup>()
|
|
|
|
.WithCommandGroup<PingCommandGroup>()
|
|
|
|
.WithCommandGroup<RemindCommandGroup>()
|
2023-09-29 15:31:45 +03:00
|
|
|
.WithCommandGroup<SettingsCommandGroup>()
|
|
|
|
.WithCommandGroup<ToolsCommandGroup>();
|
2023-09-30 16:58:32 +03:00
|
|
|
var responderTypes = typeof(Octobot).Assembly
|
2023-07-09 16:32:14 +03:00
|
|
|
.GetExportedTypes()
|
|
|
|
.Where(t => t.IsResponder());
|
2023-08-02 23:51:16 +03:00
|
|
|
foreach (var responderType in responderTypes)
|
|
|
|
{
|
|
|
|
services.AddResponder(responderType);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|