Added the basic bot with some commands
This commit is contained in:
41
src/main/java/org/sugarcanemc/wikifaqbot/Bot.java
Normal file
41
src/main/java/org/sugarcanemc/wikifaqbot/Bot.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package org.sugarcanemc.wikifaqbot;
|
||||
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.JDABuilder;
|
||||
import net.dv8tion.jda.api.entities.Activity;
|
||||
import org.sugarcanemc.wikifaqbot.commands.CoinCmd;
|
||||
import org.sugarcanemc.wikifaqbot.commands.DiceCmd;
|
||||
import org.sugarcanemc.wikifaqbot.commands.InfoCmd;
|
||||
import org.sugarcanemc.wikifaqbot.commands.PingCmd;
|
||||
import org.sugarcanemc.wikifaqbot.config.Config;
|
||||
import org.sugarcanemc.wikifaqbot.config.ConfigHandler;
|
||||
import org.sugarcanemc.wikifaqbot.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!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.sugarcanemc.wikifaqbot.commands;
|
||||
|
||||
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 org.sugarcanemc.wikifaqbot.config.ConfigHandler;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.sugarcanemc.wikifaqbot.commands;
|
||||
|
||||
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 org.sugarcanemc.wikifaqbot.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.sugarcanemc.wikifaqbot.commands;
|
||||
|
||||
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 org.sugarcanemc.wikifaqbot.config.ConfigHandler;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
public class InfoCmd extends ListenerAdapter {
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.sugarcanemc.wikifaqbot.commands;
|
||||
|
||||
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 org.sugarcanemc.wikifaqbot.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
22
src/main/java/org/sugarcanemc/wikifaqbot/config/Config.java
Normal file
22
src/main/java/org/sugarcanemc/wikifaqbot/config/Config.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package org.sugarcanemc.wikifaqbot.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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package org.sugarcanemc.wikifaqbot.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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package org.sugarcanemc.wikifaqbot.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\"\nmode: \"default\"");
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
77
src/main/java/org/sugarcanemc/wikifaqbot/webapi/WebAPI.java
Normal file
77
src/main/java/org/sugarcanemc/wikifaqbot/webapi/WebAPI.java
Normal file
@@ -0,0 +1,77 @@
|
||||
package org.sugarcanemc.wikifaqbot.webapi;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
|
||||
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";
|
||||
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);
|
||||
//TODO Make sys.out read IP
|
||||
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.");
|
||||
}
|
||||
}
|
||||
else{
|
||||
|
||||
}
|
||||
} catch(IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user