Search in sources :

Example 21 with Command

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

the class GetReleaseCommand method onCommand.

@Override
public void onCommand(TextChannel channel, Member m, SlashCommandEvent e) {
    String plugin = e.getOption("plugin").getAsString();
    if (plugin.equals("error"))
        return;
    if (SUPPORT_CATEGORIES.query().stream().anyMatch(c -> c.getId().equals(channel.getParent().getId()))) {
        e.reply("Getting release... please wait.").queue(q -> {
            GithubRelease release = GitHubUtil.getLatestRelease(plugin);
            if (release == null) {
                q.editOriginal("**Failed!** Could not get the release!\n\n**Possible reasons:**\n- The repo isn't valid.\n- There is no release in the repo.\n- Github is down.").queue();
            } else if (release.getFile() != null) {
                q.editOriginal(release.getFile(), plugin + ".jar").queue(msg2 -> release.getFile().delete());
                q.editOriginalEmbeds(new TechEmbedBuilder(release.getRelease().getName()).text("```" + (release.getRelease().getBody().isEmpty() ? "No changes specified." : release.getRelease().getBody().replaceAll(" \\|\\| ", "\n")) + "```").build()).queue();
            } else {
                q.editOriginal("**Failed!** Could not get the file!\n\n**Possible reasons:**\n- The developer messed up.\n- The release has no files for some reason.\n- GitHub is down.").queue();
            }
        });
    } else {
        StringBuilder channels = new StringBuilder();
        SUPPORT_CATEGORIES.query().forEach(c -> channels.append("\n - ").append(c.getAsMention()));
        e.replyEmbeds(new TechEmbedBuilder("Get Release - Error").text("You can not use this command in this channel's category.\n\n**Available Categories:**" + channels).error().build()).setEphemeral(true).queue();
    }
}
Also used : OptionType(net.dv8tion.jda.api.interactions.commands.OptionType) TechEmbedBuilder(me.TechsCode.TechDiscordBot.util.TechEmbedBuilder) TechDiscordBot(me.TechsCode.TechDiscordBot.TechDiscordBot) Category(net.dv8tion.jda.api.entities.Category) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) Member(net.dv8tion.jda.api.entities.Member) TextChannel(net.dv8tion.jda.api.entities.TextChannel) Command(net.dv8tion.jda.api.interactions.commands.Command) Collectors(java.util.stream.Collectors) SlashCommandEvent(net.dv8tion.jda.api.events.interaction.SlashCommandEvent) CommandModule(me.TechsCode.TechDiscordBot.module.CommandModule) GitHubUtil(me.TechsCode.TechDiscordBot.github.GitHubUtil) Role(net.dv8tion.jda.api.entities.Role) GithubRelease(me.TechsCode.TechDiscordBot.github.GithubRelease) CommandPrivilege(net.dv8tion.jda.api.interactions.commands.privileges.CommandPrivilege) DefinedQuery(me.TechsCode.TechDiscordBot.objects.DefinedQuery) Query(me.TechsCode.TechDiscordBot.objects.Query) TechEmbedBuilder(me.TechsCode.TechDiscordBot.util.TechEmbedBuilder) GithubRelease(me.TechsCode.TechDiscordBot.github.GithubRelease)

Example 22 with Command

use of net.dv8tion.jda.api.interactions.commands.Command in project SqueethDiscordBot by AlphaSerpentis.

the class CommandsHandler method checkAndSetSlashCommands.

public static void checkAndSetSlashCommands() {
    JDA api = Launcher.api;
    List<Command> listOfActiveCommands = api.retrieveCommands().complete();
    List<String> detectedCommandNames = new ArrayList<>();
    // Checks for the detected commands
    for (Iterator<Command> it = listOfActiveCommands.iterator(); it.hasNext(); ) {
        Command cmd = it.next();
        if (mappingOfCommands.containsKey(cmd.getName())) {
            mappingOfCommands.get(cmd.getName()).setCommandId(cmd.getIdLong());
            detectedCommandNames.add(cmd.getName());
            it.remove();
        }
    }
    // Fills in any gaps or removes any commands
    for (Command cmd : listOfActiveCommands) {
        // Removes unused commands
        System.out.println("[CommandsHandler] Removing slash command: " + cmd.getName());
        api.deleteCommandById(cmd.getId()).complete();
    }
    if (detectedCommandNames.size() < mappingOfCommands.size()) {
        // Adds new commands
        List<String> missingCommands = new ArrayList<>(mappingOfCommands.keySet());
        missingCommands.removeAll(detectedCommandNames);
        for (String cmdName : missingCommands) {
            System.out.println("[CommandsHandler] Adding new slash command: " + cmdName);
            Command cmd = api.upsertCommand(cmdName, mappingOfCommands.get(cmdName).getDescription()).complete();
            mappingOfCommands.get(cmdName).setCommandId(cmd.getIdLong());
        }
    }
}
Also used : Command(net.dv8tion.jda.api.interactions.commands.Command) JDA(net.dv8tion.jda.api.JDA) ArrayList(java.util.ArrayList)

Aggregations

Command (net.dv8tion.jda.api.interactions.commands.Command)22 ArrayList (java.util.ArrayList)8 Guild (net.dv8tion.jda.api.entities.Guild)7 List (java.util.List)6 CommandListUpdateAction (net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction)6 IOException (java.io.IOException)4 Collectors (java.util.stream.Collectors)4 JDA (net.dv8tion.jda.api.JDA)4 MessageContextInteractionEvent (net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent)4 NotNull (org.jetbrains.annotations.NotNull)4 Nullable (org.jetbrains.annotations.Nullable)4 Bean (at.xirado.bean.Bean)3 CommandFlag (at.xirado.bean.command.CommandFlag)3 GenericCommand (at.xirado.bean.command.GenericCommand)3 SlashCommand (at.xirado.bean.command.SlashCommand)3 SlashCommandContext (at.xirado.bean.command.SlashCommandContext)3 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)3 ListTag (com.denizenscript.denizencore.objects.core.ListTag)3 java.util (java.util)3 Member (net.dv8tion.jda.api.entities.Member)3