Skip to content

Commit

Permalink
EasyCommands v0.8.11
Browse files Browse the repository at this point in the history
Fixed potential error with MySQL not creating the GuildProperties table correctly on other bots than EasyCommands ??
  • Loading branch information
FrostedCA committed Jan 12, 2024
1 parent 2bead6a commit d88ae4d
Showing 1 changed file with 22 additions and 38 deletions.
60 changes: 22 additions & 38 deletions src/main/java/ca/tristan/easycommands/EasyCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,31 +142,25 @@ public JDA buildJDA() throws InterruptedException {
}

if(configFile.getUseMySQL()) {
try {
this.mysqlInit();
} catch (SQLException e) {
throw new RuntimeException(e);
}
this.mysqlInit();
} else {
Logger.log(LogType.OK, "Not using MySQL -> Saving properties locally.");
}

if(configFile.getUseMusicBot()) {
enableMusicBot();
this.jda.addEventListener(new AutoDisconnectEvent(), new ButtonEvents());
}

if(configFile.getUsePrefixCommands()) {
this.jda.addEventListener(prefixCommands);
}

this.jda.addEventListener(new AutoRoleEvents());

updateCommands();
logCurrentExecutors();

/*
* Loads the properties for a guild. (Log channel id, auto roles, etc.);
* Loads the properties for each guild. (Log channel id, auto roles, etc.)
*/
ecGuilds = new ECGuild[jda.getGuilds().size()];
for (int index = 0; index < jda.getGuilds().size(); index++) {
Expand Down Expand Up @@ -220,7 +214,7 @@ public PrefixCommands getPrefixCommands() {
/**
* Connects a MySQL database to EasyCommands.
*/
private void mysqlInit() throws SQLException {
private void mysqlInit() {
mysql = new MySQL(configFile.getDBHost(), configFile.getDBPort(), configFile.getDBName(), configFile.getDBUser(), configFile.getDBPassword());
try {
mysql.connect();
Expand All @@ -230,39 +224,28 @@ private void mysqlInit() throws SQLException {
return;
}

if(mysql.checkConnection(0)) {
DatabaseMetaData dbm = mysql.getConnection().getMetaData();
ResultSet tables = dbm.getTables(null, null, "guildProperties", null);
if(tables.next()) {
// Means that the table is valid
// Return the code here to let the bot start and register the guilds using mysql; See more inside: ECGuild.java
return;
}
try {
if(mysql.checkConnection(0)) {
DatabaseMetaData dbm = mysql.getConnection().getMetaData();
ResultSet tables = dbm.getTables(null, null, "guildproperties", null);
if(tables.next()) {
// Means that the table is valid
// Return the code here to let the bot start and register the guilds using mysql; See more inside: ECGuild.java
Logger.log(LogType.OK, "Database GuildProperties table is valid.");
return;
}

// Create table when missing
String table = "CREATE TABLE guildProperties ( guildId varchar(255) primary key, member_role varchar(255), bot_role varchar(255), music_channel varchar(255), log_channel varchar(255) )";
PreparedStatement preparedStatement = mysql.getConnection().prepareStatement(table);
preparedStatement.execute();
// Create table when missing
String table = "CREATE TABLE guildproperties ( guildId varchar(255) primary key, member_role varchar(255), bot_role varchar(255), music_channel varchar(255), log_channel varchar(255) )";
PreparedStatement preparedStatement = mysql.getConnection().prepareStatement(table);
preparedStatement.execute();
Logger.log(LogType.OK, "Database GuildProperties table has been created.");
}
} catch (SQLException e) {
Logger.log(LogType.ERROR, "Database Table couldn't be created.");
}
}

private void loadMySQLGuilds() throws SQLException {

if(jda.getGuilds().isEmpty()) {
return;
}

PreparedStatement preparedStatement = mysql.getConnection().prepareStatement("SELECT * FROM guildProperties");
ResultSet rs = preparedStatement.executeQuery();
while(rs.next()) {
if((rs.getString(1) == null || rs.getString(1).isEmpty()) || (rs.getString(2) == null || rs.getString(2).isEmpty())) {
continue;
}
if(jda.getGuilds().contains(jda.getGuildById(rs.getString(1)))) {
Guild guild = jda.getGuildById(rs.getString(1));
guildsMusicChannel.put(guild, guild.getTextChannelById(rs.getString(2)));
}
}
}

public static MySQL getMySQL() {
Expand Down Expand Up @@ -355,6 +338,7 @@ public EasyCommands registerListeners(ListenerAdapter... listeners) {

private void enableMusicBot() {
this.addExecutor(new PlayCmd(this), new StopCmd(this), new NowPlayingCmd(this), new SkipCmd(this), new PauseCmd(this), new LyricsCmd(this), new SetMusicChannel());
this.jda.addEventListener(new AutoDisconnectEvent(), new ButtonEvents());
Logger.log(LogType.OK, "EasyCommands MusicBot has been enabled successfully.");
}

Expand Down

0 comments on commit d88ae4d

Please sign in to comment.