Add NoDev to reduce loading time on dev mode

This commit is contained in:
Sebastien 2018-09-10 13:02:27 +03:00
parent 5106dd5098
commit fe13b286dc
10 changed files with 75 additions and 19 deletions

View File

@ -1,11 +1,14 @@
package net.Broken.Commands.Over18; package net.Broken.Commands.Over18;
import net.Broken.Tools.Command.NoDev;
import net.Broken.Tools.Command.NumberedCommande; import net.Broken.Tools.Command.NumberedCommande;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
/** /**
* Ass command, return random picture from les400culs.com * Ass command, return random picture from les400culs.com
*/ */
@NoDev()
public class Ass extends NumberedCommande { public class Ass extends NumberedCommande {
public Ass() { public Ass() {

View File

@ -1,11 +1,13 @@
package net.Broken.Commands.Over18; package net.Broken.Commands.Over18;
import net.Broken.Tools.Command.NoDev;
import net.Broken.Tools.Command.NumberedCommande; import net.Broken.Tools.Command.NumberedCommande;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
/** /**
* Boobs command, return random picture from lesaintdesseins.fr * Boobs command, return random picture from lesaintdesseins.fr
*/ */
@NoDev()
public class Boobs extends NumberedCommande { public class Boobs extends NumberedCommande {
public Boobs() { public Boobs() {

View File

@ -1,11 +1,13 @@
package net.Broken.Commands.Over18; package net.Broken.Commands.Over18;
import net.Broken.Tools.Command.NoDev;
import net.Broken.Tools.Command.NumberedCommande; import net.Broken.Tools.Command.NumberedCommande;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
/** /**
* Ass command, return random picture from feelation.com * Ass command, return random picture from feelation.com
*/ */
@NoDev()
public class Pipe extends NumberedCommande { public class Pipe extends NumberedCommande {
public Pipe() { public Pipe() {
super(LogManager.getLogger(), "http://feelation.com/","featured-img","img"); super(LogManager.getLogger(), "http://feelation.com/","featured-img","img");

View File

@ -7,7 +7,9 @@ import net.Broken.Tools.MessageTimeOut;
import net.Broken.Tools.PrivateMessage; import net.Broken.Tools.PrivateMessage;
import net.Broken.Tools.UserSpamUtils; import net.Broken.Tools.UserSpamUtils;
import net.Broken.audio.Youtube.YoutubeTools; import net.Broken.audio.Youtube.YoutubeTools;
import net.dv8tion.jda.core.AccountType;
import net.dv8tion.jda.core.JDA; import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.JDABuilder;
import net.dv8tion.jda.core.Permission; import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.entities.ChannelType;
import net.dv8tion.jda.core.entities.Member; import net.dv8tion.jda.core.entities.Member;
@ -40,6 +42,7 @@ public class MainBot {
public static HashMap<Member, UserSpamUtils> spamUtils = new HashMap<>(); public static HashMap<Member, UserSpamUtils> spamUtils = new HashMap<>();
public static JDA jda; public static JDA jda;
public static boolean ready = false; public static boolean ready = false;
public static boolean dev = false;
@ -56,7 +59,7 @@ public class MainBot {
logger.info("--------------Starting Bot-------------"); logger.info("--------------Starting Bot-------------");
logger.info("======================================="); logger.info("=======================================");
boolean dev = false;
String token = null; String token = null;
int i = 0; int i = 0;
for(String aArg: args){ for(String aArg: args){
@ -70,8 +73,10 @@ public class MainBot {
i++; i++;
} }
token = System.getenv("TOKEN"); token = System.getenv("TOKEN");
jda = Init.initJda(token, dev); jda = Init.initJda(token, dev);
ConfigurableApplicationContext ctx = SpringApplication.run(MainBot.class, args); ConfigurableApplicationContext ctx = SpringApplication.run(MainBot.class, args);
if(jda == null) { if(jda == null) {
System.exit(SpringApplication.exit(ctx, (ExitCodeGenerator) () -> { System.exit(SpringApplication.exit(ctx, (ExitCodeGenerator) () -> {
@ -89,6 +94,8 @@ public class MainBot {
} }
/** /**

View File

@ -20,7 +20,7 @@ public class CommandLoader {
/** /**
* Search all implemented Command interface class and add it to MainBot.commands HashMap * Search all implemented Command interface class and add it to MainBot.commands HashMap
*/ */
public static void load(){ public static void load() {
logger.info("Loading Command..."); logger.info("Loading Command...");
Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage( Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage(
"net.Broken.Commands", "net.Broken.Commands",
@ -34,13 +34,22 @@ public class CommandLoader {
String reference = command.getName(); String reference = command.getName();
String[] splited = reference.split("\\."); String[] splited = reference.split("\\.");
String name = splited[splited.length-1].toLowerCase(); String name = splited[splited.length - 1].toLowerCase();
if (!command.isAnnotationPresent(Ignore.class)) {
logger.info("..." + name);
logger.info("..." + name); if (!command.isAnnotationPresent(NoDev.class) && MainBot.dev) {
try { try {
MainBot.commandes.put(name, command.newInstance()); MainBot.commandes.put(name, command.newInstance());
} catch (InstantiationException | IllegalAccessException e) { } catch (InstantiationException | IllegalAccessException e) {
logger.error("Failed to load " + name + "!"); logger.error("Failed to load " + name + "!");
}
}else{
logger.warn("Command disable in dev mode");
}
} else {
logger.trace("Ignored command: " + name);
} }
} }

View File

@ -0,0 +1,14 @@
package net.Broken.Tools.Command;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Used to disable command on dev mode
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Ignore {
}

View File

@ -0,0 +1,14 @@
package net.Broken.Tools.Command;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Used to disable command on dev mode
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface NoDev {
}

View File

@ -13,6 +13,7 @@ import java.net.URL;
/** /**
* Abstact class used for all command that need to find the max number of page on a web site. * Abstact class used for all command that need to find the max number of page on a web site.
*/ */
@Ignore
public abstract class NumberedCommande implements Commande{ public abstract class NumberedCommande implements Commande{
private Logger logger = LogManager.getLogger(); private Logger logger = LogManager.getLogger();
private int minNumber = 1; private int minNumber = 1;
@ -35,9 +36,9 @@ public abstract class NumberedCommande implements Commande{
this.divClass = divClass; this.divClass = divClass;
this.htmlType = htmlType; this.htmlType = htmlType;
try { try {
logger.info("Checking max..."); logger.debug("Checking max...");
maxNumber = LimitChecker.doYourJob(baseURL, minNumber); maxNumber = LimitChecker.doYourJob(baseURL, minNumber);
logger.info("New limit is "+maxNumber); logger.info("Limit is "+maxNumber);
} catch (IOException e) { } catch (IOException e) {
logger.catching(e); logger.catching(e);
} }

View File

@ -34,13 +34,13 @@ public class LimitChecker {
huc.setRequestMethod ("GET"); huc.setRequestMethod ("GET");
huc.connect (); huc.connect ();
result = huc.getResponseCode(); result = huc.getResponseCode();
logger.debug("URL: "+u.toString()+" Result: "+result); logger.trace("URL: "+u.toString()+" Result: "+result);
if(result!=404) if(result!=404)
number += 500; number += 500;
} }
number-=400; number-=400;
result = -1; result = -1;
logger.debug("First pass: "+number); logger.trace("First pass: "+number);
while(result != 404 ) while(result != 404 )
{ {
u = new URL( baseURL+number+"-2/"); u = new URL( baseURL+number+"-2/");
@ -48,13 +48,13 @@ public class LimitChecker {
huc.setRequestMethod ("GET"); huc.setRequestMethod ("GET");
huc.connect (); huc.connect ();
result = huc.getResponseCode(); result = huc.getResponseCode();
logger.debug("URL: "+u.toString()+" Result: "+result); logger.trace("URL: "+u.toString()+" Result: "+result);
if(result!=404) if(result!=404)
number += 100; number += 100;
} }
number-=90; number-=90;
result = -1; result = -1;
logger.debug("Second pass: "+number); logger.trace("Second pass: "+number);
while(result != 404 ) while(result != 404 )
{ {
u = new URL( baseURL+number+"-2/"); u = new URL( baseURL+number+"-2/");
@ -62,13 +62,13 @@ public class LimitChecker {
huc.setRequestMethod ("GET"); huc.setRequestMethod ("GET");
huc.connect (); huc.connect ();
result = huc.getResponseCode(); result = huc.getResponseCode();
logger.debug("URL: "+u.toString()+" Result: "+result); logger.trace("URL: "+u.toString()+" Result: "+result);
if(result!=404) if(result!=404)
number += 10; number += 10;
} }
number-=9; number-=9;
result = -1; result = -1;
logger.debug("Third pass: "+number); logger.trace("Third pass: "+number);
while(result != 404 ) while(result != 404 )
{ {
u = new URL( baseURL+number+"-2/"); u = new URL( baseURL+number+"-2/");
@ -76,12 +76,12 @@ public class LimitChecker {
huc.setRequestMethod ("GET"); huc.setRequestMethod ("GET");
huc.connect (); huc.connect ();
result = huc.getResponseCode(); result = huc.getResponseCode();
logger.debug("URL: "+u.toString()+" Result: "+result); logger.trace("URL: "+u.toString()+" Result: "+result);
if(result!=404) if(result!=404)
number += 1; number += 1;
} }
number-=1; number-=1;
logger.debug("Final pass: "+number); logger.trace("Final pass: "+number);
return number; return number;

View File

@ -0,0 +1,4 @@
package net.Broken.Tools.UserManager;
public class Oauth {
}