我正在为一个有趣的java项目编写一个机器人,我正在建立一个警告系统。我目前遇到的问题是每次重启机器人时输入的数据都会丢失。我已经搜索了5天,现在试图让它工作。请帮忙!
//warn_command
if(message.startsWith("!warn") || message.startsWith("!Warn")) {
String[] command = message.split(" ");
String warn_target = command[1];
String warn_reason = "";
//admin/op_target checker
for(String admin: adminList) {
if(warn_target.equals(admin)) {
target_a_bool = true;
} else {
target_a_bool = false;
}
}
for(String op: opList) {
if(warn_target.equals(op)) {
target_o_bool = true;
} else {
target_o_bool = false;
}
}
//end_admin/op_target checker
for(int i = 2; i < command.length; i++) {
warn_reason += command[i];
if (i != (command.length - 1)) {
warn_reason += " ";
}
}
if(target_a_bool == true) {
sendMessage(channel, "This user cannot be warned");
target_a_bool = false;
} else if(target_o_bool == true) {
if(sender_a_bool == true) {
sendMessage(channel, sender + " has warned " + warn_target + " for reason " + warn_reason);
warn_list.add("User Warned: " + warn_target + " | Reason Warned: " + warn_reason + " | Warned by: " + sender);
sender_a_bool = false;
} else {
sendMessage(channel, "This user cannot be warned");
} target_o_bool = false;
} else {
sendMessage(channel, sender + " has warned " + warn_target + " for reason " + warn_reason);
warn_list.add("User Warned: " + warn_target + " | Reason Warned: " + warn_reason + " | Warned by: " + sender);
}
}
//end_warn_command
//warn_checker
if(message.equalsIgnoreCase("!w list")) {
for(int i = 0; i < warn_list.size(); i++) {
if(i != warn_list.size()) {
String warnlist = warn_list.toString();
sendMessage(channel, warnlist);
}
}
}
//end_warn_checker`
//warn_list_clearer
if(message.equalsIgnoreCase("!c warnlist")) {
if(sender_a_bool == true) {
warn_list.clear();
sendMessage(channel, sender + " has cleared the warn list!");
sender_a_bool = false;
}
}
//end_warn_list_clearer
所有布尔值和数组都是在机器人的配置部分预定义的。所有这些代码目前都在运行,只是每次重启bot时,存储在数组中的所有信息都会被删除。
编辑:我希望将它连接到我使用phpMyAdmin的mysql数据库。这一切都是可能的。答案 0 :(得分:0)
您可以使用Data * Stream来编写和读取值。由于你没有显示你的变量,我将使用一个样本:
boolean[] ab = new boolean[5];
boolean b = false;
// write
FileOutputStream fos = new FileOutputStream("myconfig.data");
DataOutputStream dso = new DataOutputStream(fos);
dso.writeBoolean(b);
dso.writeInt(ab.length);
for (boolean a: ab)
dso.writeBoolean(a);
dso.close();
fos.close();
// read
FileInputStream fis = new FileInputStream("myconfig.data");
DataInputStream ds = new DataInputStream(fis);
b = ds.readBoolean();
int len = ds.readInt();
ab = new boolean[len];
for (int i = 0; i < len; i++)
ab[i] = ds.readBoolean();
ds.close();
fis.close();
答案 1 :(得分:0)
所有这些代码目前都在运行,只是每次重启时都是如此 机器人存储在数组中的所有信息都会被删除吗?
当您运行机器人程序时,数据将仅存储在主存储器(RAM)中,这在程序执行期间是临时的,即数组,变量等数据将仅可用于/可达独自运行。
您需要使用数据库(首选)或文件系统来通过多次运行bot程序来存储阵列/数据。
首先,您可以参考here,了解Java如何使用JDBC
API连接和使用数据库系统。