Skip to content
This repository was archived by the owner on Mar 7, 2024. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Start on swapping out JDA for Discord4j
  • Loading branch information
duncte123 committed Jul 8, 2021
commit f6b653ca23a424ee437c7edf0e5717b0d4700213
13 changes: 10 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ dependencies {
implementation(group = "com.fasterxml.jackson.core", name = "jackson-databind", version = "2.10.1")

implementation(group = "net.sf.trove4j", name = "trove4j", version = "3.0.3")

implementation(group = "com.discord4j", name = "discord4j-core", version = "3.1.6")

// TODO: remove after switch to d4j
implementation(group = "net.dv8tion", name = "JDA", version = "4.2.1_264") {
exclude(module = "opus-java")
}

// TODO: remove after switch to d4j
// TODO: custom oauth client?
// implementation(group = "com.jagrosh", name = "jda-utilities-oauth2", version = "3.0.5")
implementation(group = "com.github.JDA-Applications", name = "JDA-Utilities", version = "804d58a") {
// This is fine
Expand All @@ -43,9 +53,6 @@ dependencies {
exclude(module = "jda-utilities-command")
exclude(module = "jda-utilities-menu")
}
implementation(group = "net.dv8tion", name = "JDA", version = "4.2.1_264") {
exclude(module = "opus-java")
}

// Yes, this is JDA
// We're running this PR https://github.com/DV8FromTheWorld/JDA/pull/1178
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/dunctebot/dashboard/Container.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package com.dunctebot.dashboard

import com.dunctebot.dashboard.websocket.WebsocketClient
import com.dunctebot.duncteapi.DuncteApi
import com.dunctebot.jda.JDARestClient
import com.dunctebot.jda.DiscordRestClient
import com.fasterxml.jackson.databind.json.JsonMapper
import okhttp3.OkHttpClient

val restJDA = JDARestClient(System.getenv("BOT_TOKEN"))
val restJDA = DiscordRestClient(System.getenv("BOT_TOKEN"))
val duncteApis = DuncteApi("Bot ${System.getenv("BOT_TOKEN")}")

val httpClient = OkHttpClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package com.dunctebot.jda

import com.dunctebot.jda.impl.MemberPaginationActionImpl
import com.github.benmanes.caffeine.cache.Caffeine
import discord4j.common.util.Snowflake
import discord4j.core.DiscordClient
import discord4j.discordjson.json.UserData
import discord4j.rest.entity.RestUser
import net.dv8tion.jda.api.entities.Guild
import net.dv8tion.jda.api.entities.Member
import net.dv8tion.jda.api.entities.SelfUser
Expand All @@ -18,6 +22,7 @@ import net.dv8tion.jda.internal.utils.config.AuthorizationConfig
import net.dv8tion.jda.internal.utils.config.MetaConfig
import net.dv8tion.jda.internal.utils.config.SessionConfig
import net.dv8tion.jda.internal.utils.config.ThreadingConfig
import reactor.core.publisher.Mono
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit

Expand All @@ -27,16 +32,19 @@ import java.util.concurrent.TimeUnit
*
* This class has been inspired by GivawayBot and all credit goes to them https://github.com/jagrosh/GiveawayBot
*/
class JDARestClient(token: String) {
class DiscordRestClient(token: String) {
// create a guild cache that keeps the guilds in cache for 30 minutes
// When we stop accessing the guild it will be removed from the cache
private val guildCache = Caffeine.newBuilder()
.expireAfterAccess(30, TimeUnit.MINUTES)
.build<String, Guild>()

private val jda: JDAImpl
private val client: DiscordClient

init {
client = DiscordClient.create(token)

val authConfig = AuthorizationConfig(token)
val sessionConfig = SessionConfig.getDefault()
val threadConfig = ThreadingConfig.getDefault()
Expand All @@ -61,6 +69,15 @@ class JDARestClient(token: String) {
guildCache.invalidate(guildId.toString())
}

// Note: instantly starts the request
fun retrieveD4JUserById(id: Long): Mono<UserData> {
return client.userService.getUser(id)
}

fun retrieveD4JSelfUser(): Mono<UserData> {
return client.userService.currentUser
}

fun retrieveUserById(id: String): RestAction<User> {
val route = Route.Users.GET_USER.compile(id)

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/dunctebot/jda/FakeJDA.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import net.dv8tion.jda.api.entities.User
import net.dv8tion.jda.api.requests.RestAction
import net.dv8tion.jda.internal.JDAImpl

class FakeJDA(private val restClient: JDARestClient, fakeClient: JDAImpl) : JDA by fakeClient {
class FakeJDA(private val restClient: DiscordRestClient, fakeClient: JDAImpl) : JDA by fakeClient {
override fun retrieveUserById(id: Long, update: Boolean): RestAction<User> {
return restClient.retrieveUserById(id.toString())
}
Expand Down