Search in sources :

Example 1 with CustomCommand

use of ml.duncte123.skybot.objects.command.custom.CustomCommand in project SkyBot by duncte123.

the class CommandManager method removeCustomCommand.

public boolean removeCustomCommand(String name, long guildId, boolean updateDB) {
    final CustomCommand cmd = getCustomCommand(name, guildId);
    if (cmd == null) {
        return false;
    }
    if (!updateDB) {
        this.customCommands.remove(cmd);
        return true;
    }
    try {
        final CompletableFuture<Boolean> future = new CompletableFuture<>();
        this.variables.getDatabaseAdapter().deleteCustomCommand(guildId, name, future::complete);
        final boolean result = future.get();
        if (result) {
            this.customCommands.remove(cmd);
        }
        return result;
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
        return false;
    }
}
Also used : CustomCommand(ml.duncte123.skybot.objects.command.custom.CustomCommand)

Example 2 with CustomCommand

use of ml.duncte123.skybot.objects.command.custom.CustomCommand in project SkyBot by duncte123.

the class MessageListener method handleMessageEventChecked.

private void handleMessageEventChecked(String raw, Guild guild, GuildMessageReceivedEvent event) {
    final GuildSetting settings = GuildSettingsUtils.getGuild(guild.getIdLong(), this.variables);
    final String customPrefix = settings.getCustomPrefix();
    final Message message = event.getMessage();
    if (settings.isMessageLogging()) {
        final MessageData data = MessageData.from(message);
        this.redis.storeMessage(data, isGuildPatron(guild));
    }
    if (!commandManager.isCommand(customPrefix, raw) && doAutoModChecks(event, settings, raw)) {
        return;
    }
    final User selfUser = event.getJDA().getSelfUser();
    final long selfId = selfUser.getIdLong();
    final String selfRegex = "<@!?" + selfId + '>';
    if (raw.matches(selfRegex)) {
        sendMsg(new MessageConfig.Builder().setChannel(event.getChannel()).setMessageFormat("Hey %s, try `%shelp` for a list of commands. If it doesn't work scream at _duncte123#1245_", event.getAuthor(), customPrefix).build());
        return;
    }
    final String[] split = raw.replaceFirst(Pattern.quote(Settings.PREFIX), "").split("\\s+");
    final List<CustomCommand> autoResponses = commandManager.getAutoResponses(guild.getIdLong());
    if (!autoResponses.isEmpty() && invokeAutoResponse(autoResponses, split, event)) {
        return;
    }
    if (doesNotStartWithPrefix(selfId, raw, customPrefix) || !canRunCommands(raw, customPrefix, event)) {
        return;
    }
    if (raw.matches(selfRegex + "(.*)")) {
        // Handle the chat command
        Objects.requireNonNull(commandManager.getCommand("chat")).executeCommand(new CommandContext("chat", Arrays.asList(split).subList(1, split.length), event, variables));
    } else {
        // Handle the command
        commandManager.runCommand(event, customPrefix);
    }
}
Also used : UnknownUser(ml.duncte123.skybot.objects.user.UnknownUser) CustomCommand(ml.duncte123.skybot.objects.command.custom.CustomCommand) CommandContext(ml.duncte123.skybot.objects.command.CommandContext) MessageData(ml.duncte123.skybot.objects.discord.MessageData) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) GuildSetting(com.dunctebot.models.settings.GuildSetting)

Example 3 with CustomCommand

use of ml.duncte123.skybot.objects.command.custom.CustomCommand in project SkyBot by DuncteBot.

the class CommandManager method runCustomCommand.

private void runCustomCommand(ICommand cmd, String invoke, List<String> args, GuildMessageReceivedEvent event) {
    final CustomCommand cusomCommand = (CustomCommand) cmd;
    if (cusomCommand.getGuildId() != event.getGuild().getIdLong()) {
        return;
    }
    try {
        MDC.put("command.custom.message", cusomCommand.getMessage());
        final Parser parser = CommandUtils.getParser(new CommandContext(invoke, args, event, variables));
        final String message = parser.parse(cusomCommand.getMessage());
        final MessageConfig.Builder messageBuilder = MessageConfig.Builder.fromEvent(event);
        final DataObject object = parser.get("embed");
        boolean hasContent = false;
        if (!message.isEmpty()) {
            messageBuilder.setMessage("\u200B" + message);
            hasContent = true;
        }
        if (object != null) {
            final JDAImpl jda = (JDAImpl) event.getJDA();
            final EmbedBuilder embed = new EmbedBuilder(jda.getEntityBuilder().createMessageEmbed(object));
            messageBuilder.addEmbed(true, embed);
            hasContent = true;
        }
        if (hasContent) {
            sendMsg(messageBuilder.build());
        }
        parser.clear();
    } catch (Exception e) {
        sendMsg(MessageConfig.Builder.fromEvent(event).setMessage("Error with parsing custom command: " + e.getMessage()).build());
        Sentry.captureException(e);
    }
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) CustomCommand(ml.duncte123.skybot.objects.command.custom.CustomCommand) MessageConfig(me.duncte123.botcommons.messaging.MessageConfig) DataObject(net.dv8tion.jda.api.utils.data.DataObject) CommandContext(ml.duncte123.skybot.objects.command.CommandContext) JDAImpl(net.dv8tion.jda.internal.JDAImpl) Parser(com.jagrosh.jagtag.Parser)

Example 4 with CustomCommand

use of ml.duncte123.skybot.objects.command.custom.CustomCommand in project SkyBot by DuncteBot.

the class CommandManager method removeCustomCommand.

public boolean removeCustomCommand(String name, long guildId, boolean updateDB) {
    final CustomCommand cmd = getCustomCommand(name, guildId);
    if (cmd == null) {
        return false;
    }
    if (!updateDB) {
        this.customCommands.remove(cmd);
        return true;
    }
    try {
        final CompletableFuture<Boolean> future = new CompletableFuture<>();
        this.variables.getDatabaseAdapter().deleteCustomCommand(guildId, name, future::complete);
        final boolean result = future.get();
        if (result) {
            this.customCommands.remove(cmd);
        }
        return result;
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
        return false;
    }
}
Also used : CustomCommand(ml.duncte123.skybot.objects.command.custom.CustomCommand)

Example 5 with CustomCommand

use of ml.duncte123.skybot.objects.command.custom.CustomCommand in project SkyBot by DuncteBot.

the class MessageListener method invokeAutoResponse.

private boolean invokeAutoResponse(List<CustomCommand> autoResponses, String[] split, GuildMessageReceivedEvent event) {
    final String stripped = event.getMessage().getContentStripped().toLowerCase();
    final Optional<CustomCommand> match = autoResponses.stream().filter((cmd) -> stripped.contains(cmd.getName().toLowerCase())).findFirst();
    if (match.isPresent()) {
        final CustomCommand cmd = match.get();
        commandManager.dispatchCommand(cmd, "", Arrays.asList(split).subList(1, split.length), event);
        return true;
    }
    return false;
}
Also used : MessageData(ml.duncte123.skybot.objects.discord.MessageData) MessageBulkDeleteEvent(net.dv8tion.jda.api.events.message.MessageBulkDeleteEvent) EmbedUtils(me.duncte123.botcommons.messaging.EmbedUtils) StringUtils(me.duncte123.botcommons.StringUtils) Permission(net.dv8tion.jda.api.Permission) CommandCategory(ml.duncte123.skybot.objects.command.CommandCategory) PerspectiveApi(ml.duncte123.skybot.utils.PerspectiveApi) UNKNOWN_MESSAGE(net.dv8tion.jda.api.requests.ErrorResponse.UNKNOWN_MESSAGE) SpamFilter(ml.duncte123.skybot.utils.SpamFilter) Matcher(java.util.regex.Matcher) GuildSettingsUtils(ml.duncte123.skybot.utils.GuildSettingsUtils) GuildMessageReceivedEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent) GuildSetting(com.dunctebot.models.settings.GuildSetting) GuildMessageUpdateEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageUpdateEvent) Variables(ml.duncte123.skybot.Variables) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) UnknownUser(ml.duncte123.skybot.objects.user.UnknownUser) Pattern(java.util.regex.Pattern) MISSING_PERMISSIONS(net.dv8tion.jda.api.requests.ErrorResponse.MISSING_PERMISSIONS) ICommand(ml.duncte123.skybot.objects.command.ICommand) net.dv8tion.jda.api.entities(net.dv8tion.jda.api.entities) BotCommons(me.duncte123.botcommons.BotCommons) java.util(java.util) JDA(net.dv8tion.jda.api.JDA) MarkdownSanitizer(net.dv8tion.jda.api.utils.MarkdownSanitizer) CustomCommand(ml.duncte123.skybot.objects.command.custom.CustomCommand) GuildMessageDeleteEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageDeleteEvent) MessageUtils(me.duncte123.botcommons.messaging.MessageUtils) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Sentry(io.sentry.Sentry) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) ErrorHandler(net.dv8tion.jda.api.exceptions.ErrorHandler) GenericGuildMessageEvent(net.dv8tion.jda.api.events.message.guild.GenericGuildMessageEvent) CommandUtils.isGuildPatron(ml.duncte123.skybot.utils.CommandUtils.isGuildPatron) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) MessageUtils.sendMsg(me.duncte123.botcommons.messaging.MessageUtils.sendMsg) CommandContext(ml.duncte123.skybot.objects.command.CommandContext) CommandUtils.isDev(ml.duncte123.skybot.utils.CommandUtils.isDev) ModerationUtils.modLog(ml.duncte123.skybot.utils.ModerationUtils.modLog) CommandManager(ml.duncte123.skybot.CommandManager) DunctebotGuild(ml.duncte123.skybot.entities.jda.DunctebotGuild) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) AirUtils.setJDAContext(ml.duncte123.skybot.utils.AirUtils.setJDAContext) Triple(kotlin.Triple) Settings(ml.duncte123.skybot.Settings) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) MessageConfig(me.duncte123.botcommons.messaging.MessageConfig) RedisConnection(ml.duncte123.skybot.database.RedisConnection) DateTimeFormatter(java.time.format.DateTimeFormatter) CustomCommand(ml.duncte123.skybot.objects.command.custom.CustomCommand)

Aggregations

CustomCommand (ml.duncte123.skybot.objects.command.custom.CustomCommand)8 CommandContext (ml.duncte123.skybot.objects.command.CommandContext)6 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)6 GuildSetting (com.dunctebot.models.settings.GuildSetting)4 MessageConfig (me.duncte123.botcommons.messaging.MessageConfig)4 MessageData (ml.duncte123.skybot.objects.discord.MessageData)4 UnknownUser (ml.duncte123.skybot.objects.user.UnknownUser)4 Parser (com.jagrosh.jagtag.Parser)2 Sentry (io.sentry.Sentry)2 Instant (java.time.Instant)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 java.util (java.util)2 Executors (java.util.concurrent.Executors)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 TimeUnit (java.util.concurrent.TimeUnit)2 Consumer (java.util.function.Consumer)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 Collectors (java.util.stream.Collectors)2 Nonnull (javax.annotation.Nonnull)2