1919import org .mangorage .mangobotapi .core .plugin .PluginManager ;
2020import org .mangorage .mangobotgithub .MangoBotGithub ;
2121import org .mangorage .mangobotgithub .core .integration .MangoBotSiteIntegration ;
22+ import org .mangorage .mangobotgithub .link .LinkExtractorList ;
23+
2224import java .io .IOException ;
2325import java .io .InputStream ;
2426import java .nio .charset .StandardCharsets ;
27+ import java .util .ArrayList ;
2528import java .util .HashMap ;
2629import java .util .List ;
2730import java .util .concurrent .ExecutionException ;
28- import java .util .concurrent .atomic .AtomicBoolean ;
2931import java .util .concurrent .atomic .AtomicInteger ;
32+ import java .util .regex .Matcher ;
33+ import java .util .regex .Pattern ;
3034
3135public final class PasteRequestModule {
36+ private static final Pattern urlPattern = Pattern .compile ("(https?://\\ S+)" );
37+
3238 public static final LogAnalyser analyser = LogAnalyserModule .MAIN ;
3339
3440 static final LazyReference <GitHubClient > GITHUB_CLIENT = LazyReference .create (() -> new GitHubClient ().setOAuth2Token (MangoBotGithub .GITHUB_TOKEN .get ()));
@@ -154,83 +160,72 @@ public static void createGists(Message msg, User requester) {
154160 });
155161 }
156162
163+ public static List <String > extractUrls (String text ) {
164+ List <String > urls = new ArrayList <>();
165+ Matcher matcher = urlPattern .matcher (text );
166+ while (matcher .find ()) {
167+ urls .add (matcher .group (1 ));
168+ }
169+ return urls ;
170+ }
171+
157172 public static void analyzeLog (Message message ) {
158173 var attachments = message .getAttachments ();
159174
160175 var builder = new StringBuilder ();
161176 analyser .scanMessage (message , builder );
162177
163- if (!attachments .isEmpty ()) {
164- TaskScheduler .getExecutor ().execute (() -> {
165- var suceeeded = new AtomicBoolean (false );
166- for (Message .Attachment attachment : attachments ) {
167- try {
168- byte [] bytes = getData (attachment .getProxy ().download ().get ());
169- if (bytes == null ) continue ;
170- String content = new String (bytes , StandardCharsets .UTF_8 );
171- if (containsPrintableCharacters (content )) {
172- suceeeded .set (true );
173- analyser .readLog (builder , content );
174- break ;
175- }
176- } catch (InterruptedException | ExecutionException e ) {
177- throw new RuntimeException (e );
178+ TaskScheduler .getExecutor ().execute (() -> {
179+ for (Message .Attachment attachment : attachments ) {
180+ try {
181+ byte [] bytes = getData (attachment .getProxy ().download ().get ());
182+ if (bytes == null ) continue ;
183+ String content = new String (bytes , StandardCharsets .UTF_8 );
184+ if (containsPrintableCharacters (content )) {
185+ analyser .readLog (builder , content );
186+ break ;
178187 }
188+ } catch (InterruptedException | ExecutionException e ) {
189+ throw new RuntimeException (e );
179190 }
191+ }
180192
181- if (!builder .isEmpty ()) {;
182- String id = null ;
183- if (PluginManager .isLoaded ("mangobotsite" )) {
184- try {
185- id = MangoBotSiteIntegration .handleLogResult (builder );
186- if (id != null ) {
187- message .reply ("[[Log Analyzer](https://mangobot.mangorage.org/file?id=%s)]" .formatted (id )).setSuppressEmbeds (true ).mentionRepliedUser (false ).queue ();
188- }
189- } catch (IOException ignored ) {}
190- }
191- }
193+ // Handle Links in the actual message
194+ for (String extractUrl : extractUrls (message .getContentRaw ())) {
195+ var log = LinkExtractorList .LIST .fetch (extractUrl );
196+ if (log != null )
197+ analyser .readLog (builder , log );
198+ }
192199
200+ if (!builder .isEmpty ()) {;
201+ String id ;
202+ if (PluginManager .isLoaded ("mangobotsite" )) {
203+ try {
204+ id = MangoBotSiteIntegration .handleLogResult (builder );
205+ if (id != null ) {
206+ message .reply ("[[Log Analyzer](https://mangobot.mangorage.org/file?id=%s)]" .formatted (id )).setSuppressEmbeds (true ).mentionRepliedUser (false ).queue ();
207+ }
208+ } catch (IOException ignored ) {}
209+ }
210+ }
211+ });
193212
194- if (suceeeded .get ()) message .addReaction (CREATE_GISTS ).queue ();
195- });
196- }
197213 }
198214
199215 public static void onMessage (DiscordEvent <MessageReceivedEvent > event ) {
200- var dEvent = event .getInstance ();
201- var message = dEvent .getMessage ();
202- var attachments = message .getAttachments ();
203-
204- var builder = new StringBuilder ();
205- analyser .scanMessage (message , builder );
216+ var discordEvent = event .getInstance ();
217+ var message = discordEvent .getMessage ();
206218
207- if (!attachments .isEmpty ()) {
208- TaskScheduler .getExecutor ().execute (() -> {
209- var suceeeded = new AtomicBoolean (false );
210- for (Message .Attachment attachment : attachments ) {
211- try {
212- byte [] bytes = getData (attachment .getProxy ().download ().get ());
213- if (bytes == null ) continue ;
214- String content = new String (bytes , StandardCharsets .UTF_8 );
215- if (containsPrintableCharacters (content )) {
216- suceeeded .set (true );
217- analyser .readLog (builder , content );
218- break ;
219- }
220- } catch (InterruptedException | ExecutionException e ) {
221- throw new RuntimeException (e );
222- }
223- }
219+ if (message .getAuthor ().isBot ()) return ;
220+ if (message .getAuthor ().isSystem ()) return ;
224221
225- if (!builder .isEmpty ()) {;
226- if (PluginManager .isLoaded ("mangobotsite" )) {
227- message .addReaction (ANALYZE ).queue ();
228- }
229- }
230222
223+ if (message .getContentRaw ().contains ("https://" )) {
224+ message .addReaction (ANALYZE ).queue ();
225+ }
231226
232- if (suceeeded . get ()) message .addReaction ( CREATE_GISTS ). queue ();
233- } );
227+ if (! message .getAttachments (). isEmpty ()) {
228+ message . addReaction ( CREATE_GISTS ). queue ( );
234229 }
235230 }
236231
@@ -242,7 +237,6 @@ public static void onReact(DiscordEvent<MessageReactionAddEvent> event) {
242237 if (dEvent .getUser ().isBot ()) return ;
243238
244239 dEvent .retrieveMessage ().queue (a -> {
245- if (a .getAttachments ().isEmpty ()) return ;
246240 a .retrieveReactionUsers (CREATE_GISTS ).queue (b -> {
247241 b .stream ().filter (user -> !user .isBot ()).findFirst ().ifPresent (c -> {
248242 a .clearReactions (CREATE_GISTS ).queue ();
0 commit comments