Added Features

This commit is contained in:
Steven Tracey 2021-07-21 22:10:55 -04:00
parent c9fe5bd7f9
commit bf341dd8d3
14 changed files with 12 additions and 238 deletions

6
.gitignore vendored
View File

@ -24,6 +24,6 @@ hs_err_pid*
#Files with sensitive info
config.yml
/WebAPI
/build
/.idea
WebAPI/
build/
.idea/

View File

@ -4,8 +4,8 @@ tech/nevets/lunarbot/config/ConfigUtils.java
tech.nevets.lunarbot.config.ConfigUtils
tech/nevets/lunarbot/config/Config.java
tech.nevets.lunarbot.config.Config
tech/nevets/lunarbot/webserver/WebServer.java
tech.nevets.lunarbot.webserver.WebServer
tech/nevets/lunarbot/webserver/WebGenerator.java
tech.nevets.lunarbot.webserver.WebGenerator
tech/nevets/lunarbot/commands/games/CoinCmd.java
tech.nevets.lunarbot.commands.games.CoinCmd
tech/nevets/lunarbot/commands/wiki/InfoCmd.java

View File

@ -41,7 +41,7 @@ public class Bot {
System.out.println("Finished Building Bot!");
WebAPI.main(null);
WebGenerator.main(null);
WebGenerator.generator(null);
}
}

View File

@ -19,11 +19,10 @@ public class InfoCmd extends ListenerAdapter {
if (content.equalsIgnoreCase(prefix + "info")) {
EmbedBuilder info = new EmbedBuilder();
info.setTitle("📚・Information・📚");
info.setDescription("Information about Sugarcane");
info.addField("Creator", "nevetS-718", false);
info.setFooter("SugarcaneMC", "https://cdn.discordapp.com/icons/855918593497759754/a_978a67a83330554987cd7521f638fea8.gif?size=4096");
info.setColor(0x73fc03);
info.setTitle("📚・LunarBot・📚");
info.setDescription("Lunarbot cuz y not");
info.setFooter("LunarBot: Created by nevetS & 5gi", "https://cdn.discordapp.com/icons/855918593497759754/a_978a67a83330554987cd7521f638fea8.gif?size=4096");
info.setColor(0x2a2ea3);
e.getChannel().sendTyping().queue();
e.getChannel().sendMessage(info.build()).queue();

View File

@ -209,31 +209,6 @@ public class WebAPI implements Runnable{
}
/** static final File WEB_ROOT = new File("/WebAPI");
static final String DEFAULT_File = "index.html";
static final String FILE_NOT_FOUND = "404.html";

View File

@ -5,9 +5,9 @@ import java.util.Scanner;
public class WebGenerator {
public static void main(String args[]) {
public static void generator(String args[]) {
System.out.println("");
System.out.println();
Scanner sc = new Scanner(System.in);
String path = sc.next();
//Using Scanner class to get the folder name from the user

View File

@ -1,200 +0,0 @@
package tech.nevets.lunarbot.webserver;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.util.StringTokenizer;
public class WebServer implements Runnable {
static final File WEB_ROOT = new File(".");
static final String DEFAULT_FILE = "index.html";
static final String FILE_NOT_FOUND = "404.html";
static final String METHOD_NOT_SUPPORTED = "mns.html";
static final int PORT = 80;
private Socket connect;
public WebServer(Socket c) {
connect = c;
}
public static void main(String[] args) {
try {
ServerSocket serverConnect = new ServerSocket(PORT);
while (true) {
WebServer myServer = new WebServer(serverConnect.accept());
Thread thread = new Thread(myServer);
thread.start();
}
} catch (IOException e) {
System.out.println("Error occurred while connecting");
e.printStackTrace();
}
}
@Override
public void run() {
BufferedReader in = null;
PrintWriter out = null;
BufferedOutputStream dataOut = null;
String fileRequested = null;
try {
in = new BufferedReader(new InputStreamReader(connect.getInputStream()));
out = new PrintWriter(connect.getOutputStream());
dataOut = new BufferedOutputStream(connect.getOutputStream());
String input = in.readLine();
StringTokenizer parse = new StringTokenizer(input);
String method = parse.nextToken().toUpperCase();
fileRequested = parse.nextToken().toLowerCase();
if (!method.equals("GET") && !method.equals("HEAD")) {
File file = new File(WEB_ROOT, METHOD_NOT_SUPPORTED);
int fileLength = (int) file.length();
String contentMimeType = "text/html";
byte[] fileData = readFileData(file, fileLength);
out.println("HTTP/1.1 501 Not Implemented");
out.println("Server: Java HTTP Server from SSaurel : 1.0");
out.println("Date: " + new Date());
out.println("Content-type: " + contentMimeType);
out.println("Content-length: " + fileLength);
out.println();
out.flush();
dataOut.write(fileData, 0, fileLength);
dataOut.flush();
} else {
if (fileRequested.endsWith("/")) {
fileRequested += DEFAULT_FILE;
}
File file = new File(WEB_ROOT, fileRequested);
int fileLength = (int) file.length();
String content = getContentType(fileRequested);
if (method.equals("GET")) {
byte[] fileData = readFileData(file, fileLength);
out.println("HTTP/1.1 200 OK");
out.println("Server: Java HTTP Server from SSaurel : 1.0");
out.println("Date: " + new Date());
out.println("Content-type: " + content);
out.println("Content-length: " + fileLength);
out.println();
out.flush();
dataOut.write(fileData, 0, fileLength);
dataOut.flush();
}
}
} catch (FileNotFoundException fnfe) {
try {
fileNotFound(out, dataOut, fileRequested);
} catch (IOException ioe) {
System.err.println("Error with file not found exception : " + ioe.getMessage());
}
} catch (IOException ioe) {
System.err.println("Server error : " + ioe);
} finally {
try {
in.close();
out.close();
dataOut.close();
connect.close();
} catch (Exception e) {
System.err.println("Error closing stream : " + e.getMessage());
}
}
}
private byte[] readFileData(File file, int fileLength) throws IOException {
FileInputStream fileIn = null;
byte[] fileData = new byte[fileLength];
try {
fileIn = new FileInputStream(file);
fileIn.read(fileData);
} finally {
if (fileIn != null)
fileIn.close();
}
return fileData;
}
private String getContentType(String fileRequested) {
if (fileRequested.endsWith(".htm") || fileRequested.endsWith(".html"))
return "text/html";
else
return "text/plain";
}
private void fileNotFound(PrintWriter out, OutputStream dataOut, String fileRequested) throws IOException {
File file = new File(WEB_ROOT, FILE_NOT_FOUND);
int fileLength = (int) file.length();
String content = "text/html";
byte[] fileData = readFileData(file, fileLength);
out.println("HTTP/1.1 404 File Not Found");
out.println("Server: Java HTTP Server from SSaurel : 1.0");
out.println("Date: " + new Date());
out.println("Content-type: " + content);
out.println("Content-length: " + fileLength);
out.println();
out.flush();
dataOut.write(fileData, 0, fileLength);
dataOut.flush();
}
/**
protected void start() {
ServerSocket s;
System.out.println("Webserver starting up on port 80");
System.out.println("(press ctrl-c to exit)");
try {
// create the main server socket
s = new ServerSocket(80);
} catch (Exception e) {
System.out.println("Error: " + e);
return;
}
System.out.println("Waiting for connection");
for (;;) {
try {
Socket remote = s.accept();
System.out.println("Connection, sending data.");
BufferedReader in = new BufferedReader(new InputStreamReader(
remote.getInputStream()));
PrintWriter out = new PrintWriter(remote.getOutputStream());
String str = ".";
while (!str.equals(""))
str = in.readLine();
out.println("HTTP/1.0 200 OK");
out.println("Content-Type: text/html");
out.println("Server: Bot");
out.println("");
out.println("<h1>WebAPI is running</h1>");
out.flush();
remote.close();
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
}
**/
}