Search in sources :

Example 6 with IDiscordClient

use of sx.blah.discord.api.IDiscordClient in project DiscordSailv2 by Vaerys-Dawn.

the class Client method main.

public static void main(String[] args) {
    IDiscordClient client = new ClientBuilder().withToken(args[0]).build();
    client.getDispatcher().registerListener((IListener<ReadyEvent>) readyEvent -> System.out.println("login successful"));
    client.login();
}
Also used : RequestHandler(com.github.vaerys.handlers.RequestHandler) Campaign(com.patreon.resources.Campaign) ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) TimedEvent(com.github.vaerys.objects.TimedEvent) Pledge(com.patreon.resources.Pledge) HttpStatusException(org.jsoup.HttpStatusException) ReadyEvent(sx.blah.discord.handle.impl.events.ReadyEvent) ArrayList(java.util.ArrayList) PasteBin(com.github.kennedyoliveira.pastebin4j.PasteBin) ZoneOffset(java.time.ZoneOffset) Logger(org.slf4j.Logger) IEmoji(sx.blah.discord.handle.obj.IEmoji) PatreonOAuth(com.patreon.PatreonOAuth) EventAvatar(com.github.vaerys.objects.EventAvatar) IOException(java.io.IOException) FileHandler(com.github.vaerys.handlers.FileHandler) AccountCredentials(com.github.kennedyoliveira.pastebin4j.AccountCredentials) File(java.io.File) IDiscordClient(sx.blah.discord.api.IDiscordClient) Image(sx.blah.discord.util.Image) List(java.util.List) StringHandler(com.github.vaerys.handlers.StringHandler) IListener(sx.blah.discord.api.events.IListener) DiscordException(sx.blah.discord.util.DiscordException) PatreonAPI(com.github.vaerys.objects.PatreonAPI) FilenameUtils(org.apache.commons.io.FilenameUtils) ClientBuilder(sx.blah.discord.api.ClientBuilder) ReadyEvent(sx.blah.discord.handle.impl.events.ReadyEvent) IDiscordClient(sx.blah.discord.api.IDiscordClient) ClientBuilder(sx.blah.discord.api.ClientBuilder)

Example 7 with IDiscordClient

use of sx.blah.discord.api.IDiscordClient in project DiscordSailv2 by Vaerys-Dawn.

the class Main method main.

public static void main(String[] args) throws UnknownHostException {
    // important, do not move
    PatchHandler.preInitPatches();
    logger = LoggerFactory.getLogger(Main.class);
    logger.info("Starting bot...");
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            logger.info(">>> Running Shutdown Process <<<");
            if (Globals.savingFiles) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    Utility.sendStack(e);
                }
            }
            Globals.saveFiles(true);
        }
    });
    String token = null;
    // you need to set a token in Token/Token.txt for the bot to run
    try {
        Discord4J.disableAudio();
        FileHandler.createDirectory(Constants.DIRECTORY_STORAGE);
        FileHandler.createDirectory(Constants.DIRECTORY_GLOBAL_IMAGES);
        FileHandler.createDirectory(Constants.DIRECTORY_COMP);
        FileHandler.createDirectory(Constants.DIRECTORY_BACKUPS);
        FileHandler.createDirectory(Constants.DIRECTORY_TEMP);
        FileHandler.createDirectory(Constants.DIRECTORY_OLD_FILES);
        FileHandler.createDirectory(Constants.DIRECTORY_ERROR);
        // load config phase 1
        Config config = (Config) Config.create(Constants.FILE_CONFIG, new Config());
        GlobalData globalData = (GlobalData) GlobalData.create(Constants.FILE_GLOBAL_DATA, new GlobalData());
        config = Config.check(config);
        // getting bot token
        try {
            token = FileHandler.readFromFile(Constants.FILE_TOKEN).get(0);
        } catch (IndexOutOfBoundsException e) {
            logger.error("!!!BOT TOKEN NOT VALID PLEASE CHECK \"Storage/Token.txt\" AND UPDATE THE TOKEN!!!");
            System.exit(Constants.EXITCODE_STOP);
        }
        try {
            List<String> pastebinToken = FileHandler.readFromFile(Constants.FILE_PASTEBIN_TOKEN);
            Client.initPastebin(pastebinToken);
        } catch (IndexOutOfBoundsException e) {
            logger.info("No Pastebin Token found.");
        }
        // stuff that i cant getToggles to work because reasons, ignore completely
        // try{
        // List<String> richPresesnce = FileHandler.readFromFile(Constants.FILE_RPC_TOKEN);
        // Client.initRichPresence(richPresesnce);
        // }catch (IndexOutOfBoundsException e){
        // logger.info("Rich presence information missing.");
        // }
        // try {
        // List<String> imgurToken = FileHandler.readFromFile(Constants.FILE_IMGUR_TOKEN);
        // Client.initImgur(imgurToken);
        // } catch (IndexOutOfBoundsException e) {
        // logger.info("No Patreon Token found.");
        // }
        IDiscordClient client = Client.createClient(token, false);
        // load config phase 2
        Globals.initConfig(client, config, globalData);
        if (Globals.creatorID == 153159020528533505L) {
            try {
                List<String> patreonToken = FileHandler.readFromFile(Constants.FILE_PATREON_TOKEN);
                Client.initPatreon(patreonToken);
            } catch (IndexOutOfBoundsException e) {
                logger.info("No Patreon Token found.");
            }
        }
        Globals.validateConfig();
        if (Globals.errorStack != null) {
            logger.error(">\n> Begin Config Error Report <<\n" + "at " + Constants.DIRECTORY_STORAGE + Constants.FILE_CONFIG + "\n" + Globals.errorStack + ">> End Error Report <<");
            System.exit(Constants.EXITCODE_STOP);
        }
        ThreadGroup group = new ThreadGroup("GuildCreateGroup");
        final int[] count = new int[] { 0 };
        // login + register listener.
        client.login();
        // initialize creatorID if it is completely unset:
        if (config.creatorID == 0) {
            IUser botOwner = client.getApplicationOwner();
            config.creatorID = botOwner.getLongID();
            Globals.creatorID = config.creatorID;
            logger.info("Default creatorID set to user " + botOwner.getName() + "#" + botOwner.getDiscriminator());
            // save it back out to file.
            config.flushFile();
        }
        ExecutorService guildService = new ThreadPoolExecutor(2, 50, 1, TimeUnit.MINUTES, new ArrayBlockingQueue<>(1000), r -> new Thread(group, r, group.getName() + "-Thread-" + ++count[0]));
        ExecutorService commandService = new ThreadPoolExecutor(2, 50, 1, TimeUnit.MINUTES, new ArrayBlockingQueue<>(1000), r -> new Thread(group, r, group.getName() + "-Thread-" + ++count[0]));
        ExecutorService creatorService = new ThreadPoolExecutor(2, 50, 1, TimeUnit.MINUTES, new ArrayBlockingQueue<>(1000), r -> new Thread(group, r, group.getName() + "-Thread-" + ++count[0]));
        EventDispatcher dispatcher = client.getDispatcher();
        dispatcher.registerListener(guildService, new GuildCreateListener());
        dispatcher.registerListener(commandService, new AnnotationListener());
        dispatcher.registerListener(creatorService, new CreatorHandler());
        dispatcher.registerTemporaryListener(new InitEvent());
        // validate config file
        Globals.setVersion();
        // Init Patch system.
        // timed events getSlashCommands
        new TimerHandler();
    } catch (DiscordException ex) {
        logger.error(ex.getErrorMessage());
    } catch (RateLimitException e) {
        Utility.sendStack(e);
    }
}
Also used : RateLimitException(sx.blah.discord.util.RateLimitException) Config(com.github.vaerys.pogos.Config) IDiscordClient(sx.blah.discord.api.IDiscordClient) EventDispatcher(sx.blah.discord.api.events.EventDispatcher) GlobalData(com.github.vaerys.pogos.GlobalData) ExecutorService(java.util.concurrent.ExecutorService) DiscordException(sx.blah.discord.util.DiscordException) IUser(sx.blah.discord.handle.obj.IUser) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 8 with IDiscordClient

use of sx.blah.discord.api.IDiscordClient in project BoltBot by DiscordBolt.

the class BoltBot method registerModules.

private static void registerModules(IDiscordClient client) {
    // API Modules
    Logger.trace("Loading API modules.");
    client.getDispatcher().registerListener(new DataSync(client));
    commandManager = new CommandManager(client, "com.discordbolt.boltbot");
    client.getGuilds().stream().map(g -> GuildData.getById(g.getLongID())).filter(Optional::isPresent).filter(gd -> gd.get().getCommandPrefix() != null).forEach(gd -> commandManager.setCommandPrefix(client.getGuildByID(gd.get().getGuildId()), gd.get().getCommandPrefix().charAt(0)));
    Logger.trace("Finished loading API modules");
    // Feature Modules
    Logger.trace("Loading feature modules.");
    client.getModuleLoader().loadModule(new MusicModule(client));
    client.getModuleLoader().loadModule(new CuntModule(client));
    client.getModuleLoader().loadModule(new DiceModule(client));
    client.getModuleLoader().loadModule(new DisconnectModule(client));
    client.getModuleLoader().loadModule(new RedditModule(client));
    client.getModuleLoader().loadModule(new TableFixerModule(client));
    client.getModuleLoader().loadModule(new SeenModule(client));
    client.getModuleLoader().loadModule(new StatusModule(client));
    client.getModuleLoader().loadModule(new TagModule(client));
    client.getModuleLoader().loadModule(new TwitchModule(client));
    Logger.trace("Finished loading feature modules.");
}
Also used : CuntModule(com.discordbolt.boltbot.modules.misc.CuntModule) TwitchModule(com.discordbolt.boltbot.modules.twitch.TwitchModule) TagModule(com.discordbolt.boltbot.modules.tags.TagModule) TableFixerModule(com.discordbolt.boltbot.modules.misc.TableFixerModule) MySQL(com.discordbolt.boltbot.system.mysql.MySQL) GuildData(com.discordbolt.boltbot.system.mysql.data.persistent.GuildData) ReadyEvent(sx.blah.discord.handle.impl.events.ReadyEvent) IDiscordClient(sx.blah.discord.api.IDiscordClient) DiceModule(com.discordbolt.boltbot.modules.dice.DiceModule) SQLException(java.sql.SQLException) CommandManager(com.discordbolt.api.command.CommandManager) SeenModule(com.discordbolt.boltbot.modules.seen.SeenModule) DataSync(com.discordbolt.boltbot.system.mysql.data.DataSync) MusicModule(com.discordbolt.boltbot.modules.music.MusicModule) Optional(java.util.Optional) RedditModule(com.discordbolt.boltbot.modules.reddit.RedditModule) IListener(sx.blah.discord.api.events.IListener) StatusModule(com.discordbolt.boltbot.system.status.StatusModule) Logger(com.discordbolt.boltbot.utils.Logger) DisconnectModule(com.discordbolt.boltbot.modules.disconnect.DisconnectModule) ClientBuilder(sx.blah.discord.api.ClientBuilder) CuntModule(com.discordbolt.boltbot.modules.misc.CuntModule) RedditModule(com.discordbolt.boltbot.modules.reddit.RedditModule) SeenModule(com.discordbolt.boltbot.modules.seen.SeenModule) Optional(java.util.Optional) StatusModule(com.discordbolt.boltbot.system.status.StatusModule) TableFixerModule(com.discordbolt.boltbot.modules.misc.TableFixerModule) CommandManager(com.discordbolt.api.command.CommandManager) DisconnectModule(com.discordbolt.boltbot.modules.disconnect.DisconnectModule) TwitchModule(com.discordbolt.boltbot.modules.twitch.TwitchModule) DataSync(com.discordbolt.boltbot.system.mysql.data.DataSync) MusicModule(com.discordbolt.boltbot.modules.music.MusicModule) DiceModule(com.discordbolt.boltbot.modules.dice.DiceModule) TagModule(com.discordbolt.boltbot.modules.tags.TagModule)

Example 9 with IDiscordClient

use of sx.blah.discord.api.IDiscordClient in project DiscordSailv2 by Vaerys-Dawn.

the class ProfileObject method addSailModNote.

public void addSailModNote(String contents, long timeStamp, boolean isStrike) {
    if (modNotes == null)
        modNotes = new LinkedList<>();
    IDiscordClient client = Client.getClient();
    modNotes.add(new ModNoteObject(contents, client.getOurUser().getLongID(), timeStamp, isStrike));
}
Also used : IDiscordClient(sx.blah.discord.api.IDiscordClient)

Example 10 with IDiscordClient

use of sx.blah.discord.api.IDiscordClient in project Discord4J by Discord4J.

the class BaseBot method login.

/**
 * A custom login() method to handle all of the possible exceptions and set the bot instance.
 */
public static BaseBot login(String token) {
    // Initializing the bot variable
    BaseBot bot = null;
    // Creates a new client builder instance
    ClientBuilder builder = new ClientBuilder();
    // Sets the bot token for the client
    builder.withToken(token);
    try {
        // Builds the IDiscordClient instance and logs it in
        IDiscordClient client = builder.login();
        // Creating the bot instance
        bot = new BaseBot(client);
    } catch (DiscordException e) {
        // Error occurred logging in
        System.err.println("Error occurred while logging in!");
        e.printStackTrace();
    }
    return bot;
}
Also used : DiscordException(sx.blah.discord.util.DiscordException) IDiscordClient(sx.blah.discord.api.IDiscordClient) ClientBuilder(sx.blah.discord.api.ClientBuilder)

Aggregations

IDiscordClient (sx.blah.discord.api.IDiscordClient)12 ClientBuilder (sx.blah.discord.api.ClientBuilder)8 ReadyEvent (sx.blah.discord.handle.impl.events.ReadyEvent)6 DiscordException (sx.blah.discord.util.DiscordException)5 IListener (sx.blah.discord.api.events.IListener)4 File (java.io.File)2 SQLException (java.sql.SQLException)2 IUser (sx.blah.discord.handle.obj.IUser)2 CommandManager (com.discordbolt.api.command.CommandManager)1 DiceModule (com.discordbolt.boltbot.modules.dice.DiceModule)1 DisconnectModule (com.discordbolt.boltbot.modules.disconnect.DisconnectModule)1 CuntModule (com.discordbolt.boltbot.modules.misc.CuntModule)1 TableFixerModule (com.discordbolt.boltbot.modules.misc.TableFixerModule)1 MusicModule (com.discordbolt.boltbot.modules.music.MusicModule)1 RedditModule (com.discordbolt.boltbot.modules.reddit.RedditModule)1 SeenModule (com.discordbolt.boltbot.modules.seen.SeenModule)1 TagModule (com.discordbolt.boltbot.modules.tags.TagModule)1 TwitchModule (com.discordbolt.boltbot.modules.twitch.TwitchModule)1 MySQL (com.discordbolt.boltbot.system.mysql.MySQL)1 DataSync (com.discordbolt.boltbot.system.mysql.data.DataSync)1