Use JDA 4.2.0's new intent and member caching policies

parent af55c08c
Pipeline #136053646 passed with stages
in 11 minutes and 38 seconds
......@@ -48,7 +48,7 @@ fun main() {
logger.info("Starting background process to remove unused Guilds.")
Timer("remove-old-guilds", true)
.scheduleAtFixedRate(0L, Duration.ofDays(1L).toMillis()) {
val jda = it.api
val jda = it.api()
val afterDays = app.koin.getProperty("BOT_LEAVE_GUILD_AFTER", 30)
if (afterDays <= 0) {
......
......@@ -4,7 +4,7 @@ import fi.iki.elonen.NanoHTTPD
import tech.gdragon.discord.Bot
class HttpServer(bot: Bot, val port: Int = 8080) {
val inviteUrl: String = bot.api.getInviteUrl(Bot.PERMISSIONS)
val inviteUrl: String = bot.api().getInviteUrl(Bot.PERMISSIONS)
val server = object : NanoHTTPD(port) {
override fun serve(session: IHTTPSession): Response {
......
......@@ -3,8 +3,11 @@ package tech.gdragon.discord
import mu.KotlinLogging
import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.Permission
import net.dv8tion.jda.api.requests.GatewayIntent
import net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder
import net.dv8tion.jda.api.sharding.ShardManager
import net.dv8tion.jda.api.utils.ChunkingFilter
import net.dv8tion.jda.api.utils.MemberCachePolicy
import net.dv8tion.jda.api.utils.cache.CacheFlag
import org.koin.core.KoinComponent
import tech.gdragon.commands.CommandHandler
......@@ -35,22 +38,26 @@ class Bot : KoinComponent {
)
}
lateinit var api: JDA
lateinit var shardManager: ShardManager
fun api(): JDA {
while (!shardManager.statuses.any { it.value == JDA.Status.CONNECTED }) {
Thread.sleep(50)
}
return shardManager.shards.find { shard -> shard.status == JDA.Status.CONNECTED }!!
}
init {
try {
// create shard manager
val shardManager = DefaultShardManagerBuilder(token)
.setDisabledCacheFlags(EnumSet.of(CacheFlag.ACTIVITY, CacheFlag.EMOTE, CacheFlag.CLIENT_STATUS))
shardManager = DefaultShardManagerBuilder
.create(token, GatewayIntent.GUILD_MESSAGES, GatewayIntent.GUILD_VOICE_STATES)
.disableCache(CacheFlag.ACTIVITY, CacheFlag.EMOTE, CacheFlag.CLIENT_STATUS)
.setChunkingFilter(ChunkingFilter.NONE)
.setMemberCachePolicy(MemberCachePolicy.VOICE)
.addEventListeners(EventListener(), SystemEventListener())
.build()
while (!shardManager.statuses.all { it.value == JDA.Status.CONNECTED }) {
Thread.sleep(50)
}
api = shardManager.shards.first()
} catch (e: LoginException) {
logger.error(e) {
"Could not authenticate using token: $token"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment