Search in sources :

Example 6 with CommandData

use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project Nameless-Link by NamelessMC.

the class Command method sendCommands.

public static void sendCommands(final Guild guild) {
    final Language language = Language.getGuildLanguage(guild);
    final CommandData[] commands = new CommandData[COMMANDS.length];
    for (int i = 0; i < commands.length; i++) {
        commands[i] = COMMANDS[i].getCommandData(language);
    }
    guild.updateCommands().addCommands(commands).complete();
}
Also used : Language(com.namelessmc.bot.Language) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData)

Example 7 with CommandData

use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project Emolga by TecToast.

the class EmolgaMain method start.

public static void start() throws Exception {
    emolgajda = JDABuilder.createDefault(Command.tokens.getString("discord")).enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_PRESENCES).addEventListeners(new EmolgaListener(), messageWaiter).setMemberCachePolicy(MemberCachePolicy.ALL).build();
    emolgajda.addEventListener(new SlashListener(emolgajda));
    flegmonjda = JDABuilder.createDefault(Command.tokens.getString("discordflegmon")).enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_PRESENCES).addEventListeners(new EmolgaListener(), messageWaiter).setMemberCachePolicy(MemberCachePolicy.ALL).build();
    flegmonjda.addEventListener(new SlashListener(emolgajda));
    emolgajda.awaitReady();
    flegmonjda.awaitReady();
    setupJetty();
    Guild g = emolgajda.getGuildById(MYSERVER);
    g.updateCommands().addCommands(commands.values().stream().filter(Command::isSlash).map(c -> {
        CommandData dt = new CommandData(c.getName(), c.getHelp());
        List<ArgumentManagerTemplate.Argument> args = c.getArgumentTemplate().arguments;
        for (ArgumentManagerTemplate.Argument arg : args) {
            dt.addOption(arg.getType().asOptionType(), arg.getName().toLowerCase(), arg.getHelp(), !arg.isOptional());
        }
        return dt;
    }).toArray(CommandData[]::new)).queue();
    awaitNextDay();
    flegmonjda.getPresence().setActivity(Activity.playing("mit seiner Rute"));
    updatePresence();
    ReactionManager manager = new ReactionManager(emolgajda);
    manager.registerReaction("715249205186265178", "813025531779743774", "813025179114405898", "719928482544484352").registerReaction("715249205186265178", "813025531779743774", "813025403098628097", "813005659619590184").registerReaction("715249205186265178", "813025531779743774", "813025709232488480", "813027599743713320").registerReaction("540899923789611018", "820784528888561715", "820781668586618901", "820783085976420372").registerReaction("830146866812420116", "830391184459300915", "540969934457667613", "830392346348355594").registerReaction("827608009571958806", "884567614918111233", "884564674744561684", "884565654227812364").registerReaction("827608009571958806", "884567614918111233", "884564533295869962", "884565697479458826").registerReaction("827608009571958806", "884567614918111233", "884565288564195348", "884565609663320086").registerReaction("827608009571958806", "884567614918111233", "886748333484441650", "886746672120606771").registerReaction("827608009571958806", "884567614918111233", "886748333484441650", "886746672120606771").registerReaction("827608009571958806", "884567614918111233", "921389285188440115", "921387730200584222");
    ScheduledExecutorService giveawayscheduler = new ScheduledThreadPoolExecutor(5);
    HashMap<Long, ScheduledFuture<?>> giveawayFutures = new HashMap<>();
    DBManagers.GIVEAWAY.forAll(r -> new Giveaway(r.getLong("channelid"), r.getLong("hostid"), r.getTimestamp("end").toInstant(), r.getInt("winners"), r.getString("prize"), r.getLong("messageid")));
}
Also used : X509Certificate(java.security.cert.X509Certificate) JSONObject(org.jsolf.JSONObject) SSLContext(javax.net.ssl.SSLContext) JDA(net.dv8tion.jda.api.JDA) ScheduledFuture(java.util.concurrent.ScheduledFuture) CertificateFactory(java.security.cert.CertificateFactory) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpVersion(org.eclipse.jetty.http.HttpVersion) HashMap(java.util.HashMap) DatatypeConverter(jakarta.xml.bind.DatatypeConverter) GatewayIntent(net.dv8tion.jda.api.requests.GatewayIntent) ArrayList(java.util.ArrayList) Guild(net.dv8tion.jda.api.entities.Guild) ByteArrayInputStream(java.io.ByteArrayInputStream) JDABuilder(net.dv8tion.jda.api.JDABuilder) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Giveaway(de.tectoast.emolga.utils.Giveaway) DBManagers(de.tectoast.emolga.utils.sql.DBManagers) HttpHandler(de.tectoast.emolga.jetty.HttpHandler) MemberCachePolicy(net.dv8tion.jda.api.utils.MemberCachePolicy) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) Files(java.nio.file.Files) Activity(net.dv8tion.jda.api.entities.Activity) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) IOException(java.io.IOException) KeyStore(java.security.KeyStore) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData) CertificateException(java.security.cert.CertificateException) KeyManager(javax.net.ssl.KeyManager) File(java.io.File) KeyFactory(java.security.KeyFactory) Consumer(java.util.function.Consumer) Command(de.tectoast.emolga.commands.Command) List(java.util.List) Certificate(java.security.cert.Certificate) org.eclipse.jetty.server(org.eclipse.jetty.server) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MYSERVER(de.tectoast.emolga.utils.Constants.MYSERVER) ReactionManager(de.tectoast.toastilities.managers.ReactionManager) MessageWaiter(de.tectoast.emolga.utils.MessageWaiter) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) HashMap(java.util.HashMap) Giveaway(de.tectoast.emolga.utils.Giveaway) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Guild(net.dv8tion.jda.api.entities.Guild) ScheduledFuture(java.util.concurrent.ScheduledFuture) ReactionManager(de.tectoast.toastilities.managers.ReactionManager) Command(de.tectoast.emolga.commands.Command) ArrayList(java.util.ArrayList) List(java.util.List) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData)

Example 8 with CommandData

use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project Essentials by EssentialsX.

the class InteractionControllerImpl method processBatchRegistration.

public void processBatchRegistration() {
    if (!initialBatchRegistration && !batchRegistrationQueue.isEmpty()) {
        initialBatchRegistration = true;
        final List<CommandData> list = new ArrayList<>();
        for (final InteractionCommand command : batchRegistrationQueue.values()) {
            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());
                }
            }
            list.add(data);
        }
        jda.getGuild().updateCommands().addCommands(list).queue(success -> {
            for (final Command command : success) {
                commandMap.put(command.getName(), batchRegistrationQueue.get(command.getName()));
                batchRegistrationQueue.remove(command.getName());
                if (jda.isDebug()) {
                    logger.info("Registered guild command " + command.getName() + " with id " + command.getId());
                }
            }
            if (!batchRegistrationQueue.isEmpty()) {
                logger.warning(batchRegistrationQueue.size() + " Discord commands were lost during command registration!");
                if (jda.isDebug()) {
                    logger.warning("Lost commands: " + batchRegistrationQueue.keySet());
                }
                batchRegistrationQueue.clear();
            }
        }, 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) InteractionCommand(net.essentialsx.api.v2.services.discord.InteractionCommand) Command(net.dv8tion.jda.api.interactions.commands.Command) InteractionCommand(net.essentialsx.api.v2.services.discord.InteractionCommand) ArrayList(java.util.ArrayList) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData)

Example 9 with CommandData

use of net.dv8tion.jda.api.interactions.commands.build.CommandData 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 10 with CommandData

use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project Essentials by drtshock.

the class InteractionControllerImpl method processBatchRegistration.

public void processBatchRegistration() {
    if (!initialBatchRegistration && !batchRegistrationQueue.isEmpty()) {
        initialBatchRegistration = true;
        final List<CommandData> list = new ArrayList<>();
        for (final InteractionCommand command : batchRegistrationQueue.values()) {
            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());
                }
            }
            list.add(data);
        }
        jda.getGuild().updateCommands().addCommands(list).queue(success -> {
            for (final Command command : success) {
                commandMap.put(command.getName(), batchRegistrationQueue.get(command.getName()));
                batchRegistrationQueue.remove(command.getName());
                if (jda.isDebug()) {
                    logger.info("Registered guild command " + command.getName() + " with id " + command.getId());
                }
            }
            if (!batchRegistrationQueue.isEmpty()) {
                logger.warning(batchRegistrationQueue.size() + " Discord commands were lost during command registration!");
                if (jda.isDebug()) {
                    logger.warning("Lost commands: " + batchRegistrationQueue.keySet());
                }
                batchRegistrationQueue.clear();
            }
        }, 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) InteractionCommand(net.essentialsx.api.v2.services.discord.InteractionCommand) Command(net.dv8tion.jda.api.interactions.commands.Command) InteractionCommand(net.essentialsx.api.v2.services.discord.InteractionCommand) ArrayList(java.util.ArrayList) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData)

Aggregations

CommandData (net.dv8tion.jda.api.interactions.commands.build.CommandData)20 ArrayList (java.util.ArrayList)6 JDA (net.dv8tion.jda.api.JDA)5 SlashCommandData (net.dv8tion.jda.api.interactions.commands.build.SlashCommandData)5 Guild (net.dv8tion.jda.api.entities.Guild)4 List (java.util.List)3 ErrorResponseException (net.dv8tion.jda.api.exceptions.ErrorResponseException)3 InteractionCommandArgument (net.essentialsx.api.v2.services.discord.InteractionCommandArgument)3 BaseContextCommand (com.dynxsty.dih4jda.commands.interactions.context_command.dao.BaseContextCommand)2 Nonnull (javax.annotation.Nonnull)2 WrappedException (net.anweisen.utilities.common.collection.WrappedException)2 Command (net.dv8tion.jda.api.interactions.commands.Command)2 OptionData (net.dv8tion.jda.api.interactions.commands.build.OptionData)2 SubcommandData (net.dv8tion.jda.api.interactions.commands.build.SubcommandData)2 CommandListUpdateAction (net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction)2 InteractionCommand (net.essentialsx.api.v2.services.discord.InteractionCommand)2 Test (org.junit.jupiter.api.Test)2 IMessageContextCommand (com.dynxsty.dih4jda.commands.interactions.context_command.IMessageContextCommand)1 MessageContextInteraction (com.dynxsty.dih4jda.commands.interactions.context_command.MessageContextInteraction)1 UserContextInteraction (com.dynxsty.dih4jda.commands.interactions.context_command.UserContextInteraction)1