From 77f4a93c2f77cbdff7429345732ff1236d4aaab0 Mon Sep 17 00:00:00 2001 From: mani1232 Date: Wed, 14 Dec 2022 19:30:32 +0200 Subject: [PATCH 1/2] Update DiscordSocial depend --- build.gradle | 4 +- .../socialaddon/social/DiscordSocial.java | 279 +++++++++--------- 2 files changed, 143 insertions(+), 140 deletions(-) diff --git a/build.gradle b/build.gradle index 02f178f..822b261 100644 --- a/build.gradle +++ b/build.gradle @@ -43,7 +43,7 @@ dependencies { compileOnly("com.velocitypowered:velocity-api:3.1.2-SNAPSHOT") annotationProcessor("com.velocitypowered:velocity-api:3.1.2-SNAPSHOT") - implementation("net.dv8tion:JDA:5.0.0-alpha.9") { + implementation("net.dv8tion:JDA:5.0.0-beta.2") { exclude module: 'opus-java' } implementation('org.telegram:telegrambots:6.0.1') @@ -52,7 +52,7 @@ dependencies { implementation('org.bstats:bstats-velocity:3.0.0') compileOnly('com.github.spotbugs:spotbugs-annotations:4.6.0') - implementation("com.maxmind.geoip2:geoip2:3.0.1") + implementation('com.maxmind.geoip2:geoip2:3.0.2') implementation("org.apache.commons:commons-compress:1.21") } diff --git a/src/main/java/net/elytrium/limboauth/socialaddon/social/DiscordSocial.java b/src/main/java/net/elytrium/limboauth/socialaddon/social/DiscordSocial.java index 35cbc4f..a7656f2 100644 --- a/src/main/java/net/elytrium/limboauth/socialaddon/social/DiscordSocial.java +++ b/src/main/java/net/elytrium/limboauth/socialaddon/social/DiscordSocial.java @@ -17,17 +17,16 @@ package net.elytrium.limboauth.socialaddon.social; -import java.util.List; -import java.util.stream.Collectors; -import javax.security.auth.login.LoginException; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.exceptions.InvalidTokenException; import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.interactions.components.ActionRow; +import net.dv8tion.jda.api.interactions.components.LayoutComponent; import net.dv8tion.jda.api.interactions.components.buttons.Button; import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle; import net.dv8tion.jda.api.requests.GatewayIntent; @@ -36,162 +35,166 @@ import net.elytrium.limboauth.socialaddon.model.SocialPlayer; import org.jetbrains.annotations.NotNull; +import java.util.List; +import java.util.stream.Collectors; + public class DiscordSocial extends AbstractSocial { - private JDA jda; + private JDA jda; - public DiscordSocial(SocialMessageListener onMessageReceived, SocialButtonListener onButtonClicked) { - super(onMessageReceived, onButtonClicked); - } + public DiscordSocial(SocialMessageListener onMessageReceived, SocialButtonListener onButtonClicked) { + super(onMessageReceived, onButtonClicked); + } - @Override - public boolean isEnabled() { - return Settings.IMP.MAIN.DISCORD.ENABLED; - } + @Override + public boolean isEnabled() { + return Settings.IMP.MAIN.DISCORD.ENABLED; + } - @Override - public void init() throws SocialInitializationException { - try { - this.jda = JDABuilder - .create(Settings.IMP.MAIN.DISCORD.TOKEN, GatewayIntent.DIRECT_MESSAGES) - .disableCache(CacheFlag.ACTIVITY, CacheFlag.VOICE_STATE, CacheFlag.EMOTE, CacheFlag.CLIENT_STATUS, CacheFlag.ONLINE_STATUS) - .build(); + @Override + public void init() throws SocialInitializationException { + try { + this.jda = JDABuilder + .create(Settings.IMP.MAIN.DISCORD.TOKEN, GatewayIntent.DIRECT_MESSAGES) + .disableCache(CacheFlag.ACTIVITY, CacheFlag.VOICE_STATE, CacheFlag.EMOJI, CacheFlag.CLIENT_STATUS, CacheFlag.ONLINE_STATUS, CacheFlag.STICKER, CacheFlag.FORUM_TAGS) + .build(); + + this.jda.addEventListener(new Listener(this::proceedMessage, this::proceedButton)); + } catch (InvalidTokenException e) { + throw new SocialInitializationException(e); + } + } - this.jda.addEventListener(new Listener(this::proceedMessage, this::proceedButton)); - } catch (LoginException e) { - throw new SocialInitializationException(e); + @Override + public void stop() { + if (this.jda != null) { + this.jda.shutdown(); + } } - } - @Override - public void stop() { - if (this.jda != null) { - this.jda.shutdown(); + @Override + public String getDbField() { + return SocialPlayer.DISCORD_DB_FIELD; } - } - - @Override - public String getDbField() { - return SocialPlayer.DISCORD_DB_FIELD; - } - - @Override - public void onPlayerAdded(Long id) { - this.parseCommands(id, Settings.IMP.MAIN.DISCORD.ON_PLAYER_ADDED); - } - - @Override - public void onPlayerRemoved(SocialPlayer player) { - this.parseCommands(player.getDiscordID(), Settings.IMP.MAIN.DISCORD.ON_PLAYER_REMOVED); - } - - @SuppressWarnings("ConstantConditions") - private void parseCommands(Long id, List commands) { - for (String argsString : commands) { - String[] args = argsString.split(" "); - String command = args[0]; - switch (command) { - case "addrole": { - long roleId = Long.parseLong(args[1]); - Role role = this.jda.getRoleById(roleId); - role.getGuild().addRoleToMember(id, role).queue(); - break; - } - case "remrole": { - long roleId = Long.parseLong(args[1]); - Role role = this.jda.getRoleById(roleId); - role.getGuild().removeRoleFromMember(id, role).queue(); - break; - } - default: { - break; - } - } + + @Override + public void onPlayerAdded(Long id) { + this.parseCommands(id, Settings.IMP.MAIN.DISCORD.ON_PLAYER_ADDED); } - } - @Override - public void sendMessage(Long id, String content, List> buttons) { - User user = this.jda.retrieveUserById(id).complete(); + @Override + public void onPlayerRemoved(SocialPlayer player) { + this.parseCommands(player.getDiscordID(), Settings.IMP.MAIN.DISCORD.ON_PLAYER_REMOVED); + } - if (user == null) { - return; + @SuppressWarnings("ConstantConditions") + private void parseCommands(Long id, List commands) { + User user = this.jda.getUserById(id); + for (String argsString : commands) { + String[] args = argsString.split(" "); + String command = args[0]; + switch (command) { + case "addrole": { + long roleId = Long.parseLong(args[1]); + Role role = this.jda.getRoleById(roleId); + role.getGuild().addRoleToMember(user, role).queue(); + break; + } + case "remrole": { + long roleId = Long.parseLong(args[1]); + Role role = this.jda.getRoleById(roleId); + role.getGuild().removeRoleFromMember(user, role).queue(); + break; + } + default: { + break; + } + } + } } - List actionRowList = buttons.stream().map(row -> - ActionRow.of(row.stream().map(e -> { - ButtonStyle style; - - switch (e.getColor()) { - case RED: - style = ButtonStyle.DANGER; - break; - case GREEN: - style = ButtonStyle.SUCCESS; - break; - case LINK: - style = ButtonStyle.LINK; - break; - case PRIMARY: - style = ButtonStyle.PRIMARY; - break; - case SECONDARY: - default: - style = ButtonStyle.SECONDARY; - break; - } - - return Button.of(style, e.getId(), e.getValue()); - }).collect(Collectors.toList())) - ).collect(Collectors.toList()); - - user.openPrivateChannel() - .submit() - .thenAccept(privateChannel -> privateChannel - .sendMessage(content) - .setActionRows(actionRowList) - .submit() - .exceptionally(e -> { - e.printStackTrace(); - return null; - })) - .exceptionally(e -> { - e.printStackTrace(); - return null; - }); - } - - @Override - public void sendMessage(SocialPlayer player, String content, List> buttons) { - this.sendMessage(player.getDiscordID(), content, buttons); - } - - @Override - public boolean canSend(SocialPlayer player) { - return player.getDiscordID() != null; - } - - private static class Listener extends ListenerAdapter { - - private final SocialMessageListener onMessageReceived; - private final SocialButtonListener onButtonClicked; - - Listener(SocialMessageListener onMessageReceived, SocialButtonListener onButtonClicked) { - this.onMessageReceived = onMessageReceived; - this.onButtonClicked = onButtonClicked; + @Override + public void sendMessage(Long id, String content, List> buttons) { + User user = this.jda.retrieveUserById(id).complete(); + + if (user == null) { + return; + } + + List actionRowList = buttons.stream().map(row -> + ActionRow.of(row.stream().map(e -> { + ButtonStyle style; + + switch (e.getColor()) { + case RED: + style = ButtonStyle.DANGER; + break; + case GREEN: + style = ButtonStyle.SUCCESS; + break; + case LINK: + style = ButtonStyle.LINK; + break; + case PRIMARY: + style = ButtonStyle.PRIMARY; + break; + case SECONDARY: + default: + style = ButtonStyle.SECONDARY; + break; + } + + return Button.of(style, e.getId(), e.getValue()); + }).collect(Collectors.toList())) + ).collect(Collectors.toList()); + + user.openPrivateChannel() + .submit() + .thenAccept(privateChannel -> privateChannel + .sendMessage(content) + .setComponents(actionRowList) + .submit() + .exceptionally(e -> { + e.printStackTrace(); + return null; + })) + .exceptionally(e -> { + e.printStackTrace(); + return null; + }); } @Override - public void onMessageReceived(@NotNull MessageReceivedEvent event) { - this.onMessageReceived.accept(SocialPlayer.DISCORD_DB_FIELD, event.getAuthor().getIdLong(), event.getMessage().getContentRaw()); + public void sendMessage(SocialPlayer player, String content, List> buttons) { + this.sendMessage(player.getDiscordID(), content, buttons); } @Override - public void onButtonInteraction(@NotNull ButtonInteractionEvent event) { - event.deferEdit().queue(); - this.onButtonClicked.accept(SocialPlayer.DISCORD_DB_FIELD, event.getUser().getIdLong(), event.getButton().getId()); + public boolean canSend(SocialPlayer player) { + return player.getDiscordID() != null; } - } + private static class Listener extends ListenerAdapter { + + private final SocialMessageListener onMessageReceived; + private final SocialButtonListener onButtonClicked; + + Listener(SocialMessageListener onMessageReceived, SocialButtonListener onButtonClicked) { + this.onMessageReceived = onMessageReceived; + this.onButtonClicked = onButtonClicked; + } + + @Override + public void onMessageReceived(@NotNull MessageReceivedEvent event) { + this.onMessageReceived.accept(SocialPlayer.DISCORD_DB_FIELD, event.getAuthor().getIdLong(), event.getMessage().getContentRaw()); + } + + @Override + public void onButtonInteraction(@NotNull ButtonInteractionEvent event) { + event.deferEdit().queue(); + this.onButtonClicked.accept(SocialPlayer.DISCORD_DB_FIELD, event.getUser().getIdLong(), event.getButton().getId()); + } + + } } From eac037b0d6b164f52f10d44c7ad1c7566368a685 Mon Sep 17 00:00:00 2001 From: mani1232 Date: Wed, 14 Dec 2022 19:38:12 +0200 Subject: [PATCH 2/2] Fix code style --- .../socialaddon/social/DiscordSocial.java | 284 +++++++++--------- 1 file changed, 143 insertions(+), 141 deletions(-) diff --git a/src/main/java/net/elytrium/limboauth/socialaddon/social/DiscordSocial.java b/src/main/java/net/elytrium/limboauth/socialaddon/social/DiscordSocial.java index a7656f2..2cd2595 100644 --- a/src/main/java/net/elytrium/limboauth/socialaddon/social/DiscordSocial.java +++ b/src/main/java/net/elytrium/limboauth/socialaddon/social/DiscordSocial.java @@ -17,6 +17,8 @@ package net.elytrium.limboauth.socialaddon.social; +import java.util.List; +import java.util.stream.Collectors; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.entities.Role; @@ -35,166 +37,166 @@ import net.elytrium.limboauth.socialaddon.model.SocialPlayer; import org.jetbrains.annotations.NotNull; -import java.util.List; -import java.util.stream.Collectors; - public class DiscordSocial extends AbstractSocial { - private JDA jda; - - public DiscordSocial(SocialMessageListener onMessageReceived, SocialButtonListener onButtonClicked) { - super(onMessageReceived, onButtonClicked); + private JDA jda; + + public DiscordSocial(SocialMessageListener onMessageReceived, SocialButtonListener onButtonClicked) { + super(onMessageReceived, onButtonClicked); + } + + @Override + public boolean isEnabled() { + return Settings.IMP.MAIN.DISCORD.ENABLED; + } + + @Override + public void init() throws SocialInitializationException { + try { + this.jda = JDABuilder + .create(Settings.IMP.MAIN.DISCORD.TOKEN, GatewayIntent.DIRECT_MESSAGES) + .disableCache( + CacheFlag.ACTIVITY, CacheFlag.VOICE_STATE, CacheFlag.EMOJI, CacheFlag.CLIENT_STATUS, CacheFlag.ONLINE_STATUS, CacheFlag.STICKER, + CacheFlag.FORUM_TAGS + ) + .build(); + + this.jda.addEventListener(new Listener(this::proceedMessage, this::proceedButton)); + } catch (InvalidTokenException e) { + throw new SocialInitializationException(e); } + } - @Override - public boolean isEnabled() { - return Settings.IMP.MAIN.DISCORD.ENABLED; + @Override + public void stop() { + if (this.jda != null) { + this.jda.shutdown(); } - - @Override - public void init() throws SocialInitializationException { - try { - this.jda = JDABuilder - .create(Settings.IMP.MAIN.DISCORD.TOKEN, GatewayIntent.DIRECT_MESSAGES) - .disableCache(CacheFlag.ACTIVITY, CacheFlag.VOICE_STATE, CacheFlag.EMOJI, CacheFlag.CLIENT_STATUS, CacheFlag.ONLINE_STATUS, CacheFlag.STICKER, CacheFlag.FORUM_TAGS) - .build(); - - this.jda.addEventListener(new Listener(this::proceedMessage, this::proceedButton)); - } catch (InvalidTokenException e) { - throw new SocialInitializationException(e); + } + + @Override + public String getDbField() { + return SocialPlayer.DISCORD_DB_FIELD; + } + + @Override + public void onPlayerAdded(Long id) { + this.parseCommands(id, Settings.IMP.MAIN.DISCORD.ON_PLAYER_ADDED); + } + + @Override + public void onPlayerRemoved(SocialPlayer player) { + this.parseCommands(player.getDiscordID(), Settings.IMP.MAIN.DISCORD.ON_PLAYER_REMOVED); + } + + @SuppressWarnings("ConstantConditions") + private void parseCommands(Long id, List commands) { + User user = this.jda.getUserById(id); + for (String argsString : commands) { + String[] args = argsString.split(" "); + String command = args[0]; + switch (command) { + case "addrole": { + long roleId = Long.parseLong(args[1]); + Role role = this.jda.getRoleById(roleId); + role.getGuild().addRoleToMember(user, role).queue(); + break; } - } - - @Override - public void stop() { - if (this.jda != null) { - this.jda.shutdown(); + case "remrole": { + long roleId = Long.parseLong(args[1]); + Role role = this.jda.getRoleById(roleId); + role.getGuild().removeRoleFromMember(user, role).queue(); + break; } - } - - @Override - public String getDbField() { - return SocialPlayer.DISCORD_DB_FIELD; - } - - @Override - public void onPlayerAdded(Long id) { - this.parseCommands(id, Settings.IMP.MAIN.DISCORD.ON_PLAYER_ADDED); - } - - @Override - public void onPlayerRemoved(SocialPlayer player) { - this.parseCommands(player.getDiscordID(), Settings.IMP.MAIN.DISCORD.ON_PLAYER_REMOVED); - } - - @SuppressWarnings("ConstantConditions") - private void parseCommands(Long id, List commands) { - User user = this.jda.getUserById(id); - for (String argsString : commands) { - String[] args = argsString.split(" "); - String command = args[0]; - switch (command) { - case "addrole": { - long roleId = Long.parseLong(args[1]); - Role role = this.jda.getRoleById(roleId); - role.getGuild().addRoleToMember(user, role).queue(); - break; - } - case "remrole": { - long roleId = Long.parseLong(args[1]); - Role role = this.jda.getRoleById(roleId); - role.getGuild().removeRoleFromMember(user, role).queue(); - break; - } - default: { - break; - } - } + default: { + break; } + } } + } - @Override - public void sendMessage(Long id, String content, List> buttons) { - User user = this.jda.retrieveUserById(id).complete(); + @Override + public void sendMessage(Long id, String content, List> buttons) { + User user = this.jda.retrieveUserById(id).complete(); - if (user == null) { - return; - } + if (user == null) { + return; + } - List actionRowList = buttons.stream().map(row -> - ActionRow.of(row.stream().map(e -> { - ButtonStyle style; - - switch (e.getColor()) { - case RED: - style = ButtonStyle.DANGER; - break; - case GREEN: - style = ButtonStyle.SUCCESS; - break; - case LINK: - style = ButtonStyle.LINK; - break; - case PRIMARY: - style = ButtonStyle.PRIMARY; - break; - case SECONDARY: - default: - style = ButtonStyle.SECONDARY; - break; - } - - return Button.of(style, e.getId(), e.getValue()); - }).collect(Collectors.toList())) - ).collect(Collectors.toList()); - - user.openPrivateChannel() - .submit() - .thenAccept(privateChannel -> privateChannel - .sendMessage(content) - .setComponents(actionRowList) - .submit() - .exceptionally(e -> { - e.printStackTrace(); - return null; - })) - .exceptionally(e -> { - e.printStackTrace(); - return null; - }); + List actionRowList = buttons.stream().map(row -> + ActionRow.of(row.stream().map(e -> { + ButtonStyle style; + + switch (e.getColor()) { + case RED: + style = ButtonStyle.DANGER; + break; + case GREEN: + style = ButtonStyle.SUCCESS; + break; + case LINK: + style = ButtonStyle.LINK; + break; + case PRIMARY: + style = ButtonStyle.PRIMARY; + break; + case SECONDARY: + default: + style = ButtonStyle.SECONDARY; + break; + } + + return Button.of(style, e.getId(), e.getValue()); + }).collect(Collectors.toList())) + ).collect(Collectors.toList()); + + user.openPrivateChannel() + .submit() + .thenAccept(privateChannel -> privateChannel + .sendMessage(content) + .setComponents(actionRowList) + .submit() + .exceptionally(e -> { + e.printStackTrace(); + return null; + })) + .exceptionally(e -> { + e.printStackTrace(); + return null; + }); + } + + @Override + public void sendMessage(SocialPlayer player, String content, List> buttons) { + this.sendMessage(player.getDiscordID(), content, buttons); + } + + @Override + public boolean canSend(SocialPlayer player) { + return player.getDiscordID() != null; + } + + private static class Listener extends ListenerAdapter { + + private final SocialMessageListener onMessageReceived; + private final SocialButtonListener onButtonClicked; + + Listener(SocialMessageListener onMessageReceived, SocialButtonListener onButtonClicked) { + this.onMessageReceived = onMessageReceived; + this.onButtonClicked = onButtonClicked; } @Override - public void sendMessage(SocialPlayer player, String content, List> buttons) { - this.sendMessage(player.getDiscordID(), content, buttons); + public void onMessageReceived(@NotNull MessageReceivedEvent event) { + this.onMessageReceived.accept(SocialPlayer.DISCORD_DB_FIELD, event.getAuthor().getIdLong(), event.getMessage().getContentRaw()); } @Override - public boolean canSend(SocialPlayer player) { - return player.getDiscordID() != null; + public void onButtonInteraction(@NotNull ButtonInteractionEvent event) { + event.deferEdit().queue(); + this.onButtonClicked.accept(SocialPlayer.DISCORD_DB_FIELD, event.getUser().getIdLong(), event.getButton().getId()); } - private static class Listener extends ListenerAdapter { - - private final SocialMessageListener onMessageReceived; - private final SocialButtonListener onButtonClicked; - - Listener(SocialMessageListener onMessageReceived, SocialButtonListener onButtonClicked) { - this.onMessageReceived = onMessageReceived; - this.onButtonClicked = onButtonClicked; - } - - @Override - public void onMessageReceived(@NotNull MessageReceivedEvent event) { - this.onMessageReceived.accept(SocialPlayer.DISCORD_DB_FIELD, event.getAuthor().getIdLong(), event.getMessage().getContentRaw()); - } - - @Override - public void onButtonInteraction(@NotNull ButtonInteractionEvent event) { - event.deferEdit().queue(); - this.onButtonClicked.accept(SocialPlayer.DISCORD_DB_FIELD, event.getUser().getIdLong(), event.getButton().getId()); - } - - } + } }