-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
84 lines (72 loc) · 2.7 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
84
using chatbot;
using Discord.WebSocket;
using OpenAI.Chat;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Console.WriteLine("Loading variables...");
Config config = Config.LoadFromEnvFile();
// Set up dependency injection
var services = new ServiceCollection()
.AddHttpClient()
.AddLogging(builder =>
{
builder.AddSimpleConsole();
builder.AddSeq(config.SeqUrl, config.SeqApiKey);
}).BuildServiceProvider();
ILogger<Program> log = services.GetRequiredService<ILogger<Program>>();
var cancellationTokenSource = new CancellationTokenSource();
Console.CancelKeyPress += (sender, e) =>
{
e.Cancel = true;
cancellationTokenSource.Cancel();
log.LogInformation("Cancellation requested via Ctrl+C.");
};
log.LogInformation("Constructing Discord config...");
var discordConfig = new DiscordSocketConfig { MessageCacheSize = 100, GatewayIntents = Discord.GatewayIntents.AllUnprivileged | Discord.GatewayIntents.MessageContent };
var client = new DiscordSocketClient(discordConfig);
bool isConnected = false;
var aiClient = new ChatClient(model: "gpt-4o", apiKey: config.OpenAiApiKey);
var archive = await Archive.CreateAsync();
var bot = new Bot { Client = client, Config = config, AI = aiClient, Archive = archive, Logger = log, Cancellation = cancellationTokenSource };
client.Log += LogAsync;
client.Ready += ReadyAsync;
Task LogAsync(Discord.LogMessage arg)
{
log.LogInformation(arg.Message);
return Task.CompletedTask;
}
Task ReadyAsync()
{
if (isConnected)
{
log.LogWarning("ReadyAsync was called, but discord User is already connected!");
return Task.CompletedTask;
}
isConnected = true;
client.MessageReceived += async (arg) => await Task.Run(() => bot.MessageReceivedAsync(arg));
client.InteractionCreated += async (arg) => await Task.Run(() => bot.InteractionCreatedAsync(arg));
log.LogInformation($"Discord User {client.CurrentUser} is connected!");
return Task.CompletedTask;
}
log.LogInformation("Logging into Discord...");
await client.LoginAsync(Discord.TokenType.Bot, config.DiscordToken);
log.LogInformation("Connecting to Discord...");
await client.StartAsync();
log.LogInformation("Ready. Press cancel (Ctrl+C) or send !shutdown message in discord to exit.");
try
{
await Task.Delay(Timeout.Infinite, cancellationTokenSource.Token);
}
catch (TaskCanceledException)
{
log.LogInformation("Cancellation requested, shutting down...");
}
catch (Exception ex)
{
log.LogError(ex, "An error occurred. Shutting down...");
}
log.LogInformation("Logging out...");
await client.LogoutAsync();
log.LogInformation("Disposing client...");
await client.DisposeAsync();
log.LogInformation("Shutdown complete.");