Search in sources :

Example 1 with InteractionException

use of net.essentialsx.api.v2.services.discord.InteractionException in project Essentials by drtshock.

the class JDADiscordService method startup.

public void startup() throws LoginException, InterruptedException {
    shutdown();
    invalidStartup = true;
    logger.log(Level.INFO, tl("discordLoggingIn"));
    if (plugin.getSettings().getBotToken().replace("INSERT-TOKEN-HERE", "").trim().isEmpty()) {
        throw new IllegalArgumentException(tl("discordErrorNoToken"));
    }
    jda = JDABuilder.createDefault(plugin.getSettings().getBotToken()).addEventListeners(new DiscordListener(this)).enableCache(CacheFlag.EMOTE).disableCache(CacheFlag.MEMBER_OVERRIDES, CacheFlag.VOICE_STATE).setContextEnabled(false).build().awaitReady();
    invalidStartup = false;
    updatePresence();
    logger.log(Level.INFO, tl("discordLoggingInDone", jda.getSelfUser().getAsTag()));
    if (jda.getGuilds().isEmpty()) {
        invalidStartup = true;
        throw new IllegalArgumentException(tl("discordErrorNoGuildSize"));
    }
    guild = jda.getGuildById(plugin.getSettings().getGuildId());
    if (guild == null) {
        invalidStartup = true;
        throw new IllegalArgumentException(tl("discordErrorNoGuild"));
    }
    interactionController = new InteractionControllerImpl(this);
    // Each will throw an exception if disabled
    try {
        interactionController.registerCommand(new ExecuteCommand(this));
    } catch (InteractionException ignored) {
    }
    try {
        interactionController.registerCommand(new MessageCommand(this));
    } catch (InteractionException ignored) {
    }
    try {
        interactionController.registerCommand(new ListCommand(this));
    } catch (InteractionException ignored) {
    }
    // Load emotes into cache, JDA will handle updates from here on out.
    guild.retrieveEmotes().queue();
    updatePrimaryChannel();
    updateConsoleRelay();
    updateTypesRelay();
    // We will see you in the future :balloon:
    // DiscordUtil.cleanWebhooks(guild, DiscordUtil.CONSOLE_RELAY_NAME);
    // DiscordUtil.cleanWebhooks(guild, DiscordUtil.ADVANCED_RELAY_NAME);
    Bukkit.getPluginManager().registerEvents(new BukkitListener(this), plugin);
    try {
        if (VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_12_0_R01)) {
            try {
                Class.forName("io.papermc.paper.advancement.AdvancementDisplay");
                Bukkit.getPluginManager().registerEvents(new PaperAdvancementListenerProvider(), plugin);
            } catch (ClassNotFoundException e) {
                Bukkit.getPluginManager().registerEvents(new AdvancementListenerProvider(), plugin);
            }
        } else {
            Bukkit.getPluginManager().registerEvents(new AchievementListenerProvider(), plugin);
        }
    } catch (final Throwable e) {
        logger.log(Level.WARNING, "Error while loading the achievement/advancement listener. You will not receive achievement/advancement notifications on Discord.", e);
    }
    getPlugin().getEss().scheduleSyncDelayedTask(() -> DiscordUtil.dispatchDiscordMessage(JDADiscordService.this, MessageType.DefaultTypes.SERVER_START, getSettings().getStartMessage(), true, null, null, null));
    Bukkit.getServicesManager().register(DiscordService.class, this, plugin, ServicePriority.Normal);
}
Also used : ListCommand(net.essentialsx.discord.interactions.commands.ListCommand) MessageCommand(net.essentialsx.discord.interactions.commands.MessageCommand) AchievementListenerProvider(net.ess3.nms.refl.providers.AchievementListenerProvider) InteractionControllerImpl(net.essentialsx.discord.interactions.InteractionControllerImpl) BukkitListener(net.essentialsx.discord.listeners.BukkitListener) InteractionException(net.essentialsx.api.v2.services.discord.InteractionException) AdvancementListenerProvider(net.ess3.nms.refl.providers.AdvancementListenerProvider) PaperAdvancementListenerProvider(net.ess3.provider.providers.PaperAdvancementListenerProvider) ExecuteCommand(net.essentialsx.discord.interactions.commands.ExecuteCommand) PaperAdvancementListenerProvider(net.ess3.provider.providers.PaperAdvancementListenerProvider) DiscordListener(net.essentialsx.discord.listeners.DiscordListener)

Example 2 with InteractionException

use of net.essentialsx.api.v2.services.discord.InteractionException in project Essentials by drtshock.

the class InteractionControllerImpl method registerCommand.

@Override
public void registerCommand(InteractionCommand command) throws InteractionException {
    if (command.isDisabled()) {
        throw new InteractionException("The given command has been disabled!");
    }
    if (commandMap.containsKey(command.getName())) {
        throw new InteractionException("A command with that name is already registered!");
    }
    if (!initialBatchRegistration) {
        if (jda.isDebug()) {
            logger.info("Marked guild command for batch registration: " + command.getName());
        }
        batchRegistrationQueue.put(command.getName(), command);
        return;
    }
    final CommandData data = new CommandData(command.getName(), command.getDescription());
    if (command.getArguments() != null) {
        for (final InteractionCommandArgument argument : command.getArguments()) {
            data.addOption(OptionType.valueOf(argument.getType().name()), argument.getName(), argument.getDescription(), argument.isRequired());
        }
    }
    jda.getGuild().upsertCommand(data).queue(success -> {
        commandMap.put(command.getName(), command);
        if (jda.isDebug()) {
            logger.info("Registered guild command " + success.getName() + " with id " + success.getId());
        }
    }, failure -> {
        if (failure instanceof ErrorResponseException && ((ErrorResponseException) failure).getErrorResponse() == ErrorResponse.MISSING_ACCESS) {
            logger.severe(tl("discordErrorCommand"));
            return;
        }
        logger.log(Level.SEVERE, "Error while registering command", failure);
    });
}
Also used : InteractionCommandArgument(net.essentialsx.api.v2.services.discord.InteractionCommandArgument) InteractionException(net.essentialsx.api.v2.services.discord.InteractionException) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData)

Example 3 with InteractionException

use of net.essentialsx.api.v2.services.discord.InteractionException in project Essentials by EssentialsX.

the class InteractionControllerImpl method registerCommand.

@Override
public void registerCommand(InteractionCommand command) throws InteractionException {
    if (command.isDisabled()) {
        throw new InteractionException("The given command has been disabled!");
    }
    if (commandMap.containsKey(command.getName())) {
        throw new InteractionException("A command with that name is already registered!");
    }
    if (!initialBatchRegistration) {
        if (jda.isDebug()) {
            logger.info("Marked guild command for batch registration: " + command.getName());
        }
        batchRegistrationQueue.put(command.getName(), command);
        return;
    }
    final CommandData data = new CommandData(command.getName(), command.getDescription());
    if (command.getArguments() != null) {
        for (final InteractionCommandArgument argument : command.getArguments()) {
            data.addOption(OptionType.valueOf(argument.getType().name()), argument.getName(), argument.getDescription(), argument.isRequired());
        }
    }
    jda.getGuild().upsertCommand(data).queue(success -> {
        commandMap.put(command.getName(), command);
        if (jda.isDebug()) {
            logger.info("Registered guild command " + success.getName() + " with id " + success.getId());
        }
    }, failure -> {
        if (failure instanceof ErrorResponseException && ((ErrorResponseException) failure).getErrorResponse() == ErrorResponse.MISSING_ACCESS) {
            logger.severe(tl("discordErrorCommand"));
            return;
        }
        logger.log(Level.SEVERE, "Error while registering command", failure);
    });
}
Also used : InteractionCommandArgument(net.essentialsx.api.v2.services.discord.InteractionCommandArgument) InteractionException(net.essentialsx.api.v2.services.discord.InteractionException) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData)

Example 4 with InteractionException

use of net.essentialsx.api.v2.services.discord.InteractionException in project Essentials by EssentialsX.

the class JDADiscordService method startup.

public void startup() throws LoginException, InterruptedException {
    shutdown();
    invalidStartup = true;
    logger.log(Level.INFO, tl("discordLoggingIn"));
    if (plugin.getSettings().getBotToken().replace("INSERT-TOKEN-HERE", "").trim().isEmpty()) {
        throw new IllegalArgumentException(tl("discordErrorNoToken"));
    }
    jda = JDABuilder.createDefault(plugin.getSettings().getBotToken()).addEventListeners(new DiscordListener(this)).enableCache(CacheFlag.EMOTE).disableCache(CacheFlag.MEMBER_OVERRIDES, CacheFlag.VOICE_STATE).setContextEnabled(false).build().awaitReady();
    invalidStartup = false;
    updatePresence();
    logger.log(Level.INFO, tl("discordLoggingInDone", jda.getSelfUser().getAsTag()));
    if (jda.getGuilds().isEmpty()) {
        invalidStartup = true;
        throw new IllegalArgumentException(tl("discordErrorNoGuildSize"));
    }
    guild = jda.getGuildById(plugin.getSettings().getGuildId());
    if (guild == null) {
        invalidStartup = true;
        throw new IllegalArgumentException(tl("discordErrorNoGuild"));
    }
    interactionController = new InteractionControllerImpl(this);
    // Each will throw an exception if disabled
    try {
        interactionController.registerCommand(new ExecuteCommand(this));
    } catch (InteractionException ignored) {
    }
    try {
        interactionController.registerCommand(new MessageCommand(this));
    } catch (InteractionException ignored) {
    }
    try {
        interactionController.registerCommand(new ListCommand(this));
    } catch (InteractionException ignored) {
    }
    // Load emotes into cache, JDA will handle updates from here on out.
    guild.retrieveEmotes().queue();
    updatePrimaryChannel();
    updateConsoleRelay();
    updateTypesRelay();
    // We will see you in the future :balloon:
    // DiscordUtil.cleanWebhooks(guild, DiscordUtil.CONSOLE_RELAY_NAME);
    // DiscordUtil.cleanWebhooks(guild, DiscordUtil.ADVANCED_RELAY_NAME);
    Bukkit.getPluginManager().registerEvents(new BukkitListener(this), plugin);
    try {
        if (VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_12_0_R01)) {
            try {
                Class.forName("io.papermc.paper.advancement.AdvancementDisplay");
                Bukkit.getPluginManager().registerEvents(new PaperAdvancementListenerProvider(), plugin);
            } catch (ClassNotFoundException e) {
                Bukkit.getPluginManager().registerEvents(new AdvancementListenerProvider(), plugin);
            }
        } else {
            Bukkit.getPluginManager().registerEvents(new AchievementListenerProvider(), plugin);
        }
    } catch (final Throwable e) {
        logger.log(Level.WARNING, "Error while loading the achievement/advancement listener. You will not receive achievement/advancement notifications on Discord.", e);
    }
    getPlugin().getEss().scheduleSyncDelayedTask(() -> DiscordUtil.dispatchDiscordMessage(JDADiscordService.this, MessageType.DefaultTypes.SERVER_START, getSettings().getStartMessage(), true, null, null, null));
    Bukkit.getServicesManager().register(DiscordService.class, this, plugin, ServicePriority.Normal);
}
Also used : ListCommand(net.essentialsx.discord.interactions.commands.ListCommand) MessageCommand(net.essentialsx.discord.interactions.commands.MessageCommand) AchievementListenerProvider(net.ess3.nms.refl.providers.AchievementListenerProvider) InteractionControllerImpl(net.essentialsx.discord.interactions.InteractionControllerImpl) BukkitListener(net.essentialsx.discord.listeners.BukkitListener) InteractionException(net.essentialsx.api.v2.services.discord.InteractionException) AdvancementListenerProvider(net.ess3.nms.refl.providers.AdvancementListenerProvider) PaperAdvancementListenerProvider(net.ess3.provider.providers.PaperAdvancementListenerProvider) ExecuteCommand(net.essentialsx.discord.interactions.commands.ExecuteCommand) PaperAdvancementListenerProvider(net.ess3.provider.providers.PaperAdvancementListenerProvider) DiscordListener(net.essentialsx.discord.listeners.DiscordListener)

Aggregations

InteractionException (net.essentialsx.api.v2.services.discord.InteractionException)4 ErrorResponseException (net.dv8tion.jda.api.exceptions.ErrorResponseException)2 CommandData (net.dv8tion.jda.api.interactions.commands.build.CommandData)2 AchievementListenerProvider (net.ess3.nms.refl.providers.AchievementListenerProvider)2 AdvancementListenerProvider (net.ess3.nms.refl.providers.AdvancementListenerProvider)2 PaperAdvancementListenerProvider (net.ess3.provider.providers.PaperAdvancementListenerProvider)2 InteractionCommandArgument (net.essentialsx.api.v2.services.discord.InteractionCommandArgument)2 InteractionControllerImpl (net.essentialsx.discord.interactions.InteractionControllerImpl)2 ExecuteCommand (net.essentialsx.discord.interactions.commands.ExecuteCommand)2 ListCommand (net.essentialsx.discord.interactions.commands.ListCommand)2 MessageCommand (net.essentialsx.discord.interactions.commands.MessageCommand)2 BukkitListener (net.essentialsx.discord.listeners.BukkitListener)2 DiscordListener (net.essentialsx.discord.listeners.DiscordListener)2