Search in sources :

Example 1 with Command

use of com.github.vaerys.templates.Command in project DiscordSailv2 by Vaerys-Dawn.

the class SetupHandler method handleCommand.

/**
 * Handle commands in the Setup Mode DMs.
 *
 * @param command The {@link CommandObject} associated with this setup.
 * @param args    The contents of the message sent.
 * @return false if command isn't run, otherwise true.
 */
private static boolean handleCommand(CommandObject command, String args) {
    List<Command> commands = Globals.getSetupCommands();
    IChannel currentChannel = command.channel.get();
    String commandArgs;
    for (Command c : commands) {
        if (c.isCall(args, command)) {
            commandArgs = c.getArgs(args, command);
            // log command
            MessageHandler.handleLogging(command, c, commandArgs);
            if (c.requiresArgs && (commandArgs == null || commandArgs.isEmpty())) {
                RequestHandler.sendMessage(Utility.getCommandInfo(c, command), currentChannel);
                return true;
            }
            RequestBuffer.request(() -> command.channel.get().setTypingStatus(true)).get();
            String response = c.execute(commandArgs, command);
            RequestHandler.sendMessage(response, currentChannel);
            RequestBuffer.request(() -> command.channel.get().setTypingStatus(false)).get();
            return true;
        }
    }
    return false;
}
Also used : IChannel(sx.blah.discord.handle.obj.IChannel) Command(com.github.vaerys.templates.Command)

Example 2 with Command

use of com.github.vaerys.templates.Command in project DiscordSailv2 by Vaerys-Dawn.

the class SettingsStage method stepText.

@Override
public void stepText(CommandObject command) {
    StringHandler output = new StringHandler();
    output.append("Next step is to pick which settings you want to use.\n").append("There's a lot of settings in modules, and those will be set there.\n").append("Here's a list of the settings that aren't tied to any specific module.\n");
    // Get the settings and modules.
    List<GuildToggle> globalToggles = ToggleInit.getToggles(false);
    List<GuildToggle> modules = ToggleInit.getToggles(true);
    // Sort settings
    globalToggles.sort(Comparator.comparing(GuildToggle::name));
    // Init additional vars
    List<String> enabled = new ArrayList<>();
    List<String> disabled = new ArrayList<>();
    String format = "\t> **%s** - %s\n";
    // skip debug mode after it's defaulted to off:
    if (!new DebugMode().getDefault()) {
        globalToggles.removeIf(t -> t.name() == SAILType.DEBUG_MODE);
    }
    // Filter toggles that are not part of modules.
    List<SAILType> types = new LinkedList<>();
    modules.forEach(t -> types.addAll(t.settings.stream().map(s -> s.name()).collect(Collectors.toList())));
    ListIterator iterator = globalToggles.listIterator();
    while (iterator.hasNext()) {
        GuildToggle toggle = (GuildToggle) iterator.next();
        if (types.contains(toggle.name())) {
            // Is part of a module, remove from list
            iterator.remove();
        } else {
            // is not, get extra information...
            if (toggle.enabled(command.guild.config))
                enabled.add(toggle.name().toString());
            else
                disabled.add(toggle.name().toString());
            // append to list of things.
            output.appendFormatted(format, toggle.name().toString(), toggle.shortDesc(command));
        }
    }
    // Send message
    output.append("\nYou can switch settings on and off with **" + Command.get(Toggle.class).getCommand(command) + "** and get more info on each setting with **" + Command.get(HelpSettings.class).getCommand(command) + "**.");
    // Append additional enabled/disabled state info.
    XEmbedBuilder embed = new XEmbedBuilder(command);
    embed.withTitle("Global Settings");
    embed.appendField("Enabled", "```" + Utility.listFormatter(enabled, true) + "```", false);
    embed.appendField("Disabled", "```" + Utility.listFormatter(disabled, true) + "```", false);
    RequestHandler.sendEmbedMessage(output.toString(), embed, command.channel);
}
Also used : ToggleInit(com.github.vaerys.guildtoggles.ToggleInit) XEmbedBuilder(com.github.vaerys.objects.XEmbedBuilder) java.util(java.util) CommandObject(com.github.vaerys.commands.CommandObject) HelpSettings(com.github.vaerys.commands.help.HelpSettings) DebugMode(com.github.vaerys.guildtoggles.toggles.DebugMode) RequestHandler(com.github.vaerys.handlers.RequestHandler) SAILType(com.github.vaerys.enums.SAILType) Toggle(com.github.vaerys.commands.admin.Toggle) SetupHandler(com.github.vaerys.handlers.SetupHandler) Collectors(java.util.stream.Collectors) GuildToggle(com.github.vaerys.templates.GuildToggle) StringHandler(com.github.vaerys.handlers.StringHandler) Command(com.github.vaerys.templates.Command) Utility(com.github.vaerys.main.Utility) XEmbedBuilder(com.github.vaerys.objects.XEmbedBuilder) GuildToggle(com.github.vaerys.templates.GuildToggle) DebugMode(com.github.vaerys.guildtoggles.toggles.DebugMode) StringHandler(com.github.vaerys.handlers.StringHandler) SAILType(com.github.vaerys.enums.SAILType)

Example 3 with Command

use of com.github.vaerys.templates.Command in project DiscordSailv2 by Vaerys-Dawn.

the class Utility method getUser.

public static UserObject getUser(CommandObject command, String args, boolean doContains, boolean hasProfile) {
    if (args != null && !args.isEmpty()) {
        IUser user = null;
        IUser conUser = null;
        String toTest;
        if (args.split(" ").length != 1) {
            toTest = escapeRegex(args);
        } else {
            toTest = escapeRegex(args).replace("_", "[_| ]");
        }
        List<IUser> guildUsers = command.guild.getUsers();
        guildUsers.sort(Comparator.comparing(o -> o.getRolesForGuild(command.guild.get()).size()));
        Collections.reverse(guildUsers);
        for (IUser u : guildUsers) {
            if (user != null) {
                break;
            }
            try {
                UserObject object = new UserObject(u, command.guild, true);
                if (hasProfile) {
                    ProfileObject profile = object.getProfile(command.guild);
                    if (profile == null || profile.isEmpty())
                        continue;
                }
                if ((u.getName() + "#" + u.getDiscriminator()).matches("(?i)" + toTest)) {
                    user = u;
                }
                if (u.getName().matches("(?i)" + toTest) && user == null) {
                    user = u;
                }
                String displayName = u.getDisplayName(command.guild.get());
                if (displayName.matches("(?i)" + toTest) && user == null) {
                    user = u;
                }
                if (doContains && conUser == null) {
                    if (u.getName().matches("(?i).*" + toTest + ".*")) {
                        conUser = u;
                    }
                    if (displayName.matches("(?i).*" + toTest + ".*") && conUser == null) {
                        conUser = u;
                    }
                }
            } catch (PatternSyntaxException e) {
            // continue.
            }
        }
        UserObject userObject = null;
        try {
            long uID = Long.parseLong(args);
            return new UserObject(uID, command.guild);
        } catch (NumberFormatException e) {
            if (command.message.get().getMentions().size() > 0) {
                user = command.message.get().getMentions().get(0);
            }
        }
        if (user == null && doContains) {
            user = conUser;
        }
        if (user != null) {
            userObject = new UserObject(user, command.guild);
        }
        return userObject;
    }
    return null;
}
Also used : com.github.vaerys.objects(com.github.vaerys.objects) java.util(java.util) RequestHandler(com.github.vaerys.handlers.RequestHandler) GuildObject(com.github.vaerys.masterobjects.GuildObject) ReactionEmoji(sx.blah.discord.handle.impl.obj.ReactionEmoji) URL(java.net.URL) ZonedDateTime(java.time.ZonedDateTime) ChannelSetting(com.github.vaerys.enums.ChannelSetting) LoggerFactory(org.slf4j.LoggerFactory) SAILType(com.github.vaerys.enums.SAILType) GuildHandler(com.github.vaerys.handlers.GuildHandler) StringUtils(org.apache.commons.lang3.StringUtils) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) CommandObject(com.github.vaerys.commands.CommandObject) Logger(org.slf4j.Logger) PatternSyntaxException(java.util.regex.PatternSyntaxException) MalformedURLException(java.net.MalformedURLException) EmbedObject(sx.blah.discord.api.internal.json.objects.EmbedObject) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) EmojiManager(com.vdurmont.emoji.EmojiManager) sx.blah.discord.handle.obj(sx.blah.discord.handle.obj) Stream(java.util.stream.Stream) StringHandler(com.github.vaerys.handlers.StringHandler) UserObject(com.github.vaerys.masterobjects.UserObject) Command(com.github.vaerys.templates.Command) Emoji(com.vdurmont.emoji.Emoji) Pattern(java.util.regex.Pattern) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) UserObject(com.github.vaerys.masterobjects.UserObject) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 4 with Command

use of com.github.vaerys.templates.Command in project DiscordSailv2 by Vaerys-Dawn.

the class CreatorHandler method creatorCommands.

@EventSubscriber
public void creatorCommands(MessageReceivedEvent event) {
    if (event.getAuthor().getLongID() != Globals.creatorID)
        return;
    List<Command> commands;
    CommandObject command = new CommandObject(event.getMessage());
    if (event.getChannel().isPrivate()) {
        commands = new ArrayList<>(Globals.getCreatorCommands(true));
    } else {
        commands = new ArrayList<>(Globals.getCreatorCommands(false));
    }
    String message = event.getMessage().getContent();
    for (Command c : commands) {
        if (c.isCall(message, command)) {
            String args = c.getArgs(message, command);
            RequestHandler.sendMessage(c.execute(args, command), command.channel.get());
            MessageHandler.handleLogging(command, c, args);
            return;
        }
    }
}
Also used : CommandObject(com.github.vaerys.commands.CommandObject) Command(com.github.vaerys.templates.Command) EventSubscriber(sx.blah.discord.api.events.EventSubscriber)

Example 5 with Command

use of com.github.vaerys.templates.Command in project DiscordSailv2 by Vaerys-Dawn.

the class MessageHandler method handleCommand.

// Command Handler
private boolean handleCommand(CommandObject command, String args) {
    List<Command> commands = new ArrayList<>(command.guild.commands);
    IChannel currentChannel = command.channel.get();
    String commandArgs;
    for (Command c : commands) {
        if (c.isCall(args, command)) {
            commandArgs = c.getArgs(args, command);
            // log command
            command.guild.sendDebugLog(command, "COMMAND", c.getCommand(command), c.getArgs(args, command));
            // test if user has permissions
            if (!GuildHandler.testForPerms(command, c.perms)) {
                RequestHandler.sendMessage(command.user.notAllowed, currentChannel);
                return true;
            }
            // check if it is a valid channel
            if (!currentChannel.isPrivate()) {
                if (c.channel != null && !GuildHandler.testForPerms(command, Permissions.MANAGE_CHANNELS)) {
                    List<IChannel> channels = command.guild.getChannelsByType(c.channel);
                    if (channels.size() != 0 && !channels.contains(command.channel.get())) {
                        List<String> list = Utility.getChannelMentions(command.user.getVisibleChannels(channels));
                        RequestHandler.sendMessage(Utility.getChannelMessage(list), command.channel.get());
                        return true;
                    }
                }
            }
            if (c.requiresArgs && (commandArgs == null || commandArgs.isEmpty())) {
                RequestHandler.sendMessage(Utility.getCommandInfo(c, command), currentChannel);
                return true;
            }
            // command logging
            handleLogging(command, c, commandArgs);
            RequestBuffer.request(() -> command.channel.get().setTypingStatus(true)).get();
            // if (!command.channel.getToggles().getTypingStatus()) {
            // command.channel.getToggles().toggleTypingStatus();
            // }
            String response = c.execute(commandArgs, command);
            RequestHandler.sendMessage(response, currentChannel);
            RequestBuffer.request(() -> command.channel.get().setTypingStatus(false)).get();
            return true;
        }
    }
    return false;
}
Also used : IChannel(sx.blah.discord.handle.obj.IChannel) Command(com.github.vaerys.templates.Command) ArrayList(java.util.ArrayList)

Aggregations

Command (com.github.vaerys.templates.Command)24 CommandObject (com.github.vaerys.commands.CommandObject)10 SAILType (com.github.vaerys.enums.SAILType)10 Collectors (java.util.stream.Collectors)9 ChannelSetting (com.github.vaerys.enums.ChannelSetting)8 RequestHandler (com.github.vaerys.handlers.RequestHandler)7 Utility (com.github.vaerys.main.Utility)7 XEmbedBuilder (com.github.vaerys.objects.XEmbedBuilder)7 ArrayList (java.util.ArrayList)7 Permissions (sx.blah.discord.handle.obj.Permissions)7 List (java.util.List)6 SubCommandObject (com.github.vaerys.objects.SubCommandObject)5 GuildHandler (com.github.vaerys.handlers.GuildHandler)4 UserObject (com.github.vaerys.masterobjects.UserObject)3 java.util (java.util)3 UserSetting (com.github.vaerys.enums.UserSetting)2 StringHandler (com.github.vaerys.handlers.StringHandler)2 Constants (com.github.vaerys.main.Constants)2 GuildToggle (com.github.vaerys.templates.GuildToggle)2 IChannel (sx.blah.discord.handle.obj.IChannel)2