Skip to content

Commit

Permalink
Added standalone sync button
Browse files Browse the repository at this point in the history
  • Loading branch information
KarlOfDuty committed Jan 22, 2023
1 parent be67db9 commit f93c172
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 28 deletions.
1 change: 0 additions & 1 deletion Commands/AdminUnlinkCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using DSharpPlus.Entities;
using System;
using System.Threading.Tasks;
using DSharpPlus.SlashCommands;
using DSharpPlus.SlashCommands.Attributes;
Expand Down
19 changes: 19 additions & 0 deletions Commands/CreateSyncButtonCommand.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
77 changes: 52 additions & 25 deletions Commands/LinkCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DiscordComponent> 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<DiscordComponent>)> 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<DiscordComponent>());
}

Github.Account githubAccount = null;
List<Github.Issue> 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;
Expand All @@ -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<DiscordComponent>
{
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<DiscordComponent>());
}

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<DiscordComponent>());
}

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<DiscordComponent>());
}

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<Github.Issue> issues = await Github.GetIssues();
foreach (Github.Issue issue in issues)
Expand Down
12 changes: 10 additions & 2 deletions EventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DiscordComponent> 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;
Expand Down
1 change: 1 addition & 0 deletions SponsorBoi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public static async void Initialize()
commands.RegisterCommands<LinkCommand>();
commands.RegisterCommands<RecheckCommand>();
commands.RegisterCommands<UnlinkCommand>();
commands.RegisterCommands<CreateSyncButtonCommand>();

Logger.Log("Hooking events...");
discordClient.Ready += EventHandler.OnReady;
Expand Down

0 comments on commit f93c172

Please sign in to comment.