java - 如何从文件中获取变量并使其成为全局变量?
问题描述
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();
}
}
我正在尝试token
在 JDA 构建器中使用该变量,但我似乎无法将其变为全局变量。我有很多使用这些变量的函数。
如果我只是对它们进行硬编码,我可以看到它们正在工作,但是由于我打算在完成后分发它,所以它需要这种类型的灵活性。
抱歉,如果问题看起来很糟糕,我不是问题专家。
解决方案
编辑:这不起作用
首先使变量全局:
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
等在任何地方使用它们。
编辑:这不起作用
推荐阅读
- ruby-on-rails - 如何在 ruby 中检索活动存储附件 url 作为 json
- php - 如何获取json响应的最后结果
- power-platform - 新环境默认添加了所有用户,无法移除用户?
- sql-server - 在 if 语句上声明 SQL 临时表时出错
- openshift - 如何将秘密链接到 OpenShift 模板中的默认管道 ServiceAccount?
- javascript - 为什么即使将超时设置为0,回调函数仍然延迟
- safari - 无法在 Safari 上设置远程视频描述发送参数,但在 Chrome 和 Firefox 中运行良好
- ios - 如何创建与已初始化的枚举一起使用的协议扩展?
- cuda - 在 GPU 上使用两个以上列表生成笛卡尔积
- excel - Excel宏需要很长时间