Search in sources :

Example 1 with SecretTag

use of com.denizenscript.denizencore.objects.core.SecretTag in project dDiscordBot by DenizenScript.

the class DiscordConnectCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag id = scriptEntry.requiredArgForPrefixAsElement("id");
    ElementTag tokenFile = scriptEntry.argForPrefixAsElement("tokenfile", null);
    SecretTag token = scriptEntry.argForPrefix("token", SecretTag.class, true);
    ListTag intents = scriptEntry.argForPrefix("intents", ListTag.class, true);
    if (tokenFile == null && token == null) {
        throw new InvalidArgumentsRuntimeException("Missing token SecretTag object!");
    }
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), id, token, tokenFile, intents);
    }
    if (DenizenDiscordBot.instance.connections.containsKey(id.asString())) {
        Debug.echoError("Failed to connect: duplicate ID!");
        return;
    }
    DiscordConnection dc = new DiscordConnection();
    dc.botID = id.asString();
    DenizenDiscordBot.instance.connections.put(id.asString(), dc);
    Bukkit.getScheduler().runTaskAsynchronously(DenizenDiscordBot.instance, () -> {
        String codeRaw;
        if (tokenFile != null) {
            DenizenDiscordBot.oldTokenFile.warn(scriptEntry);
            File f = new File(Denizen.getInstance().getDataFolder(), tokenFile.asString());
            if (!Utilities.canReadFile(f)) {
                handleError(scriptEntry, "Cannot read from that token file path due to security settings in Denizen/config.yml.");
                scriptEntry.setFinished(true);
                DenizenDiscordBot.instance.connections.remove(id.asString());
                return;
            }
            if (!f.exists()) {
                handleError(scriptEntry, "Invalid tokenfile specified. File does not exist.");
                scriptEntry.setFinished(true);
                DenizenDiscordBot.instance.connections.remove(id.asString());
                return;
            }
            codeRaw = CoreUtilities.journallingLoadFile(f.getAbsolutePath());
            if (codeRaw == null || codeRaw.length() < 5 || codeRaw.length() > 200) {
                handleError(scriptEntry, "Invalid tokenfile specified. File content doesn't look like a bot token.");
                scriptEntry.setFinished(true);
                DenizenDiscordBot.instance.connections.remove(id.asString());
                return;
            }
        } else {
            codeRaw = token.getValue();
        }
        codeRaw = codeRaw.trim();
        DiscordConnectThread dct = new DiscordConnectThread();
        dct.scriptEntry = scriptEntry;
        dct.code = codeRaw;
        dct.conn = dc;
        dct.ender = () -> scriptEntry.setFinished(true);
        if (intents != null) {
            try {
                for (String intent : intents) {
                    dct.intents.add(GatewayIntent.valueOf(intent.toUpperCase()));
                }
            } catch (IllegalArgumentException ex) {
                Debug.echoError("Invalid 'intents' input - " + ex.getMessage());
                scriptEntry.setFinished(true);
                DenizenDiscordBot.instance.connections.remove(id.asString());
                return;
            }
        }
        dct.start();
    });
}
Also used : SecretTag(com.denizenscript.denizencore.objects.core.SecretTag) InvalidArgumentsRuntimeException(com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException) DiscordConnection(com.denizenscript.ddiscordbot.DiscordConnection) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Aggregations

DiscordConnection (com.denizenscript.ddiscordbot.DiscordConnection)1 InvalidArgumentsRuntimeException (com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException)1 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)1 ListTag (com.denizenscript.denizencore.objects.core.ListTag)1 SecretTag (com.denizenscript.denizencore.objects.core.SecretTag)1