Search in sources :

Example 1 with ErrorResponseException

use of github.scarsz.discordsrv.dependencies.jda.api.exceptions.ErrorResponseException in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class DiscordCommands method reload.

public void reload() {
    try {
        Guild guild = discordsrv.getMainGuild();
        String memberLabel = InteractiveChatDiscordSrvAddon.plugin.discordMemberLabel;
        String memberDescription = InteractiveChatDiscordSrvAddon.plugin.discordMemberDescription;
        String slotLabel = InteractiveChatDiscordSrvAddon.plugin.discordSlotLabel;
        String slotDescription = InteractiveChatDiscordSrvAddon.plugin.discordSlotDescription;
        guild.retrieveCommands().complete().forEach(each -> {
            switch(each.getName()) {
                case RESOURCEPACK_LABEL:
                    if (InteractiveChatDiscordSrvAddon.plugin.resourcepackCommandIsMainServer) {
                        each.delete().complete();
                    }
                    break;
                case PLAYERLIST_LABEL:
                    if (InteractiveChatDiscordSrvAddon.plugin.playerlistCommandIsMainServer) {
                        each.delete().complete();
                    }
                    break;
                case ITEM_LABEL:
                case ITEM_OTHER_LABEL:
                    if (InteractiveChatDiscordSrvAddon.plugin.shareItemCommandIsMainServer) {
                        each.delete().complete();
                    }
                    break;
                case INVENTORY_LABEL:
                case INVENTORY_OTHER_LABEL:
                    if (InteractiveChatDiscordSrvAddon.plugin.shareInvCommandIsMainServer) {
                        each.delete().complete();
                    }
                    break;
                case ENDERCHEST_LABEL:
                case ENDERCHEST_OTHER_LABEL:
                    if (InteractiveChatDiscordSrvAddon.plugin.shareEnderCommandIsMainServer) {
                        each.delete().complete();
                    }
                    break;
            }
        });
        if (InteractiveChatDiscordSrvAddon.plugin.resourcepackCommandEnabled && InteractiveChatDiscordSrvAddon.plugin.resourcepackCommandIsMainServer) {
            guild.upsertCommand(RESOURCEPACK_LABEL, ChatColorUtils.stripColor(InteractiveChatDiscordSrvAddon.plugin.resourcepackCommandDescription)).setDefaultEnabled(false).queue(command -> {
                command.updatePrivileges(guild, JDAUtils.toWhitelistedCommandPrivileges(guild, JDAUtils.toRoles(guild, InteractiveChatDiscordSrvAddon.plugin.resourcepackCommandRoles))).queue();
            });
        }
        if (InteractiveChatDiscordSrvAddon.plugin.playerlistCommandEnabled && InteractiveChatDiscordSrvAddon.plugin.playerlistCommandIsMainServer) {
            guild.upsertCommand(PLAYERLIST_LABEL, ChatColorUtils.stripColor(InteractiveChatDiscordSrvAddon.plugin.playerlistCommandDescription)).setDefaultEnabled(false).queue(command -> {
                command.updatePrivileges(guild, JDAUtils.toWhitelistedCommandPrivileges(guild, JDAUtils.toRoles(guild, InteractiveChatDiscordSrvAddon.plugin.playerlistCommandRoles))).queue();
            });
        }
        Optional<ICPlaceholder> optItemPlaceholder = InteractiveChat.placeholderList.values().stream().filter(each -> each.getKeyword().equals(InteractiveChat.itemPlaceholder)).findFirst();
        if (InteractiveChatDiscordSrvAddon.plugin.shareItemCommandEnabled && optItemPlaceholder.isPresent() && InteractiveChatDiscordSrvAddon.plugin.shareItemCommandIsMainServer) {
            String itemDescription = ChatColorUtils.stripColor(optItemPlaceholder.get().getDescription());
            SubcommandData mainhandSubcommand = new SubcommandData("mainhand", itemDescription);
            SubcommandData offhandSubcommand = new SubcommandData("offhand", itemDescription);
            SubcommandData hotbarSubcommand = new SubcommandData("hotbar", itemDescription).addOptions(new OptionData(OptionType.INTEGER, slotLabel, slotDescription, true).setRequiredRange(1, 9));
            SubcommandData inventorySubcommand = new SubcommandData("inventory", itemDescription).addOptions(new OptionData(OptionType.INTEGER, slotLabel, slotDescription, true).setRequiredRange(1, 41));
            SubcommandData armorSubcommand = new SubcommandData("armor", itemDescription).addOptions(new OptionData(OptionType.STRING, slotLabel, slotDescription, true).addChoice("head", "head").addChoice("chest", "chest").addChoice("legs", "legs").addChoice("feet", "feet"));
            SubcommandData enderSubcommand = new SubcommandData("ender", itemDescription).addOptions(new OptionData(OptionType.INTEGER, slotLabel, slotDescription, true).setRequiredRange(1, 27));
            guild.upsertCommand(ITEM_LABEL, ChatColorUtils.stripColor(optItemPlaceholder.get().getDescription())).addSubcommands(mainhandSubcommand).addSubcommands(offhandSubcommand).addSubcommands(hotbarSubcommand).addSubcommands(inventorySubcommand).addSubcommands(armorSubcommand).addSubcommands(enderSubcommand).setDefaultEnabled(false).queue(command -> {
                command.updatePrivileges(guild, JDAUtils.toWhitelistedCommandPrivileges(guild, JDAUtils.toRoles(guild, InteractiveChatDiscordSrvAddon.plugin.shareItemCommandSelfRoles))).queue();
            });
            if (InteractiveChatDiscordSrvAddon.plugin.shareItemCommandAsOthers) {
                SubcommandData mainhandOtherSubcommand = new SubcommandData("mainhand", itemDescription).addOption(OptionType.USER, memberLabel, memberDescription, true);
                SubcommandData offhandOtherSubcommand = new SubcommandData("offhand", itemDescription).addOption(OptionType.USER, memberLabel, memberDescription, true);
                SubcommandData hotbarOtherSubcommand = new SubcommandData("hotbar", itemDescription).addOptions(new OptionData(OptionType.INTEGER, slotLabel, slotDescription, true).setRequiredRange(1, 9)).addOption(OptionType.USER, memberLabel, memberDescription, true);
                SubcommandData inventoryOtherSubcommand = new SubcommandData("inventory", itemDescription).addOptions(new OptionData(OptionType.INTEGER, slotLabel, slotDescription, true).setRequiredRange(1, 41)).addOption(OptionType.USER, memberLabel, memberDescription, true);
                SubcommandData armorOtherSubcommand = new SubcommandData("armor", itemDescription).addOptions(new OptionData(OptionType.STRING, slotLabel, slotDescription, true).addChoice("head", "head").addChoice("chest", "chest").addChoice("legs", "legs").addChoice("feet", "feet")).addOption(OptionType.USER, memberLabel, memberDescription, true);
                SubcommandData enderOtherSubcommand = new SubcommandData("ender", itemDescription).addOptions(new OptionData(OptionType.INTEGER, slotLabel, slotDescription, true).setRequiredRange(1, 27)).addOption(OptionType.USER, memberLabel, memberDescription, true);
                guild.upsertCommand(ITEM_OTHER_LABEL, ChatColorUtils.stripColor(optItemPlaceholder.get().getDescription())).addSubcommands(mainhandOtherSubcommand).addSubcommands(offhandOtherSubcommand).addSubcommands(hotbarOtherSubcommand).addSubcommands(inventoryOtherSubcommand).addSubcommands(armorOtherSubcommand).addSubcommands(enderOtherSubcommand).setDefaultEnabled(false).queue(command -> {
                    command.updatePrivileges(guild, JDAUtils.toWhitelistedCommandPrivileges(guild, JDAUtils.toRoles(guild, InteractiveChatDiscordSrvAddon.plugin.shareItemCommandOthersRoles))).queue();
                });
            }
        }
        Optional<ICPlaceholder> optInvPlaceholder = InteractiveChat.placeholderList.values().stream().filter(each -> each.getKeyword().equals(InteractiveChat.invPlaceholder)).findFirst();
        if (InteractiveChatDiscordSrvAddon.plugin.shareInvCommandEnabled && optInvPlaceholder.isPresent() && InteractiveChatDiscordSrvAddon.plugin.shareInvCommandIsMainServer) {
            guild.upsertCommand(INVENTORY_LABEL, ChatColorUtils.stripColor(optInvPlaceholder.get().getDescription())).setDefaultEnabled(false).queue(command -> {
                command.updatePrivileges(guild, JDAUtils.toWhitelistedCommandPrivileges(guild, JDAUtils.toRoles(guild, InteractiveChatDiscordSrvAddon.plugin.shareInvCommandSelfRoles))).queue();
            });
            if (InteractiveChatDiscordSrvAddon.plugin.shareInvCommandAsOthers) {
                guild.upsertCommand(INVENTORY_OTHER_LABEL, ChatColorUtils.stripColor(optInvPlaceholder.get().getDescription())).addOption(OptionType.USER, memberLabel, memberDescription, true).setDefaultEnabled(false).queue(command -> {
                    command.updatePrivileges(guild, JDAUtils.toWhitelistedCommandPrivileges(guild, JDAUtils.toRoles(guild, InteractiveChatDiscordSrvAddon.plugin.shareInvCommandOthersRoles))).queue();
                });
            }
        }
        Optional<ICPlaceholder> optEnderPlaceholder = InteractiveChat.placeholderList.values().stream().filter(each -> each.getKeyword().equals(InteractiveChat.enderPlaceholder)).findFirst();
        if (InteractiveChatDiscordSrvAddon.plugin.shareEnderCommandEnabled && optEnderPlaceholder.isPresent() && InteractiveChatDiscordSrvAddon.plugin.shareEnderCommandIsMainServer) {
            guild.upsertCommand(ENDERCHEST_LABEL, ChatColorUtils.stripColor(optEnderPlaceholder.get().getDescription())).setDefaultEnabled(false).queue(command -> {
                command.updatePrivileges(guild, JDAUtils.toWhitelistedCommandPrivileges(guild, JDAUtils.toRoles(guild, InteractiveChatDiscordSrvAddon.plugin.shareEnderCommandSelfRoles))).queue();
            });
            if (InteractiveChatDiscordSrvAddon.plugin.shareEnderCommandAsOthers) {
                guild.upsertCommand(ENDERCHEST_OTHER_LABEL, ChatColorUtils.stripColor(optEnderPlaceholder.get().getDescription())).addOption(OptionType.USER, memberLabel, memberDescription, true).setDefaultEnabled(false).queue(command -> {
                    command.updatePrivileges(guild, JDAUtils.toWhitelistedCommandPrivileges(guild, JDAUtils.toRoles(guild, InteractiveChatDiscordSrvAddon.plugin.shareEnderCommandOthersRoles))).queue();
                });
            }
        }
    } catch (ErrorResponseException e) {
        if (e.getResponse().code == 50001) {
            throw new DiscordCommandRegistrationException("Scope \"applications.commands\" missing in discord bot application.\nCheck the Q&A section in https://www.spigotmc.org/resources/83917/ for more information", e);
        }
        throw new DiscordCommandRegistrationException(e);
    }
}
Also used : Color(java.awt.Color) Arrays(java.util.Arrays) InventoryUtils(com.loohp.interactivechat.utils.InventoryUtils) PlayerUtils(com.loohp.interactivechat.utils.PlayerUtils) ResourcePackInfo(com.loohp.interactivechatdiscordsrvaddon.resources.ResourcePackInfo) TextChannel(github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel) ICPlayer(com.loohp.interactivechat.objectholders.ICPlayer) Map(java.util.Map) ComponentStringUtils(com.loohp.interactivechatdiscordsrvaddon.utils.ComponentStringUtils) Material(org.bukkit.Material) EquipmentSlot(org.bukkit.inventory.EquipmentSlot) DiscordToolTip(com.loohp.interactivechatdiscordsrvaddon.utils.DiscordItemStackUtils.DiscordToolTip) Message(github.scarsz.discordsrv.dependencies.jda.api.entities.Message) Guild(github.scarsz.discordsrv.dependencies.jda.api.entities.Guild) Set(java.util.Set) OfflinePlayer(org.bukkit.OfflinePlayer) ItemStack(org.bukkit.inventory.ItemStack) InteractiveChat(com.loohp.interactivechat.InteractiveChat) MessageEmbed(github.scarsz.discordsrv.dependencies.jda.api.entities.MessageEmbed) CustomStringUtils(com.loohp.interactivechat.utils.CustomStringUtils) OptionData(github.scarsz.discordsrv.dependencies.jda.api.interactions.commands.build.OptionData) Chat(net.milkbowl.vault.chat.Chat) EmbedBuilder(github.scarsz.discordsrv.dependencies.jda.api.EmbedBuilder) ImageUtils(com.loohp.interactivechatdiscordsrvaddon.graphics.ImageUtils) BungeeMessageSender(com.loohp.interactivechat.bungeemessaging.BungeeMessageSender) MCVersion(com.loohp.interactivechat.utils.MCVersion) HoverEvent(com.loohp.interactivechat.libs.net.kyori.adventure.text.event.HoverEvent) PostPacketComponentProcessEvent(com.loohp.interactivechat.api.events.PostPacketComponentProcessEvent) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) LanguageUtils(com.loohp.interactivechat.utils.LanguageUtils) ColorUtils(com.loohp.interactivechat.utils.ColorUtils) InteractiveChatDiscordSRVConfigReloadEvent(com.loohp.interactivechatdiscordsrvaddon.api.events.InteractiveChatDiscordSRVConfigReloadEvent) OptionType(github.scarsz.discordsrv.dependencies.jda.api.interactions.commands.OptionType) Component(com.loohp.interactivechat.libs.net.kyori.adventure.text.Component) DiscordDescription(com.loohp.interactivechatdiscordsrvaddon.utils.DiscordItemStackUtils.DiscordDescription) IOException(java.io.IOException) WebhookMessageUpdateAction(github.scarsz.discordsrv.dependencies.jda.api.requests.restaction.WebhookMessageUpdateAction) Field(java.lang.reflect.Field) ExecutionException(java.util.concurrent.ExecutionException) ValueTrios(com.loohp.interactivechat.objectholders.ValueTrios) TextReplacementConfig(com.loohp.interactivechat.libs.net.kyori.adventure.text.TextReplacementConfig) PlaceholderParser(com.loohp.interactivechat.utils.PlaceholderParser) RegisteredServiceProvider(org.bukkit.plugin.RegisteredServiceProvider) LegacyComponentSerializer(com.loohp.interactivechat.libs.net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer) DiscordItemStackUtils(com.loohp.interactivechatdiscordsrvaddon.utils.DiscordItemStackUtils) VanishUtils(com.loohp.interactivechat.utils.VanishUtils) ComponentReplacing(com.loohp.interactivechat.utils.ComponentReplacing) InteractiveChatAPI(com.loohp.interactivechat.api.InteractiveChatAPI) ListenerAdapter(github.scarsz.discordsrv.dependencies.jda.api.hooks.ListenerAdapter) Player(org.bukkit.entity.Player) Inventory(org.bukkit.inventory.Inventory) ComponentModernizing(com.loohp.interactivechat.utils.ComponentModernizing) EventHandler(org.bukkit.event.EventHandler) InteractiveChatComponentSerializer(com.loohp.interactivechat.utils.InteractiveChatComponentSerializer) SlashCommandEvent(github.scarsz.discordsrv.dependencies.jda.api.events.interaction.SlashCommandEvent) OptionMapping(github.scarsz.discordsrv.dependencies.jda.api.interactions.commands.OptionMapping) InventoryDisplay(com.loohp.interactivechat.modules.InventoryDisplay) TranslatableComponent(com.loohp.interactivechat.libs.net.kyori.adventure.text.TranslatableComponent) XMaterial(com.loohp.interactivechat.libs.com.cryptomorin.xseries.XMaterial) Bukkit(org.bukkit.Bukkit) BufferedImage(java.awt.image.BufferedImage) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ClickEvent(com.loohp.interactivechat.libs.net.kyori.adventure.text.event.ClickEvent) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) ImageGeneration(com.loohp.interactivechatdiscordsrvaddon.graphics.ImageGeneration) List(java.util.List) ErrorResponseException(github.scarsz.discordsrv.dependencies.jda.api.exceptions.ErrorResponseException) SubcommandData(github.scarsz.discordsrv.dependencies.jda.api.interactions.commands.build.SubcommandData) TextDecoration(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextDecoration) JDAUtils(com.loohp.interactivechatdiscordsrvaddon.utils.JDAUtils) Entry(java.util.Map.Entry) Optional(java.util.Optional) NBTEditor(com.loohp.interactivechat.libs.io.github.bananapuncher714.nbteditor.NBTEditor) ArrayUtils(com.loohp.interactivechat.libs.org.apache.commons.lang3.ArrayUtils) Permission(net.milkbowl.vault.permission.Permission) ItemMeta(org.bukkit.inventory.meta.ItemMeta) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) InteractiveChatDiscordSrvAddon(com.loohp.interactivechatdiscordsrvaddon.InteractiveChatDiscordSrvAddon) SkinUtils(com.loohp.interactivechat.utils.SkinUtils) ComponentStyling(com.loohp.interactivechat.utils.ComponentStyling) HashSet(java.util.HashSet) PlainTextComponentSerializer(com.loohp.interactivechat.libs.net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer) DiscordSRV(github.scarsz.discordsrv.DiscordSRV) SharedType(com.loohp.interactivechat.api.InteractiveChatAPI.SharedType) ICPlaceholder(com.loohp.interactivechat.objectholders.ICPlaceholder) OfflineICPlayer(com.loohp.interactivechat.objectholders.OfflineICPlayer) ChatColor(net.md_5.bungee.api.ChatColor) Listener(org.bukkit.event.Listener) ItemDisplay(com.loohp.interactivechat.modules.ItemDisplay) TimeUnit(java.util.concurrent.TimeUnit) ICPlayerFactory(com.loohp.interactivechat.objectholders.ICPlayerFactory) ChatColorUtils(com.loohp.interactivechat.utils.ChatColorUtils) TranslationKeyUtils(com.loohp.interactivechatdiscordsrvaddon.utils.TranslationKeyUtils) NamedTextColor(com.loohp.interactivechat.libs.net.kyori.adventure.text.format.NamedTextColor) Comparator(java.util.Comparator) HashUtils(com.loohp.interactivechat.utils.HashUtils) ModelDisplayPosition(com.loohp.interactivechatdiscordsrvaddon.resources.models.ModelDisplay.ModelDisplayPosition) Collections(java.util.Collections) ICPlaceholder(com.loohp.interactivechat.objectholders.ICPlaceholder) OptionData(github.scarsz.discordsrv.dependencies.jda.api.interactions.commands.build.OptionData) ErrorResponseException(github.scarsz.discordsrv.dependencies.jda.api.exceptions.ErrorResponseException) Guild(github.scarsz.discordsrv.dependencies.jda.api.entities.Guild) SubcommandData(github.scarsz.discordsrv.dependencies.jda.api.interactions.commands.build.SubcommandData)

Example 2 with ErrorResponseException

use of github.scarsz.discordsrv.dependencies.jda.api.exceptions.ErrorResponseException in project Foundation by kangarko.

the class DiscordListener method sendWebhookMessage.

/**
 * Sends a webhook message from the given sender in case he's a valid Player
 *
 * @param sender
 * @param channelName
 * @param message
 */
protected final void sendWebhookMessage(CommandSender sender, String channelName, String message) {
    final List<TextChannel> channels = this.findChannels(channelName);
    final TextChannel channel = channels.isEmpty() ? null : channels.get(0);
    if (channel == null)
        return;
    // Send the message
    Common.runAsync(() -> {
        try {
            Debugger.debug("discord", "[Minecraft > Discord] Send MC message from '" + channelName + "' to Discord's '" + channel.getName() + "' channel: " + message);
            // You can remove this if you don't want to use webhooks
            if (sender instanceof Player)
                WebhookUtil.deliverMessage(channel, (Player) sender, message);
            else
                channel.sendMessage(message).complete();
        } catch (final ErrorResponseException ex) {
            Debugger.debug("discord", "Unable to send message to Discord channel " + channelName + ", message: " + message);
        }
    });
}
Also used : TextChannel(github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel) Player(org.bukkit.entity.Player) ErrorResponseException(github.scarsz.discordsrv.dependencies.jda.api.exceptions.ErrorResponseException)

Aggregations

InteractiveChat (com.loohp.interactivechat.InteractiveChat)1 InteractiveChatAPI (com.loohp.interactivechat.api.InteractiveChatAPI)1 SharedType (com.loohp.interactivechat.api.InteractiveChatAPI.SharedType)1 PostPacketComponentProcessEvent (com.loohp.interactivechat.api.events.PostPacketComponentProcessEvent)1 BungeeMessageSender (com.loohp.interactivechat.bungeemessaging.BungeeMessageSender)1 XMaterial (com.loohp.interactivechat.libs.com.cryptomorin.xseries.XMaterial)1 NBTEditor (com.loohp.interactivechat.libs.io.github.bananapuncher714.nbteditor.NBTEditor)1 Component (com.loohp.interactivechat.libs.net.kyori.adventure.text.Component)1 TextReplacementConfig (com.loohp.interactivechat.libs.net.kyori.adventure.text.TextReplacementConfig)1 TranslatableComponent (com.loohp.interactivechat.libs.net.kyori.adventure.text.TranslatableComponent)1 ClickEvent (com.loohp.interactivechat.libs.net.kyori.adventure.text.event.ClickEvent)1 HoverEvent (com.loohp.interactivechat.libs.net.kyori.adventure.text.event.HoverEvent)1 NamedTextColor (com.loohp.interactivechat.libs.net.kyori.adventure.text.format.NamedTextColor)1 TextDecoration (com.loohp.interactivechat.libs.net.kyori.adventure.text.format.TextDecoration)1 LegacyComponentSerializer (com.loohp.interactivechat.libs.net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer)1 PlainTextComponentSerializer (com.loohp.interactivechat.libs.net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer)1 ArrayUtils (com.loohp.interactivechat.libs.org.apache.commons.lang3.ArrayUtils)1 InventoryDisplay (com.loohp.interactivechat.modules.InventoryDisplay)1 ItemDisplay (com.loohp.interactivechat.modules.ItemDisplay)1 ICPlaceholder (com.loohp.interactivechat.objectholders.ICPlaceholder)1