From eb06fc810285df052615ab06bbf0394ab569b6be Mon Sep 17 00:00:00 2001 From: skyjoshua Date: Sun, 15 Mar 2026 05:44:32 +0000 Subject: [PATCH] Holy Big Commit! --- SkyBot/Commands/CommandRegistry.cs | 33 +++++++++ SkyBot/Commands/CommandTemplate.cs | 29 ++++++++ SkyBot/Commands/Dev/Test.cs | 32 +++++++++ SkyBot/Commands/Fun/Echo.cs | 42 +++++++++++ SkyBot/Commands/HelpCommand.cs | 30 -------- SkyBot/Commands/Info/Devcentral.cs | 29 ++++++++ SkyBot/Commands/Info/Help.cs | 92 +++++++++++++++++++++++++ SkyBot/Commands/Info/JoinSite.cs | 29 ++++++++ SkyBot/Commands/Info/Minecraft.cs | 31 +++++++++ SkyBot/Commands/Info/Source.cs | 29 ++++++++ SkyBot/Commands/Info/Suggest.cs | 29 ++++++++ SkyBot/Commands/Info/SwaggerAPI.cs | 29 ++++++++ SkyBot/Commands/Info/UserCount.cs | 31 +++++++++ SkyBot/Commands/Info/Version.cs | 30 ++++++++ SkyBot/Commands/Mod/Ban.cs | 36 ++++++++++ SkyBot/Commands/Mod/Kick.cs | 37 ++++++++++ SkyBot/Config.cs | 3 +- SkyBot/Helpers/MessageHelper.cs | 24 +++++++ SkyBot/Helpers/PermissionHelper.cs | 8 +++ SkyBot/Helpers/ValourUsercountHelper.cs | 32 +++++++++ SkyBot/Models/CommandContext.cs | 18 +++++ SkyBot/Models/ICommand.cs | 14 ++++ SkyBot/Services/BotService.cs | 4 ++ SkyBot/Services/ChannelService.cs | 23 +++---- SkyBot/Services/Messages/Create.cs | 38 +++++----- SkyBot/SkyBot.cs | 4 -- SkyBot/SkyBot.csproj | 1 + 27 files changed, 672 insertions(+), 65 deletions(-) create mode 100644 SkyBot/Commands/CommandRegistry.cs create mode 100644 SkyBot/Commands/CommandTemplate.cs create mode 100644 SkyBot/Commands/Dev/Test.cs create mode 100644 SkyBot/Commands/Fun/Echo.cs delete mode 100644 SkyBot/Commands/HelpCommand.cs create mode 100644 SkyBot/Commands/Info/Devcentral.cs create mode 100644 SkyBot/Commands/Info/Help.cs create mode 100644 SkyBot/Commands/Info/JoinSite.cs create mode 100644 SkyBot/Commands/Info/Minecraft.cs create mode 100644 SkyBot/Commands/Info/Source.cs create mode 100644 SkyBot/Commands/Info/Suggest.cs create mode 100644 SkyBot/Commands/Info/SwaggerAPI.cs create mode 100644 SkyBot/Commands/Info/UserCount.cs create mode 100644 SkyBot/Commands/Info/Version.cs create mode 100644 SkyBot/Commands/Mod/Ban.cs create mode 100644 SkyBot/Commands/Mod/Kick.cs create mode 100644 SkyBot/Helpers/MessageHelper.cs create mode 100644 SkyBot/Helpers/ValourUsercountHelper.cs create mode 100644 SkyBot/Models/CommandContext.cs create mode 100644 SkyBot/Models/ICommand.cs diff --git a/SkyBot/Commands/CommandRegistry.cs b/SkyBot/Commands/CommandRegistry.cs new file mode 100644 index 0000000..59650ff --- /dev/null +++ b/SkyBot/Commands/CommandRegistry.cs @@ -0,0 +1,33 @@ +using SkyBot.Models; + +namespace SkyBot.Commands +{ + public static class CommandRegistry + { + public static readonly Dictionary Commands = new(); + public static readonly Dictionary> Sections = new(); + + static CommandRegistry() + { + var allCommands = AppDomain.CurrentDomain.GetAssemblies() + .SelectMany(a => a.GetTypes()) + .Where(t => typeof(ICommand).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract) + .Select(t => (ICommand?)Activator.CreateInstance(t)) + .Select(c => c!); + + foreach (var cmd in allCommands) + { + Commands[cmd.Name.ToLower()] = cmd; + foreach (var alias in cmd.Aliases) + { + Commands[alias.ToLower()] = cmd; + } + + Sections = Commands.Values + .Distinct() + .GroupBy(c => c.Section.ToLower()) + .ToDictionary(g => g.Key, g => g.ToList()); + } + } + } +} \ No newline at end of file diff --git a/SkyBot/Commands/CommandTemplate.cs b/SkyBot/Commands/CommandTemplate.cs new file mode 100644 index 0000000..d725003 --- /dev/null +++ b/SkyBot/Commands/CommandTemplate.cs @@ -0,0 +1,29 @@ +using System.Collections.Concurrent; +using SkyBot.Models; +using Valour.Sdk.Models; + +namespace SkyBot.Commands +{ + public class CommandTemplate : ICommand + { + public string Name => "template"; + public string[] Aliases => []; + public string Description => ""; + public string Section => "template"; + public string Usage => ""; + + public async Task Execute(CommandContext ctx) + { + ConcurrentDictionary channelCache = ctx.ChannelCache; + long channelId = ctx.ChannelId; + PlanetMember member = ctx.Member; + + string message = $""; + + if (channelCache.TryGetValue(channelId, out var channel)) + { + await MessageHelper.ReplyAsync(ctx, channel, message); + } + } + } +} \ No newline at end of file diff --git a/SkyBot/Commands/Dev/Test.cs b/SkyBot/Commands/Dev/Test.cs new file mode 100644 index 0000000..abf2287 --- /dev/null +++ b/SkyBot/Commands/Dev/Test.cs @@ -0,0 +1,32 @@ +using System.Collections.Concurrent; +using SkyBot.Helpers; +using SkyBot.Models; +using Valour.Sdk.Client; +using Valour.Sdk.Models; + +namespace SkyBot.Commands +{ + public class Test : ICommand + { + public string Name => "test"; + public string[] Aliases => []; + public string Description => "Just a test command"; + public string Section => "Dev"; + public string Usage => "test"; + + public async Task Execute(CommandContext ctx) + { + ConcurrentDictionary channelCache = ctx.ChannelCache; + long channelId = ctx.ChannelId; + ValourClient client = ctx.Client; + PlanetMember member = ctx.Member; + Message message = ctx.Message; + Planet planet = ctx.Planet; + + if (channelCache.TryGetValue(channelId, out var channel)) + { + await MessageHelper.ReplyAsync(ctx, channel, "This is a test message"); + } + } + } +} \ No newline at end of file diff --git a/SkyBot/Commands/Fun/Echo.cs b/SkyBot/Commands/Fun/Echo.cs new file mode 100644 index 0000000..c680f76 --- /dev/null +++ b/SkyBot/Commands/Fun/Echo.cs @@ -0,0 +1,42 @@ +using System.Collections.Concurrent; +using System.Net.NetworkInformation; +using SkyBot.Helpers; +using SkyBot.Models; +using Valour.Sdk.Models; + +namespace SkyBot.Commands +{ + public class Echo : ICommand + { + public string Name => "echo"; + public string[] Aliases => []; + public string Description => "Echos what you said through the bot."; + public string Section => "Fun"; + public string Usage => "echo "; + + public async Task Execute(CommandContext ctx) + { + ConcurrentDictionary channelCache = ctx.ChannelCache; + long channelId = ctx.ChannelId; + PlanetMember member = ctx.Member; + String[] args = ctx.Args; + Message message = ctx.Message; + + string reply = string.Join(" ", args); + + if (channelCache.TryGetValue(channelId, out var channel)) + { + if (string.IsNullOrWhiteSpace(reply)) await channel.SendMessageAsync($"{MentionHelper.Mention(member)} Enter a message to echo."); + + reply = $"{member.Name} ยป {reply}"; + + if (reply.Length > 2048) + { + reply = reply.Substring(0, 2048); + } + + await MessageHelper.ReplyAsync(ctx, channel, reply); + } + } + } +} \ No newline at end of file diff --git a/SkyBot/Commands/HelpCommand.cs b/SkyBot/Commands/HelpCommand.cs deleted file mode 100644 index 48cc518..0000000 --- a/SkyBot/Commands/HelpCommand.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System.Collections.Concurrent; -using SkyBot.Helpers; -using Valour.Sdk.Models; - -namespace SkyBot.Commands -{ - public static class HelpCommand - { - public static async Task Execute(ConcurrentDictionary channelCache, long channelId, String prefix, PlanetMember member) - { - string helpMessage = $@"**Skybot Commands**: - - `s/echo ` - Echos text into the chat - - `s/suggest` - Shares the suggestions link - - `s/source` - Sends link for the source code - - `s/joincode` - Sends a link to a github that you can use to make your bot join your planet. - - `s/joinsite` - Sends a link to a website that you can use to make yout bot join your planet. - - `s/api|swagger` - Sends a link to the Swagger API - - `s/cmds|help` - Shows this list - - `s/usercount` - Shows the user count of Valour - - `s/devcentral` - Sends the invite link to the Dev Central Planet - - `s/mc` - Sends Unofficial ValourSMP IPs - "; - - if (channelCache.TryGetValue(channelId, out var channel)) - { - await channel.SendMessageAsync($"{MentionHelper.Mention(member)}\n{helpMessage}"); - } - } - } -} \ No newline at end of file diff --git a/SkyBot/Commands/Info/Devcentral.cs b/SkyBot/Commands/Info/Devcentral.cs new file mode 100644 index 0000000..d0c5556 --- /dev/null +++ b/SkyBot/Commands/Info/Devcentral.cs @@ -0,0 +1,29 @@ +using System.Collections.Concurrent; +using SkyBot.Models; +using Valour.Sdk.Models; + +namespace SkyBot.Commands +{ + public class Devcentral : ICommand + { + public string Name => "devcentral"; + public string[] Aliases => ["dev"]; + public string Description => "Sends an invite link to the Dev Central Planet."; + public string Section => "Info"; + public string Usage => "devcentral|dev"; + + public async Task Execute(CommandContext ctx) + { + ConcurrentDictionary channelCache = ctx.ChannelCache; + long channelId = ctx.ChannelId; + PlanetMember member = ctx.Member; + + string message = $"you can join the Dev Central (ID: 42439954653511681) planet here: https://app.valour.gg/I/k2tz9c4i"; + + if (channelCache.TryGetValue(channelId, out var channel)) + { + await MessageHelper.ReplyAsync(ctx, channel, message); + } + } + } +} \ No newline at end of file diff --git a/SkyBot/Commands/Info/Help.cs b/SkyBot/Commands/Info/Help.cs new file mode 100644 index 0000000..340ac63 --- /dev/null +++ b/SkyBot/Commands/Info/Help.cs @@ -0,0 +1,92 @@ +using System.Collections.Concurrent; +using System.Text; +using SkyBot.Helpers; +using SkyBot.Models; +using Valour.Sdk.Models; +using Valour.Shared.Authorization; + +namespace SkyBot.Commands +{ + public class Help : ICommand + { + public string Name => "help"; + public string[] Aliases => ["h"]; + public string Description => "Shows all the commands and their descriptions."; + public string Section => "Info"; + public string Usage => "help|h [section] [page]"; + private const int PageSize = 5; + + public async Task Execute(CommandContext ctx) + { + ConcurrentDictionary channelCache = ctx.ChannelCache; + long channelId = ctx.ChannelId; + string[] args = ctx.Args; + PlanetMember member = ctx.Member; + + bool isOwner = await PermissionHelper.IsOwner(member); + + if (!channelCache.TryGetValue(channelId, out var channel)) return; + + // Show all sections. + if (args.Length == 0) + { + var sb = new StringBuilder(); + sb.AppendLine("**Available Categories**"); + foreach (var section in CommandRegistry.Sections.Keys) + { + if (section == "template") continue; + if (section == "dev" && !isOwner) continue; + if (section == "mod" && !PermissionHelper.HasPermAsync(member, [PlanetPermissions.Kick, PlanetPermissions.Ban, PlanetPermissions.ManageRoles]).Result) continue; + sb.AppendLine($"- `{section.ToTitleCase()}` ({CommandRegistry.Sections[section].Count})"); + } + sb.AppendLine($"\nUse `{Config.Prefix}help ` to see commands in a category."); + await MessageHelper.ReplyAsync(ctx, channel, sb.ToString()); + return; + } + + // section [page] + string sectionName = args[0].ToLower(); + if (!CommandRegistry.Sections.TryGetValue(sectionName, out var commands)) + { + await MessageHelper.ReplyAsync(ctx, channel, $"Unknown category `{sectionName}`."); + return; + } + + if (sectionName == "dev" && !isOwner) + { + await MessageHelper.ReplyAsync(ctx, channel, $"Unknown category `{sectionName}`."); + return; + } + + if (sectionName == "mod" && !PermissionHelper.HasPermAsync(member, [PlanetPermissions.Kick, PlanetPermissions.Ban, PlanetPermissions.ManageRoles]).Result) + { + await MessageHelper.ReplyAsync(ctx, channel, $"Unknown category `{sectionName}`."); + return; + } + + int page = 1; + if (args.Length >= 2 && int.TryParse(args[1], out int parsedPage)) + { + page = parsedPage; + } + + int totalPages = (int)Math.Ceiling(commands.Count / (double)PageSize); + page = Math.Clamp(page, 1, totalPages); + + var pageCommands = commands.Skip((page - 1) * PageSize).Take(PageSize); + + var sb2 = new StringBuilder(); + sb2.AppendLine($"**{sectionName.ToTitleCase()} commands** (Page {page}/{totalPages}):"); + foreach (var cmd in pageCommands) + { + var name = cmd.Aliases.Length > 0 + ? $"{cmd.Name}|{string.Join("|", cmd.Aliases)}" + : cmd.Name; + sb2.AppendLine($"`{Config.Prefix}{name}` - {cmd.Description}"); + } + sb2.AppendLine($"\nUse `{Config.Prefix}help {sectionName} ` to see more."); + + await MessageHelper.ReplyAsync(ctx, channel, sb2.ToString()); + } + } +} \ No newline at end of file diff --git a/SkyBot/Commands/Info/JoinSite.cs b/SkyBot/Commands/Info/JoinSite.cs new file mode 100644 index 0000000..70bc667 --- /dev/null +++ b/SkyBot/Commands/Info/JoinSite.cs @@ -0,0 +1,29 @@ +using System.Collections.Concurrent; +using SkyBot.Models; +using Valour.Sdk.Models; + +namespace SkyBot.Commands +{ + public class JoinSite : ICommand + { + public string Name => "joinsite"; + public string[] Aliases => []; + public string Description => "Links to a site to help your bots join a planet."; + public string Section => "Info"; + public string Usage => "joinsite"; + + public async Task Execute(CommandContext ctx) + { + ConcurrentDictionary channelCache = ctx.ChannelCache; + long channelId = ctx.ChannelId; + PlanetMember member = ctx.Member; + + string message = $"You can use this website to easily add your bot to a planet: https://skyjoshua.xyz/planetjoiner"; + + if (channelCache.TryGetValue(channelId, out var channel)) + { + await MessageHelper.ReplyAsync(ctx, channel, message); + } + } + } +} \ No newline at end of file diff --git a/SkyBot/Commands/Info/Minecraft.cs b/SkyBot/Commands/Info/Minecraft.cs new file mode 100644 index 0000000..af1b987 --- /dev/null +++ b/SkyBot/Commands/Info/Minecraft.cs @@ -0,0 +1,31 @@ +using System.Collections.Concurrent; +using SkyBot.Models; +using Valour.Sdk.Models; + +namespace SkyBot.Commands +{ + public class Minecraft : ICommand + { + public string Name => "minecraft"; + public string[] Aliases => ["mc"]; + public string Description => "Sends the Unofficial ValourSMP IPs"; + public string Section => "Info"; + public string Usage => "minecraft|mc"; + + public async Task Execute(CommandContext ctx) + { + ConcurrentDictionary channelCache = ctx.ChannelCache; + long channelId = ctx.ChannelId; + PlanetMember member = ctx.Member; + + string message = @$"you can join the Unofficial ValourSMP Minecraft Server by using this ip: + Java: `valour.sxsc.xyz`, Bedrock: `valourbr.sxsc.xyz` Both with the default ports. + Cool features can be found here: https://sxsc.xyz/servers/valour/"; + + if (channelCache.TryGetValue(channelId, out var channel)) + { + await MessageHelper.ReplyAsync(ctx, channel, message); + } + } + } +} \ No newline at end of file diff --git a/SkyBot/Commands/Info/Source.cs b/SkyBot/Commands/Info/Source.cs new file mode 100644 index 0000000..ca77c87 --- /dev/null +++ b/SkyBot/Commands/Info/Source.cs @@ -0,0 +1,29 @@ +using System.Collections.Concurrent; +using SkyBot.Models; +using Valour.Sdk.Models; + +namespace SkyBot.Commands +{ + public class Source : ICommand + { + public string Name => "source"; + public string[] Aliases => ["src"]; + public string Description => "Shows the source code for this bot."; + public string Section => "Info"; + public string Usage => "source"; + + public async Task Execute(CommandContext ctx) + { + ConcurrentDictionary channelCache = ctx.ChannelCache; + long channelId = ctx.ChannelId; + PlanetMember member = ctx.Member; + + string message = $"You can find my source code here: {Config.SourceLink}"; + + if (channelCache.TryGetValue(channelId, out var channel)) + { + await MessageHelper.ReplyAsync(ctx, channel, message); + } + } + } +} \ No newline at end of file diff --git a/SkyBot/Commands/Info/Suggest.cs b/SkyBot/Commands/Info/Suggest.cs new file mode 100644 index 0000000..e152666 --- /dev/null +++ b/SkyBot/Commands/Info/Suggest.cs @@ -0,0 +1,29 @@ +using System.Collections.Concurrent; +using SkyBot.Models; +using Valour.Sdk.Models; + +namespace SkyBot.Commands +{ + public class Suggest : ICommand + { + public string Name => "suggest"; + public string[] Aliases => []; + public string Description => "Shows the source code for this bot."; + public string Section => "Info"; + public string Usage => "source"; + + public async Task Execute(CommandContext ctx) + { + ConcurrentDictionary channelCache = ctx.ChannelCache; + long channelId = ctx.ChannelId; + PlanetMember member = ctx.Member; + + string message = $"You can suggest a command to be added here: https://docs.google.com/spreadsheets/d/1CzcpLAuMiPL_RODrZ5x25cPj8yE-rR3mEnqrd_2Fbmk"; + + if (channelCache.TryGetValue(channelId, out var channel)) + { + await MessageHelper.ReplyAsync(ctx, channel, message); + } + } + } +} \ No newline at end of file diff --git a/SkyBot/Commands/Info/SwaggerAPI.cs b/SkyBot/Commands/Info/SwaggerAPI.cs new file mode 100644 index 0000000..34ffa92 --- /dev/null +++ b/SkyBot/Commands/Info/SwaggerAPI.cs @@ -0,0 +1,29 @@ +using System.Collections.Concurrent; +using SkyBot.Models; +using Valour.Sdk.Models; + +namespace SkyBot.Commands +{ + public class SwaggerAPI : ICommand + { + public string Name => "swagger"; + public string[] Aliases => ["api"]; + public string Description => "Sends a link to the Valour.gg Swagger API."; + public string Section => "Info"; + public string Usage => "swagger|api"; + + public async Task Execute(CommandContext ctx) + { + ConcurrentDictionary channelCache = ctx.ChannelCache; + long channelId = ctx.ChannelId; + PlanetMember member = ctx.Member; + + string message = $"Here is a link to the Swagger API: https://api.valour.gg/swagger"; + + if (channelCache.TryGetValue(channelId, out var channel)) + { + await MessageHelper.ReplyAsync(ctx, channel, message); + } + } + } +} \ No newline at end of file diff --git a/SkyBot/Commands/Info/UserCount.cs b/SkyBot/Commands/Info/UserCount.cs new file mode 100644 index 0000000..92e0421 --- /dev/null +++ b/SkyBot/Commands/Info/UserCount.cs @@ -0,0 +1,31 @@ +using System.Collections.Concurrent; +using SkyBot.Helpers; +using SkyBot.Models; +using Valour.Sdk.Models; + +namespace SkyBot.Commands +{ + public class UserCount : ICommand + { + public string Name => "usercount"; + public string[] Aliases => ["users"]; + public string Description => "Shows the user count of Valour."; + public string Section => "Info"; + public string Usage => "usercount|users"; + + public async Task Execute(CommandContext ctx) + { + ConcurrentDictionary channelCache = ctx.ChannelCache; + long channelId = ctx.ChannelId; + PlanetMember member = ctx.Member; + + string message = @$"Current Valour user count is: {ValourUsercountHelper.ValourUsercount:N0} + You can see a graph of the user count here: /meow"; + + if (channelCache.TryGetValue(channelId, out var channel)) + { + await MessageHelper.ReplyAsync(ctx, channel, message); + } + } + } +} \ No newline at end of file diff --git a/SkyBot/Commands/Info/Version.cs b/SkyBot/Commands/Info/Version.cs new file mode 100644 index 0000000..f097293 --- /dev/null +++ b/SkyBot/Commands/Info/Version.cs @@ -0,0 +1,30 @@ +using System.Collections.Concurrent; +using SkyBot.Models; +using Valour.Sdk.Models; + +namespace SkyBot.Commands +{ + public class Version : ICommand + { + public string Name => "version"; + public string[] Aliases => []; + public string Description => "Shows the current version of the Bot and Valour."; + public string Section => "Info"; + public string Usage => ""; + + public async Task Execute(CommandContext ctx) + { + ConcurrentDictionary channelCache = ctx.ChannelCache; + long channelId = ctx.ChannelId; + PlanetMember member = ctx.Member; + + string message = @$"Bot Version: {typeof(Version).Assembly.GetName().Version} + Valour Version: {typeof(Channel).Assembly.GetName().Version}"; + + if (channelCache.TryGetValue(channelId, out var channel)) + { + await MessageHelper.ReplyAsync(ctx, channel, message); + } + } + } +} \ No newline at end of file diff --git a/SkyBot/Commands/Mod/Ban.cs b/SkyBot/Commands/Mod/Ban.cs new file mode 100644 index 0000000..a776b6c --- /dev/null +++ b/SkyBot/Commands/Mod/Ban.cs @@ -0,0 +1,36 @@ +using System.Collections.Concurrent; +using SkyBot.Helpers; +using SkyBot.Models; +using Valour.Sdk.Models; +using Valour.Shared.Authorization; + +namespace SkyBot.Commands +{ + public class Ban : ICommand + { + public string Name => "ban"; + public string[] Aliases => []; + public string Description => "Bans a user from the planet."; + public string Section => "mod"; + public string Usage => "ban [reason]"; + + public async Task Execute(CommandContext ctx) + { + ConcurrentDictionary channelCache = ctx.ChannelCache; + long channelId = ctx.ChannelId; + PlanetMember member = ctx.Member; + + if (channelCache.TryGetValue(channelId, out var channel)) + { + if (!PermissionHelper.HasPermAsync(member, [PlanetPermissions.Ban]).Result) + { + await MessageHelper.ReplyAsync(ctx, channel, $"You don't have permission to use this command."); + return; + } + + string message = $"Work in progress..."; + await MessageHelper.ReplyAsync(ctx, channel, message); + } + } + } +} \ No newline at end of file diff --git a/SkyBot/Commands/Mod/Kick.cs b/SkyBot/Commands/Mod/Kick.cs new file mode 100644 index 0000000..67becc3 --- /dev/null +++ b/SkyBot/Commands/Mod/Kick.cs @@ -0,0 +1,37 @@ +using System.Collections.Concurrent; +using SkyBot.Helpers; +using SkyBot.Models; +using Valour.Sdk.Models; +using Valour.Shared.Authorization; + +namespace SkyBot.Commands +{ + public class Kick : ICommand + { + public string Name => "kick"; + public string[] Aliases => []; + public string Description => "Kicks a user from the planet."; + public string Section => "mod"; + public string Usage => "kick [reason]"; + + public async Task Execute(CommandContext ctx) + { + ConcurrentDictionary channelCache = ctx.ChannelCache; + long channelId = ctx.ChannelId; + PlanetMember member = ctx.Member; + + if (channelCache.TryGetValue(channelId, out var channel)) + { + if (!PermissionHelper.HasPermAsync(member, [PlanetPermissions.Kick]).Result) + { + await MessageHelper.ReplyAsync(ctx, channel, $"You don't have permission to use this command."); + return; + } + + string message = $"Work in progress..."; + + await MessageHelper.ReplyAsync(ctx, channel, message); + } + } + } +} \ No newline at end of file diff --git a/SkyBot/Config.cs b/SkyBot/Config.cs index b2b203b..74ffd81 100644 --- a/SkyBot/Config.cs +++ b/SkyBot/Config.cs @@ -1,9 +1,10 @@ -namespace Skybot +namespace SkyBot { public static class Config { public static readonly long OwnerId = 15652354820931584; public static readonly string Prefix = "sd/"; + public static readonly string SourceLink = "https://github.com/SkyJoshua/SkyBot"; } } \ No newline at end of file diff --git a/SkyBot/Helpers/MessageHelper.cs b/SkyBot/Helpers/MessageHelper.cs new file mode 100644 index 0000000..2d4de9c --- /dev/null +++ b/SkyBot/Helpers/MessageHelper.cs @@ -0,0 +1,24 @@ +using SkyBot.Models; +using Valour.Sdk.Models; + +public static class MessageHelper +{ + public static async Task ReplyAsync(CommandContext ctx, Channel channel, string content) + { + long? replyToId = ctx.Message.ReplyToId.HasValue ? ctx.Message.ReplyToId : ctx.Message.Id; + + var msg = new Message(ctx.Client) + { + Content = content, + ChannelId = channel.Id, + PlanetId = ctx.Planet.Id, + AuthorUserId = ctx.Client.Me.Id, + AuthorMemberId = channel.Planet?.MyMember.Id, + ReplyToId = replyToId, + Fingerprint = Guid.NewGuid().ToString() + }; + await ctx.Client.MessageService.SendMessage(msg); + } + + public static string ToTitleCase(this string str) => System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str); +} \ No newline at end of file diff --git a/SkyBot/Helpers/PermissionHelper.cs b/SkyBot/Helpers/PermissionHelper.cs index 5adb73f..a567b74 100644 --- a/SkyBot/Helpers/PermissionHelper.cs +++ b/SkyBot/Helpers/PermissionHelper.cs @@ -15,5 +15,13 @@ namespace SkyBot.Helpers ? permissions.All(permission => member.HasPermission(permission)) : permissions.Any(permission => member.HasPermission(permission)); } + + public static async Task IsOwner(PlanetMember member) + { + + if (member == null) return false; + if (member.UserId == Config.OwnerId) return true; + return false; + } } } \ No newline at end of file diff --git a/SkyBot/Helpers/ValourUsercountHelper.cs b/SkyBot/Helpers/ValourUsercountHelper.cs new file mode 100644 index 0000000..514ae6c --- /dev/null +++ b/SkyBot/Helpers/ValourUsercountHelper.cs @@ -0,0 +1,32 @@ +using System.Text.Json; + +namespace SkyBot.Helpers +{ + public static class ValourUsercountHelper { + private static readonly HttpClient _http = new HttpClient(); + private static long _valourUsercount; + public static long ValourUsercount => _valourUsercount; + public static async Task UpdateUsercount() + { + try + { + var response = await _http.GetStringAsync("https://api.valour.gg/api/users/count"); + + _valourUsercount = JsonSerializer.Deserialize(response); + + Console.WriteLine($"Valour user count updated: {_valourUsercount}"); + } catch (Exception ex) + { + Console.WriteLine($"Failed to update valour user count: {ex.Message}"); + } + } + + public static void StartUpdater() + { + var timer = new System.Timers.Timer(300_000); + timer.Elapsed += async (_, _) => await UpdateUsercount(); + timer.AutoReset = true; + timer.Start(); + } + } +} \ No newline at end of file diff --git a/SkyBot/Models/CommandContext.cs b/SkyBot/Models/CommandContext.cs new file mode 100644 index 0000000..a9cce9e --- /dev/null +++ b/SkyBot/Models/CommandContext.cs @@ -0,0 +1,18 @@ +using System.Collections.Concurrent; +using Valour.Sdk.Client; +using Valour.Sdk.Models; + +namespace SkyBot.Models +{ + public class CommandContext + { + public required ValourClient Client{ get; set; } + public required ConcurrentDictionary ChannelCache { get; set; } + public required PlanetMember Member { get; set; } + public required Message Message { get; set; } + public required Planet Planet { get; set; } + public required long ChannelId { get; set; } + public required string[] Args { get; set; } + + } +} \ No newline at end of file diff --git a/SkyBot/Models/ICommand.cs b/SkyBot/Models/ICommand.cs new file mode 100644 index 0000000..1abeb7d --- /dev/null +++ b/SkyBot/Models/ICommand.cs @@ -0,0 +1,14 @@ +using System.Collections.Concurrent; + +namespace SkyBot.Models +{ + public interface ICommand + { + string Name { get; } + string[] Aliases { get; } + string Description { get; } + string Section { get; } + string Usage { get; } + Task Execute(CommandContext ctx); + } +} \ No newline at end of file diff --git a/SkyBot/Services/BotService.cs b/SkyBot/Services/BotService.cs index 279cdc4..3162ae0 100644 --- a/SkyBot/Services/BotService.cs +++ b/SkyBot/Services/BotService.cs @@ -1,5 +1,6 @@ using System.Collections.Concurrent; using DotNetEnv; +using SkyBot.Helpers; using Valour.Sdk.Client; using Valour.Sdk.Models; @@ -21,6 +22,9 @@ namespace SkyBot.Services if (!loginResult.Success) {Console.WriteLine($"Login Failed: {loginResult.Message}"); return;} Console.WriteLine($"Logged in as {client.Me.Name} (ID: {client.Me.Id})"); + await ValourUsercountHelper.UpdateUsercount(); + ValourUsercountHelper.StartUpdater(); + await PlanetService.InitializePlanetsAsync(client, channelCache, initalizedPlanets); client.PlanetService.JoinedPlanetsUpdated += async () => { diff --git a/SkyBot/Services/ChannelService.cs b/SkyBot/Services/ChannelService.cs index 630df4e..5547048 100644 --- a/SkyBot/Services/ChannelService.cs +++ b/SkyBot/Services/ChannelService.cs @@ -1,4 +1,5 @@ using System.Collections.Concurrent; +using System.Security.Cryptography.X509Certificates; using Valour.Sdk.Models; using Valour.Shared.Models; @@ -6,32 +7,30 @@ namespace SkyBot.Services { public static class ChannelService { - private static readonly SemaphoreSlim _channelSemaphore = new SemaphoreSlim(3, 3); public static async Task InitializeChannelsAsync( ConcurrentDictionary channelCache, Planet planet) { - var tasks = planet.Channels.Select(async channel => + foreach (var channel in planet.Channels) { channelCache[channel.Id] = channel; - if (channel.ChannelType == ChannelTypeEnum.PlanetChat) - { - await _channelSemaphore.WaitAsync(); + } + + _ = Task.Run(async () => + { + foreach (var channel in planet.Channels.Where(c => c.ChannelType == ChannelTypeEnum.PlanetChat)){ try { await channel.OpenWithResult("SkyBot"); Console.WriteLine($"Realtime opened for: {planet.Name} (ID: {planet.Id}) -> {channel.Name} (ID: {channel.Id})"); - await Task.Delay(250); - } - finally + } catch (Exception ex) { - _channelSemaphore.Release(); + Console.WriteLine($"Error opening realtime for {channel.Id}: {ex.Message}"); } - } - }); - await Task.WhenAll(tasks); + Console.WriteLine($"All channels opened for {planet.Name}."); + }); } } } \ No newline at end of file diff --git a/SkyBot/Services/Messages/Create.cs b/SkyBot/Services/Messages/Create.cs index 2345934..e038458 100644 --- a/SkyBot/Services/Messages/Create.cs +++ b/SkyBot/Services/Messages/Create.cs @@ -1,7 +1,7 @@ using System.Collections.Concurrent; -using Skybot; using SkyBot.Commands; using SkyBot.Helpers; +using SkyBot.Models; using Valour.Sdk.Client; using Valour.Sdk.Models; @@ -15,36 +15,38 @@ namespace SkyBot.Services.Messages Message message ) { - string prefix = Config.Prefix; - if (message.AuthorUserId == client.Me.Id) return; - + string prefix = Config.Prefix; string content = message.Content ?? ""; if (string.IsNullOrWhiteSpace(content)) return; - if (!content.StartsWith(prefix)) return; + if (!content.ToLower().StartsWith(prefix)) return; long channelId = message.ChannelId; - PlanetMember member = await message.FetchAuthorMemberAsync(); - var parts = content.Substring(prefix.Length).Split(' ', StringSplitOptions.RemoveEmptyEntries); if (parts.Length == 0) return; string command = parts[0].ToLower(); string[] args = parts[1..]; - switch (command) + if (CommandRegistry.Commands.TryGetValue(command, out var handler)) { - case "help": - await HelpCommand.Execute(channelCache, channelId, prefix, member); - break; - - default: - if (channelCache.TryGetValue(channelId, out var channel)) - { - await channel.SendMessageAsync($"{MentionHelper.Mention(member)} Unknown command."); - } - break; + await handler.Execute(new CommandContext + { + ChannelCache = channelCache, + ChannelId = channelId, + Member = member, + Planet = message.Planet, + Args = args, + Message = message, + Client = client + }); + } else + { + if (channelCache.TryGetValue(channelId, out var channel)) + { + await channel.SendMessageAsync($"{MentionHelper.Mention(member)} Unknown command."); + } } } } diff --git a/SkyBot/SkyBot.cs b/SkyBot/SkyBot.cs index 4326c29..890299e 100644 --- a/SkyBot/SkyBot.cs +++ b/SkyBot/SkyBot.cs @@ -35,10 +35,6 @@ namespace SkyBot Console.WriteLine("Ready and listening..."); await Task.Delay(Timeout.Infinite); - } catch (InvalidOperationException ex) when (ex.Message.Contains("concurrent update")) - { - Console.WriteLine("Concurrent update detected, restarting..."); - await Task.Delay(1000); } catch (Exception ex) { Console.WriteLine($"Fatal error: {ex.Message}"); diff --git a/SkyBot/SkyBot.csproj b/SkyBot/SkyBot.csproj index ca2272e..eb93c8e 100644 --- a/SkyBot/SkyBot.csproj +++ b/SkyBot/SkyBot.csproj @@ -5,6 +5,7 @@ net10.0 enable enable + 0.2.0.0