Added implementation for Guilds, SQL, and more
This commit is contained in:
parent
91a77f0744
commit
73614bbf12
@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
||||
import io.rixa.bot.commands.CommandHandler;
|
||||
import io.rixa.bot.data.config.Configuration;
|
||||
import io.rixa.bot.data.storage.DatabaseAdapter;
|
||||
import io.rixa.bot.utils.FileUtils;
|
||||
import lombok.Getter;
|
||||
import net.dv8tion.jda.core.AccountType;
|
||||
@ -84,6 +85,7 @@ public class Rixa {
|
||||
logger.severe("Could not properly load configuration file!.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
DatabaseAdapter.getInstance().check();
|
||||
}
|
||||
|
||||
public static Rixa getInstance() {
|
||||
|
@ -1,4 +0,0 @@
|
||||
package io.rixa.bot.data;
|
||||
|
||||
public interface DataStorage {
|
||||
}
|
@ -14,4 +14,8 @@ public class Configuration {
|
||||
@Getter @Setter private int shards;
|
||||
|
||||
public Configuration() {}
|
||||
|
||||
public boolean isBotAdmin(String s) {
|
||||
return botAdmins.contains(s);
|
||||
}
|
||||
}
|
||||
|
46
src/main/java/io/rixa/bot/data/storage/DatabaseAdapter.java
Normal file
46
src/main/java/io/rixa/bot/data/storage/DatabaseAdapter.java
Normal 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 );
|
||||
}
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
package io.rixa.bot.data.storage;
|
||||
|
||||
import io.rixa.bot.data.DataStorage;
|
||||
|
||||
public class MySQLStorage implements DataStorage {
|
||||
}
|
13
src/main/java/io/rixa/bot/events/ReadyListener.java
Normal file
13
src/main/java/io/rixa/bot/events/ReadyListener.java
Normal 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));
|
||||
}
|
||||
}
|
@ -3,19 +3,27 @@ package io.rixa.bot.guild;
|
||||
import io.rixa.bot.guild.manager.IGuild;
|
||||
import io.rixa.bot.guild.modules.RixaModule;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import net.dv8tion.jda.core.entities.Guild;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class RixaGuild implements IGuild {
|
||||
|
||||
@Getter private final String id;
|
||||
@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) {
|
||||
this.guild = guild;
|
||||
id = guild.getId();
|
||||
modules = new HashMap<>();
|
||||
keywords = new ArrayList<>();
|
||||
load();
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,37 @@
|
||||
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 {
|
||||
|
||||
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.");
|
||||
}
|
||||
}
|
||||
|
@ -5,12 +5,17 @@ import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
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
|
||||
public RixaGuild mapRow(ResultSet resultSet, int i) throws SQLException {
|
||||
RixaGuild guild = new RixaGuild(null);
|
||||
public IGuild mapRow(ResultSet resultSet, int i) throws SQLException {
|
||||
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;
|
||||
return guild;
|
||||
}
|
||||
|
@ -2,6 +2,8 @@ package io.rixa.bot.guild.manager;
|
||||
|
||||
import io.rixa.bot.guild.modules.RixaModule;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface IGuild {
|
||||
|
||||
String getId();
|
||||
@ -10,4 +12,6 @@ public interface IGuild {
|
||||
RixaModule getModule(String id);
|
||||
RixaModule registerModule(String id, RixaModule module);
|
||||
boolean isRegistered(String id);
|
||||
void setDescription(String description);
|
||||
void setKeywords(List<String> keywords);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user