-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
83 lines (73 loc) · 3.17 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using Microsoft.Extensions.Options;
using MO9.Options;
using MO9.Responders;
using Remora.Discord.API.Abstractions.Gateway.Commands;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Abstractions.Rest;
using Remora.Discord.Gateway;
using Remora.Discord.Gateway.Extensions;
using Remora.Discord.Hosting.Extensions;
using Remora.Rest.Core;
using Remora.Results;
using Serilog;
namespace MO9;
internal static class Program
{
public static async Task Main(string[] args)
{
IHost host = Host.CreateDefaultBuilder(args)
.UseConsoleLifetime()
.UseSerilog((context, services, configuration) =>
{
IOptions<SeqOptions> seqOptions = services.GetRequiredService<IOptions<SeqOptions>>();
configuration
.Enrich.FromLogContext()
.Enrich.WithProperty("Source", "MO9")
.MinimumLevel.Debug()
.WriteTo.Seq(seqOptions.Value.Url, apiKey: seqOptions.Value.Key)
.WriteTo.Console();
})
.ConfigureServices((context, services) =>
{
services.Configure<SeqOptions>(context.Configuration.GetSection(SeqOptions.SECTION));
services.Configure<DiscordOptions>(context.Configuration.GetSection(DiscordOptions.SECTION));
services.Configure<DiscordGatewayClientOptions>(g =>
g.Intents |= GatewayIntents.Guilds | GatewayIntents.MessageContents);
services.AddSingleton<ThreadRepository>();
services.AddDiscordService(GetDiscordToken);
services.AddResponder<MessageCreateResponder>();
services.AddResponder<ThreadCreateResponder>();
})
.Build();
await LoadThreads(host.Services);
await host.RunAsync();
}
private static async Task LoadThreads(IServiceProvider provider)
{
ThreadRepository threadRepository = provider.GetRequiredService<ThreadRepository>();
DiscordOptions discordOptions = provider.GetRequiredService<IOptions<DiscordOptions>>().Value;
IDiscordRestGuildAPI guildApi = provider.GetRequiredService<IDiscordRestGuildAPI>();
Result<IGuildThreadQueryResponse> threadsResult =
await guildApi.ListActiveGuildThreadsAsync(new Snowflake(discordOptions.GuildId));
if (!threadsResult.IsSuccess)
{
Log.Error("Failed to get guild threads: {Error}", threadsResult.Error.ToString());
return;
}
Snowflake forumFlake = new(discordOptions.ForumId);
foreach (IChannel channel in threadsResult.Entity.Threads)
{
if (!channel.ParentID.HasValue)
continue;
if (channel.ParentID != forumFlake)
continue;
threadRepository.AddThread(channel.ID);
threadRepository.MarkThreadProcessed(channel.ID);
}
}
private static string GetDiscordToken(IServiceProvider serviceProvider)
{
IOptions<DiscordOptions> discordOptions = serviceProvider.GetRequiredService<IOptions<DiscordOptions>>();
return discordOptions.Value.Token;
}
}