Search in sources :

Example 1 with Bot

use of com.mcmoddev.mmdbot.core.bot.Bot in project MMDBot by MinecraftModDevelopment.

the class TheCommander method start.

@Override
public void start() {
    instance = this;
    EventListeners.clear();
    try {
        final var configPath = runPath.resolve("configs").resolve("general_config.conf");
        final HoconConfigurationLoader loader = HoconConfigurationLoader.builder().emitComments(true).prettyPrinting(true).defaultOptions(ConfigurationOptions.defaults().serializers(ADDED_SERIALIZERS)).path(configPath).build();
        final var cPair = ConfigurateUtils.loadConfig(loader, configPath, c -> generalConfig = c, Configuration.class, Configuration.EMPTY);
        config = cPair.second();
        generalConfig = cPair.first().get();
    } catch (ConfigurateException e) {
        LOGGER.error("Exception while trying to load general config", e);
        throw new RuntimeException(e);
    }
    MessageAction.setDefaultMentionRepliedUser(false);
    MessageAction.setDefaultMentions(Collections.emptySet());
    if (generalConfig.bot().getOwners().isEmpty()) {
        LOGGER.warn("Please provide at least one bot owner!");
        throw new RuntimeException();
    }
    final var coOwners = generalConfig.bot().getOwners().subList(1, generalConfig.bot().getOwners().size());
    commandClient = new CommandClientBuilder().setOwnerId(generalConfig.bot().getOwners().get(0)).setCoOwnerIds(coOwners.toArray(String[]::new)).forceGuildOnly(generalConfig.bot().guild()).useHelpBuilder(false).setManualUpsert(false).setActivity(null).build();
    EventListeners.COMMANDS_LISTENER.addListener((EventListener) commandClient);
    {
        // Command register
        ReflectionsUtils.getFieldsAnnotatedWith(RegisterSlashCommand.class).stream().peek(f -> f.setAccessible(true)).map(io.github.matyrobbrt.curseforgeapi.util.Utils.rethrowFunction(f -> f.get(null))).filter(SlashCommand.class::isInstance).map(SlashCommand.class::cast).forEach(commandClient::addSlashCommand);
    }
    try {
        final var builder = JDABuilder.create(dotenv.get("BOT_TOKEN"), INTENTS).disableCache(CacheFlag.VOICE_STATE).disableCache(CacheFlag.ACTIVITY).disableCache(CacheFlag.CLIENT_STATUS).disableCache(CacheFlag.ONLINE_STATUS).setEnabledIntents(INTENTS);
        EventListeners.register(builder::addEventListeners);
        jda = builder.build().awaitReady();
    } catch (final LoginException exception) {
        LOGGER.error("Error logging in the bot! Please give the bot a valid token in the config file.", exception);
        System.exit(1);
    } catch (InterruptedException e) {
        LOGGER.error("Error awaiting caching.", e);
        System.exit(1);
    }
    try {
        final var cfKey = dotenv.get("CF_API_KEY", "");
        if (!cfKey.isBlank()) {
            final var api = CurseForgeAPI.builder().apiKey(cfKey).build();
            final var cfProjects = new CFProjects(runPath.resolve("cf_projects.json"));
            this.curseForgeManager = new CurseForgeManager(api, cfProjects);
            CURSE_FORGE_UPDATE_SCHEDULER.scheduleAtFixedRate(cfProjects, 0, 10, TimeUnit.MINUTES);
            CurseForgeCommand.REFRESH_GAMES_TASK.run();
        } else {
            LOGGER.warn("Could not find a valid CurseForge API Key! Some features might not work as expected.");
        }
    } catch (LoginException e) {
        LOGGER.error("Error while authenticating to the CurseForge API. Please provide a valid token, or don't provide any value!", e);
        System.exit(1);
    }
}
Also used : LoginException(javax.security.auth.login.LoginException) CommentedConfigurationNode(org.spongepowered.configurate.CommentedConfigurationNode) TypeSerializerCollection(org.spongepowered.configurate.serialize.TypeSerializerCollection) JDA(net.dv8tion.jda.api.JDA) CommandClient(com.jagrosh.jdautilities.command.CommandClient) BotRegistry(com.mcmoddev.mmdbot.core.bot.BotRegistry) AllowedMentions(net.dv8tion.jda.api.utils.AllowedMentions) LoggerFactory(org.slf4j.LoggerFactory) ConfigurateException(org.spongepowered.configurate.ConfigurateException) ConfigurationReference(org.spongepowered.configurate.reference.ConfigurationReference) BotUserData(com.mcmoddev.mmdbot.dashboard.util.BotUserData) GatewayIntent(net.dv8tion.jda.api.requests.GatewayIntent) ConfigurationOptions(org.spongepowered.configurate.ConfigurationOptions) ConfigurateUtils(com.mcmoddev.mmdbot.core.util.ConfigurateUtils) Dotenv(io.github.cdimascio.dotenv.Dotenv) SlashCommand(com.jagrosh.jdautilities.command.SlashCommand) DotenvLoader(com.mcmoddev.mmdbot.core.util.DotenvLoader) JDABuilder(net.dv8tion.jda.api.JDABuilder) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MessageAction(net.dv8tion.jda.api.requests.restaction.MessageAction) CacheFlag(net.dv8tion.jda.api.utils.cache.CacheFlag) CurseForgeCommand(com.mcmoddev.mmdbot.commander.commands.curseforge.CurseForgeCommand) CurseForgeManager(com.mcmoddev.mmdbot.commander.cfwebhooks.CurseForgeManager) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) EventListeners(com.mcmoddev.mmdbot.commander.util.EventListeners) Logger(org.slf4j.Logger) Configuration(com.mcmoddev.mmdbot.commander.config.Configuration) RegisterBotType(com.mcmoddev.mmdbot.core.bot.RegisterBotType) RegisterSlashCommand(com.mcmoddev.mmdbot.commander.annotation.RegisterSlashCommand) Set(java.util.Set) IOException(java.io.IOException) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) CommandClientBuilder(com.jagrosh.jdautilities.command.CommandClientBuilder) CFProjects(com.mcmoddev.mmdbot.commander.cfwebhooks.CFProjects) EventListener(net.dv8tion.jda.api.hooks.EventListener) Utils(com.mcmoddev.mmdbot.core.util.Utils) Bot(com.mcmoddev.mmdbot.core.bot.Bot) BotType(com.mcmoddev.mmdbot.core.bot.BotType) HoconConfigurationLoader(org.spongepowered.configurate.hocon.HoconConfigurationLoader) CurseForgeAPI(io.github.matyrobbrt.curseforgeapi.CurseForgeAPI) Optional(java.util.Optional) ReflectionsUtils(com.mcmoddev.mmdbot.core.util.ReflectionsUtils) Collections(java.util.Collections) CurseForgeManager(com.mcmoddev.mmdbot.commander.cfwebhooks.CurseForgeManager) RegisterSlashCommand(com.mcmoddev.mmdbot.commander.annotation.RegisterSlashCommand) ConfigurateException(org.spongepowered.configurate.ConfigurateException) CommandClientBuilder(com.jagrosh.jdautilities.command.CommandClientBuilder) SlashCommand(com.jagrosh.jdautilities.command.SlashCommand) RegisterSlashCommand(com.mcmoddev.mmdbot.commander.annotation.RegisterSlashCommand) HoconConfigurationLoader(org.spongepowered.configurate.hocon.HoconConfigurationLoader) LoginException(javax.security.auth.login.LoginException) CFProjects(com.mcmoddev.mmdbot.commander.cfwebhooks.CFProjects)

Aggregations

CommandClient (com.jagrosh.jdautilities.command.CommandClient)1 CommandClientBuilder (com.jagrosh.jdautilities.command.CommandClientBuilder)1 SlashCommand (com.jagrosh.jdautilities.command.SlashCommand)1 RegisterSlashCommand (com.mcmoddev.mmdbot.commander.annotation.RegisterSlashCommand)1 CFProjects (com.mcmoddev.mmdbot.commander.cfwebhooks.CFProjects)1 CurseForgeManager (com.mcmoddev.mmdbot.commander.cfwebhooks.CurseForgeManager)1 CurseForgeCommand (com.mcmoddev.mmdbot.commander.commands.curseforge.CurseForgeCommand)1 Configuration (com.mcmoddev.mmdbot.commander.config.Configuration)1 EventListeners (com.mcmoddev.mmdbot.commander.util.EventListeners)1 Bot (com.mcmoddev.mmdbot.core.bot.Bot)1 BotRegistry (com.mcmoddev.mmdbot.core.bot.BotRegistry)1 BotType (com.mcmoddev.mmdbot.core.bot.BotType)1 RegisterBotType (com.mcmoddev.mmdbot.core.bot.RegisterBotType)1 ConfigurateUtils (com.mcmoddev.mmdbot.core.util.ConfigurateUtils)1 DotenvLoader (com.mcmoddev.mmdbot.core.util.DotenvLoader)1 ReflectionsUtils (com.mcmoddev.mmdbot.core.util.ReflectionsUtils)1 Utils (com.mcmoddev.mmdbot.core.util.Utils)1 BotUserData (com.mcmoddev.mmdbot.dashboard.util.BotUserData)1 Dotenv (io.github.cdimascio.dotenv.Dotenv)1 CurseForgeAPI (io.github.matyrobbrt.curseforgeapi.CurseForgeAPI)1