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>
|
||||
<version>1.3.53</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.28</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
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.JDABuilder;
|
||||
import net.dv8tion.jda.api.OnlineStatus;
|
||||
|
@ -15,12 +18,15 @@ import org.slf4j.LoggerFactory;
|
|||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
|
||||
public class Kiafumi {
|
||||
|
||||
private File CONFIG_FILE = new File("config.yml");
|
||||
|
||||
public YamlConfiguration yamlConfiguration;
|
||||
public YamlConfiguration yamlConfiguration = new YamlConfiguration();
|
||||
|
||||
public Logger logger = LoggerFactory.getLogger("Kiafumi");
|
||||
|
||||
|
@ -32,10 +38,12 @@ public class Kiafumi {
|
|||
|
||||
public KiafumiConfig config;
|
||||
|
||||
public KiafumiDB database;
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
Kiafumi shiki = new Kiafumi();
|
||||
shiki.start();
|
||||
Kiafumi kia = new Kiafumi();
|
||||
kia.start();
|
||||
} catch (Exception ex) {
|
||||
System.out.println("Failed to start Kiafumi Instance, check your Java installation.");
|
||||
ex.printStackTrace();
|
||||
|
@ -51,21 +59,23 @@ public class Kiafumi {
|
|||
logger.info("Config load start...");
|
||||
|
||||
if (!CONFIG_FILE.exists()) {
|
||||
try {
|
||||
CONFIG_FILE.createNewFile();
|
||||
} catch (IOException ex) {
|
||||
logger.warn("Failed to create the configuration file. Stopping. (Try creating configuration directory.)");
|
||||
try (InputStream is = this.getClass().getClassLoader().getResourceAsStream("config.yml")) {
|
||||
//Save the default cfg
|
||||
Files.copy(is, CONFIG_FILE.toPath());
|
||||
} catch (Exception ex) {
|
||||
logger.warn("Failed to create the configuration file. Stopping. (" + CONFIG_FILE.getAbsolutePath() + ")");
|
||||
ex.printStackTrace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
yamlConfiguration.load(CONFIG_FILE);
|
||||
yamlConfiguration.load("config.yml");
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
logger.warn("File not found, attempting to create.");
|
||||
|
||||
} catch (IOException | InvalidConfigurationException e) {
|
||||
logger.warn("File not found, must've failed to create...");
|
||||
} catch (Exception e) {
|
||||
logger.warn("Ensure all values are inputted properly.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
@ -81,6 +91,8 @@ public class Kiafumi {
|
|||
|
||||
PREFIX = config.getPrefix();
|
||||
|
||||
database = config.createDb();
|
||||
|
||||
//Makes our JDA instance.
|
||||
startDiscord();
|
||||
}
|
||||
|
@ -92,16 +104,20 @@ public class Kiafumi {
|
|||
try {
|
||||
JDA = JDABuilder.create(config.getToken(), GatewayIntent.GUILD_MEMBERS,
|
||||
GatewayIntent.GUILD_MESSAGES,
|
||||
GatewayIntent.DIRECT_MESSAGES)
|
||||
GatewayIntent.DIRECT_MESSAGES, GatewayIntent.GUILD_PRESENCES)
|
||||
.setChunkingFilter(ChunkingFilter.ALL)
|
||||
.setMemberCachePolicy(MemberCachePolicy.ALL)
|
||||
.setActivity(Activity.of(Activity.ActivityType.valueOf(config.getActivityType()), config.getActivityMsg()))
|
||||
.setStatus(OnlineStatus.valueOf(config.getStatusType()))
|
||||
.addEventListeners().build().awaitReady();
|
||||
.addEventListeners(new MainListener()).build().awaitReady();
|
||||
} catch(Exception ex) {
|
||||
error("Initialization broke...");
|
||||
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) {
|
||||
instance.logger.error(str);
|
||||
}
|
||||
|
||||
public KiafumiDB getDatabase() { return database; }
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package moe.oko.Kiafumi;
|
||||
|
||||
import moe.oko.Kiafumi.model.KiafumiDB;
|
||||
import org.simpleyaml.configuration.ConfigurationSection;
|
||||
import org.simpleyaml.configuration.file.YamlConfiguration;
|
||||
|
||||
|
@ -29,6 +30,15 @@ public class KiafumiConfig {
|
|||
private String activityMsg;
|
||||
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) {
|
||||
//Load config on class creation...
|
||||
this.configuration = configuration;
|
||||
|
@ -58,6 +68,13 @@ public class KiafumiConfig {
|
|||
activityType = main.getString("activityType");
|
||||
activityMsg = main.getString("activityMsg");
|
||||
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) {
|
||||
ex.printStackTrace();
|
||||
error("Failed to load configuration!");
|
||||
|
@ -82,4 +99,8 @@ public class KiafumiConfig {
|
|||
public String getActivityMsg() { return activityMsg; }
|
||||
|
||||
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
|
||||
activityMsg: "+help - gaaaaay"
|
||||
#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