Repository: pajlads/DinkPlugin (java)

Issue:
Mention boss name in Boss Slayer notifications
Currently Slayer notifications for bosses don't mention boss name since it's not included in the task complete message.
![image](https://user-images.githubusercontent.com/86596679/206470379-55bfb79f-7977-4cd5-848e-86e0b6a05ea6.png)
Looking at the game chat log, game sends a message regarding the name of the Boss and maybe location if on Konar task(unconfirmed), right before the task completion text.
![image](https://user-images.githubusercontent.com/86596679/206470970-14b3bb5e-969b-4d63-9be7-d726e21ccb72.png)
Maybe support could be added for that.

Pull request:
fix: ensure boss name is in slayer task message
Fixes #87


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d602226..0ece719 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,7 @@
 ## Unreleased
 
 - Minor: Retry failed webhook messages with exponential backoff. (#94)
+- Bugfix: Ensure boss name is included in boss slayer messages. (#88)
 - Dev: Bump mockito version to 4.10.0. (#95)
 
 ## 1.1.2
diff --git a/src/main/java/dinkplugin/notifiers/SlayerNotifier.java b/src/main/java/dinkplugin/notifiers/SlayerNotifier.java
index b096437..df84811 100644
--- a/src/main/java/dinkplugin/notifiers/SlayerNotifier.java
+++ b/src/main/java/dinkplugin/notifiers/SlayerNotifier.java
@@ -14,6 +14,7 @@ import java.util.regex.Pattern;
 
 @Singleton
 public class SlayerNotifier extends BaseNotifier {
+    private static final Pattern BOSS_REGEX = Pattern.compile("You are granted .+ Slayer XP for completing your boss task against(?: the)? (?<name>.+)\\.$");
     @VisibleForTesting
     static final Pattern SLAYER_TASK_REGEX = Pattern.compile("You have completed your task! You killed (?<task>[\\d,]+ [^.]+)\\..*");
     private static final Pattern SLAYER_COMPLETE_REGEX = Pattern.compile("You've completed (?:at least )?(?<taskCount>[\\d,]+) (?:Wilderness )?tasks?(?: and received (?<points>\\d+) points, giving you a total of [\\d,]+|\\.You'll be eligible to earn reward points if you complete tasks from a more advanced Slayer Master\\.| and reached the maximum amount of Slayer points \\((?<points2>[\\d,]+)\\))?");
@@ -32,14 +33,28 @@ public class SlayerNotifier extends BaseNotifier {
     }
 
     public void onChatMessage(String chatMessage) {
-        if (isEnabled()
-            && (chatMessage.contains("Slayer master")
-            || chatMessage.contains("Slayer Master")
-            || chatMessage.contains("completed your task!")
-        )) {
+        if (isEnabled()) {
+            if (slayerTask.isEmpty()) {
+                Matcher bossMatcher = BOSS_REGEX.matcher(chatMessage);
+                if (bossMatcher.find()) {
+                    String name = bossMatcher.group("name");
+                    this.slayerTask = name.endsWith(" boss") ? name.substring(0, name.length() - " boss".length()) : name;
+                    return;
+                }
+            }
+
             Matcher taskMatcher = SLAYER_TASK_REGEX.matcher(chatMessage);
             if (taskMatcher.find()) {
-                this.slayerTask = taskMatcher.group("task");
+                String task = taskMatcher.group("task");
+                if (slayerTask.isEmpty()) {
+                    this.slayerTask = task;
+                } else {
+                    this.slayerTask = String.format("%s %s", task.substring(0, task.indexOf(' ')), slayerTask);
+                }
+                return;
+            }
+
+            if (slayerTask.isEmpty()) {
                 return;
             }
 


Test patch:
diff --git a/src/test/java/dinkplugin/notifiers/SlayerNotifierTest.java b/src/test/java/dinkplugin/notifiers/SlayerNotifierTest.java
index ff42d12..235dbff 100644
--- a/src/test/java/dinkplugin/notifiers/SlayerNotifierTest.java
+++ b/src/test/java/dinkplugin/notifiers/SlayerNotifierTest.java
@@ -48,6 +48,63 @@ class SlayerNotifierTest extends MockedNotifierTest {
         );
     }
 
+    @Test
+    void testNotifyBoss() {
+        // fire chat messages
+        notifier.onChatMessage("You are granted an extra reward of 5k Slayer XP for completing your boss task against the Cave Kraken boss.");
+        notifier.onChatMessage("You have completed your task! You killed 50 Bosses. You gained 14,200 xp.");
+        notifier.onChatMessage("You've completed 150 tasks and received 10 points, giving you a total of 200; return to a Slayer master.");
+
+        // check notification message
+        verify(messageHandler).createMessage(
+            PRIMARY_WEBHOOK_URL,
+            false,
+            NotificationBody.builder()
+                .content(String.format("%s has completed: %s, getting %d points for a total %d tasks completed", PLAYER_NAME, "50 Cave Kraken", 10, 150))
+                .extra(new SlayerNotificationData("50 Cave Kraken", "150", "10"))
+                .type(NotificationType.SLAYER)
+                .build()
+        );
+    }
+
+    @Test
+    void testNotifyBarrows() {
+        // fire chat messages
+        notifier.onChatMessage("You are granted an extra reward of 5k Slayer XP for completing your boss task against Barrows brothers.");
+        notifier.onChatMessage("You have completed your task! You killed 36 Bosses. You gained 8,943 xp.");
+        notifier.onChatMessage("You've completed 881 tasks and received 20 points, giving you a total of 689; return to a Slayer master.");
+
+        // check notification message
+        verify(messageHandler).createMessage(
+            PRIMARY_WEBHOOK_URL,
+            false,
+            NotificationBody.builder()
+                .content(String.format("%s has completed: %s, getting %d points for a total %d tasks completed", PLAYER_NAME, "36 Barrows brothers", 20, 881))
+                .extra(new SlayerNotificationData("36 Barrows brothers", "881", "20"))
+                .type(NotificationType.SLAYER)
+                .build()
+        );
+    }
+
+    @Test
+    void testNotifyChaos() {
+        // fire chat messages
+        notifier.onChatMessage("You are granted an extra reward of 5k Slayer XP for completing your boss task against the Chaos Elemental.");
+        notifier.onChatMessage("You have completed your task! You killed 11 Bosses. You gained 7,954 xp.");
+        notifier.onChatMessage("You've completed 242 tasks and received 15 points, giving you a total of 3,254; return to a Slayer master.");
+
+        // check notification message
+        verify(messageHandler).createMessage(
+            PRIMARY_WEBHOOK_URL,
+            false,
+            NotificationBody.builder()
+                .content(String.format("%s has completed: %s, getting %d points for a total %d tasks completed", PLAYER_NAME, "11 Chaos Elemental", 15, 242))
+                .extra(new SlayerNotificationData("11 Chaos Elemental", "242", "15"))
+                .type(NotificationType.SLAYER)
+                .build()
+        );
+    }
+
     @Test
     void testIgnorePoints() {
         // fire chat messages