Skip to content

Commit 8d7a429

Browse files
committed
Rework thread resolve/unresolve logic.
for #4 to be possible.
1 parent 9005d4c commit 8d7a429

File tree

2 files changed

+56
-41
lines changed

2 files changed

+56
-41
lines changed

lib/discord/interactions/commands/resolve.dart

+55-40
Original file line numberDiff line numberDiff line change
@@ -4,70 +4,85 @@ import "package:codercord/values.dart";
44
import "package:nyxx/nyxx.dart";
55
import "package:nyxx_interactions/nyxx_interactions.dart";
66

7-
Future<void> handleResolve(ISlashCommandInteractionEvent p0, bool resolve,
7+
final resolvedWords = {true: "resolved", false: "unresolved"};
8+
9+
Future<void> handleResolve(IThreadChannel threadChannel, IUser resolver,
10+
Function respond, bool resolve,
811
[bool lock = false]) async {
9-
final interactionChannel = await p0.interaction.channel.download();
12+
final resolvedWord = resolvedWords[resolve];
1013

11-
if (interactionChannel.channelType == ChannelType.guildPublicThread) {
12-
final resolvedWord = resolve == true ? "resolved" : "unresolved";
14+
final tagToAdd = resolve == true ? resolvedTagID : unresolvedTagID;
15+
final tagToRemove = resolve == true ? unresolvedTagID : resolvedTagID;
1316

14-
final threadChannel = interactionChannel as IThreadChannel;
17+
final postTags = threadChannel.appliedTags;
1518

16-
if (await threadChannel.isHelpPost) {
17-
if (canUserInteractWithThread(threadChannel.owner, p0.interaction)) {
18-
final tagToAdd = resolve == true ? resolvedTagID : unresolvedTagID;
19-
final tagToRemove = resolve == true ? unresolvedTagID : resolvedTagID;
19+
try {
20+
if (!postTags.contains(tagToAdd)) {
21+
postTags.add(tagToAdd);
22+
}
2023

21-
final postTags = threadChannel.appliedTags;
24+
if (postTags.contains(tagToRemove)) {
25+
postTags.remove(tagToRemove);
26+
}
2227

23-
try {
24-
if (!postTags.contains(tagToAdd)) {
25-
postTags.add(tagToAdd);
26-
}
28+
await threadChannel.setPostTags(postTags);
2729

28-
if (postTags.contains(tagToRemove)) {
29-
postTags.remove(tagToRemove);
30-
}
30+
await respond(
31+
MessageBuilder.content(
32+
"${resolver.mention} marked the thread as $resolvedWord.",
33+
)..flags = (MessageFlagBuilder()..suppressNotifications = true),
34+
);
3135

32-
await threadChannel.setPostTags(postTags);
36+
if (resolve == true && threadChannel.archived == false) {
37+
try {
38+
await threadChannel.archive(true, lock);
39+
} catch (_) {}
40+
}
41+
} catch (e) {
42+
await respond(
43+
MessageBuilder.content(
44+
"Could not mark the thread as $resolvedWord because of an unexpected error.",
45+
),
46+
hidden: true,
47+
);
48+
}
49+
}
50+
51+
Future<void> handleResolveCommand(
52+
ISlashCommandInteractionEvent p0, bool resolve,
53+
[bool lock = false]) async {
54+
final interactionChannel = await p0.interaction.channel.download();
3355

34-
await p0.respond(
35-
MessageBuilder.content(
36-
"${p0.interaction.userAuthor!.mention} marked the thread as $resolvedWord.",
37-
)..flags = (MessageFlagBuilder()..suppressNotifications = true),
38-
);
56+
if (interactionChannel.channelType == ChannelType.guildPublicThread) {
57+
final threadChannel = interactionChannel as IThreadChannel;
58+
final resolvedWord = resolvedWords[resolve];
3959

40-
if (resolve == true && threadChannel.archived == false) {
41-
try {
42-
await threadChannel.archive(true, lock);
43-
} catch (_) {}
44-
}
45-
} catch (e) {
46-
await p0.respond(
47-
MessageBuilder.content(
48-
"Could not mark the thread as $resolvedWord because of an unexpected error.",
49-
),
50-
hidden: true,
51-
);
52-
}
60+
if (await threadChannel.isHelpPost) {
61+
if (canUserInteractWithThread(threadChannel.owner, p0.interaction)) {
62+
return handleResolve(
63+
threadChannel,
64+
p0.interaction.userAuthor!,
65+
p0.respond,
66+
resolve,
67+
);
5368
} else {
54-
p0.respond(
69+
await p0.respond(
5570
MessageBuilder.content(
5671
"You cannot mark this thread as $resolvedWord since you are not the OP.",
5772
),
5873
hidden: true,
5974
);
6075
}
6176
} else {
62-
p0.respond(
77+
await p0.respond(
6378
MessageBuilder.content(
6479
"Please run this command in a <#${helpChannel.id}> post.",
6580
),
6681
hidden: true,
6782
);
6883
}
6984
} else {
70-
p0.respond(
85+
await p0.respond(
7186
MessageBuilder.content(
7287
"You can only run this command in a <#${helpChannel.id}> post.",
7388
),
@@ -94,7 +109,7 @@ SlashCommandBuilder getCommand() {
94109
guild: coderServer.id,
95110
canBeUsedInDm: false,
96111
)..registerHandler((p0) async {
97-
await handleResolve(
112+
await handleResolveCommand(
98113
p0,
99114
true,
100115
p0.args.isNotEmpty ? p0.args[0].value : false,

lib/discord/interactions/commands/unresolve.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ SlashCommandBuilder getCommand() {
1111
guild: coderServer.id,
1212
canBeUsedInDm: false,
1313
)..registerHandler((p0) async {
14-
await handleResolve(p0, false);
14+
await handleResolveCommand(p0, false);
1515
});
1616
}

0 commit comments

Comments
 (0)