B4J Question [JDA] How to use the new version JDA 1.84 ? [How to use behind a proxy]

jmon

Well-Known Member
Licensed User
Longtime User
Hi, as the title says, I couldn't find a way to use the new JDA (Discord Bot) library.

Following the example:
  • - I downoaded all the dependencies from https://mvnrepository.com/
  • - The project compiles with all the latest version of each dependencies, except for Okio, which requires version 2.6.0 (otherwise it won't compile)
  • - I've successfully created a Discord Bot on the dev. console. I got my Bot Token
  • - The jda.AwaitReady doesn't seem to be part of the library now.

Compiling the following code works, the UI app opens, but freezes, seems that it's waiting for something, maybe the missing "jda.awaitReady" ?
B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600

    #AdditionalJar:JDA-5.0.0-beta.2
    '#AdditionalJar: mysql-connector-java-5.1.34-bin
    #AdditionalJar: jackson-databind-2.10.1
    #AdditionalJar: jackson-core-2.10.1
    #AdditionalJar: jackson-annotations-2.11.0
    #AdditionalJar: okhttp-3.13.0
    #AdditionalJar: slf4j-api-2.0.17
    #AdditionalJar: slf4j-jdk14-2.0.17
    #AdditionalJar: commons-collections4-4.5.0
    #AdditionalJar: okio-2.6.0
    #AdditionalJar: kotlin-stdlib-2.1.20
    #AdditionalJar: kotlin-stdlib-jdk8-2.1.20
    #AdditionalJar: nv-websocket-client-2.14
    #AdditionalJar: trove4j-3.0.3
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
   
    Dim jda As JDA
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
       
    Dim Token As String = "mytokenFromDiscordDevelopper.aDSLKjsadkljfkasf.blahblah"
   
    Dim b As JDABuilder
    b.Initialize("JDA", Token) ' String token from Developersite
    b.setAutoReconnect(True).setEnableShutdownHook(True)
    jda.Initialize("JDA",b.build)


    Dim sh As DefaultShardManagerBuilder
    sh.Initialize("ShardManager", Token)
    sh.setToken(Token)
    sh.build
   
    'jda.awaitready

End Sub

Sub JDA_onStatusChange(event As StatusChangeEvent)
    Log($"JDA_onStatusChange(${event})"$)
End Sub

Sub JDA_onMessageReceived(event As MessageReceivedEvent)
    Log($"JDA_onMessageReceived(${event})"$)    
End Sub

Sub JDA_onGuildMemberJoin(event As GuildMemberJoinEvent)
    Log($"JDA_onGuildMemberJoin(${event})"$)
    'Dim ev As GuildMemberJoinEvent = event
    Log($"GuildMemberJoin.Member ${event.Member}"$)
    Log($"GuildMemberJoin.User ${event.User}"$)
End Sub

Thank you for your help!
 
Last edited:

jmon

Well-Known Member
Licensed User
Longtime User
Ok my bad, I'm behind a corporate proxy and needed to add a few proxy info:

B4X:
Public PROXYHOST As String = "192.168.0.1"
Public PROXYPORT As Int = 3030
Private NOPROXYLIST As String = "localhost|192.168.0.50"
'...
SetSystemProperty("http.proxyHost", PROXYHOST )
SetSystemProperty("https.proxyHost", PROXYHOST )
SetSystemProperty("http.proxyPort", PROXYPORT )
SetSystemProperty("https.proxyPort", PROXYPORT )
SetSystemProperty("http.nonProxyHosts", NOPROXYLIST)
SetSystemProperty("https.nonProxyHosts", NOPROXYLIST)

EDIT:

It seems that it needs to also set the proxy on the websocket:


B4X:
Dim factory As JavaObject
factory.InitializeNewInstance("com.neovisionaries.ws.client.WebSocketFactory", Null)
Dim settings As JavaObject = factory.runmethod("getProxySettings", Null)
settings.runmethod("setServer", Array("192.168.0.1"))
I cannot access the websocket object, so far I have to work on a machine that is not behind proxy, but it would be nice to include Proxy settings in a future update
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
As i am in a "after heart-bypass-operation rehabilitation" right now i do not have access to my pc as of now.

But a few notes until i´m able to post new library files, code examples to get a bot running....

- I never used anyproxy
- awaitready is replaced (i can not post actual code due to not having access to my pc as of now). But i remember you should wait ´for jda getting ready in guilds. After all guilds are ready the jda_onready event is raised. i can not recall the exact eventnames but it is probably jda_onguildready andfurther the road it end up in a jda:_onready event which is raised when all guild are ready you bot is running in,
- I need to probably upload the newest wrap of the library as JDA has become SOME updates since i wrote the library some years ago..
-Start with the newest JDA-Downloads: https://github.com/discord-jda/JDA/releases/tag/v5.5.1
JDA is out of betasince some time now.
- I never needed access to the websocket object itself.
- My bots are using jda 5.5.x already and running for months/years now
- I´ll update the librarythread when i´m out of rehabilitation.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Take your time to fully recover.
We all wish to see "our" DonManfred in optimal shape.
There's still time for the next Olympic Games... 🙂
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
As i am in a "after heart-bypass-operation rehabilitation" right now i do not have access to my pc as of now.

But a few notes until i´m able to post new library files, code examples to get a bot running....

- I never used anyproxy
- awaitready is replaced (i can not post actual code due to not having access to my pc as of now). But i remember you should wait ´for jda getting ready in guilds. After all guilds are ready the jda_onready event is raised. i can not recall the exact eventnames but it is probably jda_onguildready andfurther the road it end up in a jda:_onready event which is raised when all guild are ready you bot is running in,
- I need to probably upload the newest wrap of the library as JDA has become SOME updates since i wrote the library some years ago..
-Start with the newest JDA-Downloads: https://github.com/discord-jda/JDA/releases/tag/v5.5.1
JDA is out of betasince some time now.
- I never needed access to the websocket object itself.
- My bots are using jda 5.5.x already and running for months/years now
- I´ll update the librarythread when i´m out of rehabilitation.
Thank you! No rush, take care of yourself and get well soon!

By the way, the example I posted on #1 is a working example. I managed to receive messages and reply to them. All the events are named JDA_xxx.
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
B4X:
Dim proxyHost As String = "192.168.1.1"
Dim proxyPort As Int = 3100

Dim okHttpClientBuilder As JavaObject
okHttpClientBuilder.InitializeNewInstance("okhttp3.OkHttpClient.Builder", Null)

Dim proxyType As JavaObject
proxyType.InitializeStatic("java.net.Proxy.Type")
Dim proxyTypeHTTP As JavaObject = proxyType.GetField("HTTP")

Dim inetSocketAddress As JavaObject
inetSocketAddress.InitializeNewInstance("java.net.InetSocketAddress", Array(proxyHost, proxyPort))

Dim javaNetProxy As JavaObject
javaNetProxy.InitializeNewInstance("java.net.Proxy", Array(proxyTypeHTTP, inetSocketAddress))

okHttpClientBuilder.RunMethod("proxy", Array(javaNetProxy))

Dim customOkHttpClient As JavaObject = okHttpClientBuilder.RunMethod("build", Null)

Dim wsFactory As JavaObject
wsFactory.InitializeNewInstance("com.neovisionaries.ws.client.WebSocketFactory", Null)

Dim proxySettings As JavaObject = wsFactory.RunMethod("getProxySettings", Null)
proxySettings.RunMethod("setHost", Array(proxyHost))
proxySettings.RunMethod("setPort", Array(proxyPort))
   
Dim b As JDABuilder
b.Initialize("JDA", Token)
b.As(JavaObject).RunMethod("setHttpClient", Array(customOkHttpClient))
b.As(JavaObject).RunMethod("setWebsocketFactory", Array(wsFactory))
b.setAutoReconnect(True).setEnableShutdownHook(True)
jda.Initialize("JDA",b.build)

Dim sh As DefaultShardManagerBuilder
sh.Initialize("ShardManager", Token)
sh.setToken(Token)

sh.build

Doing further research, and this seems to be a way to connect the websocket to the proxy. (using setHttpClient and setWebsocketFactory https://github.com/discord-jda/JDA/blob/master/src/main/java/net/dv8tion/jda/api/JDABuilder.java).

Though it doesn't work yet in my case, I now suspect that my proxy doesn't handle websockets correctly.
 
Upvote 0
Top