@@ -190,7 +190,7 @@ RestAction<Void> changeChannelActivity(ThreadChannel channel, ThreadActivity act
190
190
191
191
private static RestAction <Void > changeMatchingTagOfChannel (String tagName ,
192
192
Set <String > tagNamesToMatch , ThreadChannel channel ) {
193
- Collection <ForumTag > tags = new ArrayList <>(channel .getAppliedTags ());
193
+ List <ForumTag > tags = new ArrayList <>(channel .getAppliedTags ());
194
194
195
195
Optional <ForumTag > currentTag = getFirstMatchingTagOfChannel (tagNamesToMatch , channel );
196
196
if (currentTag .isPresent ()) {
@@ -203,10 +203,24 @@ private static RestAction<Void> changeMatchingTagOfChannel(String tagName,
203
203
}
204
204
205
205
ForumTag nextTag = requireTag (tagName , channel .getParentChannel ().asForumChannel ());
206
- tags .add (nextTag );
206
+ // In case the tag was already there, but not in front, we first remove it
207
+ tags .remove (nextTag );
208
+
209
+ if (tags .size () >= ForumChannel .MAX_POST_TAGS ) {
210
+ // If still at max size, remove last to make place for the new tag.
211
+ // The last tag is the least important.
212
+ // NOTE In practice, this can happen if the user selected 5 categories and
213
+ // the bot then tries to add the activity tag
214
+ tags .remove (tags .size () - 1 );
215
+ }
216
+
217
+ Collection <ForumTag > nextTags = new ArrayList <>(tags .size ());
218
+ // Tag should be in front, to take priority over others
219
+ nextTags .add (nextTag );
220
+ nextTags .addAll (tags );
207
221
208
222
List <ForumTagSnowflake > tagSnowflakes =
209
- tags .stream ().map (ForumTag ::getIdLong ).map (ForumTagSnowflake ::fromId ).toList ();
223
+ nextTags .stream ().map (ForumTag ::getIdLong ).map (ForumTagSnowflake ::fromId ).toList ();
210
224
return channel .getManager ().setAppliedTags (tagSnowflakes );
211
225
}
212
226
0 commit comments