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

122 lines
4.2 KiB
Java
Raw Normal View History

package net.Broken;
2018-03-03 14:21:47 +01:00
import net.Broken.RestApi.ApiCommandLoader;
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;
2018-03-02 15:57:10 +01:00
import net.Broken.audio.Youtube.YoutubeTools;
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;
2018-04-25 16:40:21 +02:00
import net.dv8tion.jda.core.entities.RichPresence;
import net.dv8tion.jda.core.entities.impl.JDAImpl;
import net.dv8tion.jda.core.exceptions.RateLimitedException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2018-04-25 16:40:21 +02:00
import org.json.JSONObject;
import javax.security.auth.login.LoginException;
2018-03-02 15:57:10 +01:00
import java.io.IOException;
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
*/
2018-11-22 14:31:26 +01:00
static JDA initJda(String token){
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...");
}
else
{
//Token présent
try
{
2018-04-25 16:40:21 +02:00
logger.info("Connecting to Discord api...");
//connection au bot
2018-04-28 22:57:48 +02:00
jda = new JDABuilder(AccountType.BOT).setToken(token).setBulkDeleteSplittingEnabled(false).buildBlocking();
2018-01-12 12:50:12 +01:00
MainBot.jda = jda;
jda.setAutoReconnect(true);
2018-09-10 14:53:56 +02:00
/*************************************
* Definition des commande *
*************************************/
2018-04-25 16:40:21 +02:00
jda.getPresence().setPresence(OnlineStatus.DO_NOT_DISTURB, Game.playing("Loading..."));
jda.getTextChannels().forEach(textChannel -> textChannel.sendTyping().queue());
//On recupere le l'id serveur
2018-04-25 17:12:23 +02:00
for( Guild server : jda.getGuilds()){
//on recupere les utilisateur
List<Member> utilisateurCo = server.getMembers();
logger.info("Online users: "+utilisateurCo.size());
for (Member anUtilisateurCo : utilisateurCo)
{
if (anUtilisateurCo.getOnlineStatus().equals(OnlineStatus.ONLINE))
logger.debug("\t*" + anUtilisateurCo.getEffectiveName());
}
logger.debug("Do not disturb users: ");
for (Member anUtilisateurCo : utilisateurCo)
{
if (anUtilisateurCo.getOnlineStatus().equals(OnlineStatus.DO_NOT_DISTURB))
logger.debug("\t*" + anUtilisateurCo.getEffectiveName());
}
logger.debug("Offline users: ");
for (Member anUtilisateurCo : utilisateurCo)
{
if (anUtilisateurCo.getOnlineStatus().equals(OnlineStatus.OFFLINE))
logger.debug("\t*" + anUtilisateurCo.getEffectiveName());
}
}
2018-04-25 17:12:23 +02:00
2017-12-22 19:20:26 +01:00
logger.debug("-----------------FIN INITIALISATION-----------------");
}
2018-04-25 16:40:21 +02:00
catch (LoginException | InterruptedException e)
{
logger.catching(e);
}
}
return jda;
}
2018-03-03 14:21:47 +01:00
2018-03-03 14:56:57 +01:00
static void polish(JDA jda){
2018-03-03 14:21:47 +01:00
CommandLoader.load();
ApiCommandLoader.load();
2018-06-06 15:51:59 +02:00
DayListener dayListener = DayListener.getInstance();
dayListener.addListener(new ResetSpam());
dayListener.addListener(new DailyMadame());
dayListener.start();
2018-04-28 22:57:48 +02:00
jda.addEventListener(new BotListener());
2018-04-25 16:40:21 +02:00
jda.getPresence().setPresence(OnlineStatus.ONLINE, Game.playing("bot.seb6596.ovh"));
2018-03-03 14:56:57 +01:00
2018-03-03 14:21:47 +01:00
}
}