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

huangapple go评论75阅读模式
英文:

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

问题

以下是您要翻译的内容:

我正在尝试将随机生成的字符串存储在字符串变量中以便将其用于其他用途基本上这是一个密码生成器用于存储密码以供需要它的命令使用然而应用似乎在存储密码以供以后使用时出现了问题以下是代码和输出与期望的对比

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

    String id = "discord用户ID字符串";
    long idlong = Long.parseLong(id);

    String generatedString = RandomStringUtils.random(15, true, true);
    StringBuilder stored = new StringBuilder(15);

    // 密码生成器
    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();
        }
    }
    
    // 设置密码命令
    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); // 用新的随机密码替换!passwordgen生成的随机密码

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

由于您只需要翻译代码部分,以上是代码的翻译。如果您有其他问题或需要进一步的帮助,请告诉我。

英文:

I am trying to store a randomly generated string in a string variable to use for in for other things. Basically, it is a password generator that stores the password for use with commands that need it. The application seems to have problems with storing the password for later use though. Code and output vs expected below.

public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
super.onGuildMessageReceived(event);
String id = &quot;discord userid String&quot;;
long idlong = Long.parseLong(id);
String generatedString = RandomStringUtils.random(15, true, true);
StringBuilder stored = new StringBuilder(15);
//password generator
if (event.getMessage().getContentRaw().equalsIgnoreCase(&quot;!passwordgen&quot;)) {
if (stored.toString().isEmpty() == true || idlong == 
event.getMessage().getAuthor().getIdLong()) {
stored.replace(0, 14, generatedString);
User user = event.getMessage().getAuthor();
user.openPrivateChannel().complete()
.sendMessage(&quot;Your new password is: &quot; + stored + &quot;. You can change your password 
in the future with !setpassword &lt;currentpassword&gt;.&quot;).queue();
}
else {
event.getChannel().sendMessage(&quot;You do not have permission to generate a new password  
and/or an initial password has already been set.&quot;).queue();
}
}
//set password command		
if (event.getMessage().getContentRaw().startsWith(&quot;!setpassword&quot;)) {	
char[] chararr = event.getMessage().getContentRaw().toCharArray();
for (int i = 0; i &lt; chararr.length; i++) {
if (chararr[i] == &#39; &#39;) {
passwordkeyed += event.getMessage().getContentRaw().substring(13);
}
}
if (passwordkeyed.equals(stored.toString()) &amp;&amp; !(passwordkeyed.isEmpty())) {
stored.replace(0, 14, generatedString); //replaces random password from !passwordgen 
// with new random password
event.getChannel().sendMessage(&quot;Stored: &quot; + stored).queue();
event.getChannel().sendMessage(&quot;Check your DMs!&quot;).queue();
User user1 = event.getMessage().getAuthor();
user1.openPrivateChannel().complete()
.sendMessage(&quot;Your new password is: &quot; + stored).queue();  
}
if (!(passwordkeyed.equals(stored.toString()))) {
event.getChannel().sendMessage(&quot;Incorrect password. Stored: &quot; + stored).queue();
}
if (stored.toString().isEmpty()) {
event.getChannel().sendMessage(&quot;Please use !passwordgen to generate an initial password.&quot;).queue();
}
}
}

For some reason, in the //password generator section, stored is replaced with a random string just fine but when you enter that password using the !setpassword command, stored is empty so it is like the replace method I used in the password generator section is not being saved even though the StringBuilder has the proper scope for the method.
I've been trying to do this for a bit now. I have tried with String, StringBuilder, and StringBuffer to see if any had different results but all had the same output.

Output:

!passwordgen //discord bot command
Your new password is: h50uSKlrWa30Q40. You can change your password in the future with !setpassword 
&lt;currentpassword&gt;. //private message to whoever ran the command
!setpassword h50uSKlrWa30Q40 //discord bot command
Incorrect password. Stored:
Please use !passwordgen to generate an initial password.

Expected Output:

 !passwordgen //discord bot command
Your new password is: h50uSKlrWa30Q40. You can change your password in the future with !setpassword 
&lt;currentpassword&gt;. //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

In the back of my mind I am thinking it may be how I am generating the random password and storing it but I am not sure since this seems to be a problem with the string declaration itself and its scope

答案1

得分: 1

以下是翻译好的部分:

尝试将:

StringBuilder stored = new StringBuilder(15);

改为:

String stored = "";

或者

尝试在 public void onGuildMessageReceived(GuildMessageReceivedEvent event) 方法上方添加以下内容:

private String Stored = "";

每次想要使用它时,请使用 this.Stored

然后看看是否有效。

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

英文:

Try:

StringBuilder stored = new StringBuilder(15);

to

String stored = &quot;&quot;;

OR

try to add this above the public void onGuildMessageReceived(GuildMessageReceivedEvent event):

private String Stored = &quot;&quot;;

Everytime you want to use it, do this.Stored

See if it works then.

Also, try to add a statement to send in dms the current stored string at random points in the code this is what i call print–debugging to simply see when the value changes to reduce our search.

huangapple
  • 本文由 发表于 2020年8月11日 09:07:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63350095.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定