字符串内容不会在与字符串声明相同的方法中被替换。 (JDA)

String contents not being replaced within the same method as string declaration. (JDA)

我正在尝试将随机生成的字符串存储在字符串变量中以用于其他用途。基本上,它是一个密码生成器,用于存储密码以供需要它的命令使用。该应用程序似乎在存储密码以备后用方面存在问题。代码和输出与预期如下。

public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
    super.onGuildMessageReceived(event);

    String id = "discord userid String";
    long idlong = Long.parseLong(id);
    
    String generatedString = RandomStringUtils.random(15, true, true);
    StringBuilder stored = new StringBuilder(15);

   //password generator
    if (event.getMessage().getContentRaw().equalsIgnoreCase("!passwordgen")) {
        if (stored.toString().isEmpty() == true || idlong == 
        event.getMessage().getAuthor().getIdLong()) {
            stored.replace(0, 14, generatedString);

            User user = event.getMessage().getAuthor();
            user.openPrivateChannel().complete()
                .sendMessage("Your new password is: " + stored + ". You can change your password 
                in the future with !setpassword <currentpassword>.").queue();
            }
            else {
                event.getChannel().sendMessage("You do not have permission to generate a new password  
                and/or an initial password has already been set.").queue();
            }
        }
        
        //set password command      
        if (event.getMessage().getContentRaw().startsWith("!setpassword")) {    
            char[] chararr = event.getMessage().getContentRaw().toCharArray();
            
            for (int i = 0; i < chararr.length; i++) {
                if (chararr[i] == ' ') {
                    passwordkeyed += event.getMessage().getContentRaw().substring(13);
                }
            }
            
            if (passwordkeyed.equals(stored.toString()) && !(passwordkeyed.isEmpty())) {
                stored.replace(0, 14, generatedString); //replaces random password from !passwordgen 
               // with new random password
    
                event.getChannel().sendMessage("Stored: " + stored).queue();
                event.getChannel().sendMessage("Check your DMs!").queue();
                
                User user1 = event.getMessage().getAuthor();
                user1.openPrivateChannel().complete()
                    .sendMessage("Your new password is: " + stored).queue();  
            }
                if (!(passwordkeyed.equals(stored.toString()))) {
                event.getChannel().sendMessage("Incorrect password. Stored: " + stored).queue();
            }
            
                if (stored.toString().isEmpty()) {
                event.getChannel().sendMessage("Please use !passwordgen to generate an initial password.").queue();
            }
        }
}

出于某种原因,在 //password generator 部分中,stored 被替换为随机字符串就好了,但是当您使用 !setpassword 命令输入该密码时,stored 是空的,所以它就像我在密码生成器部分中使用的 replace 方法没有被保存,即使 StringBuilder 具有该方法的适当范围。 我一直在尝试这样做。我已经尝试使用 String、StringBuilder 和 StringBuffer 来查看是否有不同的结果但都具有相同的输出。

输出:

!passwordgen //discord bot command

Your new password is: h50uSKlrWa30Q40. You can change your password in the future with !setpassword 
<currentpassword>. //private message to whoever ran the command

!setpassword h50uSKlrWa30Q40 //discord bot command

Incorrect password. Stored:
Please use !passwordgen to generate an initial password.

预期输出:

 !passwordgen //discord bot command

Your new password is: h50uSKlrWa30Q40. You can change your password in the future with !setpassword 
<currentpassword>. //private message to whoever ran the command

!setpassword h50uSKlrWa30Q40 //discord bot command

Stored: 2Fx8buVxpIEyNYX
Check your DMs!

Your new password is: 2Fx8buVxpIEyNYX //private message to whoever ran the command

在我的脑海里,我在想这可能是我生成随机密码并存储它的方式,但我不确定,因为这似乎是字符串声明本身及其范围的问题

尝试:

StringBuilder stored = new StringBuilder(15);

String stored = "";

尝试将其添加到 public void onGuildMessageReceived(GuildMessageReceivedEvent event) 上方:

private String Stored = "";

每次你想用它的时候,做this.Stored

然后看看它是否有效。

此外,尝试添加一条语句,在代码中的随机点将当前存储的字符串发送到 dms 中,这就是我所说的打印 - 调试,以便简单地查看值何时更改以减少我们的搜索。