2024-10-14 14:56:40 +02:00
|
|
|
// Copyright (C) 2021-present sam (starshines.gay)
|
|
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
2024-10-18 22:13:23 +02:00
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.Json.Serialization;
|
2024-11-08 17:12:00 +01:00
|
|
|
using Catalogger.Backend;
|
2024-08-13 13:08:50 +02:00
|
|
|
using Catalogger.Backend.Bot.Commands;
|
|
|
|
|
using Catalogger.Backend.Extensions;
|
2024-08-20 20:19:24 +02:00
|
|
|
using Catalogger.Backend.Services;
|
2024-11-08 17:12:00 +01:00
|
|
|
using NodaTime.Serialization.SystemTextJson;
|
2024-08-20 20:19:24 +02:00
|
|
|
using Prometheus;
|
2024-08-13 13:08:50 +02:00
|
|
|
using Remora.Commands.Extensions;
|
|
|
|
|
using Remora.Discord.API.Abstractions.Gateway.Commands;
|
2024-10-13 17:08:32 +02:00
|
|
|
using Remora.Discord.API.Abstractions.Objects;
|
|
|
|
|
using Remora.Discord.API.Gateway.Commands;
|
|
|
|
|
using Remora.Discord.API.Objects;
|
2024-08-13 13:08:50 +02:00
|
|
|
using Remora.Discord.Commands.Extensions;
|
2024-11-18 00:47:27 +01:00
|
|
|
using Remora.Discord.Commands.Responders;
|
2024-08-13 13:08:50 +02:00
|
|
|
using Remora.Discord.Extensions.Extensions;
|
|
|
|
|
using Remora.Discord.Gateway;
|
|
|
|
|
using Remora.Discord.Interactivity.Extensions;
|
|
|
|
|
using Remora.Discord.Pagination.Extensions;
|
|
|
|
|
using Serilog;
|
2024-08-20 20:19:24 +02:00
|
|
|
using Metrics = Prometheus.Metrics;
|
2024-08-13 13:08:50 +02:00
|
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
var config = builder.AddConfiguration();
|
2024-08-16 22:28:05 +02:00
|
|
|
builder.AddSerilog(config);
|
2024-08-13 13:08:50 +02:00
|
|
|
|
2024-10-09 17:35:11 +02:00
|
|
|
builder
|
|
|
|
|
.Services.AddControllers()
|
2024-10-18 22:13:23 +02:00
|
|
|
.AddJsonOptions(options =>
|
|
|
|
|
{
|
|
|
|
|
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower;
|
|
|
|
|
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
|
|
|
|
|
options.JsonSerializerOptions.IncludeFields = true;
|
|
|
|
|
options.JsonSerializerOptions.NumberHandling =
|
|
|
|
|
JsonNumberHandling.WriteAsString | JsonNumberHandling.AllowReadingFromString;
|
2024-11-08 17:12:00 +01:00
|
|
|
options.JsonSerializerOptions.ConfigureForNodaTime(JsonUtils.NodaTimeSettings);
|
2024-10-18 22:13:23 +02:00
|
|
|
});
|
2024-08-13 13:08:50 +02:00
|
|
|
|
2024-10-09 17:35:11 +02:00
|
|
|
builder
|
|
|
|
|
.Host.AddShardedDiscordService(_ => config.Discord.Token)
|
2024-08-13 13:08:50 +02:00
|
|
|
.ConfigureServices(s =>
|
|
|
|
|
s.AddRespondersFromAssembly(typeof(Program).Assembly)
|
|
|
|
|
.Configure<DiscordGatewayClientOptions>(g =>
|
2024-10-13 17:08:32 +02:00
|
|
|
{
|
2024-10-09 17:35:11 +02:00
|
|
|
g.Intents =
|
|
|
|
|
GatewayIntents.Guilds
|
2024-10-13 17:08:32 +02:00
|
|
|
// Actually GUILD_MODERATION
|
|
|
|
|
| GatewayIntents.GuildBans
|
2024-10-09 17:35:11 +02:00
|
|
|
| GatewayIntents.GuildInvites
|
|
|
|
|
| GatewayIntents.GuildMembers
|
|
|
|
|
| GatewayIntents.GuildMessages
|
|
|
|
|
| GatewayIntents.GuildWebhooks
|
|
|
|
|
| GatewayIntents.MessageContents
|
2024-10-13 17:08:32 +02:00
|
|
|
// Actually GUILD_EXPRESSIONS
|
|
|
|
|
| GatewayIntents.GuildEmojisAndStickers;
|
|
|
|
|
|
|
|
|
|
// Set a default status for all shards. This is updated to a shard-specific one in StatusUpdateService.
|
|
|
|
|
g.Presence = new UpdatePresence(
|
|
|
|
|
Status: UserStatus.Online,
|
|
|
|
|
IsAFK: false,
|
|
|
|
|
Since: null,
|
|
|
|
|
Activities:
|
|
|
|
|
[
|
|
|
|
|
new Activity(
|
|
|
|
|
Name: "Beep",
|
|
|
|
|
Type: ActivityType.Custom,
|
|
|
|
|
State: "/catalogger help"
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
})
|
2024-11-18 00:47:27 +01:00
|
|
|
.Configure<InteractionResponderOptions>(opts => opts.SuppressAutomaticResponses = true)
|
2024-10-25 16:18:27 +02:00
|
|
|
.AddDiscordCommands(
|
|
|
|
|
enableSlash: true,
|
|
|
|
|
useDefaultCommandResponder: false,
|
|
|
|
|
useDefaultInteractionResponder: false
|
|
|
|
|
)
|
2024-08-13 13:08:50 +02:00
|
|
|
.AddCommandTree()
|
|
|
|
|
// Start command tree
|
|
|
|
|
.WithCommandGroup<MetaCommands>()
|
2024-08-14 16:05:43 +02:00
|
|
|
.WithCommandGroup<ChannelCommands>()
|
2024-09-02 15:06:22 +02:00
|
|
|
.WithCommandGroup<KeyRoleCommands>()
|
2024-10-14 00:26:17 +02:00
|
|
|
.WithCommandGroup<InviteCommands>()
|
2024-11-18 00:47:27 +01:00
|
|
|
.WithCommandGroup<IgnoreMessageCommands>()
|
2024-11-18 21:26:47 +01:00
|
|
|
.WithCommandGroup<IgnoreEntitiesCommands>()
|
2024-10-16 14:53:30 +02:00
|
|
|
.WithCommandGroup<RedirectCommands>()
|
2024-10-28 16:25:42 +01:00
|
|
|
.WithCommandGroup<WatchlistCommands>()
|
2024-08-13 13:08:50 +02:00
|
|
|
// End command tree
|
|
|
|
|
.Finish()
|
|
|
|
|
.AddPagination()
|
|
|
|
|
.AddInteractivity()
|
2024-08-14 16:05:43 +02:00
|
|
|
.AddInteractionGroup<ChannelCommandsComponents>()
|
2024-10-14 00:26:17 +02:00
|
|
|
.AddAutocompleteProvider<InviteAutocompleteProvider>()
|
2024-08-13 13:08:50 +02:00
|
|
|
);
|
|
|
|
|
|
2024-08-20 20:19:24 +02:00
|
|
|
// Add metric server
|
|
|
|
|
// If metrics are disabled (Logging.EnableMetrics = false), also add a background service that updates
|
|
|
|
|
// metrics every minute, as some commands rely on them.
|
|
|
|
|
builder.Services.AddMetricServer(o => o.Port = (ushort)config.Logging.MetricsPort);
|
|
|
|
|
if (!config.Logging.EnableMetrics)
|
|
|
|
|
builder.Services.AddHostedService<BackgroundMetricsCollectionService>();
|
2024-08-15 17:23:56 +02:00
|
|
|
|
2024-11-19 00:10:12 +01:00
|
|
|
builder.Services.MaybeAddDashboardServices(config).MaybeAddRedisCaches(config).AddCustomServices();
|
2024-08-13 13:08:50 +02:00
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
|
|
await app.Initialize();
|
2024-10-18 22:13:23 +02:00
|
|
|
app.MaybeAddDashboard();
|
2024-08-13 13:08:50 +02:00
|
|
|
|
|
|
|
|
app.Urls.Clear();
|
|
|
|
|
app.Urls.Add(config.Web.Address);
|
|
|
|
|
|
2024-08-20 20:19:24 +02:00
|
|
|
// Make sure metrics are updated whenever Prometheus scrapes them
|
|
|
|
|
Metrics.DefaultRegistry.AddBeforeCollectCallback(async ct =>
|
2024-10-09 17:35:11 +02:00
|
|
|
await app.Services.GetRequiredService<MetricsCollectionService>().CollectMetricsAsync(ct)
|
|
|
|
|
);
|
2024-08-20 20:19:24 +02:00
|
|
|
|
2024-08-13 13:08:50 +02:00
|
|
|
app.Run();
|
2024-10-09 17:35:11 +02:00
|
|
|
Log.CloseAndFlush();
|