Search in sources :

Example 1 with CommandCreateAction

use of net.dv8tion.jda.api.requests.restaction.CommandCreateAction in project JDACommand by Badbird-5907.

the class JDACommand method registerCommand.

private void registerCommand(Command command, String name, Method method, Object o) {
    try {
        out.println("Registering command: " + name);
        if (command.disable() || method.isAnnotationPresent(Disable.class))
            return;
        if (!COMMAND_REGEX.matcher(name.toLowerCase()).matches()) {
            throw new IllegalArgumentException("Command name must match regex: " + COMMAND_REGEX.pattern() + " see https://discord.com/developers/docs/interactions/application-commands for more info");
        }
        if (commandMap.containsKey(name) || name.isEmpty()) {
            return;
        }
        List<CommandCreateAction> actions = new ArrayList<>();
        for (Guild guild : jda.getGuilds()) {
            boolean upsert = returnCallBack == null || returnCallBack.shouldUpsertCommand(guild);
            if (upsert) {
                actions.add(guild.upsertCommand(name.toLowerCase(), command.description()));
            }
        }
        Parameter[] params = method.getParameters();
        List<Pair<ParameterContext, Provider<?>>> parameters = new ArrayList<>();
        for (int i = 0; i < params.length; i++) {
            Parameter param = params[i];
            Provider<?> provider = getProvider(param);
            if (provider == null) {
                throw new IllegalArgumentException("Could not find a Parameter Provider for " + param.getType().getName() + " in " + method.getName());
            }
            ParameterContext pCtx = new ParameterContext(params, i, param, param.getDeclaredAnnotations());
            if (provider.getOptionData(pCtx) != null) {
                for (CommandCreateAction action : actions) {
                    OptionData option = provider.getOptionData(pCtx);
                    if (!option.isRequired() && pCtx.isRequired())
                        option.setRequired(true);
                    action.addOptions(option);
                }
            }
            parameters.add(new Pair<>(pCtx, provider));
        }
        CommandWrapper wrapper = new CommandWrapper(command, name.toLowerCase(), method, o, params, parameters);
        for (CommandCreateAction action : actions) {
            action.queue((c) -> {
                System.out.println("Registered command " + c);
                commandMap.put(c.getName(), wrapper);
            });
        }
        System.out.println("Done Registering command " + name);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : CommandCreateAction(net.dv8tion.jda.api.requests.restaction.CommandCreateAction) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) CommandWrapper(net.badbird5907.jdacommand.util.object.CommandWrapper) Guild(net.dv8tion.jda.api.entities.Guild) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) Parameter(java.lang.reflect.Parameter) ParameterContext(net.badbird5907.jdacommand.context.ParameterContext) Disable(net.badbird5907.jdacommand.annotation.Disable) Pair(net.badbird5907.jdacommand.util.object.Pair)

Example 2 with CommandCreateAction

use of net.dv8tion.jda.api.requests.restaction.CommandCreateAction in project dDiscordBot by DenizenScript.

the class DiscordCommandCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    DiscordBotTag bot = scriptEntry.requiredArgForPrefix("id", DiscordBotTag.class);
    ElementTag commandInstruction = scriptEntry.getElement("instruction");
    DiscordGroupTag group = scriptEntry.getObjectTag("group");
    ElementTag name = scriptEntry.getElement("name");
    ElementTag description = scriptEntry.getElement("description");
    MapTag options = scriptEntry.getObjectTag("options");
    ElementTag enabled = scriptEntry.getElement("enabled");
    ListTag enableFor = scriptEntry.getObjectTag("enable_for");
    ListTag disableFor = scriptEntry.getObjectTag("disable_for");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), bot, commandInstruction, group, name, description, options, enabled, enableFor, disableFor);
    }
    if (group != null && group.bot == null) {
        group.bot = bot.bot;
    }
    JDA client = bot.getConnection().client;
    Bukkit.getScheduler().runTaskAsynchronously(DenizenDiscordBot.instance, () -> {
        try {
            if (commandInstruction == null) {
                Debug.echoError(scriptEntry, "Must have a command instruction!");
                scriptEntry.setFinished(true);
                return;
            } else if (name == null) {
                Debug.echoError(scriptEntry, "Must specify a name!");
                scriptEntry.setFinished(true);
                return;
            }
            DiscordCommandInstruction commandInstructionEnum = DiscordCommandInstruction.valueOf(commandInstruction.asString().toUpperCase());
            boolean isEnabled = enabled == null || enabled.asBoolean();
            switch(commandInstructionEnum) {
                case CREATE:
                    {
                        if (description == null) {
                            Debug.echoError(scriptEntry, "Must specify a description!");
                            scriptEntry.setFinished(true);
                            return;
                        }
                        SlashCommandData data = Commands.slash(name.asString(), description.asString());
                        if (options != null) {
                            for (ObjectTag optionObj : options.map.values()) {
                                MapTag option = optionObj.asType(MapTag.class, scriptEntry.getContext());
                                ElementTag typeStr = (ElementTag) option.getObject("type");
                                if (typeStr == null) {
                                    Debug.echoError(scriptEntry, "Command options must specify a type!");
                                    scriptEntry.setFinished(true);
                                    return;
                                }
                                OptionType optionType = OptionType.valueOf(typeStr.toString().toUpperCase());
                                ElementTag optionName = (ElementTag) option.getObject("name");
                                ElementTag optionDescription = (ElementTag) option.getObject("description");
                                ElementTag optionIsRequired = (ElementTag) option.getObject("required");
                                MapTag optionChoices = (MapTag) option.getObject("choices");
                                if (optionName == null) {
                                    Debug.echoError(scriptEntry, "Command options must specify a name!");
                                    scriptEntry.setFinished(true);
                                    return;
                                } else if (optionDescription == null) {
                                    Debug.echoError(scriptEntry, "Command options must specify a description!");
                                    scriptEntry.setFinished(true);
                                    return;
                                }
                                if (optionType == OptionType.SUB_COMMAND) {
                                    data.addSubcommands(new SubcommandData(optionName.asString(), optionDescription.asString()));
                                } else /*
                                        support these later
                                        needs recursive logic

                                        else if (optionType == OptionType.SUB_COMMAND_GROUP) {
                                            data.addSubcommandGroups(new SubcommandGroupData(optionName.asString(), optionDescription.asString()));
                                        }
                                        */
                                {
                                    OptionData optionData = new OptionData(optionType, optionName.asString(), optionDescription.asString(), optionIsRequired == null ? true : optionIsRequired.asBoolean());
                                    if (optionChoices != null) {
                                        if (optionType != OptionType.STRING && optionType != OptionType.INTEGER) {
                                            Debug.echoError(scriptEntry, "Command options with choices must be either STRING or INTEGER!");
                                            scriptEntry.setFinished(true);
                                            return;
                                        }
                                        for (Map.Entry<StringHolder, ObjectTag> subChoiceValue : optionChoices.map.entrySet()) {
                                            MapTag choice = subChoiceValue.getValue().asType(MapTag.class, scriptEntry.getContext());
                                            ElementTag choiceName = (ElementTag) choice.getObject("name");
                                            ElementTag choiceValue = (ElementTag) choice.getObject("value");
                                            if (choiceName == null) {
                                                Debug.echoError(scriptEntry, "Command option choices must specify a name!");
                                                scriptEntry.setFinished(true);
                                                return;
                                            } else if (choiceValue == null) {
                                                Debug.echoError(scriptEntry, "Command option choices must specify a value!");
                                                scriptEntry.setFinished(true);
                                                return;
                                            }
                                            if (optionType == OptionType.STRING) {
                                                optionData.addChoice(choiceName.asString(), choiceValue.asString());
                                            } else {
                                                optionData.addChoice(choiceName.asString(), choiceValue.asInt());
                                            }
                                        }
                                    }
                                    data.addOptions(optionData);
                                }
                            }
                        }
                        CommandCreateAction action;
                        if (group == null) {
                            Debug.log("Registering a slash command globally may take up to an hour.");
                            action = (CommandCreateAction) client.upsertCommand(data);
                        } else {
                            action = (CommandCreateAction) group.getGuild().upsertCommand(data);
                        }
                        action.setDefaultEnabled(isEnabled);
                        Command slashCommand = action.complete();
                        scriptEntry.addObject("command", new DiscordCommandTag(bot.bot, group == null ? null : group.getGuild(), slashCommand));
                        break;
                    }
                case PERMS:
                    {
                        if (enableFor == null && disableFor == null) {
                            Debug.echoError(scriptEntry, "Must specify privileges!");
                            scriptEntry.setFinished(true);
                            return;
                        } else if (group == null) {
                            Debug.echoError(scriptEntry, "Must specify a group!");
                            scriptEntry.setFinished(true);
                            return;
                        }
                        Command bestMatch = matchCommandByName(scriptEntry, name, client, group);
                        if (bestMatch == null) {
                            return;
                        }
                        List<CommandPrivilege> privileges = new ArrayList<>();
                        if (enableFor != null) {
                            addPrivileges(scriptEntry, true, enableFor, privileges);
                        }
                        if (disableFor != null) {
                            addPrivileges(scriptEntry, false, disableFor, privileges);
                        }
                        bestMatch.editCommand().setDefaultEnabled(isEnabled).complete();
                        group.guild.updateCommandPrivilegesById(bestMatch.getIdLong(), privileges).complete();
                        break;
                    }
                case DELETE:
                    {
                        Command bestMatch = matchCommandByName(scriptEntry, name, client, group);
                        if (bestMatch == null) {
                            return;
                        }
                        if (group == null) {
                            client.deleteCommandById(bestMatch.getIdLong()).complete();
                        } else {
                            group.getGuild().deleteCommandById(bestMatch.getIdLong()).complete();
                        }
                        break;
                    }
            }
        } catch (Exception e) {
            Debug.echoError(e);
        }
        scriptEntry.setFinished(true);
    });
}
Also used : JDA(net.dv8tion.jda.api.JDA) CommandCreateAction(net.dv8tion.jda.api.requests.restaction.CommandCreateAction) ListTag(com.denizenscript.denizencore.objects.core.ListTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) Command(net.dv8tion.jda.api.interactions.commands.Command) ArrayList(java.util.ArrayList) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) OptionType(net.dv8tion.jda.api.interactions.commands.OptionType)

Aggregations

CommandCreateAction (net.dv8tion.jda.api.requests.restaction.CommandCreateAction)2 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)1 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)1 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)1 ListTag (com.denizenscript.denizencore.objects.core.ListTag)1 MapTag (com.denizenscript.denizencore.objects.core.MapTag)1 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)1 Parameter (java.lang.reflect.Parameter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Disable (net.badbird5907.jdacommand.annotation.Disable)1 ParameterContext (net.badbird5907.jdacommand.context.ParameterContext)1 CommandWrapper (net.badbird5907.jdacommand.util.object.CommandWrapper)1 Pair (net.badbird5907.jdacommand.util.object.Pair)1 JDA (net.dv8tion.jda.api.JDA)1 Guild (net.dv8tion.jda.api.entities.Guild)1 Command (net.dv8tion.jda.api.interactions.commands.Command)1 OptionType (net.dv8tion.jda.api.interactions.commands.OptionType)1 OptionData (net.dv8tion.jda.api.interactions.commands.build.OptionData)1