如何从文件中获取变量并使其全局化


class vars{
public static void main(String[] args) {
try {
File tokenFile = new File("./TOKEN");
final Scanner token = new Scanner(tokenFile);
} catch (FileNotFoundException e) {
System.out.println("Make a file name "TOKEN" and put your token in it");
e.printStackTrace();
}
try {
File prefixFile = new File("./PREFIX");
final Scanner prefix = new Scanner(prefixFile);
} catch (FileNotFoundException e) {
final String prefix = "-- ";
}
final long Creator = ******************L; // ----
final long[] Dev = {******************L, ******************L, ******************L}; // -----, ----, ------
}
}

public class discordbot extends ListenerAdapter {
public static void main(String[] args) {
System.out.println("nJDA version: " + JDAInfo.VERSION + "n");
try {
EnumSet<GatewayIntent> intents = EnumSet.of(
GatewayIntent.GUILD_MESSAGES,
GatewayIntent.GUILD_VOICE_STATES,
GatewayIntent.DIRECT_MESSAGES,
GatewayIntent.GUILD_INVITES,
GatewayIntent.GUILD_MEMBERS
);
JDA jda = JDABuilder.createDefault(vars.main().token, intents)
.addEventListeners(new discordbot())
.setActivity(Activity.watching("out for the summons"))
.setStatus(OnlineStatus.ONLINE)
.enableCache(CacheFlag.VOICE_STATE)
.disableCache(CacheFlag.EMOTE)
.build();
jda.awaitReady();
System.out.println("nJDA is ready.n");
} catch (LoginException loginerror) {
System.out.println("nSomething went wrong with authentication.n");
loginerror.printStackTrace();
} catch (InterruptedException interrupted) {
System.out.println("nJDA got interrupted.n");
interrupted.printStackTrace();
}
}

我正试图在JDA构建器中使用token变量,但似乎无法将其作为全局变量。我有很多函数都使用这些变量。

如果我只是把它们硬编码,我可以看到它们在工作,但由于我计划在完成后分发它,它需要这种灵活性。

很抱歉,如果这个问题看起来很糟糕,我不是提问专家。

编辑:这不起作用

首先使变量全局化:

class vars{
public enum Global {
token,
prefix,
Creator,
Dev
}
}

然后在其他地方设置变量:

class vars{
public enum Global {
token,
prefix,
Creator,
Dev
}
public static void main(String[] args) {
Scanner rawToken = null;
try {
File tokenFile = new File("./TOKEN");
token = new Scanner(tokenFile);
} catch (FileNotFoundException e) {
System.out.println("Make a file name "TOKEN" and put your token in it");
e.printStackTrace();
}
try {
File prefixFile = new File("./PREFIX");
Scanner prefix = new Scanner(prefixFile);
} catch (FileNotFoundException e) {
final String prefix = "-- ";
}
final long Creator = ******************L; // ----
final long[] Dev = {******************L, ******************L, ******************L}; // -----, ----, ------

}

现在使用可以通过调用vars.Global.token等在任何地方使用它们。

编辑:这不起作用

最新更新