Skip to content

Commit

Permalink
Rework thread resolve/unresolve logic.
Browse files Browse the repository at this point in the history
for #4 to be possible.
  • Loading branch information
phorcys420 committed Feb 23, 2023
1 parent 9005d4c commit 8d7a429
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 41 deletions.
95 changes: 55 additions & 40 deletions lib/discord/interactions/commands/resolve.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,85 @@ import "package:codercord/values.dart";
import "package:nyxx/nyxx.dart";
import "package:nyxx_interactions/nyxx_interactions.dart";

Future<void> handleResolve(ISlashCommandInteractionEvent p0, bool resolve,
final resolvedWords = {true: "resolved", false: "unresolved"};

Future<void> 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<void> 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.",
),
hidden: true,
);
}
} else {
p0.respond(
await p0.respond(
MessageBuilder.content(
"Please run this command in a <#${helpChannel.id}> post.",
),
hidden: true,
);
}
} else {
p0.respond(
await p0.respond(
MessageBuilder.content(
"You can only run this command in a <#${helpChannel.id}> post.",
),
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lib/discord/interactions/commands/unresolve.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ SlashCommandBuilder getCommand() {
guild: coderServer.id,
canBeUsedInDm: false,
)..registerHandler((p0) async {
await handleResolve(p0, false);
await handleResolveCommand(p0, false);
});
}

0 comments on commit 8d7a429

Please sign in to comment.