Search in sources :

Example 16 with CommandData

use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project DIH4JDA by DynxstyGIT.

the class InteractionHandler method getGuildContextCommandData.

/**
 * Gets all Guild Context commands registered in {@link InteractionHandler#registerContextCommands()} and
 * returns their {@link CommandData} as a List.
 *
 * @param guild The context command's guild.
 * @throws Exception If an error occurs.
 */
private Set<CommandData> getGuildContextCommandData(@NotNull Guild guild) throws Exception {
    Set<CommandData> commands = new HashSet<>();
    for (Class<? extends BaseContextCommand> contextCommandClass : this.guildContexts) {
        BaseContextCommand instance = (BaseContextCommand) this.getClassInstance(guild, contextCommandClass);
        commands.add(this.getContextCommandData(instance, contextCommandClass));
    }
    DIH4JDALogger.info(String.format("[%s] Queuing Guild Context Commands", guild.getName()), DIH4JDALogger.Type.CONTEXT_COMMANDS_QUEUED);
    return commands;
}
Also used : BaseContextCommand(com.dynxsty.dih4jda.commands.interactions.context_command.dao.BaseContextCommand) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData) SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData)

Example 17 with CommandData

use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project cds by iamysko.

the class AppConfig method jda.

/**
 * Jda.
 *
 * @param discordUpListener   the discord up listener
 * @param discordDownListener the discord down listener
 * @param reactionListener    the reaction listener
 * @param messageListener     the message listener
 * @param updateRoleListener  the role Listener
 * @param slashCommandListener the slashCommand Listener
 * @param buttonClickListener the buttonClick Listener
 * @param jdaToken            the jda token
 * @return the jda
 */
@Bean
JDA jda(@Qualifier("discordUpListener") final DiscordUpListener discordUpListener, @Qualifier("discordDownListener") final DiscordDownListener discordDownListener, @Qualifier("reactionListener") final ReactionListener reactionListener, @Qualifier("messageListener") final MessageListener messageListener, @Qualifier("slashCommandListener") final SlashCommandListener slashCommandListener, @Qualifier("buttonClickListener") final ButtonClickListener buttonClickListener, @Qualifier("updateRoleListener") final UpdateRoleListener updateRoleListener, @Value("${jda.token}") final String jdaToken) {
    final JDABuilder builder = JDABuilder.createDefault(jdaToken);
    builder.addEventListeners(discordUpListener, discordDownListener, reactionListener, messageListener, slashCommandListener, buttonClickListener, updateRoleListener);
    builder.setActivity(Activity.watching("the Roblox Discord"));
    builder.enableIntents(GatewayIntent.GUILD_MEMBERS);
    builder.setMemberCachePolicy(MemberCachePolicy.ALL);
    builder.disableCache(CacheFlag.ACTIVITY, CacheFlag.CLIENT_STATUS, CacheFlag.VOICE_STATE, CacheFlag.MEMBER_OVERRIDES);
    builder.setChunkingFilter(ChunkingFilter.NONE);
    builder.disableIntents(GatewayIntent.GUILD_PRESENCES, GatewayIntent.GUILD_MESSAGE_TYPING);
    builder.setLargeThreshold(50);
    try {
        final JDA jda = builder.build();
        jda.awaitReady();
        jda.getGuildById(Properties.GUILD_ROBLOX_DISCORD_ID).updateCommands().addCommands(new CommandData(SlashCommandConstants.COMMAND_HELP, "Shows all current commands")).addCommands(new CommandData(SlashCommandConstants.COMMAND_ABOUT, "About the bot")).addCommands(new CommandData(SlashCommandConstants.COMMAND_USER_INFO, "Shows information about the user").addOption(OptionType.USER, "user", "The user to see the info", true)).addCommands(new CommandData(SlashCommandConstants.COMMAND_ROBLOX_USER_INFO, "Shows information about the Roblox user").addOption(OptionType.STRING, "username", "The username to see the info", true)).queue();
        return jda;
    } catch (final LoginException e) {
        TelegramService.sendToTelegram(Instant.now(), TelegramService.ERROR_UNKNOWN);
    } catch (final InterruptedException e) {
        TelegramService.sendToTelegram(Instant.now(), TelegramService.ERROR_WAIT_JDA);
    }
    return null;
}
Also used : JDABuilder(net.dv8tion.jda.api.JDABuilder) JDA(net.dv8tion.jda.api.JDA) LoginException(javax.security.auth.login.LoginException) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData) Bean(org.springframework.context.annotation.Bean)

Example 18 with CommandData

use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project Discord-Core-Bot-Apple by demodude4u.

the class DiscordBot method buildUpdateCommands.

// Hold my beer
@SuppressWarnings("unchecked")
private void buildUpdateCommands(CommandListUpdateAction updateCommands) {
    Map<String, Object> root = new LinkedHashMap<>();
    for (SlashCommandDefinition command : commandSlash.values()) {
        String[] pathSplit = command.getPath().split("/");
        Map<String, Object> group = root;
        for (int i = 0; i < pathSplit.length; i++) {
            String name = pathSplit[i];
            if (i == pathSplit.length - 1) {
                group.put(name, command);
            } else {
                Object subGroup = group.get(name);
                if (subGroup == null) {
                    group.put(name, subGroup = new LinkedHashMap<String, Object>());
                }
                group = (Map<String, Object>) subGroup;
            }
        }
    }
    for (Entry<String, Object> rootEntry : root.entrySet()) {
        SlashCommandData commandData;
        if (rootEntry.getValue() instanceof SlashCommandDefinition) {
            SlashCommandDefinition commandDefinition = (SlashCommandDefinition) rootEntry.getValue();
            commandData = Commands.slash(rootEntry.getKey(), commandDefinition.getDescription());
            for (SlashCommandOptionDefinition option : commandDefinition.getOptions()) {
                commandData = commandData.addOption(option.getType(), option.getName(), option.getDescription(), option.isRequired(), option.isAutoComplete());
            }
        } else {
            Map<String, Object> sub = (Map<String, Object>) rootEntry.getValue();
            commandData = Commands.slash(rootEntry.getKey(), sub.keySet().stream().collect(Collectors.joining(", ")));
            for (Entry<String, Object> subEntry : sub.entrySet()) {
                if (subEntry.getValue() instanceof SlashCommandDefinition) {
                    SlashCommandDefinition commandDefinition = (SlashCommandDefinition) subEntry.getValue();
                    SubcommandData subcommandData = new SubcommandData(subEntry.getKey(), commandDefinition.getDescription());
                    for (SlashCommandOptionDefinition option : commandDefinition.getOptions()) {
                        subcommandData = subcommandData.addOption(option.getType(), option.getName(), option.getDescription(), option.isRequired(), option.isAutoComplete());
                    }
                    commandData = commandData.addSubcommands(subcommandData);
                } else {
                    Map<String, SlashCommandDefinition> subSub = (Map<String, SlashCommandDefinition>) subEntry.getValue();
                    SubcommandGroupData subcommandGroupData = new SubcommandGroupData(subEntry.getKey(), subSub.keySet().stream().collect(Collectors.joining(", ")));
                    for (Entry<String, SlashCommandDefinition> subSubEntry : subSub.entrySet()) {
                        SubcommandData subcommandData = new SubcommandData(subSubEntry.getKey(), subSubEntry.getValue().getDescription());
                        for (SlashCommandOptionDefinition option : subSubEntry.getValue().getOptions()) {
                            subcommandData = subcommandData.addOption(option.getType(), option.getName(), option.getDescription(), option.isRequired(), option.isAutoComplete());
                        }
                        subcommandGroupData = subcommandGroupData.addSubcommands(subcommandData);
                    }
                    commandData = commandData.addSubcommandGroups(subcommandGroupData);
                }
            }
        }
        updateCommands = updateCommands.addCommands(commandData);
    }
    for (MessageCommandDefinition commandDefinition : commandMessage.values()) {
        CommandData commandData = Commands.message(commandDefinition.getName());
        updateCommands = updateCommands.addCommands(commandData);
    }
}
Also used : SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData) LinkedHashMap(java.util.LinkedHashMap) JSONObject(org.json.JSONObject) SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) SubcommandGroupData(net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData)

Example 19 with CommandData

use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project TAS-Battle by MCPfannkuchenYT.

the class TASDiscordBot method run.

@Override
public void run() {
    for (Guild g : jda.getGuilds()) {
        CommandListUpdateAction updater = g.updateCommands();
        updater.addCommands(new CommandData("play", "Show other players that you are ready to play!"));
        updater.addCommands(new CommandData("online", "Show all players that are online on the server"));
        updater.addCommands(new CommandData("link", "Link your Discord account to your Minecraft Account"));
        updater.queue();
    }
    recreateMessage();
}
Also used : CommandListUpdateAction(net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction) Guild(net.dv8tion.jda.api.entities.Guild) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData)

Example 20 with CommandData

use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project TechDiscordBot by TechsCode-Team.

the class ModulesManager method load.

public void load() {
    TechDiscordBot.getJDA().updateCommands().queue();
    CommandListUpdateAction commands = TechDiscordBot.getGuild().updateCommands();
    for (Class<?> each : ProjectUtil.getClasses("me.TechsCode.TechDiscordBot.module")) {
        if (CommandModule.class.isAssignableFrom(each) && !Modifier.isAbstract(each.getModifiers())) {
            try {
                CommandModule module = (CommandModule) each.getConstructor(TechDiscordBot.class).newInstance(TechDiscordBot.getBot());
                if (module.getName() == null)
                    continue;
                cmdModules.add(module);
                CommandData cmdData = new CommandData(module.getName(), module.getDescription() == null ? "No description set." : module.getDescription()).addOptions(module.getOptions()).setDefaultEnabled(module.getCommandPrivileges().length == 0);
                commands.addCommands(cmdData);
            } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                e.printStackTrace();
            }
        } else if (Module.class.isAssignableFrom(each) && !Modifier.isAbstract(each.getModifiers())) {
            try {
                Module module = (Module) each.getConstructor(TechDiscordBot.class).newInstance(TechDiscordBot.getBot());
                module.enable();
                modules.add(module);
            } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
    }
    commands.addCommands(new CommandData("ticket", "Manage tickets.").addSubcommands(new SubcommandData("add", "Add a member to a ticket.").addOptions(new OptionData(OptionType.USER, "member", "Member to add.", true)), new SubcommandData("remove", "Remove a member from a ticket.").addOptions(new OptionData(OptionType.USER, "member", "Member to remove.", true)), new SubcommandData("transcript", "Force make a ticket transcript."), new SubcommandData("close", "Close a ticket.").addOptions(new OptionData(OptionType.STRING, "reason", "Reason to close the ticket. (Optional)")))).queue(cmds -> {
        cmds.forEach(command -> {
            CommandPrivilege[] privilege = cmdModules.stream().filter(c -> c.getName().equals(command.getName())).map(CommandModule::getCommandPrivileges).findFirst().orElse(new CommandPrivilege[] {});
            if (privilege.length > 0)
                TechDiscordBot.getGuild().updateCommandPrivilegesById(command.getId(), Arrays.asList(privilege)).queue();
        });
    });
    TechDiscordBot.getJDA().addEventListener(modules.toArray());
    TechDiscordBot.getJDA().addEventListener(cmdModules.toArray());
    Runtime.getRuntime().addShutdownHook(new Thread(() -> modules.forEach(Module::onDisable)));
}
Also used : OptionType(net.dv8tion.jda.api.interactions.commands.OptionType) Arrays(java.util.Arrays) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) TechDiscordBot(me.TechsCode.TechDiscordBot.TechDiscordBot) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) SlashCommandEvent(net.dv8tion.jda.api.events.interaction.SlashCommandEvent) SubscribeEvent(net.dv8tion.jda.api.hooks.SubscribeEvent) List(java.util.List) Modifier(java.lang.reflect.Modifier) ProjectUtil(me.TechsCode.TechDiscordBot.util.ProjectUtil) CommandPrivilege(net.dv8tion.jda.api.interactions.commands.privileges.CommandPrivilege) CommandListUpdateAction(net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction) Cooldown(me.TechsCode.TechDiscordBot.objects.Cooldown) TechDiscordBot(me.TechsCode.TechDiscordBot.TechDiscordBot) CommandListUpdateAction(net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction) InvocationTargetException(java.lang.reflect.InvocationTargetException) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData) CommandPrivilege(net.dv8tion.jda.api.interactions.commands.privileges.CommandPrivilege) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData)

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