-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
98 lines (88 loc) · 3.3 KB
/
Program.cs
File metadata and controls
98 lines (88 loc) · 3.3 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using Discord;
using Discord.WebSocket;
using NeuroSync.Config;
using NeuroSync.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
var host = Host.CreateDefaultBuilder(args)
.UseSerilog((ctx, lc) => lc
.MinimumLevel.Information()
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
.WriteTo.File("logs/neurosync-.log",
rollingInterval: RollingInterval.Day,
retainedFileCountLimit: 14))
.ConfigureServices((ctx, services) =>
{
var config = new NeuroConfig();
ctx.Configuration.GetSection("Discord").Bind(config);
if (string.IsNullOrWhiteSpace(config.Token) || config.Token == "YOUR_BOT_TOKEN_HERE")
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("ERROR: Set your bot token in appsettings.json (Discord:Token)");
Console.ResetColor();
Environment.Exit(1);
}
services.AddSingleton(config);
var socketConfig = new DiscordSocketConfig
{
GatewayIntents =
GatewayIntents.Guilds |
GatewayIntents.GuildMembers |
GatewayIntents.GuildPresences,
AlwaysDownloadUsers = true,
LogLevel = LogSeverity.Info,
MessageCacheSize = 0,
};
var client = new DiscordSocketClient(socketConfig);
services.AddSingleton(client);
services.AddSingleton<SuspicionAnalyzer>();
services.AddSingleton<SlashCommandHandler>();
services.AddHostedService<ProtectionService>();
services.AddHostedService<BotStartupService>();
})
.Build();
await host.RunAsync();
/// <summary>
/// Connects the Discord client and registers slash commands on application startup.
/// </summary>
file sealed class BotStartupService(
DiscordSocketClient client,
SlashCommandHandler commands,
NeuroConfig config,
ILogger<BotStartupService> log) : IHostedService
{
public async Task StartAsync(CancellationToken ct)
{
client.Log += msg =>
{
var level = msg.Severity switch
{
LogSeverity.Critical => LogLevel.Critical,
LogSeverity.Error => LogLevel.Error,
LogSeverity.Warning => LogLevel.Warning,
LogSeverity.Info => LogLevel.Information,
LogSeverity.Debug => LogLevel.Debug,
_ => LogLevel.Trace
};
log.Log(level, msg.Exception, "[Discord.Net] {Message}", msg.Message);
return Task.CompletedTask;
};
client.Ready += () =>
{
log.LogInformation("Bot is ready — connected to {Count} guild(s)", client.Guilds.Count);
foreach (var g in client.Guilds)
log.LogInformation(" Guild: {Name} ({Id}) — {Members} members", g.Name, g.Id, g.MemberCount);
return Task.CompletedTask;
};
await commands.RegisterAsync();
await client.LoginAsync(TokenType.Bot, config.Token);
await client.StartAsync();
}
public async Task StopAsync(CancellationToken ct)
{
await client.StopAsync();
}
}