1010import java .util .Map ;
1111import java .util .logging .Logger ;
1212import java .util .stream .Collectors ;
13- import org .javacord .api .DiscordApi ;
14- import org .javacord .api .DiscordApiBuilder ;
15- import org .javacord .api .entity .activity .ActivityType ;
16- import org .javacord .api .entity .channel .Channel ;
17- import org .javacord .api .entity .message .embed .EmbedBuilder ;
18- import org .javacord .api .util .logging .ExceptionLogger ;
13+ import net .dv8tion .jda .api .JDA ;
14+ import net .dv8tion .jda .api .JDABuilder ;
15+ import net .dv8tion .jda .api .EmbedBuilder ;
16+ import net .dv8tion .jda .api .entities .Activity ;
17+ import net .dv8tion .jda .api .entities .TextChannel ;
18+ import net .dv8tion .jda .api .entities .Guild ;
19+ import net .dv8tion .jda .api .entities .Message ;
20+ import net .dv8tion .jda .api .requests .GatewayIntent ;
1921import tc .oc .pgm .api .PGM ;
2022import tc .oc .pgm .api .Permissions ;
2123import tc .oc .pgm .api .map .Contributor ;
3032
3133public class DiscordBot {
3234
33- private DiscordApi api ;
35+ private JDA api ;
3436 private BotConfig config ;
3537 private Logger logger ;
3638
@@ -51,59 +53,57 @@ public BotConfig getConfig() {
5153 public void enable () {
5254 if (config .isEnabled ()) {
5355 logger .info ("Enabling DiscordBot..." );
54- new DiscordApiBuilder ()
55- .setToken (config .getToken ())
56- .setWaitForServersOnStartup (false )
57- .setWaitForUsersOnStartup (false )
58- .login ()
59- .thenAcceptAsync (api -> {
60- setAPI (api );
61- api .setMessageCacheSize (1 , 60 * 60 );
62- api .addServerBecomesAvailableListener (
63- listener -> logger .info (listener .getServer ().getName () + " is now available" ));
64- logger .info ("Discord Bot (MatchBot) is now active!" );
65- })
66- .exceptionally (throwable -> {
67- logger .info ("Failed to login to Discord: " + throwable .getMessage ());
68- return null ;
69- });
56+ try {
57+ this .api = JDABuilder .createDefault (config .getToken ())
58+ .enableIntents (GatewayIntent .GUILD_MESSAGES , GatewayIntent .GUILD_MEMBERS )
59+ .setActivity (Activity .playing ("Starting up..." ))
60+ .build ();
61+ this .api .awaitReady ();
62+ logger .info ("Discord Bot (MatchBot) is now active!" );
63+ } catch (Exception e ) {
64+ logger .info ("Failed to login to Discord: " + e .getMessage ());
65+ }
7066 }
7167 }
7268
73- private void setAPI (DiscordApi api ) {
69+ private void setAPI (JDA api ) {
7470 this .api = api ;
7571 }
7672
7773 public void disable () {
7874 if (this .api != null ) {
79- this .api .disconnect ();
75+ this .api .shutdown ();
8076 }
8177 this .api = null ;
8278 }
8379
8480 public void sendMatchEmbed (EmbedBuilder embed , Match match ) {
8581 if (api != null ) {
86- api .updateActivity (ActivityType .PLAYING , match .getMap ().getName ());
87- api .getServerById (config .getServerId ())
88- .flatMap (server ->
89- server .getChannelById (config .getMatchChannel ()).flatMap (Channel ::asTextChannel ))
90- .ifPresent (textChannel -> textChannel
91- .sendMessage (embed )
92- .thenAccept (message -> matchMessageMap .put (Long .valueOf (match .getId ()), message .getId ()))
93- .exceptionally (ExceptionLogger .get ()));
82+ api .getPresence ().setActivity (Activity .playing (match .getMap ().getName ()));
83+ Guild guild = api .getGuildById (config .getServerId ());
84+ if (guild != null ) {
85+ TextChannel textChannel = guild .getTextChannelById (config .getMatchChannel ());
86+ if (textChannel != null ) {
87+ textChannel .sendMessageEmbeds (embed .build ()).queue (message -> {
88+ matchMessageMap .put (Long .valueOf (match .getId ()), message .getIdLong ());
89+ });
90+ }
91+ }
9492 }
9593 }
9694
9795 public void editMatchEmbed (long matchId , EmbedBuilder newEmbed ) {
9896 if (api != null && matchMessageMap .containsKey (matchId )) {
99- long messageId = matchMessageMap .get (matchId );
100- api .getServerById (config .getServerId ())
101- .flatMap (server ->
102- server .getChannelById (config .getMatchChannel ()).flatMap (Channel ::asTextChannel ))
103- .ifPresent (textChannel -> textChannel
104- .getMessageById (messageId )
105- .thenAccept (message -> message .edit (newEmbed ))
106- .exceptionally (ExceptionLogger .get ()));
97+ Guild guild = api .getGuildById (config .getServerId ());
98+ if (guild != null ) {
99+ TextChannel textChannel = guild .getTextChannelById (config .getMatchChannel ());
100+ if (textChannel != null ) {
101+ long messageId = matchMessageMap .get (matchId );
102+ textChannel .retrieveMessageById (messageId ).queue (message -> {
103+ message .editMessageEmbeds (newEmbed .build ()).queue ();
104+ });
105+ }
106+ }
107107 }
108108 }
109109
0 commit comments