Search in sources :

Example 1 with Command

use of ws.nmathe.saber.commands.Command in project Saber-Bot by notem.

the class InitCommand method action.

@Override
public void action(String prefix, String[] args, MessageReceivedEvent event) {
    String body;
    if (args.length > 0) {
        boolean isAChannel = false;
        TextChannel chan = null;
        String chanId = null;
        try {
            chanId = args[0].replaceAll("[^\\d]", "");
            chan = event.getGuild().getTextChannelById(chanId);
            if (chan != null)
                isAChannel = true;
        } catch (Exception ignored) {
        }
        if (// use the argument as the new channel's name
        !isAChannel) {
            String chanTitle = args[0].replaceAll("[^A-Za-z0-9_ \\-]", "").replaceAll("[ ]", "_");
            Main.getScheduleManager().createSchedule(event.getGuild().getId(), chanTitle);
            body = "A new schedule channel named **" + chanTitle.toLowerCase() + "** has been created!\n" + "You can now use the create command to create events on that schedule, or the sync command to sync " + "that schedule to a Google Calendar.";
        } else // convert the channel to a schedule
        {
            if (Main.getScheduleManager().isASchedule(chan.getId())) {
                // clear the channel of events
                TextChannel finalChan = chan;
                Main.getDBDriver().getEventCollection().find(eq("channelId", chan.getId())).forEach((Consumer<? super Document>) document -> {
                    String msgId = document.getString("messageId");
                    finalChan.deleteMessageById(msgId).complete();
                    Main.getEntryManager().removeEntry(document.getInteger("_id"));
                });
                body = "The schedule <#" + chanId + "> has been cleared!";
            } else {
                // create a new schedule
                Main.getScheduleManager().createSchedule(chan);
                body = "The channel <#" + chanId + "> has been converted to a schedule channel!\n" + "You can now use the create command to create events on that schedule, or the sync " + "command to sync that schedule to a Google Calendar.";
            }
        }
    } else // create a new schedule using the default name
    {
        Main.getScheduleManager().createSchedule(event.getGuild().getId(), null);
        body = "A new schedule channel named **new_schedule** has been created!\n" + "You can now use the create command to create events on that schedule, or the sync command" + " to sync that schedule to a Google Calendar.";
    }
    MessageUtilities.sendMsg(body, event.getChannel(), null);
}
Also used : Document(org.bson.Document) Command(ws.nmathe.saber.commands.Command) CommandInfo(ws.nmathe.saber.commands.CommandInfo) TextChannel(net.dv8tion.jda.core.entities.TextChannel) Logging(ws.nmathe.saber.utils.Logging) Channel(net.dv8tion.jda.core.entities.Channel) MessageChannel(net.dv8tion.jda.core.entities.MessageChannel) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) List(java.util.List) Permission(net.dv8tion.jda.core.Permission) MessageReceivedEvent(net.dv8tion.jda.core.events.message.MessageReceivedEvent) MessageUtilities(ws.nmathe.saber.utils.MessageUtilities) Filters.eq(com.mongodb.client.model.Filters.eq) Main(ws.nmathe.saber.Main) TextChannel(net.dv8tion.jda.core.entities.TextChannel)

Example 2 with Command

use of ws.nmathe.saber.commands.Command in project Saber-Bot by notem.

the class HelpCommand method action.

@Override
public void action(String prefix, String[] args, MessageReceivedEvent event) {
    // send the bot intro with a brief list of commands to the user
    if (args.length < 1) {
        String intro = "```diff\n- Intro```\nI am **" + Main.getShardManager().getJDA().getSelfUser().getName() + "**, the event scheduling discord bot." + " I can provide your discord with basic event schedule management.\nInvite me to your discord and create " + "a dedicated command channel named **" + Main.getBotSettingsManager().getControlChan() + "** to get started.\n\n" + "github: <https://github.com/notem/Saber-Bot>\n" + "userdocs: <https://nmathe.ws/bots/saber>\n" + "support: <https://discord.gg/ZQZnXsC>\n" + "invite: <https://discordapp.com/api/oauth2/authorize?client_id=" + Main.getShardManager().getJDA().getSelfUser().getId() + "&scope=bot&permissions=523344>\n\n";
        // generate list of commands
        String commands = "```diff\n- Command List```\n";
        commands += "**Core commands**\n================\n";
        for (Command cmd : Main.getCommandHandler().getCommands()) {
            CommandInfo info = cmd.info(prefix);
            if (info.getType() == CommandInfo.CommandType.CORE)
                commands += info.getUsage() + "\n";
        }
        commands += "\n**User commands**\n================\n";
        for (Command cmd : Main.getCommandHandler().getCommands()) {
            CommandInfo info = cmd.info(prefix);
            if (info.getType() == CommandInfo.CommandType.USER)
                commands += info.getUsage() + "\n";
        }
        commands += "\n**Google commands**\n================\n";
        for (Command cmd : Main.getCommandHandler().getCommands()) {
            CommandInfo info = cmd.info(prefix);
            if (info.getType() == CommandInfo.CommandType.GOOGLE)
                commands += info.getUsage() + "\n";
        }
        commands += "\n**Misc commands**\n================\n";
        for (Command cmd : Main.getCommandHandler().getCommands()) {
            CommandInfo info = cmd.info(prefix);
            if (info.getType() == CommandInfo.CommandType.MISC)
                commands += info.getUsage() + "\n";
        }
        String boot = "\nTo view detailed information for any of the above commands, DM me ``help command``.";
        String msg = intro + commands + boot;
        MessageUtilities.sendPrivateMsg(msg, event.getAuthor(), null);
    } else // generate help info for the requested command
    {
        Command cmd = Main.getCommandHandler().getCommand(args[0]);
        if (cmd != null) {
            CommandInfo info = cmd.info(prefix);
            String msg = "";
            if (!info.getUsageExtended().keySet().isEmpty()) {
                for (String key : info.getUsageExtended().keySet()) {
                    msg += "```diff\n" + key + "```\n" + info.getUsageExtended().get(key) + "\n\n";
                    if (msg.length() > 1000) {
                        MessageUtilities.sendPrivateMsg(msg, event.getAuthor(), null);
                        msg = "";
                    }
                }
            }
            if (!info.getUsageExamples().isEmpty()) {
                msg += "```diff\n- Examples```";
                for (String example : info.getUsageExamples()) {
                    msg += "\n``" + example + "``";
                }
            }
            MessageUtilities.sendPrivateMsg(msg, event.getAuthor(), null);
        } else {
            String msg = "There is no command called *" + args[0] + "*!";
            MessageUtilities.sendPrivateMsg(msg, event.getAuthor(), null);
        }
    }
}
Also used : CommandInfo(ws.nmathe.saber.commands.CommandInfo) Command(ws.nmathe.saber.commands.Command)

Aggregations

Command (ws.nmathe.saber.commands.Command)2 CommandInfo (ws.nmathe.saber.commands.CommandInfo)2 Filters.eq (com.mongodb.client.model.Filters.eq)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 Collectors (java.util.stream.Collectors)1 Permission (net.dv8tion.jda.core.Permission)1 Channel (net.dv8tion.jda.core.entities.Channel)1 MessageChannel (net.dv8tion.jda.core.entities.MessageChannel)1 TextChannel (net.dv8tion.jda.core.entities.TextChannel)1 MessageReceivedEvent (net.dv8tion.jda.core.events.message.MessageReceivedEvent)1 Document (org.bson.Document)1 Main (ws.nmathe.saber.Main)1 Logging (ws.nmathe.saber.utils.Logging)1 MessageUtilities (ws.nmathe.saber.utils.MessageUtilities)1