首页 > 解决方案 > 如何从文件中获取变量并使其成为全局变量?

问题描述

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 构建器中使用该变量,但我似乎无法将其变为全局变量。我有很多使用这些变量的函数。

如果我只是对它们进行硬编码,我可以看到它们正在工作,但是由于我打算在完成后分发它,所以它需要这种类型的灵活性。

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

标签: javaglobal-variables

解决方案


编辑:这不起作用

首先使变量全局:

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等在任何地方使用它们。

编辑:这不起作用


推荐阅读