Search in sources :

Example 1 with DynamicModifiers

use of net.kodehawa.mantarobot.commands.custom.legacy.DynamicModifiers in project MantaroBot by Mantaro.

the class WelcomeUtils method sendJoinLeaveMessage.

public static void sendJoinLeaveMessage(User user, Guild guild, TextChannel tc, List<String> extraMessages, String msg, boolean test) {
    var message = msg;
    if (!extraMessages.isEmpty()) {
        if (msg != null) {
            extraMessages.add(msg);
        }
        message = extraMessages.get(RANDOM.nextInt(extraMessages.size()));
        ;
    }
    if (tc != null && message != null) {
        if (!tc.canTalk()) {
            return;
        }
        if (message.contains("$(")) {
            message = new DynamicModifiers().mapFromJoinLeave("event", tc, user, guild).resolve(message);
        }
        var modIndex = message.indexOf(':');
        if (modIndex != -1) {
            // Wonky?
            var matcher = MODIFIER_PATTERN.matcher(message);
            var modifier = "none";
            // Find the first occurrence of a modifier (word:)
            if (matcher.find()) {
                modifier = matcher.group().replace(":", "");
            }
            var json = message.substring(modIndex + 1);
            var extra = "";
            // Somehow (?) this fails sometimes? I really dunno how, but sure.
            try {
                extra = message.substring(0, modIndex - modifier.length()).trim();
            } catch (Exception ignored) {
            }
            try {
                if (modifier.equals("embed")) {
                    EmbedJSON embed;
                    try {
                        embed = JsonDataManager.fromJson('{' + json + '}', EmbedJSON.class);
                    } catch (Exception e) {
                        tc.sendMessage(EmoteReference.ERROR2 + "The string\n```json\n{" + json + "}```\n" + "Is not a valid welcome/leave message (failed to Convert to EmbedJSON). Check the wiki for more information.").queue();
                        // So I know what is going on, regardless.
                        e.printStackTrace();
                        return;
                    }
                    var builder = new MessageBuilder().setEmbeds(embed.gen(null));
                    if (!extra.isEmpty()) {
                        builder.append(extra);
                    }
                    if (test) {
                        builder.append("\n**This is a test message. No mentions will be shown or resolved.**");
                    }
                    tc.sendMessage(builder.build()).allowedMentions(test ? EnumSet.noneOf(Message.MentionType.class) : EnumSet.of(Message.MentionType.USER, Message.MentionType.ROLE)).queue(success -> {
                    }, error -> tc.sendMessage("Failed to send join/leave message.").queue());
                    return;
                }
            } catch (Exception e) {
                if (e.getMessage().toLowerCase().contains("url must be a valid")) {
                    tc.sendMessage("Failed to send join/leave message: Wrong image URL in thumbnail, image, footer and/or author.").queue();
                } else {
                    tc.sendMessage("Failed to send join/leave message: Unknown error, try checking your message.").queue();
                    e.printStackTrace();
                }
            }
        }
        if (test) {
            message += "\n**This is a test message. No mentions will be shown or resolved.**";
        }
        tc.sendMessage(message).allowedMentions(test ? EnumSet.noneOf(Message.MentionType.class) : EnumSet.of(Message.MentionType.USER, Message.MentionType.ROLE)).queue(success -> {
        }, failure -> tc.sendMessage("Failed to send join/leave message.").queue());
    }
}
Also used : EmbedJSON(net.kodehawa.mantarobot.commands.custom.EmbedJSON) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) DynamicModifiers(net.kodehawa.mantarobot.commands.custom.legacy.DynamicModifiers)

Aggregations

MessageBuilder (net.dv8tion.jda.api.MessageBuilder)1 EmbedJSON (net.kodehawa.mantarobot.commands.custom.EmbedJSON)1 DynamicModifiers (net.kodehawa.mantarobot.commands.custom.legacy.DynamicModifiers)1