@@ -190,7 +190,7 @@ RestAction<Void> changeChannelActivity(ThreadChannel channel, ThreadActivity act
190190
191191 private static RestAction <Void > changeMatchingTagOfChannel (String tagName ,
192192 Set <String > tagNamesToMatch , ThreadChannel channel ) {
193- Collection <ForumTag > tags = new ArrayList <>(channel .getAppliedTags ());
193+ List <ForumTag > tags = new ArrayList <>(channel .getAppliedTags ());
194194
195195 Optional <ForumTag > currentTag = getFirstMatchingTagOfChannel (tagNamesToMatch , channel );
196196 if (currentTag .isPresent ()) {
@@ -203,10 +203,24 @@ private static RestAction<Void> changeMatchingTagOfChannel(String tagName,
203203 }
204204
205205 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 );
207221
208222 List <ForumTagSnowflake > tagSnowflakes =
209- tags .stream ().map (ForumTag ::getIdLong ).map (ForumTagSnowflake ::fromId ).toList ();
223+ nextTags .stream ().map (ForumTag ::getIdLong ).map (ForumTagSnowflake ::fromId ).toList ();
210224 return channel .getManager ().setAppliedTags (tagSnowflakes );
211225 }
212226
0 commit comments