diff --git a/src/main/java/link/locutus/discord/commands/manager/v2/impl/pw/CommandManager2.java b/src/main/java/link/locutus/discord/commands/manager/v2/impl/pw/CommandManager2.java index d6367980..f2d2f933 100644 --- a/src/main/java/link/locutus/discord/commands/manager/v2/impl/pw/CommandManager2.java +++ b/src/main/java/link/locutus/discord/commands/manager/v2/impl/pw/CommandManager2.java @@ -282,6 +282,7 @@ public CommandManager2 registerDefaults() { this.commands.registerMethod(new PlayerSettingCommands(), List.of("announcement"), "viewAnnouncement", "view"); this.commands.registerMethod(new TestCommands(), List.of("announcement"), "ocr", "ocr"); + this.commands.registerMethod(new TestCommands(), List.of("announcement"), "find_announcement", "find"); StringBuilder output = new StringBuilder(); this.commands.generatePojo("", output, 0); diff --git a/src/main/java/link/locutus/discord/commands/manager/v2/impl/pw/commands/AdminCommands.java b/src/main/java/link/locutus/discord/commands/manager/v2/impl/pw/commands/AdminCommands.java index 332849c9..b3ea4d82 100644 --- a/src/main/java/link/locutus/discord/commands/manager/v2/impl/pw/commands/AdminCommands.java +++ b/src/main/java/link/locutus/discord/commands/manager/v2/impl/pw/commands/AdminCommands.java @@ -315,6 +315,49 @@ public String archiveAnnouncement(@Me GuildDB db, int announcementId, @Default B return (archive ? "Archived" : "Unarchived") + " announcement with id: #" + announcementId; } + @Command(desc = "Find the announcement closest matching a message") + @RolePermission(Roles.ADMIN) + @NoFormat + public String find_announcement(@Me GuildDB db, int announcementId, String message) throws IOException { +// Announcement announcement = db.getAnnouncement(announcementId); + List announcements = db.getPlayerAnnouncementsByAnnId(announcementId); + if (announcements.isEmpty()) { + return "No announcements found with id: #" + announcementId; + } + long diffMin = Long.MAX_VALUE; + List matches = new ArrayList<>(); + for (Announcement.PlayerAnnouncement announcement : announcements) { + String content = announcement.getContent(); + if (message.equalsIgnoreCase(content)) { + return "Announcement sent to nation id: " + announcement.receiverNation; + } + byte[] diff = StringMan.getDiffBytes(message, content); + if (diff.length < diffMin) { + diffMin = diff.length; + matches.clear(); + matches.add(announcement); + } else if (diff.length == diffMin) { + matches.add(announcement); + } + } + + if (matches.isEmpty()) { + return "No announcements found with id: #" + announcementId; + } else if (matches.size() == 1) { + Announcement.PlayerAnnouncement match = matches.get(0); + return "Closest match: " + match.receiverNation + " with " + diffMin + " differences:\n```\n" + match.getContent() + "\n```"; + } else { + StringBuilder response = new StringBuilder(); + response.append(matches.size() + " matches with " + diffMin + " differences:\n"); + for (Announcement.PlayerAnnouncement match : matches) { + response.append("- " + match.receiverNation + "\n"); + // content in ``` + response.append("```\n" + match.getContent() + "\n```\n"); + } + return response.toString(); + } + } + @Command(desc = "Send an announcement to multiple nations, with random variations for each receiver\n") @RolePermission(Roles.ADMIN) @HasApi