0
import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.hooks.ListenerAdapter;

import java.awt.*;
import java.util.Random;

public class GuildMemberJoinEvent extends ListenerAdapter {

    String[] messages = {
            "[member] joined. You must construct additional pylons.",
            "Never gonna give [member] up. Never let [member] down!",
            "Hey! Listen! [member] has joined!",
            "Ha! [member] has joined! You activated my trap card!",
            "We've been expecting you, [member].",
            "It's dangerous to go alone, take [member]!",
            "Swoooosh. [member] just landed.",
            "Brace yourselves. [member] just joined the server.",
            "A wild [member] appeared.",
            "Is it hot in here or is it just me? Oh [member] joined that explains it"
    };
    public void onGuildMemberJoin(GuildMemberJoinEvent event){
        Random rand = new Random();
        int number = rand.nextInt(messages.length);

        EmbedBuilder join = new EmbedBuilder();
        join.setColor(Color.red);
        join.setDescription(messages[number].replaceAll("[member]]", event.getMember().getAsMention()));

    }
}

For some reason jda is not allowing me to event.getMember() when using replaceAll on guildjoinevent and i really dont have a clue as to why that is.

  • Try using `replace` instead of `replaceAll`. – Minn Jul 14 '20 at 00:11
  • I tired using replace but it still doesnt work. – Berdasco 19 Jul 14 '20 at 00:53
  • _For some reason jda is not allowing me to event.getMember() when using replaceAll on guildjoinevent_ – always to be as specific as possible – with this description we have to guess that _probably_ your problem is your usage of `replaceAll`; but maybe there's another problem (which would not be easy to see). So, if there's an error message, include it; if there's no error message, then include what you expect and what's actually happening (the unexpected outcome). Also refer to [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Ivo Mori Jul 14 '20 at 01:39
  • _i really dont have a clue as to why that is._ – Have a look at [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Ivo Mori Jul 14 '20 at 01:42
  • Also, don't forget to accept an answer (tick the check-mark next to an answer) when it answers your question. In this way your question won't show up as unanswered in searches. – Ivo Mori Jul 14 '20 at 01:44

1 Answers1

1

You have two problems

  1. There is a typo.
  2. The 1st argument for replaceAll is interpreted as a Regular Expression (regex), and [ ] defines a character class (has a special meaning within a regex).

How to solve it?

  1. Fix your typo: "[member]]""[member]"
  2. If you want to use replaceAll you need to escape [ ]"\\[member]"

As an alternative you may use the replace method which does not take a regex as its 1st argument, but the passed in string as a literal (meaning as is). However, you still need to fix your typo as in messages[number].replace("[member]", ...).

Probably it's easiest to look at it in a simplified manner

static String[] messages = {
        "[member] joined. You must construct additional pylons.",
        "Never gonna give [member] up. Never let [member] down!",
        "Hey! Listen! [member] has joined!",
        "Ha! [member] has joined! You activated my trap card!",
        "We've been expecting you, [member].",
        "It's dangerous to go alone, take [member]!",
        "Swoooosh. [member] just landed.",
        "Brace yourselves. [member] just joined the server.",
        "A wild [member] appeared.",
        "Is it hot in here or is it just me? Oh [member] joined that explains it"
};

public static void main(String[] args) {
    Random rand = new Random();
    int number = rand.nextInt(messages.length);

    String member = "Stanley42";

    String original = messages[number].replaceAll("[member]]", member);
    String originalWithEscaping = messages[number].replaceAll("\\[member]]", member);
    String fixedOriginal = messages[number].replaceAll("\\[member]", member);

    String useReplace = messages[number].replace("[member]]", member);
    String fixedUseReplace = messages[number].replace("[member]", member);

    System.out.println(original);
    System.out.println(originalWithEscaping);
    System.out.println(fixedOriginal);
    System.out.println(useReplace);
    System.out.println(fixedUseReplace);
}

will then give you this sample output

Never gonna give [membeStanley42 up. Never let [membeStanley42 down!
Never gonna give [member] up. Never let [member] down!
Never gonna give Stanley42 up. Never let Stanley42 down!
Never gonna give [member] up. Never let [member] down!
Never gonna give Stanley42 up. Never let Stanley42 down!
Ivo Mori
  • 2,177
  • 5
  • 24
  • 35
  • Thank you so much! this helped me understand it way better thanks to the simplied example, It still didnt allow me to use event.getMember().getAsMention but I figured it was bcs of the "public void onGuildMemberJoin(GuildMemberJoinEvent event)" which for some reason didnt allow that to happen so it changed it to "public void onGuildMemberJoin(net.dv8tion.jda.core.events.guild.member.GuildMemberJoinEvent event)" – Berdasco 19 Jul 14 '20 at 09:18