22
33import net .dv8tion .jda .api .entities .emoji .CustomEmoji ;
44import net .dv8tion .jda .api .entities .emoji .Emoji ;
5- import net .dv8tion .jda .api .entities .emoji .EmojiUnion ;
65import net .dv8tion .jda .api .events .message .MessageReceivedEvent ;
76import net .dv8tion .jda .api .events .message .react .MessageReactionAddEvent ;
87
98import org .togetherjava .tjbot .features .MessageReceiverAdapter ;
109
11- import java .util .regex . Pattern ;
10+ import java .util .Map ;
1211
1312/**
1413 * Listener that tracks emoji usage across all channels for analytics purposes.
1514 * <p>
16- * Counts emojis used in messages and reactions so admins can see which emojis are unused and should
17- * be removed.
15+ * Counts custom emojis used in messages and reactions so admins can see which emojis are unused and
16+ * should be removed.
1817 * <p>
19- * Custom emojis are tracked by their Discord ID (e.g. {@code emoji-custom-123456789}) rather than
20- * by name, since emoji names are not unique and may change over time. Animated custom emojis are
21- * tracked separately (e.g. {@code emoji-custom-animated-123456789}). Unicode emojis are tracked by
22- * name (e.g. {@code emoji-unicode-thumbsup}).
18+ * Custom emojis are tracked by their Discord ID via the {@code id} dimension, and whether they are
19+ * animated via the {@code animated} dimension.
2320 */
2421public final class EmojiTrackerListener extends MessageReceiverAdapter {
25- private static final Pattern ALL_CHANNELS = Pattern .compile (".*" );
2622
2723 private final Metrics metrics ;
2824
@@ -32,14 +28,14 @@ public final class EmojiTrackerListener extends MessageReceiverAdapter {
3228 * @param metrics to track emoji usage events
3329 */
3430 public EmojiTrackerListener (Metrics metrics ) {
35- super (ALL_CHANNELS );
31+ super ();
3632
3733 this .metrics = metrics ;
3834 }
3935
4036 @ Override
4137 public void onMessageReceived (MessageReceivedEvent event ) {
42- if (event .getAuthor (). isBot () || event . isWebhookMessage ()) {
38+ if (event .isWebhookMessage ()) {
4339 return ;
4440 }
4541
@@ -48,23 +44,14 @@ public void onMessageReceived(MessageReceivedEvent event) {
4844
4945 @ Override
5046 public void onMessageReactionAdd (MessageReactionAddEvent event ) {
51- if (event .getUser () != null && event . getUser (). isBot () ) {
47+ if (event .getEmoji (). getType () != Emoji . Type . CUSTOM ) {
5248 return ;
5349 }
5450
55- trackEmojiUnion (event .getEmoji ());
56- }
57-
58- private void trackEmojiUnion (EmojiUnion emoji ) {
59- if (emoji .getType () == Emoji .Type .CUSTOM ) {
60- trackCustomEmoji (emoji .asCustom ());
61- } else {
62- metrics .count ("emoji-unicode-" + emoji .asUnicode ().getName ());
63- }
51+ trackCustomEmoji (event .getEmoji ().asCustom ());
6452 }
6553
6654 private void trackCustomEmoji (CustomEmoji emoji ) {
67- String prefix = emoji .isAnimated () ? "emoji-custom-animated-" : "emoji-custom-" ;
68- metrics .count (prefix + emoji .getIdLong ());
55+ metrics .count ("emoji" , Map .of ("id" , emoji .getIdLong (), "animated" , emoji .isAnimated ()));
6956 }
7057}
0 commit comments