diff --git a/build.gradle b/build.gradle index 97e7bfd..6f0682b 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ defaultTasks 'build', 'shadowJar' group = 'com.nguyenquyhy' version = '1.2.0' -description = 'An example plugin' +description = 'A plugin to connect Discord and Minecraft' sourceCompatibility = 1.8 repositories { @@ -24,7 +24,7 @@ repositories { name 'sponge' url 'http://repo.spongepowered.org/maven' } - maven { url "http://repo.maven.apache.org/maven2" } + //maven { url "http://repo.maven.apache.org/maven2" } //maven { url "http://dl.bintray.com/austinv11/maven" } } @@ -40,6 +40,7 @@ spongestart{ dependencies { compile 'org.spongepowered:spongeapi:4.0.3' + compile files('libs/Discord4J-2.4.9-shaded.jar') // compile "com.github.austinv11:Discord4j:2.4.9:shaded" // compile files('libs/Discord4J-2.2.7.jar') // compile 'org.apache.httpcomponents:httpcore:4.3.2' diff --git a/libs/Discord4J-2.2.7.jar b/libs/Discord4J-2.2.7.jar deleted file mode 100644 index 8977255..0000000 Binary files a/libs/Discord4J-2.2.7.jar and /dev/null differ diff --git a/libs/Discord4J-2.4.9-shaded.jar b/libs/Discord4J-2.4.9-shaded.jar index eb59865..4422b00 100644 Binary files a/libs/Discord4J-2.4.9-shaded.jar and b/libs/Discord4J-2.4.9-shaded.jar differ diff --git a/src/main/java/com/nguyenquyhy/spongediscord/SpongeDiscord.java b/src/main/java/com/nguyenquyhy/spongediscord/SpongeDiscord.java index 2d0e0a0..86424ff 100644 --- a/src/main/java/com/nguyenquyhy/spongediscord/SpongeDiscord.java +++ b/src/main/java/com/nguyenquyhy/spongediscord/SpongeDiscord.java @@ -56,6 +56,7 @@ @Plugin(id = "com.nguyenquyhy.spongediscord", name = "Sponge Discord", version = "1.2.0") public class SpongeDiscord { public static String DEBUG = ""; + public static String BOT_TOKEN = ""; public static String CHANNEL_ID = ""; public static String INVITE_CODE = ""; public static String JOINED_MESSAGE = ""; @@ -160,7 +161,18 @@ public void onServerStart(GameStartedServerEvent event) { getLogger().info("/discord command registered."); String cachedToken = getStorage().getDefaultToken(); - if (null != cachedToken && !cachedToken.isEmpty()) { + if (StringUtils.isNotBlank(BOT_TOKEN)) { + getLogger().info("Logging in to bot Discord account..."); + + try { + ClientBuilder clientBuilder = new ClientBuilder(); + IDiscordClient client = clientBuilder.withToken(BOT_TOKEN).asBot().build(); + prepareDefaultClient(client, null); + client.login(); + } catch (DiscordException e) { + e.printStackTrace(); + } + } else if (StringUtils.isNotBlank(cachedToken)) { getLogger().info("Logging in to default Discord account..."); try { @@ -219,7 +231,7 @@ public void onJoin(ClientConnectionEvent.Join event) throws URISyntaxException { if (!loggedIn) { unauthenticatedPlayers.add(playerId); - if (JOINED_MESSAGE != null && defaultClient != null && defaultClient.isReady()) { + if (StringUtils.isNotBlank(JOINED_MESSAGE) && defaultClient != null && defaultClient.isReady()) { try { IChannel channel = defaultClient.getChannelByID(CHANNEL_ID); channel.sendMessage(String.format(JOINED_MESSAGE, player.get().getName()), NONCE, false); @@ -240,7 +252,7 @@ public void onDisconnect(ClientConnectionEvent.Disconnect event) throws IOExcept Optional player = event.getCause().first(Player.class); if (player.isPresent()) { UUID playerId = player.get().getUniqueId(); - if (CHANNEL_ID != null && !CHANNEL_ID.isEmpty() && LEFT_MESSAGE != null) { + if (CHANNEL_ID != null && !CHANNEL_ID.isEmpty() && StringUtils.isNotBlank(LEFT_MESSAGE)) { IDiscordClient client = clients.get(playerId); if (client != null && client.isReady()) { IChannel channel = client.getChannelByID(CHANNEL_ID); @@ -305,6 +317,7 @@ public void loadConfiguration() { configNode = configLoader.load(); DEBUG = configNode.getNode("Debug").getString(); + BOT_TOKEN = ConfigUtil.readString(configNode, "BotToken", ""); CHANNEL_ID = ConfigUtil.readString(configNode, "Channel", ""); INVITE_CODE = ConfigUtil.readString(configNode, "InviteCode", ""); JOINED_MESSAGE = ConfigUtil.readString(configNode, "JoinedMessageTemplate", "_%s just joined the server_");