问题描述
我正在尝试将随机生成的字符串存储在字符串变量中,以用于其他用途。基本上,它是一个密码生成器,用于存储密码以供需要它的命令使用。该应用程序似乎在存储密码以供以后使用时遇到问题。代码和输出与预期的相符。
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,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 = "";
OR
尝试将其添加到public void onGuildMessageReceived(GuildMessageReceivedEvent event)
上方:
private String Stored = "";
每次要使用它时,请执行this.Stored
看看它是否行得通。
此外,尝试添加一条语句以在代码中的随机点向dms发送当前存储的字符串,这就是我所说的打印-调试以仅查看值何时更改以减少搜索。