Skip to content

Commit

Permalink
Kill process when socket dies (1.2.3)
Browse files Browse the repository at this point in the history
This approach to dealing with reconnecting should hopefully work 24/7.
  • Loading branch information
Tim Speckhals committed Dec 1, 2018
1 parent e652a92 commit a28824f
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 217 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ target/
values/
dependency-reduced-pom.xml
SuprDiscordbot.iml
SuprDiscordBot.jar
SuprDiscordBot.zip
config.json
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.timmyRS</groupId>
<artifactId>SuprDiscordBot</artifactId>
<version>1.2.2</version>
<version>1.2.3</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Expand All @@ -27,6 +27,16 @@
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>de.timmyrs.suprdiscordbot.Main</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
Expand Down
2 changes: 0 additions & 2 deletions src/META-INF/MANIFEST.MF

This file was deleted.

10 changes: 7 additions & 3 deletions src/de/timmyrs/suprdiscordbot/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import de.timmyrs.suprdiscordbot.apis.PermissionAPI;
import de.timmyrs.suprdiscordbot.apis.ScriptAPI;
import de.timmyrs.suprdiscordbot.scripts.ScriptManager;
import de.timmyrs.suprdiscordbot.websocket.WebSocket;
import de.timmyrs.suprdiscordbot.websocket.WebSocketEndpoint;
import de.timmyrs.suprdiscordbot.websocket.WebSocketHeart;

Expand Down Expand Up @@ -40,7 +41,7 @@
*/
public class Main
{
private static final String version = "1.2.2";
private static final String version = "1.2.3";
private static final File valuesDir = new File("values");
private final static File confFile = new File("config.json");
public static boolean debug = false;
Expand Down Expand Up @@ -80,12 +81,15 @@ public static void main(String[] args) throws DeploymentException, IOException,
if(Main.configuration.has("botToken") && !Main.configuration.getString("botToken").equals("BOT_TOKEN"))
{
Main.gson = new Gson();
if(!Main.configuration.has("gateway"))
{
Main.configuration.set("gateway", Main.jsonParser.parse(Main.discordAPI.request("/gateway").toString()).getAsJsonObject().get("url").getAsString());
}
Main.scriptManager = new ScriptManager();
Main.discordAPI = new DiscordAPI(new WebSocket(Main.configuration.getString("gateway") + "/?v=6&encoding=json"));
Main.consoleAPI = new ConsoleAPI();
Main.discordAPI = new DiscordAPI();
Main.internetAPI = new InternetAPI();
Main.permissionAPI = new PermissionAPI();
DiscordAPI.openWebSocket();
new WebSocketHeart();
try
{
Expand Down
55 changes: 0 additions & 55 deletions src/de/timmyrs/suprdiscordbot/RAMCleaner.java

This file was deleted.

71 changes: 12 additions & 59 deletions src/de/timmyrs/suprdiscordbot/apis/DiscordAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
import org.apache.commons.io.IOUtils;
import sun.net.www.protocol.https.HttpsURLConnectionImpl;

import javax.websocket.DeploymentException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -42,12 +40,13 @@ public class DiscordAPI
*/
public User user;

public DiscordAPI(WebSocket ws)
{
DiscordAPI.ws = ws;
}

/**
* Not accessible within script.
*
* @param endpoint Endpoint
* @param structure Structure
* @return Object
*/
public static Object request(String endpoint, Structure structure)
{
Expand All @@ -56,11 +55,6 @@ public static Object request(String endpoint, Structure structure)

/**
* Not accessible within script.
*
* @param method Method
* @param endpoint Endpoint
* @param structure Structure
* @return Object
*/
public static Object request(String method, String endpoint, Structure structure)
{
Expand All @@ -69,12 +63,6 @@ public static Object request(String method, String endpoint, Structure structure

/**
* Not accessible within script.
*
* @param method Method
* @param endpoint Endpoint
* @param args Args
* @param structure Structure
* @return Object
*/
public static Object request(String method, String endpoint, String args, Structure structure)
{
Expand Down Expand Up @@ -237,36 +225,7 @@ else if(res.startsWith("["))
}

/**
* Not accessible within script.
*/
public static void openWebSocket() throws DeploymentException, IOException, URISyntaxException
{
if(ws == null)
{
if(!Main.configuration.has("gateway"))
{
Main.configuration.set("gateway", Main.jsonParser.parse(Main.discordAPI.request("/gateway").toString()).getAsJsonObject().get("url").getAsString());
}
ws = new WebSocket(Main.configuration.getString("gateway") + "/?v=6&encoding=json");
}
}

/**
* Not accessible within script.
*
* @param reason Close reason
*/
public static void closeWebSocket(String reason)
{
if(ws != null)
{
ws.close(reason);
ws = null;
}
}

/**
* Send a manual request to the Discord API.
* Sends a request to the Discord API.
*
* @param method HTTP Method (GET, POST, PATCH, etc.)
* @param endpoint The API endpoint (e.g. /users/_ID_)
Expand All @@ -278,7 +237,7 @@ public static Object request(String method, String endpoint)
}

/**
* Send a manual request to the Discord API.
* Sends a request to the Discord API.
*
* @param method HTTP Method (GET, POST, PATCH, etc.)
* @param endpoint The API endpoint (e.g. /users/_ID_)
Expand All @@ -291,7 +250,7 @@ public static Object request(String method, String endpoint, String args)
}

/**
* Send a manual request to the Discord API.
* Sends a request to the Discord API.
*
* @param endpoint HTTP Method (GET, POST, PATCH, etc.)
* @return Object
Expand All @@ -308,7 +267,7 @@ public Object request(String endpoint)
* @param op OP Code
* @param d Raw JSON Data
*/
public void send(int op, Object d) throws DeploymentException, IOException, URISyntaxException
public void send(int op, Object d)
{
JsonObject json = new JsonObject();
json.addProperty("op", op);
Expand All @@ -324,15 +283,9 @@ else if(d instanceof String && d.toString().startsWith("{"))
{
json.addProperty("d", d.toString());
}
if(ws == null)
{
WebSocket.afterConnectSend = json;
DiscordAPI.openWebSocket();
}
else
{
ws.send(json);
}
final String msg = json.toString();
Main.log("Socket", "< " + msg);
Main.webSocketEndpoint.session.getAsyncRemote().sendText(msg);
}

/**
Expand Down
8 changes: 0 additions & 8 deletions src/de/timmyrs/suprdiscordbot/scripts/ScriptManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public ScriptManager()
this.factory = new ScriptEngineManager();
}


Script getScript(final String name)
{
for(Script script : scripts)
Expand All @@ -35,7 +34,6 @@ Script getScript(final String name)
return null;
}


FailedScript getFailedScript(final String name)
{
for(FailedScript script : failedscripts)
Expand All @@ -48,19 +46,16 @@ FailedScript getFailedScript(final String name)
return null;
}


void removeScript(final Script script)
{
scripts.remove(script);
}


void removeFailedScript(final FailedScript script)
{
failedscripts.remove(script);
}


Script registerScript(final File f) throws ScriptException
{
String cont = "";
Expand All @@ -77,7 +72,6 @@ Script registerScript(final File f) throws ScriptException
return s;
}


void registerFailedScript(final File f)
{
String cont = "";
Expand All @@ -93,13 +87,11 @@ void registerFailedScript(final File f)
this.failedscripts.add(s);
}


public void fireEvent(final String event)
{
fireEvent(event, null);
}


public void fireEvent(final String event, final Object data)
{
for(Script script : scripts)
Expand Down
Loading

0 comments on commit a28824f

Please sign in to comment.