diff --git a/Bot.sln b/Bot.sln index 99a02dc..94dd41d 100644 --- a/Bot.sln +++ b/Bot.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 17.6.33712.159 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bot", "Bot\Bot.csproj", "{B34BEC68-5B8A-4A3F-9A8A-2BE053BC78E1}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComponentLib", "ComponentLib\ComponentLib.csproj", "{632BFA9C-D9C8-4729-B1E8-8B4F466AE2BE}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {B34BEC68-5B8A-4A3F-9A8A-2BE053BC78E1}.Debug|Any CPU.Build.0 = Debug|Any CPU {B34BEC68-5B8A-4A3F-9A8A-2BE053BC78E1}.Release|Any CPU.ActiveCfg = Release|Any CPU {B34BEC68-5B8A-4A3F-9A8A-2BE053BC78E1}.Release|Any CPU.Build.0 = Release|Any CPU + {632BFA9C-D9C8-4729-B1E8-8B4F466AE2BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {632BFA9C-D9C8-4729-B1E8-8B4F466AE2BE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {632BFA9C-D9C8-4729-B1E8-8B4F466AE2BE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {632BFA9C-D9C8-4729-B1E8-8B4F466AE2BE}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Bot/Bot.csproj b/Bot/Bot.csproj index 0dbc52f..f43d064 100644 --- a/Bot/Bot.csproj +++ b/Bot/Bot.csproj @@ -28,4 +28,8 @@ + + + + diff --git a/Bot/Modules/Buttons/StyleCycler.cs b/Bot/Modules/Buttons/StyleCycler.cs deleted file mode 100644 index 2a2a567..0000000 --- a/Bot/Modules/Buttons/StyleCycler.cs +++ /dev/null @@ -1,105 +0,0 @@ -using Discord; -using Discord.Interactions; -using Discord.WebSocket; - -namespace Bot.Modules.Buttons; - -public class StyleCycler -{ - public class CyclerGroup : InteractionsBase - { - public CyclerGroup(Database database) : base(database) { } - - [ComponentInteraction("togglebutton:*")] - [RequireUserPermission(GuildPermission.ManageGuild, Group = "user")] - public async Task Toggle(string customId) - { - var interaction = Context.Interaction as IComponentInteraction; - var message = interaction.Message as IUserMessage; - - if (Context.User.Id.ToString() == message.Interaction.User.Id.ToString()) - { - var builder = new ComponentBuilder(); - var rows = ComponentBuilder.FromMessage(interaction.Message).ActionRows; - - for (int i = 0; i < rows.Count; i++) - { - foreach (var x in rows[i].Components) - { - switch (x) - { - case ButtonComponent button: - var buttonid = button.CustomId.Split(':')[1]; - if (buttonid == customId) - { - builder.WithButton(button.ToBuilder().WithStyle(button.Style == ButtonStyle.Success ? ButtonStyle.Danger : ButtonStyle.Success), i); - } - else - builder.WithButton(button.ToBuilder(), i); - break; - case SelectMenuComponent select: - builder.WithSelectMenu(select.ToBuilder(), i); - break; - default: - Console.WriteLine($"Unknown Component"); - break; - } - } - } - - await interaction.UpdateAsync(x => x.Components = builder.Build()); - } - else - await RespondAsync("This button isn't for you!", ephemeral: true); - } - - [ComponentInteraction("tributton:*")] - [RequireUserPermission(GuildPermission.ManageGuild, Group = "user")] - public async Task TriToggle(string customId) - { - var interaction = Context.Interaction as IComponentInteraction; - var message = interaction.Message as IUserMessage; - - if (Context.User.Id.ToString() == message.Interaction.User.Id.ToString()) - { - var builder = new ComponentBuilder(); - var rows = ComponentBuilder.FromMessage(interaction.Message).ActionRows; - - for (int i = 0; i < rows.Count; i++) - { - foreach (var x in rows[i].Components) - { - switch (x) - { - case ButtonComponent button: - var buttonid = button.CustomId.Split(':')[1]; - if (buttonid == customId) - { - // Button progession is Success -> Primary -> Danger - if (button.Style == ButtonStyle.Success) - builder.WithButton(button.ToBuilder().WithStyle(ButtonStyle.Primary), i); - else if (button.Style == ButtonStyle.Primary) - builder.WithButton(button.ToBuilder().WithStyle(ButtonStyle.Danger), i); - else if (button.Style == ButtonStyle.Danger) - builder.WithButton(button.ToBuilder().WithStyle(ButtonStyle.Success), i); - } - else - builder.WithButton(button.ToBuilder(), i); - break; - case SelectMenuComponent select: - builder.WithSelectMenu(select.ToBuilder(), i); - break; - default: - Console.WriteLine($"Unknown Component"); - break; - } - } - } - - await interaction.UpdateAsync(x => x.Components = builder.Build()); - } - else - await RespondAsync("This button isn't for you!", ephemeral: true); - } - } -} \ No newline at end of file diff --git a/Bot/Modules/Interaction/GeneralModule.cs b/Bot/Modules/Interaction/GeneralModule.cs index 3debc75..ab1933e 100644 --- a/Bot/Modules/Interaction/GeneralModule.cs +++ b/Bot/Modules/Interaction/GeneralModule.cs @@ -52,7 +52,9 @@ public async Task DisplayButtons() var buttons = new ActionRowBuilder() .AddComponent(ButtonComponents.ToggleButton("Toggle Me", "toggle", false, ButtonStyle.Success)) - .AddComponent(ButtonComponents.TriButton("Cycle Me", "cycle", false, ButtonStyle.Success)); + .AddComponent(ButtonComponents.TriButton("Cycle Me", "cycle", false, ButtonStyle.Success)) + .AddComponent(ComponentLib.Buttons.ToggleButton("Lib Toggle", "toggle1", false, ButtonStyle.Success)) + .AddComponent(ComponentLib.Buttons.TriButton("Lib Toggle", "cycle1", false, ButtonStyle.Success)); var actionRow = new ComponentBuilder() .AddRow(buttons) diff --git a/Bot/Startup.cs b/Bot/Startup.cs index 8ddfe3c..2b35897 100644 --- a/Bot/Startup.cs +++ b/Bot/Startup.cs @@ -7,6 +7,8 @@ using Microsoft.Extensions.DependencyInjection; using System.Reflection; +using ComponentLib; + namespace Bot { public class Startup @@ -62,12 +64,22 @@ public async Task InstallCommandsAsync() var assembly = Assembly.GetEntryAssembly(); _interactionModules = await _interactions.AddModulesAsync(assembly, _services); + + // add modules from ComponentLib + _interactionModules = _interactionModules.Concat(await _interactions.AddModulesAsync(typeof(ComponentLib.ComponentLib).Assembly, new ComponentLib.ComponentLib()._services)); } private async Task RegisterCommands(DiscordSocketClient client) { if (client.ShardId != 0) return; + + _ = new ComponentLib.ComponentLib().Initialize(_client); await _interactions.AddModulesGloballyAsync(true, _interactionModules.ToArray()); + + foreach (var module in _interactionModules) + { + Console.WriteLine($"Registered {module.Name}"); + } } private static Task LogAsync(LogMessage log) diff --git a/ToggleButton.cs b/ToggleButton.cs new file mode 100644 index 0000000..f18a366 --- /dev/null +++ b/ToggleButton.cs @@ -0,0 +1,17 @@ +using Discord; + +namespace ComponentLib +{ + public class ToggleButton + { + public static ButtonComponent ToggleButton(string Label, string CustomId, bool IsDisabled = false, ButtonStyle Style = ButtonStyle.Success) + { + return new ButtonBuilder() + .WithLabel(Label) + .WithCustomId("togglebutton:" + CustomId) + .WithStyle(Style) + .WithDisabled(IsDisabled) + .Build(); + } + } +} \ No newline at end of file