英文:
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 = "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();
}
}
}
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
<currentpassword>. //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
<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
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 = "";
OR
try to add this above the public void onGuildMessageReceived(GuildMessageReceivedEvent event)
:
private String Stored = "";
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论