-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
110 lines (97 loc) · 3.61 KB
/
Program.cs
File metadata and controls
110 lines (97 loc) · 3.61 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
99
100
101
102
103
104
105
106
107
108
109
110
using Discord;
using Discord.WebSocket;
using DiscordOllamaBot.Handlers;
using DiscordOllamaBot.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
var host = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, config) =>
{
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
})
.ConfigureServices((context, services) =>
{
// Discord client configuration with required intents
var discordConfig = new DiscordSocketConfig
{
GatewayIntents = GatewayIntents.Guilds |
GatewayIntents.GuildMessages |
GatewayIntents.DirectMessages |
GatewayIntents.MessageContent,
LogLevel = LogSeverity.Info
};
services.AddSingleton(discordConfig);
services.AddSingleton<DiscordSocketClient>();
// Services
services.AddSingleton<OllamaService>();
services.AddSingleton<ConversationService>();
services.AddSingleton<MessageHandler>();
})
.ConfigureLogging(logging =>
{
logging.AddConsole();
logging.SetMinimumLevel(LogLevel.Information);
})
.Build();
var logger = host.Services.GetRequiredService<ILogger<Program>>();
var client = host.Services.GetRequiredService<DiscordSocketClient>();
var configuration = host.Services.GetRequiredService<IConfiguration>();
var ollamaService = host.Services.GetRequiredService<OllamaService>();
// Set up Discord logging
client.Log += logMessage =>
{
var severity = logMessage.Severity switch
{
LogSeverity.Critical => LogLevel.Critical,
LogSeverity.Error => LogLevel.Error,
LogSeverity.Warning => LogLevel.Warning,
LogSeverity.Info => LogLevel.Information,
LogSeverity.Verbose => LogLevel.Debug,
LogSeverity.Debug => LogLevel.Trace,
_ => LogLevel.Information
};
logger.Log(severity, logMessage.Exception, "[Discord] {Message}", logMessage.Message);
return Task.CompletedTask;
};
// Initialize message handler
var messageHandler = host.Services.GetRequiredService<MessageHandler>();
messageHandler.Initialize();
// Get bot token
var token = configuration["Discord:Token"];
if (string.IsNullOrWhiteSpace(token) || token == "YOUR_BOT_TOKEN")
{
logger.LogError("Please set your Discord bot token in appsettings.json");
return;
}
// Check Ollama availability
logger.LogInformation("Checking Ollama availability...");
if (!await ollamaService.IsAvailableAsync())
{
logger.LogWarning("Ollama may not be available or the configured model is not installed. The bot will start but may not respond properly.");
}
else
{
logger.LogInformation("Ollama is available with the configured model.");
}
// Connect to Discord
logger.LogInformation("Starting Discord bot...");
await client.LoginAsync(TokenType.Bot, token);
await client.StartAsync();
// Wait for ready
var readyTcs = new TaskCompletionSource();
client.Ready += () =>
{
logger.LogInformation("Bot is connected and ready! Logged in as {Username}", client.CurrentUser.Username);
readyTcs.TrySetResult();
return Task.CompletedTask;
};
await readyTcs.Task;
// Keep the bot running until shutdown
logger.LogInformation("Bot is running. Press Ctrl+C to exit.");
logger.LogInformation("- Mention the bot in channels to chat");
logger.LogInformation("- Send direct messages to chat without mentions");
await host.WaitForShutdownAsync();
logger.LogInformation("Shutting down...");
await client.StopAsync();