diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2a2c11a --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +bin/ +obj/ +SkyAI.sln +Program.cs.old +.env diff --git a/Program.cs b/Program.cs index 641ebcf..d992af5 100644 --- a/Program.cs +++ b/Program.cs @@ -20,8 +20,9 @@ public class Program const int MaxResponseLength = 2048; const string ModelName = "llama3.1:latest"; - private static readonly Dictionary ChannelCache = new(); + private static readonly Dictionary channelCache = new(); private static readonly Dictionary> ChatHistory = new(); + private static readonly HashSet InitializedPlanets = new(); public static async Task Main() { @@ -51,15 +52,7 @@ public class Program Console.WriteLine($"Logged in as {client.Me.Name}"); - await client.BotService.JoinAllChannelsAsync(); - - foreach (var planet in client.PlanetService.JoinedPlanets) - { - foreach (var channel in planet.Channels) - { - ChannelCache[channel.Id] = channel; - } - } + await Utils.InitializePlanetsAsync(client, channelCache, InitializedPlanets); var httpClient = new HttpClient { @@ -82,13 +75,13 @@ public class Program if (content.StartsWith("s.cm")) { ChatHistory.Remove(channelId); - await Utils.SendReplyAsync(ChannelCache, channelId, $"{ping} Channel memory cleared."); + await Utils.SendReplyAsync(channelCache, channelId, $"{ping} Channel memory cleared."); return; } if (content.StartsWith("s.source")) { - await Utils.SendReplyAsync(ChannelCache, channelId, $"{ping} You can find my source code here: https://github.com/SkyJoshua/SkyAI"); + await Utils.SendReplyAsync(channelCache, channelId, $"{ping} You can find my source code here: https://github.com/SkyJoshua/SkyAI"); } if (!content.StartsWith("s.ai")) @@ -97,7 +90,7 @@ public class Program var prompt = content.Substring(5).Trim(); if (string.IsNullOrWhiteSpace(prompt)) { - await Utils.SendReplyAsync(ChannelCache, channelId, $"{ping} Enter a question."); + await Utils.SendReplyAsync(channelCache, channelId, $"{ping} Enter a question."); return; } @@ -137,7 +130,7 @@ public class Program output = Truncate(output); - await Utils.SendReplyAsync(ChannelCache, channelId, output); + await Utils.SendReplyAsync(channelCache, channelId, output); }; Console.WriteLine("Listening..."); diff --git a/utils.cs b/utils.cs index be2ae8e..904e162 100644 --- a/utils.cs +++ b/utils.cs @@ -1,6 +1,7 @@ using System.Globalization; using System.Text.Json; using Valour.Sdk.Models; +using Valour.Sdk.Client; namespace SkyAI { @@ -78,5 +79,32 @@ namespace SkyAI timer.AutoReset = true; timer.Start(); } + + public static async Task InitializePlanetsAsync(ValourClient client, Dictionary channelCache, HashSet initializedPlanets) + { + foreach (var planet in client.PlanetService.JoinedPlanets) + { + if (initializedPlanets.Contains(planet.Id)) + continue; + + Console.WriteLine($"Initializing Planet: {planet.Name}"); + + await planet.EnsureReadyAsync(); + await planet.FetchInitialDataAsync(); + + foreach (var channel in planet.Channels) + { + channelCache[channel.Id] = channel; + + if (channel.ChannelType == Valour.Shared.Models.ChannelTypeEnum.PlanetChat) + { + await channel.OpenWithResult("SkyBot"); + Console.WriteLine($"Realtime opened for: {planet.Name} -> {channel.Name}"); + } + } + + initializedPlanets.Add(planet.Id); + } + } }; }; \ No newline at end of file