From f93c172bff1263b05de509b451d285b8b1dddc8e Mon Sep 17 00:00:00 2001 From: Karl Essinger Date: Sun, 22 Jan 2023 19:22:51 +0100 Subject: [PATCH] Added standalone sync button --- Commands/AdminUnlinkCommand.cs | 1 - Commands/CreateSyncButtonCommand.cs | 19 +++++++ Commands/LinkCommand.cs | 77 +++++++++++++++++++---------- EventHandler.cs | 12 ++++- SponsorBoi.cs | 1 + 5 files changed, 82 insertions(+), 28 deletions(-) create mode 100644 Commands/CreateSyncButtonCommand.cs diff --git a/Commands/AdminUnlinkCommand.cs b/Commands/AdminUnlinkCommand.cs index 38ac7f9..5d1eeda 100644 --- a/Commands/AdminUnlinkCommand.cs +++ b/Commands/AdminUnlinkCommand.cs @@ -1,5 +1,4 @@ using DSharpPlus.Entities; -using System; using System.Threading.Tasks; using DSharpPlus.SlashCommands; using DSharpPlus.SlashCommands.Attributes; diff --git a/Commands/CreateSyncButtonCommand.cs b/Commands/CreateSyncButtonCommand.cs new file mode 100644 index 0000000..71b36a8 --- /dev/null +++ b/Commands/CreateSyncButtonCommand.cs @@ -0,0 +1,19 @@ +using System.Threading.Tasks; +using DSharpPlus; +using DSharpPlus.Entities; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace SponsorBoi.Commands; + +public class CreateSyncButtonCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("createsyncbutton", "Creates a button for users to sync their Github account")] + private async Task ExecuteCommand(InteractionContext command) + { + DiscordInteractionResponseBuilder builder = new DiscordInteractionResponseBuilder().WithContent(" "); + builder.AddComponents(new DiscordButtonComponent(ButtonStyle.Primary, "sponsorboi_standalonelinkbutton", "Sync Github account")); + await command.CreateResponseAsync(builder); + } +} \ No newline at end of file diff --git a/Commands/LinkCommand.cs b/Commands/LinkCommand.cs index 0c521bb..b5e3442 100644 --- a/Commands/LinkCommand.cs +++ b/Commands/LinkCommand.cs @@ -13,21 +13,34 @@ public class LinkCommand : ApplicationCommandModule [SlashCommand("link", "Link your Github account for Sponsor syncing")] internal static async Task ExecuteCommand(InteractionContext command) { - if (Database.TryGetSponsor(command.Member.Id, out Database.SponsorEntry _)) + (DiscordEmbed embed, List buttons) = await PromptOrSyncUser(command.Guild, command.User); + DiscordInteractionResponseBuilder builder = new DiscordInteractionResponseBuilder().AddEmbed(embed); + + if (buttons.Count != 0) { - await command.CreateResponseAsync(new DiscordEmbedBuilder + builder.AddComponents(buttons); + } + + await command.CreateResponseAsync(builder.AsEphemeral()); + } + + internal static async Task<(DiscordEmbed, List)> PromptOrSyncUser(DiscordGuild guild, DiscordUser user) + { + if (Database.TryGetSponsor(user.Id, out Database.SponsorEntry _)) + { + return (new DiscordEmbedBuilder { Color = DiscordColor.Red, - Description = "The Discord account " + command.Member.Mention + " is already linked to a Github account." - }, true); - return; + Description = "The Discord account " + user.Mention + " is already linked to a Github account.\n\n" + + "Either use the /unlink command or ask an admin to help you if you want to switch accounts." + }, new List()); } Github.Account githubAccount = null; List issues = await Github.GetIssues(); foreach (Github.Issue issue in issues) { - if (issue.description.Contains(command.Member.Id.ToString())) + if (issue.description.Contains(user.Id.ToString())) { githubAccount = issue.author; break; @@ -36,50 +49,64 @@ await command.CreateResponseAsync(new DiscordEmbedBuilder if (githubAccount == null) { - DiscordInteractionResponseBuilder builder = new DiscordInteractionResponseBuilder().AddEmbed(new DiscordEmbedBuilder() + return (new DiscordEmbedBuilder { Color = DiscordColor.Cyan, - Description = "Click [here](" + Utils.GetIssueURL("Discord ID: " + command.Member.Id) + ")" - + " and click submit in order to verify your account.\n\n" - + "Click the button below when done." + Title = "Sync Github Sponsors tier", + Description = "You will verify the Github account is yours by posting your Discord ID in an issue.\n\n" + + "Simply follow these steps and the rest is done automatically:\n" + + "**1.** Press 'Submit Discord ID'\n" + + "**2.** Press 'Submit new issue' on Github\n" + + "**3.** Press 'Finish Link'\n" + + "**4.** You may now close or delete the Github issue\n" + }, new List + { + new DiscordLinkButtonComponent(Utils.GetIssueURL("Discord ID: " + user.Id), "Submit Discord ID"), + new DiscordButtonComponent(ButtonStyle.Primary, "sponsorboi_checkissuebutton", "Finish Link") }); - builder.AddComponents(new DiscordButtonComponent(ButtonStyle.Primary, "sponsorboi_linkcommandbutton", "Sync Github account")); - await command.CreateResponseAsync(builder.AsEphemeral()); - return; } if (Database.TryGetSponsor(githubAccount.id, out Database.SponsorEntry existingSponsor)) { - await command.CreateResponseAsync(new DiscordEmbedBuilder + return (new DiscordEmbedBuilder { Color = DiscordColor.Red, Description = "The Github account '" + githubAccount.name + "' is already linked to <@" + existingSponsor.discordID + ">" - }, true); - return; + }, new List()); } - if (!Database.TryAddSponsor(new Database.SponsorEntry { discordID = command.Member.Id, githubID = githubAccount.id })) + if (!Database.TryAddSponsor(new Database.SponsorEntry { discordID = user.Id, githubID = githubAccount.id })) { - await command.CreateResponseAsync(new DiscordEmbedBuilder + return (new DiscordEmbedBuilder { Color = DiscordColor.Red, Description = "Error occured when writing sponsor to database." - }, true); - return; + }, new List()); } - await command.CreateResponseAsync(new DiscordEmbedBuilder + Utils.SyncUserRoles(guild, user); + + return (new DiscordEmbedBuilder { Color = DiscordColor.Green, - Description = "The Github account '" + githubAccount.name + "' is now linked to " + command.Member.Mention + "\n\n" + Description = "The Github account '" + githubAccount.name + "' is now linked to " + user.Mention + "\n\n" + "You may now close or delete the Github issue." - }, true); - - Utils.SyncUserRoles(command.Guild, command.User); + }, new List()); } internal static async Task OnButtonPressed(DiscordInteraction interaction) { + if (Database.TryGetSponsor(interaction.User.Id, out Database.SponsorEntry _)) + { + await interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,new DiscordInteractionResponseBuilder().AddEmbed(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "The Discord account " + interaction.User.Mention + " is already linked to a Github account.\n\n" + + "Either use the /unlink command or ask an admin to help you if you want to switch accounts." + }).AsEphemeral()); + return; + } + Github.Account githubAccount = null; List issues = await Github.GetIssues(); foreach (Github.Issue issue in issues) diff --git a/EventHandler.cs b/EventHandler.cs index af026f5..617d2d2 100644 --- a/EventHandler.cs +++ b/EventHandler.cs @@ -114,11 +114,19 @@ internal static async Task OnComponentInteractionCreated(DiscordClient client, C case ComponentType.Button: switch (e.Id) { - case "sponsorboi_linkcommandbutton": + case "sponsorboi_checkissuebutton": await LinkCommand.OnButtonPressed(e.Interaction); return; case "sponsorboi_standalonelinkbutton": - //await LinkCommand.OnButtonPressed(e.Interaction); + (DiscordEmbed embed, List buttons) = await LinkCommand.PromptOrSyncUser(e.Guild, e.User); + DiscordInteractionResponseBuilder builder = new DiscordInteractionResponseBuilder().AddEmbed(embed); + + if (buttons.Count != 0) + { + builder.AddComponents(buttons); + } + + await e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder.AsEphemeral()); return; case "right": return; diff --git a/SponsorBoi.cs b/SponsorBoi.cs index fc88eb7..7fcd5e0 100644 --- a/SponsorBoi.cs +++ b/SponsorBoi.cs @@ -118,6 +118,7 @@ public static async void Initialize() commands.RegisterCommands(); commands.RegisterCommands(); commands.RegisterCommands(); + commands.RegisterCommands(); Logger.Log("Hooking events..."); discordClient.Ready += EventHandler.OnReady;