use of net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder in project Utility by anweisen.
the class DiscordBot method init.
protected final void init() throws Exception {
if (initialized)
throw new IllegalStateException("init() was already called");
initialized = true;
builder = builder().validate();
DiscordBotBuilder.logger.debug("Building bot with following configuration:" + "\n\t" + config + "\n\t" + builder + "\n\t" + (builder.databaseConfig == null ? "BotDatabaseConfig{null}" : builder.databaseConfig));
boolean requireDatabase = builder.databaseConfig != null || !builder.tables.isEmpty() || builder.requireDatabase;
if (builder.databaseConfig == null) {
builder.databaseConfig = new BotDatabaseConfig("guilds", "guildId", null, null, null);
}
if (requireDatabase) {
database = config.createDatabase();
database.connect();
builder.tables.forEach((ExceptionallyBiConsumer<String, SQLColumn[]>) database::createTable);
} else {
database = Database.unsupported();
}
builder.fileLanguages.addAll(config.getLanguageFiles());
commandManager = new DefaultCommandManager(builder.databaseConfig.getPrefixColumn() == null ? new ConstantPrefixProvider(config.getDefaultPrefix()) : new DatabasePrefixProvider(config.getDefaultPrefix(), database, builder.databaseConfig.getGuildTable(), builder.databaseConfig.getGuildKeyColumn(), builder.databaseConfig.getPrefixColumn())).setUseEmbeds(builder.useEmbeds);
if (builder.databaseConfig.getTeamRoleColumn() != null) {
commandManager.setTeamRoleManager(new DatabaseTeamRoleManager(database, builder.databaseConfig.getGuildTable(), builder.databaseConfig.getGuildKeyColumn(), builder.databaseConfig.getTeamRoleColumn()));
}
if (builder.databaseConfig.getLanguageColumn() != null) {
LanguageManager languageManager = new DatabaseLanguageManager(database, builder.databaseConfig.getGuildTable(), builder.databaseConfig.getGuildKeyColumn(), builder.databaseConfig.getLanguageColumn());
commandManager.setLanguageManager(languageManager);
for (String filename : builder.fileLanguages) languageManager.readFile(filename);
for (String filename : builder.resourceLanguages) languageManager.readResource(filename);
languageManager.setDefaultLanguage(config.getDefaultLanguage());
} else if (!builder.fileLanguages.isEmpty() || !builder.resourceLanguages.isEmpty()) {
DiscordBotBuilder.logger.warn("Languages were registered but no database for language management is setup!");
LanguageManager languageManager = new ConstantLanguageManager();
commandManager.setLanguageManager(languageManager);
for (String filename : builder.fileLanguages) languageManager.readFile(filename);
for (String filename : builder.resourceLanguages) languageManager.readResource(filename);
languageManager.setDefaultLanguage(config.getDefaultLanguage());
}
builder.parsers.forEach((key, parser) -> commandManager.getParserContext().registerParser(key, parser.getFirst(), parser.getSecond()));
commandManager.register(builder.commands);
builder.taskCommands.forEach(pair -> commandManager.register(pair.getFirst(), pair.getSecond()));
List<GatewayIntent> intents = Arrays.asList(builder.intents);
if (!intents.contains(GatewayIntent.DIRECT_MESSAGES))
DiscordBotBuilder.logger.warn("Missing GatewayIntent.DIRECT_MESSAGES, no commands will be available in private chats");
if (!intents.contains(GatewayIntent.GUILD_MESSAGES))
DiscordBotBuilder.logger.warn("Missing GatewayIntent.GUILD_MESSAGES, no commands will be available in guild chats");
DefaultShardManagerBuilder shardManagerBuilder = DefaultShardManagerBuilder.create(config.getToken(), intents).setCallbackPoolProvider(newThreadPoolProvider("Callback")).setEventPoolProvider(newThreadPoolProvider("Events")).setShardsTotal(config.getShards()).setMemberCachePolicy(builder.memberCachePolicy).setEventManagerProvider(shardId -> eventManager).setStatusProvider(shardId -> config.getOnlineStatus()).addEventListeners(new CommandListener(commandManager), this);
if (builder.chunkingFilter != null)
shardManagerBuilder.setChunkingFilter(builder.chunkingFilter);
builder.builderSettings.forEach(action -> action.accept(shardManagerBuilder));
builder.listener.forEach(shardManagerBuilder::addEventListeners);
for (CacheFlag cache : CacheFlag.values()) {
if (builder.cacheFlags.contains(cache)) {
shardManagerBuilder.enableCache(cache);
} else {
shardManagerBuilder.disableCache(cache);
}
}
shardManager = shardManagerBuilder.build();
builder.shardManagerSettings.forEach(action -> action.accept(shardManager));
if (!builder.customSlashCommands.isEmpty())
getJDA().updateCommands().addCommands(builder.customSlashCommands).queue();
else if (!builder.disableAutoSlashCommands)
commandManager.setupSlashCommands(getJDA());
getJDA().retrieveApplicationInfo().queue(applicationInfo -> this.applicationInfo = applicationInfo);
onStart();
if (!builder.activities.isEmpty()) {
new Timer("BotActivityChanger").schedule(new TimerTask() {
private int index = 0;
@Override
public void run() {
if (index >= builder.activities.size()) {
index = 0;
}
Supplier<? extends Activity> activitySupplier = builder.activities.get(index);
Activity activity = activitySupplier.get();
shardManager.setActivity(activity);
index++;
}
}, 3000, builder.activityUpdateRate * 1000L);
}
}
use of net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder in project Baymax by napstr.
the class ShardManagerConfiguration method shardManager.
// we manage the lifecycle ourselves tyvm, see shutdown hook in the launcher
@Bean(destroyMethod = "")
public ShardManager shardManager(BaymaxConfig baymaxConfig, OkHttpClient.Builder httpClientBuilder, ScheduledThreadPoolExecutor jdaThreadPool, EventWaiter eventWaiter, HelpDeskListener helpDeskListener) throws LoginException {
DefaultShardManagerBuilder shardBuilder = DefaultShardManagerBuilder.createDefault(baymaxConfig.getDiscordToken()).setChunkingFilter(// we need to fetch members from the cache at several places
ChunkingFilter.ALL).enableIntents(// required for chunking
GatewayIntent.GUILD_MEMBERS).addEventListeners(eventWaiter).addEventListeners(helpDeskListener).setHttpClientBuilder(httpClientBuilder.retryOnConnectionFailure(false)).setEnableShutdownHook(false).setRateLimitPool(jdaThreadPool, false).setCallbackPool(jdaThreadPool, false).disableCache(EnumSet.allOf(CacheFlag.class));
String statusMessage = baymaxConfig.getStatusMessage();
if (!ObjectUtils.isEmpty(statusMessage)) {
Activity.ActivityType activityType = Activity.ActivityType.fromKey(baymaxConfig.getStatusType());
Activity discordStatus = Activity.of(activityType, statusMessage);
shardBuilder.setActivity(discordStatus);
}
return shardBuilder.build();
}
use of net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder in project Me-e--bypasser by JDiscordBots.
the class MeeBypasser method main.
public static void main(String[] args) {
File tokenFile = new File(".token");
if (tokenFile.exists()) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(tokenFile), StandardCharsets.UTF_8))) {
final DefaultShardManagerBuilder builder = DefaultShardManagerBuilder.createLight(reader.readLine(), GatewayIntent.getIntents(GatewayIntent.DEFAULT)).setAutoReconnect(true).setStatus(OnlineStatus.ONLINE).setActivity(Activity.watching("https://github.com/JDiscordBots/Mee6-bypasser")).setRequestTimeoutRetry(true);
MessageAction.setDefaultMentions(Collections.emptySet());
CommandFramework framework = new CommandFramework();
framework.setSlashCommandsPerGuild(true);
framework.setPrefix("mb!");
builder.addEventListeners(framework.build(), new MsgListener());
ShardManager manager = builder.build();
for (JDA jda : manager.getShards()) {
jda.awaitReady();
}
} catch (LoginException | IOException e) {
LOG.error("Cannot initialize bot", e);
} catch (InterruptedException e) {
LOG.warn("The main thread was interruped while waiting for a shard to connect initially", e);
Thread.currentThread().interrupt();
}
} else {
try {
Files.createFile(tokenFile.toPath());
} catch (IOException e) {
LOG.error("Cannot create token file.", e);
}
}
}
Aggregations