updated gitignore as to not expose the bot token :P

This commit is contained in:
Steven Tracey
2021-07-19 23:48:23 -04:00
parent a240300fd6
commit b26aaadf2a
45 changed files with 217 additions and 647 deletions

View File

@@ -0,0 +1,41 @@
package tech.nevets.lunarbot;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import tech.nevets.lunarbot.commands.games.CoinCmd;
import tech.nevets.lunarbot.commands.games.DiceCmd;
import tech.nevets.lunarbot.commands.wiki.InfoCmd;
import tech.nevets.lunarbot.commands.games.PingCmd;
import tech.nevets.lunarbot.config.Config;
import tech.nevets.lunarbot.config.ConfigHandler;
import tech.nevets.lunarbot.config.ConfigUtils;
import java.io.File;
public class Bot {
public static void main(String[] args) throws Exception {
String configPath = ConfigHandler.configPath.toString();
File file = new File(configPath);
if (!file.exists()) {
ConfigUtils.createConfig();
}
ConfigHandler handler = ConfigHandler.getInstance();
Config config = handler.getConfig();
String prefix = config.getPrefix();
String botToken = config.getBotToken();
JDA jda = JDABuilder.createDefault(botToken)
.setActivity(Activity.competing("being the very best"))
.addEventListeners(new InfoCmd())
.addEventListeners(new PingCmd())
.addEventListeners(new DiceCmd())
.addEventListeners(new CoinCmd())
.build();
jda.awaitReady();
System.out.println("Finished Building Bot!");
}
}

View File

@@ -0,0 +1,38 @@
package tech.nevets.lunarbot.commands.games;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import tech.nevets.lunarbot.config.ConfigHandler;
import java.io.FileNotFoundException;
import java.util.Random;
public class CoinCmd extends ListenerAdapter {
@Override
public void onMessageReceived(MessageReceivedEvent e) {
if (e.getAuthor().isBot()) return;
Message message = e.getMessage();
String content = message.getContentRaw();
String prefix = null;
try { prefix = ConfigHandler.getInstance().getConfig().getPrefix(); } catch (FileNotFoundException fileNotFoundException) { fileNotFoundException.printStackTrace(); }
if (content.equalsIgnoreCase(prefix + "coinflip")) {
Random rand = new Random();
int upperbound = 2;
MessageChannel channel = e.getChannel();
int i = rand.nextInt(upperbound);
if (i == 0) {
channel.sendTyping().queue();
channel.sendMessage("The coin landed on **heads**").queue();
} else {
channel.sendTyping().queue();
channel.sendMessage("The coin landed on **tails**").queue();
}
System.out.println(i);
}
}
}

View File

@@ -0,0 +1,32 @@
package tech.nevets.lunarbot.commands.games;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import tech.nevets.lunarbot.config.ConfigHandler;
import java.io.FileNotFoundException;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
public class DiceCmd extends ListenerAdapter {
@Override
public void onMessageReceived(MessageReceivedEvent e) {
if (e.getAuthor().isBot()) return;
Message message = e.getMessage();
String content = message.getContentRaw();
String prefix = null;
try { prefix = ConfigHandler.getInstance().getConfig().getPrefix(); } catch (FileNotFoundException fileNotFoundException) { fileNotFoundException.printStackTrace(); }
if (content.equalsIgnoreCase(prefix + "dice")) {
Random rand = ThreadLocalRandom.current();
MessageChannel channel = e.getChannel();
int roll = rand.nextInt(6) + 1;
channel.sendTyping().queue();
channel.sendMessage("Your roll: " + roll).queue();
}
}
}

View File

@@ -0,0 +1,28 @@
package tech.nevets.lunarbot.commands.games;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import tech.nevets.lunarbot.config.ConfigHandler;
import java.io.FileNotFoundException;
public class PingCmd extends ListenerAdapter {
@Override
public void onMessageReceived(MessageReceivedEvent e) {
if (e.getAuthor().isBot()) return;
Message message = e.getMessage();
String content = message.getContentRaw();
String prefix = null;
try { prefix = ConfigHandler.getInstance().getConfig().getPrefix(); } catch (FileNotFoundException fileNotFoundException) { fileNotFoundException.printStackTrace(); }
if (content.equalsIgnoreCase(prefix + "ping")) {
MessageChannel channel = e.getChannel();
channel.sendTyping().queue();
channel.sendMessage("Pong!").queue();
}
}
}

View File

@@ -0,0 +1,32 @@
package tech.nevets.lunarbot.commands.wiki;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import tech.nevets.lunarbot.config.ConfigHandler;
import java.io.FileNotFoundException;
public class InfoCmd extends ListenerAdapter {
@Override
public void onMessageReceived(MessageReceivedEvent e) {
if (e.getAuthor().isBot()) return;
Message message = e.getMessage();
String content = message.getContentRaw();
String prefix = null;
try { prefix = ConfigHandler.getInstance().getConfig().getPrefix(); } catch (FileNotFoundException fileNotFoundException) { fileNotFoundException.printStackTrace();}
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);
e.getChannel().sendTyping().queue();
e.getChannel().sendMessage(info.build()).queue();
}
}
}

View File

@@ -0,0 +1,22 @@
package tech.nevets.lunarbot.config;
public class Config {
private String prefix;
private String botToken;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getBotToken() {
return botToken;
}
public void setBotToken(String botToken) {
this.botToken = botToken;
}
}

View File

@@ -0,0 +1,59 @@
package tech.nevets.lunarbot.config;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
public class ConfigHandler {
public static final Path configPath = Paths.get("./config.yml");
private static ConfigHandler configHandler;
Config config;
public static ConfigHandler getInstance() throws FileNotFoundException {
return getInstance(configPath);
}
public static ConfigHandler getInstance(Path configPath) throws FileNotFoundException {
if (configHandler == null) {
configHandler = new ConfigHandler(configPath);
}
return configHandler;
}
private ConfigHandler(Path configPath) throws FileNotFoundException {
this.config = loadConfig(configPath);
}
public Config loadConfig(Path configPath) throws FileNotFoundException {
Constructor constructor = new Constructor(Config.class);
Yaml yaml = new Yaml(constructor);
return yaml.load(new FileInputStream(configPath.toFile()));
}
public void dumpConfig() throws IllegalArgumentException, IllegalAccessException, IOException {
dumpConfig(this.config, this.configPath);
}
public void dumpConfig(Config config, Path configPath) throws IllegalArgumentException, IllegalAccessException, IOException {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setPrettyFlow(true);
Yaml yml = new Yaml(options);
yml.dump(config, new FileWriter(configPath.toFile()));
}
public Config getConfig() {
return this.config;
}
}

View File

@@ -0,0 +1,36 @@
package tech.nevets.lunarbot.config;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class ConfigUtils {
public static void createConfig() {
System.out.println("Attemption to create file...");
try {
File config = new File("config.yml");
if (config.createNewFile()) {
System.out.println(config.getName() + " has been successfully created!");
} else {
System.out.println(config.getName() + " already exists!");
}
} catch (IOException e) {
System.out.println("An error has occurred");
e.printStackTrace();
}
try {
FileWriter writer = new FileWriter("config.yml");
writer.write("prefix: \"!\"\nbotToken: \"BOTTOKEN\"");
writer.close();
System.out.println("Successfully wrote to config.yml");
} catch (IOException e) {
System.out.println("An error occurred while writing to config.yml");
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,165 @@
package tech.nevets.lunarbot.webapi;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class WebAPI {
public static void main(String args[]) {
WebServer ws = new WebServer();
ws.start();
}
}
class WebServer {
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</H2>");
out.flush();
remote.close();
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
}
}
/** static final File WEB_ROOT = new File("/WebAPI");
static final String DEFAULT_File = "index.html";
static final String FILE_NOT_FOUND = "404.html";
static final String METHOD_NOT_SUPPORTED = "mns.html"; // (file not supported)
// port
static final int PORT = 80;
// port
static final boolean verbose = true;
private Socket connect;
public void webapi(Socket c) {
connect = c;
}
public static void main(String[] args){
try {
ServerSocket serverConnect = new ServerSocket(PORT);
System.out.println("[WebAPI] WebAPI Started on " + "IP" + PORT);
while(true) {
WebAPI server = new WebAPI();
if (verbose) {
System.out.println("[WebAPI] Connection Open.");
}
Thread thread = new Thread(server);
thread.start();
}
} catch (IOException e) {
System.err.println("[WebAPI] Server Connection error");
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")) {
if (verbose) {
System.out.println("501 Not Implemeted: " + method + "method.");
}
File file = new File(WEB_ROOT, METHOD_NOT_SUPPORTED);
int fileLength = file.length();
String conetentMimeType = "ext/html";
byte[] fileData = readFileData(file, fileLength)
private byte[] readFileData(File file, int fileLength) {
FileInputStream fileIn = null;
byte[] fileData = new byte[fileLength];
fileIn = new FileInputStream(file);
fileIn.read(fileData);
}
}
else{
}
} catch(IOException e){
e.printStackTrace();
}
}
}
**/