无法在公会成员加入时使用 get Member 替换 All

Cant replaceAll using getMember on guildmemberjoin

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()));

    }
}

出于某种原因,在 guildjoinevent 上使用 replaceAll 时,jda 不允许我 event.getMember(),我真的不知道为什么会这样。

你有两个问题

  1. 有错字。
  2. replaceAll 的第 1st 个参数被解释为 Regular Expression (regex),而 [ ] 定义了一个 字符 class(在正则表达式中有特殊含义)。

如何解决?

  1. 修正你的错字:"[member]]""[member]"
  2. 如果你想使用replaceAll你需要转义[ ]"\[member]"

作为替代方案,您可以使用 replace 方法,该方法不将正则表达式作为其 1st 参数,而是将传入的字符串作为文字(意思是原样)。但是,您仍然需要更正 messages[number].replace("[member]", ...).

中的拼写错误

大概是简单的看一下最简单

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);
}

然后会给你这个示例输出

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!