Implemented sharding
This commit is contained in:
parent
6e5b7a0b42
commit
4e48aa3d6c
@ -5,9 +5,19 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
|||||||
import io.rixa.data.config.Configuration;
|
import io.rixa.data.config.Configuration;
|
||||||
import io.rixa.utils.FileUtils;
|
import io.rixa.utils.FileUtils;
|
||||||
import lombok.Getter;
|
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.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class Rixa {
|
public class Rixa {
|
||||||
@ -15,19 +25,47 @@ public class Rixa {
|
|||||||
private static Rixa instance;
|
private static Rixa instance;
|
||||||
@Getter private Configuration configuration;
|
@Getter private Configuration configuration;
|
||||||
@Getter private ObjectMapper objectMapper;
|
@Getter private ObjectMapper objectMapper;
|
||||||
|
@Getter private List<JDA> shardList;
|
||||||
@Getter private File defaultPath;
|
@Getter private File defaultPath;
|
||||||
@Getter private Logger logger;
|
@Getter private Logger logger;
|
||||||
|
@Getter private JDA jda;
|
||||||
|
|
||||||
private Rixa() {
|
private Rixa() {
|
||||||
instance = this;
|
instance = this;
|
||||||
logger = Logger.getLogger(Rixa.class.getCanonicalName());
|
logger = Logger.getLogger(Rixa.class.getCanonicalName());
|
||||||
objectMapper = new ObjectMapper(new YAMLFactory());
|
objectMapper = new ObjectMapper(new YAMLFactory());
|
||||||
defaultPath = new File("Rixa");
|
defaultPath = new File("Rixa");
|
||||||
|
shardList = new ArrayList<>();
|
||||||
defaultPath.mkdirs();
|
defaultPath.mkdirs();
|
||||||
loadConfiguration();
|
loadConfiguration();
|
||||||
|
loadJDA();
|
||||||
registerCommands();
|
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 registerCommands() {}
|
||||||
|
|
||||||
private void loadConfiguration() {
|
private void loadConfiguration() {
|
||||||
|
@ -8,9 +8,10 @@ import java.util.Map;
|
|||||||
|
|
||||||
public class Configuration {
|
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 Map<String, String> sqlCredentials;
|
||||||
|
@Getter @Setter private List<String> botAdmins;
|
||||||
|
@Getter @Setter private String token, botGame;
|
||||||
|
@Getter @Setter private int shards;
|
||||||
|
|
||||||
public Configuration() {}
|
public Configuration() {}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ public class RixaGuild implements IGuild {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void load() { }
|
public void load() {}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void save() { }
|
public void save() { }
|
||||||
|
@ -11,7 +11,6 @@ public class GuildMapper implements RowMapper<RixaGuild> {
|
|||||||
@Override
|
@Override
|
||||||
public RixaGuild mapRow(ResultSet resultSet, int i) throws SQLException {
|
public RixaGuild mapRow(ResultSet resultSet, int i) throws SQLException {
|
||||||
RixaGuild guild = new RixaGuild(null);
|
RixaGuild guild = new RixaGuild(null);
|
||||||
guild.load(resultSet);
|
|
||||||
// Register guild;
|
// Register guild;
|
||||||
return guild;
|
return guild;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
token: "YOUR_TOKEN_HERE"
|
token: "YOUR_TOKEN_HERE"
|
||||||
botGame: "Rixa 2.0 | http://rixa.io/invite"
|
botGame: "Rixa 2.0 | http://rixa.io/invite"
|
||||||
|
shards: 5
|
||||||
botAdmins:
|
botAdmins:
|
||||||
- "YOUR_USER_ID_HERE"
|
- "YOUR_USER_ID_HERE"
|
||||||
- "OTHER_ADMINS"
|
- "OTHER_ADMINS"
|
||||||
|
Loading…
Reference in New Issue
Block a user