Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

46
LINES

< > BotCompany Repo | #1026291 // discordBot40 - using JDA 4.0

JavaX fragment (include) [tags: use-pretranspiled]

Uses 10105K of libraries. Click here for Pure Java version (3075L/18K).

1  
ifndef DifferentJDA
2  
//lib 1400315 // JDA 4.0 with opus
3  
lib 1400445 // JDA 4.2 with opus
4  
5  
//static LS _stickyLibs_discordBot = ll(#1400315); // can cause loader constraint violations
6  
endifndef
7  
8  
import net.dv8tion.jda.api.*;
9  
import net.dv8tion.jda.api.entities.*;
10  
import net.dv8tion.jda.api.entities.Member;
11  
import net.dv8tion.jda.api.hooks.*;
12  
import net.dv8tion.jda.api.events.message.*;
13  
import net.dv8tion.jda.api.events.message.react.*;
14  
import static net.dv8tion.jda.api.requests.GatewayIntent.*;
15  
16  
static JDA discordBot40(VF1<MessageReceivedEvent> onMessage, O... _) {
17  
  ret discordBot40(new ListenerAdapter {
18  
    @Override
19  
    public void onMessageReceived(MessageReceivedEvent e) {
20  
      if (e.getAuthor().isBot()) ret with print("  Msg from bot, skipping");
21  
      pcallF(onMessage, e);
22  
    }
23  
  }, _);
24  
}
25  
26  
static JDA discordBot40(ListenerAdapter listener, O... _) ctex {
27  
  S token = stringPar token(_);
28  
  if (empty(token)) {
29  
    File tokenFile = javaxSecretDir("discord-bot-token");
30  
    token = assertNempty("Need: " + tokenFile, trim(loadTextFile(tokenFile)));
31  
  }
32  
  print("Making JDABuilder");
33  
  JDABuilder builder = JDABuilder.create(token,
34  
    GUILD_VOICE_STATES, GUILD_MESSAGES, GUILD_MESSAGE_REACTIONS, DIRECT_MESSAGES);
35  
  print("Have JDABuilder");
36  
  if (boolPar membersIntent(_))
37  
    builder.enableIntents(GUILD_MEMBERS);
38  
  builder.addEventListeners(listener);
39  
  print("Building JDA with token " + shorten(10, token));
40  
  JDA jda = builder.build();
41  
  print("Got JDA: " + jda);
42  
  //jda.awaitSatus(JDA.Status.CONNECTED);
43  
  jda.awaitReady();
44  
  print("Started Discord bot (token: " + shorten(token, 7) + ")");
45  
  ret jda;
46  
}

Author comment

Began life as a copy of #1021617

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1026291
Snippet name: discordBot40 - using JDA 4.0
Eternal ID of this version: #1026291/10
Text MD5: 9cd9dd646afb56f92edcefe31adead94
Transpilation MD5: 89991ab1c005ea321e3ac915ee3e8d50
Author: stefan
Category: javax / discord
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-01-26 05:21:35
Source code size: 1671 bytes / 46 lines
Pitched / IR pitched: No / No
Views / Downloads: 297 / 406
Version history: 9 change(s)
Referenced in: [show references]