ClaptrapBot/src/main/java/net/Broken/Init.java

111 lines
4.0 KiB
Java
Raw Normal View History

package net.Broken;
import net.Broken.Tools.Command.CommandLoader;
2018-02-16 15:02:53 +01:00
import net.Broken.Tools.DayListener.DayListener;
2018-02-16 15:37:10 +01:00
import net.Broken.Tools.DayListener.Listeners.DailyMadame;
2018-02-16 15:02:53 +01:00
import net.Broken.Tools.DayListener.Listeners.ResetSpam;
import net.dv8tion.jda.core.AccountType;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.JDABuilder;
import net.dv8tion.jda.core.OnlineStatus;
import net.dv8tion.jda.core.entities.Game;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.exceptions.RateLimitedException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.security.auth.login.LoginException;
import java.util.List;
2018-02-28 17:59:09 +01:00
public class Init {
static private Logger logger = LogManager.getLogger();
2018-02-28 17:59:09 +01:00
/**
* Initialize all bot functionality
* @param token bot user token
* @param dev dev Mode or not
* @return JDA object
*/
static JDA initBot(String token, boolean dev){
boolean okInit;
JDA jda = null;
2017-12-22 19:20:26 +01:00
logger.debug("-------------------INITIALISATION-------------------");
//Bot démarrer sans token
if (token == null) {
logger.fatal("Veuilliez indiquer le token du bot en argument...");
okInit=false;
}
else
{
//Token présent
try
{
logger.info("Connection au serveur...");
//connection au bot
jda = new JDABuilder(AccountType.BOT).addEventListener(new BotListener()).setToken(token).setBulkDeleteSplittingEnabled(false).buildBlocking();
2018-01-12 12:50:12 +01:00
MainBot.jda = jda;
jda.setAutoReconnect(true);
jda.addEventListener();
/*************************************
* Definition des commande *
*************************************/
jda.getPresence().setGame(Game.of("Statut: Loading..."));
jda.getTextChannels().forEach(textChannel -> textChannel.sendTyping().queue());
2018-01-12 12:50:12 +01:00
CommandLoader.load();
//On recupere le l'id serveur
Guild serveur = jda.getGuilds().get(0);
//on recupere les utilisateur
List<Member> utilisateurCo = serveur.getMembers();
2017-12-22 19:20:26 +01:00
logger.info("Utilisatieur connecté: "+utilisateurCo.size());
for (Member anUtilisateurCo : utilisateurCo)
{
if (anUtilisateurCo.getOnlineStatus().equals(OnlineStatus.ONLINE))
2017-12-22 19:20:26 +01:00
logger.debug("\t*" + anUtilisateurCo.getEffectiveName());
}
2017-12-22 19:20:26 +01:00
logger.debug("Utilisatieur absent: ");
for (Member anUtilisateurCo : utilisateurCo)
{
if (anUtilisateurCo.getOnlineStatus().equals(OnlineStatus.DO_NOT_DISTURB))
2017-12-22 19:20:26 +01:00
logger.debug("\t*" + anUtilisateurCo.getEffectiveName());
}
2017-12-22 19:20:26 +01:00
logger.debug("Utilisatieur hors ligne: ");
for (Member anUtilisateurCo : utilisateurCo)
{
if (anUtilisateurCo.getOnlineStatus().equals(OnlineStatus.OFFLINE))
2017-12-22 19:20:26 +01:00
logger.debug("\t*" + anUtilisateurCo.getEffectiveName());
}
MainBot.ModoTimer modotimer = new MainBot.ModoTimer();
modotimer.start();
2018-02-16 15:02:53 +01:00
DayListener dayListener = DayListener.getInstance();
dayListener.addListener(new ResetSpam());
2018-02-16 15:37:10 +01:00
dayListener.addListener(new DailyMadame());
dayListener.start();
2018-02-16 15:02:53 +01:00
2017-12-22 19:20:26 +01:00
logger.debug("-----------------FIN INITIALISATION-----------------");
jda.getPresence().setGame(Game.of("Statut: Ok!"));
}
catch (LoginException | InterruptedException | RateLimitedException e)
{
logger.catching(e);
}
}
return jda;
}
}