Listeners & Main Class Cleanup
This commit is contained in:
parent
c5c01bfc85
commit
bbe4915045
5
pom.xml
5
pom.xml
|
@ -41,6 +41,11 @@
|
||||||
<artifactId>lavaplayer</artifactId>
|
<artifactId>lavaplayer</artifactId>
|
||||||
<version>1.3.53</version>
|
<version>1.3.53</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>8.0.28</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
package moe.oko.Kiafumi;
|
package moe.oko.Kiafumi;
|
||||||
|
|
||||||
|
import moe.oko.Kiafumi.listener.MainListener;
|
||||||
|
import moe.oko.Kiafumi.model.KiafumiDB;
|
||||||
|
import net.dv8tion.jda.api.EmbedBuilder;
|
||||||
import net.dv8tion.jda.api.JDA;
|
import net.dv8tion.jda.api.JDA;
|
||||||
import net.dv8tion.jda.api.JDABuilder;
|
import net.dv8tion.jda.api.JDABuilder;
|
||||||
import net.dv8tion.jda.api.OnlineStatus;
|
import net.dv8tion.jda.api.OnlineStatus;
|
||||||
|
@ -15,12 +18,15 @@ import org.slf4j.LoggerFactory;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.time.temporal.TemporalAccessor;
|
||||||
|
|
||||||
public class Kiafumi {
|
public class Kiafumi {
|
||||||
|
|
||||||
private File CONFIG_FILE = new File("config.yml");
|
private File CONFIG_FILE = new File("config.yml");
|
||||||
|
|
||||||
public YamlConfiguration yamlConfiguration;
|
public YamlConfiguration yamlConfiguration = new YamlConfiguration();
|
||||||
|
|
||||||
public Logger logger = LoggerFactory.getLogger("Kiafumi");
|
public Logger logger = LoggerFactory.getLogger("Kiafumi");
|
||||||
|
|
||||||
|
@ -32,10 +38,12 @@ public class Kiafumi {
|
||||||
|
|
||||||
public KiafumiConfig config;
|
public KiafumiConfig config;
|
||||||
|
|
||||||
|
public KiafumiDB database;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try {
|
try {
|
||||||
Kiafumi shiki = new Kiafumi();
|
Kiafumi kia = new Kiafumi();
|
||||||
shiki.start();
|
kia.start();
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
System.out.println("Failed to start Kiafumi Instance, check your Java installation.");
|
System.out.println("Failed to start Kiafumi Instance, check your Java installation.");
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
|
@ -51,21 +59,23 @@ public class Kiafumi {
|
||||||
logger.info("Config load start...");
|
logger.info("Config load start...");
|
||||||
|
|
||||||
if (!CONFIG_FILE.exists()) {
|
if (!CONFIG_FILE.exists()) {
|
||||||
try {
|
try (InputStream is = this.getClass().getClassLoader().getResourceAsStream("config.yml")) {
|
||||||
CONFIG_FILE.createNewFile();
|
//Save the default cfg
|
||||||
} catch (IOException ex) {
|
Files.copy(is, CONFIG_FILE.toPath());
|
||||||
logger.warn("Failed to create the configuration file. Stopping. (Try creating configuration directory.)");
|
} catch (Exception ex) {
|
||||||
|
logger.warn("Failed to create the configuration file. Stopping. (" + CONFIG_FILE.getAbsolutePath() + ")");
|
||||||
|
ex.printStackTrace();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
yamlConfiguration.load(CONFIG_FILE);
|
yamlConfiguration.load("config.yml");
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
logger.warn("File not found, attempting to create.");
|
logger.warn("File not found, must've failed to create...");
|
||||||
|
} catch (Exception e) {
|
||||||
} catch (IOException | InvalidConfigurationException e) {
|
logger.warn("Ensure all values are inputted properly.");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,6 +91,8 @@ public class Kiafumi {
|
||||||
|
|
||||||
PREFIX = config.getPrefix();
|
PREFIX = config.getPrefix();
|
||||||
|
|
||||||
|
database = config.createDb();
|
||||||
|
|
||||||
//Makes our JDA instance.
|
//Makes our JDA instance.
|
||||||
startDiscord();
|
startDiscord();
|
||||||
}
|
}
|
||||||
|
@ -92,16 +104,20 @@ public class Kiafumi {
|
||||||
try {
|
try {
|
||||||
JDA = JDABuilder.create(config.getToken(), GatewayIntent.GUILD_MEMBERS,
|
JDA = JDABuilder.create(config.getToken(), GatewayIntent.GUILD_MEMBERS,
|
||||||
GatewayIntent.GUILD_MESSAGES,
|
GatewayIntent.GUILD_MESSAGES,
|
||||||
GatewayIntent.DIRECT_MESSAGES)
|
GatewayIntent.DIRECT_MESSAGES, GatewayIntent.GUILD_PRESENCES)
|
||||||
.setChunkingFilter(ChunkingFilter.ALL)
|
.setChunkingFilter(ChunkingFilter.ALL)
|
||||||
.setMemberCachePolicy(MemberCachePolicy.ALL)
|
.setMemberCachePolicy(MemberCachePolicy.ALL)
|
||||||
.setActivity(Activity.of(Activity.ActivityType.valueOf(config.getActivityType()), config.getActivityMsg()))
|
.setActivity(Activity.of(Activity.ActivityType.valueOf(config.getActivityType()), config.getActivityMsg()))
|
||||||
.setStatus(OnlineStatus.valueOf(config.getStatusType()))
|
.setStatus(OnlineStatus.valueOf(config.getStatusType()))
|
||||||
.addEventListeners().build().awaitReady();
|
.addEventListeners(new MainListener()).build().awaitReady();
|
||||||
} catch(Exception ex) {
|
} catch(Exception ex) {
|
||||||
error("Initialization broke...");
|
error("Initialization broke...");
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
EmbedBuilder eb = new EmbedBuilder().setTitle("Kiafumi Online")
|
||||||
|
.setAuthor("Created by Oko, Laika, and Tiddy");
|
||||||
|
JDA.getTextChannelById(config.getLogChannel()).sendMessage(eb.build()).queue();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -117,4 +133,6 @@ public class Kiafumi {
|
||||||
public static void error(String str) {
|
public static void error(String str) {
|
||||||
instance.logger.error(str);
|
instance.logger.error(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public KiafumiDB getDatabase() { return database; }
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package moe.oko.Kiafumi;
|
package moe.oko.Kiafumi;
|
||||||
|
|
||||||
|
import moe.oko.Kiafumi.model.KiafumiDB;
|
||||||
import org.simpleyaml.configuration.ConfigurationSection;
|
import org.simpleyaml.configuration.ConfigurationSection;
|
||||||
import org.simpleyaml.configuration.file.YamlConfiguration;
|
import org.simpleyaml.configuration.file.YamlConfiguration;
|
||||||
|
|
||||||
|
@ -29,6 +30,15 @@ public class KiafumiConfig {
|
||||||
private String activityMsg;
|
private String activityMsg;
|
||||||
private String statusType;
|
private String statusType;
|
||||||
|
|
||||||
|
/*
|
||||||
|
SQL Variable Section
|
||||||
|
*/
|
||||||
|
private String host;
|
||||||
|
private int port;
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
private String database;
|
||||||
|
|
||||||
public KiafumiConfig(YamlConfiguration configuration) {
|
public KiafumiConfig(YamlConfiguration configuration) {
|
||||||
//Load config on class creation...
|
//Load config on class creation...
|
||||||
this.configuration = configuration;
|
this.configuration = configuration;
|
||||||
|
@ -58,6 +68,13 @@ public class KiafumiConfig {
|
||||||
activityType = main.getString("activityType");
|
activityType = main.getString("activityType");
|
||||||
activityMsg = main.getString("activityMsg");
|
activityMsg = main.getString("activityMsg");
|
||||||
statusType = main.getString("statusType");
|
statusType = main.getString("statusType");
|
||||||
|
//SQL loaders
|
||||||
|
ConfigurationSection sql = configuration.getConfigurationSection("sql");
|
||||||
|
host = sql.getString("host");
|
||||||
|
port = sql.getInt("port");
|
||||||
|
username = sql.getString("username");
|
||||||
|
password = sql.getString("password");
|
||||||
|
database = sql.getString("database");
|
||||||
} catch(Exception ex) {
|
} catch(Exception ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
error("Failed to load configuration!");
|
error("Failed to load configuration!");
|
||||||
|
@ -82,4 +99,8 @@ public class KiafumiConfig {
|
||||||
public String getActivityMsg() { return activityMsg; }
|
public String getActivityMsg() { return activityMsg; }
|
||||||
|
|
||||||
public String getStatusType() { return statusType; }
|
public String getStatusType() { return statusType; }
|
||||||
|
|
||||||
|
public String getLogChannel() { return logChannel; }
|
||||||
|
|
||||||
|
public KiafumiDB createDb() { return new KiafumiDB(username, password, host, port, database); }
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
package moe.oko.Kiafumi.listener;
|
||||||
|
|
||||||
|
import moe.oko.Kiafumi.Kiafumi;
|
||||||
|
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
|
||||||
|
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||||
|
|
||||||
|
import static moe.oko.Kiafumi.Kiafumi.info;
|
||||||
|
|
||||||
|
public class MainListener extends ListenerAdapter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onGuildJoin(GuildJoinEvent event) {
|
||||||
|
//Automatically create our default information for the server if we don't have it already.
|
||||||
|
info("Joined a new guild, NAME: " + event.getGuild().getName() + " ID: " + event.getGuild().getId());
|
||||||
|
Kiafumi.instance.getDatabase().createServerInformation(event.getGuild());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,66 @@
|
||||||
|
package moe.oko.Kiafumi.model;
|
||||||
|
|
||||||
|
import com.mysql.cj.xdevapi.Type;
|
||||||
|
import net.dv8tion.jda.api.entities.Guild;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
|
||||||
|
import static moe.oko.Kiafumi.Kiafumi.error;
|
||||||
|
import static moe.oko.Kiafumi.Kiafumi.info;
|
||||||
|
|
||||||
|
public class KiafumiDB {
|
||||||
|
private Connection connection;
|
||||||
|
|
||||||
|
private final String CREATE_SERVERINFO_TABLE = "CREATE TABLE IF NOT EXISTS `serverInfo`" +
|
||||||
|
"(`id` LONGTEXT NOT NULL,`welcomeEnabled` TINYINT NOT NULL,`welcomeChannel` LONGTEXT NULL);";
|
||||||
|
|
||||||
|
private final String INSERT_SERVERINFO_DEFAULT = "INSERT INTO serverInfo(id,welcomeEnabled,welcomeChannel) values (?,?,?)";
|
||||||
|
private final String SERVERINFO_EXISTS = "select * from serverInfo where id = ?";
|
||||||
|
|
||||||
|
public KiafumiDB(String username, String password, String host, int port, String db) {
|
||||||
|
try {
|
||||||
|
connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + db, username, password);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
error("Failed to initialize the MySQL instance. Contact a developer.");
|
||||||
|
ex.printStackTrace();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
initializeTables();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initializeTables() {
|
||||||
|
try {
|
||||||
|
connection.prepareStatement(CREATE_SERVERINFO_TABLE).execute();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
error("Failed to initialize SQL tables. They may need to be created manually.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean createServerInformation(Guild guild) {
|
||||||
|
try {
|
||||||
|
PreparedStatement prep = connection.prepareStatement(SERVERINFO_EXISTS);
|
||||||
|
prep.setInt(1, (int) guild.getIdLong());
|
||||||
|
ResultSet rsCheck = prep.executeQuery();
|
||||||
|
while (rsCheck.next()) {
|
||||||
|
//Server already exists, no need to initialize a section for it.
|
||||||
|
if(rsCheck.getInt(1) != 0) {
|
||||||
|
info("Server already existed. Skipping initialization.");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Proceed with making defaults.
|
||||||
|
PreparedStatement ps = connection.prepareStatement(INSERT_SERVERINFO_DEFAULT);
|
||||||
|
ps.setString(1, guild.getId());
|
||||||
|
ps.setInt(2, 0); //default is false.
|
||||||
|
ps.setNull(3, Types.LONGVARCHAR);
|
||||||
|
ps.execute();
|
||||||
|
return true;
|
||||||
|
} catch (Exception ex) {
|
||||||
|
error("Failed to create default server info for guild " + guild.getId());
|
||||||
|
ex.printStackTrace();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package moe.oko.Kiafumi.model;
|
||||||
|
|
||||||
|
public class Server {
|
||||||
|
private String id;
|
||||||
|
private boolean welcomeEnabled;
|
||||||
|
private String welcomeChannel;
|
||||||
|
}
|
|
@ -33,4 +33,10 @@ main:
|
||||||
#Activity Message
|
#Activity Message
|
||||||
activityMsg: "+help - gaaaaay"
|
activityMsg: "+help - gaaaaay"
|
||||||
#Status type (ONLINE|IDLE|DO_NOT_DISTURB|INVISIBLE)
|
#Status type (ONLINE|IDLE|DO_NOT_DISTURB|INVISIBLE)
|
||||||
statusType: "IDLE"
|
statusType: "IDLE"
|
||||||
|
sql:
|
||||||
|
host: "localhost"
|
||||||
|
port: 3306
|
||||||
|
database: "kiafumi"
|
||||||
|
username: "alpine"
|
||||||
|
password: "squidlover42069"
|
Loading…
Reference in New Issue