Added implementation for Guilds, SQL, and more

This commit is contained in:
Savvy 2017-11-10 20:42:57 -05:00
parent 91a77f0744
commit 73614bbf12
10 changed files with 119 additions and 14 deletions

View File

@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.rixa.bot.commands.CommandHandler; import io.rixa.bot.commands.CommandHandler;
import io.rixa.bot.data.config.Configuration; import io.rixa.bot.data.config.Configuration;
import io.rixa.bot.data.storage.DatabaseAdapter;
import io.rixa.bot.utils.FileUtils; import io.rixa.bot.utils.FileUtils;
import lombok.Getter; import lombok.Getter;
import net.dv8tion.jda.core.AccountType; import net.dv8tion.jda.core.AccountType;
@ -84,6 +85,7 @@ public class Rixa {
logger.severe("Could not properly load configuration file!."); logger.severe("Could not properly load configuration file!.");
e.printStackTrace(); e.printStackTrace();
} }
DatabaseAdapter.getInstance().check();
} }
public static Rixa getInstance() { public static Rixa getInstance() {

View File

@ -1,4 +0,0 @@
package io.rixa.bot.data;
public interface DataStorage {
}

View File

@ -14,4 +14,8 @@ public class Configuration {
@Getter @Setter private int shards; @Getter @Setter private int shards;
public Configuration() {} public Configuration() {}
public boolean isBotAdmin(String s) {
return botAdmins.contains(s);
}
} }

View File

@ -0,0 +1,46 @@
package io.rixa.bot.data.storage;
import io.rixa.bot.Rixa;
import io.rixa.bot.data.config.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
public class DatabaseAdapter {
private static DatabaseAdapter instance;
private Rixa rixaInstance;
private JdbcTemplate jdbcTemplate;
private DatabaseAdapter() {
instance = this;
rixaInstance = Rixa.getInstance();
}
public void check() {
if (jdbcTemplate != null) {
return;
}
Configuration config = rixaInstance.getConfiguration();
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl(
String.format("jdbc:mysql://%s:%s/%s", config.getSqlCredentials().get("hostName"),
config.getSqlCredentials().get("port"), config.getSqlCredentials().get("databaseName")));
dataSource.setUsername(config.getSqlCredentials().get("userName"));
dataSource.setPassword(config.getSqlCredentials().get("password"));
jdbcTemplate = new JdbcTemplate(dataSource);
}
public JdbcTemplate get() {
check();
return jdbcTemplate;
}
public boolean exists(String table, String key, String value) {
int amount = get().queryForObject(String.format("SELECT COUNT(*) FROM `%s` WHERE `%s` = ?", table, key), new Object[] { value }, Integer.class);
return amount > 0;
}
public static DatabaseAdapter getInstance() {
return ( (instance == null) ? instance = new DatabaseAdapter() : instance );
}
}

View File

@ -1,6 +0,0 @@
package io.rixa.bot.data.storage;
import io.rixa.bot.data.DataStorage;
public class MySQLStorage implements DataStorage {
}

View File

@ -0,0 +1,13 @@
package io.rixa.bot.events;
import io.rixa.bot.guild.manager.GuildManager;
import net.dv8tion.jda.core.events.ReadyEvent;
import net.dv8tion.jda.core.hooks.SubscribeEvent;
public class ReadyListener {
@SubscribeEvent
public void onReady(ReadyEvent event) {
event.getJDA().getGuilds().forEach(guild -> GuildManager.getInstance().addGuild(guild));
}
}

View File

@ -3,19 +3,27 @@ package io.rixa.bot.guild;
import io.rixa.bot.guild.manager.IGuild; import io.rixa.bot.guild.manager.IGuild;
import io.rixa.bot.guild.modules.RixaModule; import io.rixa.bot.guild.modules.RixaModule;
import lombok.Getter; import lombok.Getter;
import lombok.Setter;
import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.Guild;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
public class RixaGuild implements IGuild { public class RixaGuild implements IGuild {
@Getter private final String id;
@Getter private final Map<String, RixaModule> modules; @Getter private final Map<String, RixaModule> modules;
@Getter @Setter private List<String> keywords;
@Getter @Setter private String description;
@Getter private final String id;
@Getter private Guild guild;
public RixaGuild(Guild guild) { public RixaGuild(Guild guild) {
this.guild = guild;
id = guild.getId(); id = guild.getId();
modules = new HashMap<>(); modules = new HashMap<>();
keywords = new ArrayList<>();
load(); load();
} }

View File

@ -1,4 +1,37 @@
package io.rixa.bot.guild.manager; package io.rixa.bot.guild.manager;
import io.rixa.bot.data.storage.DatabaseAdapter;
import io.rixa.bot.guild.RixaGuild;
import net.dv8tion.jda.core.entities.Guild;
import java.util.HashMap;
import java.util.Map;
public class GuildManager { public class GuildManager {
private Map<String, RixaGuild> rixaGuildMap = new HashMap<>();
private GuildManager() {
instance = this;
}
private static GuildManager instance;
public static GuildManager getInstance() {
return (instance == null) ? new GuildManager() : instance;
}
public void addGuild(Guild guild) {
if (!(DatabaseAdapter.getInstance().exists("core", "guild_id", guild.getId()))) {
insert(guild);
}
RixaGuild rixaGuild = (RixaGuild) DatabaseAdapter.getInstance().get().queryForObject(
"SELECT * FROM `core` WHERE `guild_name` = ?", new Object[] { guild.getId() }, new GuildMapper());
rixaGuildMap.put(guild.getId(), rixaGuild);
}
private void insert(Guild guild) {
DatabaseAdapter.getInstance().get().update
("INSERT INTO `core` (`guild_id`, `guild_name`, `description`, `keywords`) VALUES (?, ?, ?, ?)",
guild.getId(), guild.getName(), "Description not set.", "No Keywords Found.");
}
} }

View File

@ -5,12 +5,17 @@ import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
public class GuildMapper implements RowMapper<RixaGuild> { public class GuildMapper implements RowMapper<IGuild> {
@Override @Override
public RixaGuild mapRow(ResultSet resultSet, int i) throws SQLException { public IGuild mapRow(ResultSet resultSet, int i) throws SQLException {
RixaGuild guild = new RixaGuild(null); IGuild guild = new RixaGuild(null);
guild.setDescription(resultSet.getString("description"));
List<String> keywords = Arrays.asList((String[])resultSet.getArray("keywords").getArray());
guild.setKeywords(keywords);
// Register guild; // Register guild;
return guild; return guild;
} }

View File

@ -2,6 +2,8 @@ package io.rixa.bot.guild.manager;
import io.rixa.bot.guild.modules.RixaModule; import io.rixa.bot.guild.modules.RixaModule;
import java.util.List;
public interface IGuild { public interface IGuild {
String getId(); String getId();
@ -10,4 +12,6 @@ public interface IGuild {
RixaModule getModule(String id); RixaModule getModule(String id);
RixaModule registerModule(String id, RixaModule module); RixaModule registerModule(String id, RixaModule module);
boolean isRegistered(String id); boolean isRegistered(String id);
void setDescription(String description);
void setKeywords(List<String> keywords);
} }