From 8d7a4297d1e26386ee4f88360bc0d960b44e09a8 Mon Sep 17 00:00:00 2001 From: phorcys420 <57866459+phorcys420@users.noreply.github.com> Date: Thu, 23 Feb 2023 19:29:56 +0100 Subject: [PATCH] Rework thread resolve/unresolve logic. for #4 to be possible. --- .../interactions/commands/resolve.dart | 95 +++++++++++-------- .../interactions/commands/unresolve.dart | 2 +- 2 files changed, 56 insertions(+), 41 deletions(-) diff --git a/lib/discord/interactions/commands/resolve.dart b/lib/discord/interactions/commands/resolve.dart index 6223998..e9b3f4d 100644 --- a/lib/discord/interactions/commands/resolve.dart +++ b/lib/discord/interactions/commands/resolve.dart @@ -4,54 +4,69 @@ import "package:codercord/values.dart"; import "package:nyxx/nyxx.dart"; import "package:nyxx_interactions/nyxx_interactions.dart"; -Future handleResolve(ISlashCommandInteractionEvent p0, bool resolve, +final resolvedWords = {true: "resolved", false: "unresolved"}; + +Future handleResolve(IThreadChannel threadChannel, IUser resolver, + Function respond, bool resolve, [bool lock = false]) async { - final interactionChannel = await p0.interaction.channel.download(); + final resolvedWord = resolvedWords[resolve]; - if (interactionChannel.channelType == ChannelType.guildPublicThread) { - final resolvedWord = resolve == true ? "resolved" : "unresolved"; + final tagToAdd = resolve == true ? resolvedTagID : unresolvedTagID; + final tagToRemove = resolve == true ? unresolvedTagID : resolvedTagID; - final threadChannel = interactionChannel as IThreadChannel; + final postTags = threadChannel.appliedTags; - if (await threadChannel.isHelpPost) { - if (canUserInteractWithThread(threadChannel.owner, p0.interaction)) { - final tagToAdd = resolve == true ? resolvedTagID : unresolvedTagID; - final tagToRemove = resolve == true ? unresolvedTagID : resolvedTagID; + try { + if (!postTags.contains(tagToAdd)) { + postTags.add(tagToAdd); + } - final postTags = threadChannel.appliedTags; + if (postTags.contains(tagToRemove)) { + postTags.remove(tagToRemove); + } - try { - if (!postTags.contains(tagToAdd)) { - postTags.add(tagToAdd); - } + await threadChannel.setPostTags(postTags); - if (postTags.contains(tagToRemove)) { - postTags.remove(tagToRemove); - } + await respond( + MessageBuilder.content( + "${resolver.mention} marked the thread as $resolvedWord.", + )..flags = (MessageFlagBuilder()..suppressNotifications = true), + ); - await threadChannel.setPostTags(postTags); + if (resolve == true && threadChannel.archived == false) { + try { + await threadChannel.archive(true, lock); + } catch (_) {} + } + } catch (e) { + await respond( + MessageBuilder.content( + "Could not mark the thread as $resolvedWord because of an unexpected error.", + ), + hidden: true, + ); + } +} + +Future handleResolveCommand( + ISlashCommandInteractionEvent p0, bool resolve, + [bool lock = false]) async { + final interactionChannel = await p0.interaction.channel.download(); - await p0.respond( - MessageBuilder.content( - "${p0.interaction.userAuthor!.mention} marked the thread as $resolvedWord.", - )..flags = (MessageFlagBuilder()..suppressNotifications = true), - ); + if (interactionChannel.channelType == ChannelType.guildPublicThread) { + final threadChannel = interactionChannel as IThreadChannel; + final resolvedWord = resolvedWords[resolve]; - if (resolve == true && threadChannel.archived == false) { - try { - await threadChannel.archive(true, lock); - } catch (_) {} - } - } catch (e) { - await p0.respond( - MessageBuilder.content( - "Could not mark the thread as $resolvedWord because of an unexpected error.", - ), - hidden: true, - ); - } + if (await threadChannel.isHelpPost) { + if (canUserInteractWithThread(threadChannel.owner, p0.interaction)) { + return handleResolve( + threadChannel, + p0.interaction.userAuthor!, + p0.respond, + resolve, + ); } else { - p0.respond( + await p0.respond( MessageBuilder.content( "You cannot mark this thread as $resolvedWord since you are not the OP.", ), @@ -59,7 +74,7 @@ Future handleResolve(ISlashCommandInteractionEvent p0, bool resolve, ); } } else { - p0.respond( + await p0.respond( MessageBuilder.content( "Please run this command in a <#${helpChannel.id}> post.", ), @@ -67,7 +82,7 @@ Future handleResolve(ISlashCommandInteractionEvent p0, bool resolve, ); } } else { - p0.respond( + await p0.respond( MessageBuilder.content( "You can only run this command in a <#${helpChannel.id}> post.", ), @@ -94,7 +109,7 @@ SlashCommandBuilder getCommand() { guild: coderServer.id, canBeUsedInDm: false, )..registerHandler((p0) async { - await handleResolve( + await handleResolveCommand( p0, true, p0.args.isNotEmpty ? p0.args[0].value : false, diff --git a/lib/discord/interactions/commands/unresolve.dart b/lib/discord/interactions/commands/unresolve.dart index 9c839c5..ed5d363 100644 --- a/lib/discord/interactions/commands/unresolve.dart +++ b/lib/discord/interactions/commands/unresolve.dart @@ -11,6 +11,6 @@ SlashCommandBuilder getCommand() { guild: coderServer.id, canBeUsedInDm: false, )..registerHandler((p0) async { - await handleResolve(p0, false); + await handleResolveCommand(p0, false); }); }