@@ -54,8 +54,19 @@ public static void handle(CodeMCBot bot, InteractionHook hook, Guild guild, long
5454 requestChannel .retrieveMessageById (messageId ).queue (message -> {
5555 Request req = DatabaseAPI .getRequest (messageId );
5656 if (req == null ){
57- CommandUtil .EmbedReply .from (hook ).error ("Request not found in Database." ).send ();
58- return ;
57+ // try to parse from message
58+ if (message .getEmbeds ().isEmpty ()) {
59+ CommandUtil .EmbedReply .from (hook ).error ("Request not found in Database and Message has no embeds to parse from." ).send ();
60+ return ;
61+ }
62+
63+ MessageEmbed embed = message .getEmbeds ().get (0 );
64+ req = fromEmbed (message .getIdLong (), embed );
65+
66+ if (req == null ) {
67+ CommandUtil .EmbedReply .from (hook ).error ("Request not found in Database and data could not be parsed from embed." ).send ();
68+ return ;
69+ }
5970 }
6071
6172 hook .editOriginal (
@@ -283,4 +294,91 @@ static MessageCreateData getMessage(CodeMCBot bot, String userId, String userLin
283294 .setEmbeds (embed )
284295 .build ();
285296 }
297+
298+ private static final String GITHUB_PREFIX = "https://github.com/" ;
299+
300+ @ VisibleForTesting
301+ static Request fromEmbed (long messageId , MessageEmbed embed ) {
302+ if (embed == null ) return null ;
303+
304+ String userLink = null ;
305+ String repoLink = null ;
306+ Long userId = null ;
307+
308+ for (MessageEmbed .Field field : embed .getFields ()) {
309+ switch (field .getName ()) {
310+ case CommandUtil .USER_ORGANISATION -> userLink = field .getValue ();
311+ case CommandUtil .REPOSITORY -> repoLink = field .getValue ();
312+ case CommandUtil .SUBMITTED_BY -> {
313+ String submitter = field .getValue ();
314+ String [] parts = submitter .split ("\\ s+" );
315+ if (parts .length == 2 ) {
316+ String mention = parts [1 ];
317+ String idStr ;
318+ if (mention .startsWith ("(<@" ) && mention .endsWith (">)" )) {
319+ idStr = mention .substring (3 , mention .length () - 2 );
320+ } else if (mention .startsWith ("<@" ) && mention .endsWith (">" )) {
321+ idStr = mention .substring (2 , mention .length () - 1 );
322+ } else {
323+ LOGGER .warn ("Unexpected mention format in submitter field: {}" , submitter );
324+ break ;
325+ }
326+
327+ try {
328+ userId = Long .parseLong (idStr );
329+ } catch (NumberFormatException e ) {
330+ LOGGER .warn ("Failed to parse user ID from mention: {}" , mention , e );
331+ }
332+ }
333+ }
334+ }
335+ }
336+
337+ if (userLink == null || repoLink == null || userId == null ) {
338+ LOGGER .warn ("Embed is missing required fields: userLink={}, repoLink={}, userId={}" , userLink , repoLink , userId );
339+ return null ;
340+ }
341+
342+ String githubName = null ;
343+ String repoName = null ;
344+ try {
345+ if (userLink .startsWith ("[" ) && userLink .contains ("](" ) && userLink .endsWith (")" )) {
346+ int start = userLink .indexOf ("](" ) + 2 ;
347+ int end = userLink .length () - 1 ;
348+ String url = userLink .substring (start , end );
349+ if (url .startsWith (GITHUB_PREFIX )) {
350+ githubName = url .substring (GITHUB_PREFIX .length ());
351+ } else {
352+ LOGGER .warn ("User link does not start with expected prefix: {}" , userLink );
353+ }
354+ } else {
355+ LOGGER .warn ("User link is not in expected markdown format: {}" , userLink );
356+ }
357+
358+ if (repoLink .startsWith ("[" ) && repoLink .contains ("](" ) && repoLink .endsWith (")" )) {
359+ int start = repoLink .indexOf ("](" ) + 2 ;
360+ int end = repoLink .length () - 1 ;
361+ String url = repoLink .substring (start , end );
362+ if (url .startsWith (GITHUB_PREFIX ) && url .contains ("/" )) {
363+ String path = url .substring (GITHUB_PREFIX .length ());
364+ int slashIndex = path .indexOf ("/" );
365+ githubName = path .substring (0 , slashIndex );
366+ repoName = path .substring (slashIndex + 1 );
367+ } else {
368+ LOGGER .warn ("Repository link does not start with expected prefix or is malformed: {}" , repoLink );
369+ }
370+ } else {
371+ LOGGER .warn ("Repository link is not in expected markdown format: {}" , repoLink );
372+ }
373+ } catch (Exception e ) {
374+ LOGGER .warn ("Failed to parse GitHub name or repository name from links: userLink={}, repoLink={}" , userLink , repoLink , e );
375+ }
376+
377+ if (githubName == null || repoName == null ) {
378+ LOGGER .warn ("Failed to extract githubName or repoName: githubName={}, repoName={}" , githubName , repoName );
379+ return null ;
380+ }
381+
382+ return new Request (messageId , userId , githubName , repoName );
383+ }
286384}
0 commit comments