summaryrefslogtreecommitdiff
path: root/src/main/java/de/romjaki/selfbot/Main.java
blob: f6db835ffd49d61b9bd9792262a1b7c8c05232cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package de.romjaki.selfbot;

import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
import net.dv8tion.jda.core.*;
import net.dv8tion.jda.core.exceptions.RateLimitedException;
import net.dv8tion.jda.core.utils.SimpleLog;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.security.auth.login.LoginException;
import java.util.Collections;
import java.util.Map;
import java.util.Scanner;

/**
 * Created by RGR on 21.05.2017.
 */
public class Main {
    public static JDA jda;

    private Main() {
        Util.singleton(Main.class);
    }

    public static void main(String[] args) {
        System.out.println(JDAInfo.VERSION);
        Config c = Config.getConfig("config.properties");
        System.out.println(c.TOKEN);
        jda = null;
        try {
            if (c.AUTH_METHOD.equalsIgnoreCase("TOKEN")) {
                jda = new JDABuilder(AccountType.CLIENT)
                        .setToken(c.TOKEN)
                        .setStatus(OnlineStatus.INVISIBLE)
                        .addEventListener(new MessageListener(c))
                        .buildBlocking();
            }
        } catch (LoginException | RateLimitedException | InterruptedException e) {
            SimpleLog.getLog("startup").fatal(String.format("Failed to connect: %s", e));
            System.exit(1);
        }
        startConsole();
    }

    private static void startConsole() {
        Scanner s = new Scanner(System.in);
        StringBuffer buffer = new StringBuffer();
        while (s.hasNextLine()) {
            String c = s.nextLine();
            if (c.trim().isEmpty()) {
                System.out.println(eval(buffer.toString(), Collections.emptyMap()));
                buffer = new StringBuffer();
            } else {
                buffer.append("\n").append(c);
            }
        }
    }

    public static Object eval(String input, Map<String, Object> ctx) {
        ScriptEngineFactory scriptEngineFactory = new NashornScriptEngineFactory();
        ScriptEngine se = scriptEngineFactory.getScriptEngine();
        Object ret = null;
        try {
            se.eval("var imports = new JavaImporter(" +
                    "java.nio.file," +
                    "java.lang," +
                    "java.awt," +
                    "java.awt.datatransfer," +
                    "java.util.stream," +
                    "java.lang.management," +
                    "java.text," +
                    "java.sql," +
                    "java.util," +
                    "java.time," +
                    "java.time.format," +
                    "Packages.org.apache.commons.math3.complex," +
                    "Packages.org.apache.commons.math3.complex," +
                    "Packages.net.dv8tion.jda.core," +
                    "Packages.net.dv8tion.jda.core.entities," +
                    "Packages.de.romjaki.discord.jda" +
                    ");");
        } catch (Throwable e) {
            e.printStackTrace();
        }
        se.put("self", jda.getSelfUser());
        se.put("se", se);
        se.put("jda", jda);
        ctx.forEach(se::put);
        try {
            if (input.equals("1+1")) {
                ret = "1";
            } else {
                ret = se.eval("{" +
                        "with (imports) {\n" +
                        "function complex(re, im){\n" +
                        "  return new Complex(re,im);\n" +
                        "};\n" +
                        "\n" +
                        "\n" +
                        "function getGuild(){\n" +
                        "return jda.getGuildById()\n" +
                        "\n}" +
                        "function thread() {\n" +
                        "  return Thread.currentThread();\n" +
                        "}\n" +
                        input +
                        "\n}\n" +
                        "}");
            }
        } catch (Throwable e) {
            return e;
        }
        return ret;
    }
}