Implemented sharding

This commit is contained in:
Savvy 2017-11-10 16:28:47 -05:00
parent 6e5b7a0b42
commit 4e48aa3d6c
5 changed files with 43 additions and 4 deletions

View File

@ -5,9 +5,19 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.rixa.data.config.Configuration;
import io.rixa.utils.FileUtils;
import lombok.Getter;
import net.dv8tion.jda.core.AccountType;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.JDABuilder;
import net.dv8tion.jda.core.OnlineStatus;
import net.dv8tion.jda.core.entities.Game;
import net.dv8tion.jda.core.exceptions.RateLimitedException;
import net.dv8tion.jda.core.hooks.AnnotatedEventManager;
import javax.security.auth.login.LoginException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
public class Rixa {
@ -15,19 +25,47 @@ public class Rixa {
private static Rixa instance;
@Getter private Configuration configuration;
@Getter private ObjectMapper objectMapper;
@Getter private List<JDA> shardList;
@Getter private File defaultPath;
@Getter private Logger logger;
@Getter private JDA jda;
private Rixa() {
instance = this;
logger = Logger.getLogger(Rixa.class.getCanonicalName());
objectMapper = new ObjectMapper(new YAMLFactory());
defaultPath = new File("Rixa");
shardList = new ArrayList<>();
defaultPath.mkdirs();
loadConfiguration();
loadJDA();
registerCommands();
}
private void loadJDA() {
for (int i = 0; i < configuration.getShards(); i++) {
getLogger().info("Loading Shard #" + (i + 1) + "!");
try {
jda = new JDABuilder(AccountType.BOT)
.setToken(configuration.getToken())
.setGame(Game.of(configuration.getBotGame()))
.setEventManager(new AnnotatedEventManager())
.setAutoReconnect(true)
.setStatus(OnlineStatus.ONLINE)
.setAudioEnabled(true)
.setEnableShutdownHook(false)
.useSharding(i, configuration.getShards())
.buildBlocking();
getShardList().add(jda);
getLogger().info("Shard #" + (i + 1) + " has been loaded");
Thread.sleep(5000);
} catch (LoginException | RateLimitedException | InterruptedException e) {
e.printStackTrace();
}
}
Runtime.getRuntime().addShutdownHook(new Thread(() -> getShardList().forEach(JDA::shutdown)));
}
private void registerCommands() {}
private void loadConfiguration() {

View File

@ -8,9 +8,10 @@ import java.util.Map;
public class Configuration {
@Getter @Setter private String token, botGame;
@Getter @Setter private List<String> botAdmins;
@Getter @Setter private Map<String, String> sqlCredentials;
@Getter @Setter private List<String> botAdmins;
@Getter @Setter private String token, botGame;
@Getter @Setter private int shards;
public Configuration() {}
}

View File

@ -11,7 +11,6 @@ public class GuildMapper implements RowMapper<RixaGuild> {
@Override
public RixaGuild mapRow(ResultSet resultSet, int i) throws SQLException {
RixaGuild guild = new RixaGuild(null);
guild.load(resultSet);
// Register guild;
return guild;
}

View File

@ -1,5 +1,6 @@
token: "YOUR_TOKEN_HERE"
botGame: "Rixa 2.0 | http://rixa.io/invite"
shards: 5
botAdmins:
- "YOUR_USER_ID_HERE"
- "OTHER_ADMINS"