From 36dd65773c614a5bde0d6bd9a729847a0ab00ee6 Mon Sep 17 00:00:00 2001
From: Stalitsa <kolokotronakos@gmail.com>
Date: Sun, 22 Nov 2020 22:19:37 +0000
Subject: [PATCH] Discord Bot init commit

---
 .../datapack/custom/events/Elpies/Elpies.java |  6 +-
 .../custom/service/discord/DiscordBot.java    | 94 +++++++++++++++++++
 .../custom/service/discord/StartListener.java | 81 ++++++++++++++++
 src/main/resources/data/scripts.cfg           |  1 +
 4 files changed, 178 insertions(+), 4 deletions(-)
 create mode 100644 src/main/java/com/l2jserver/datapack/custom/service/discord/DiscordBot.java
 create mode 100644 src/main/java/com/l2jserver/datapack/custom/service/discord/StartListener.java

diff --git a/src/main/java/com/l2jserver/datapack/custom/events/Elpies/Elpies.java b/src/main/java/com/l2jserver/datapack/custom/events/Elpies/Elpies.java
index a254612e06..7b0617483b 100644
--- a/src/main/java/com/l2jserver/datapack/custom/events/Elpies/Elpies.java
+++ b/src/main/java/com/l2jserver/datapack/custom/events/Elpies/Elpies.java
@@ -39,8 +39,7 @@ public final class Elpies extends Event {
 	// Event duration in minutes
 	private static final int EVENT_DURATION_MINUTES = 2;
 	// @formatter:off
-	private static final int[][] DROPLIST_CONSUMABLES =
-	{
+	private static final int[][] DROPLIST_CONSUMABLES = {
 		// itemId, chance, min amount, max amount
 		{  1540, 80, 10, 15 },	// Quick Healing Potion
 		{  1538, 60,  5, 10 },	// Blessed Scroll of Escape
@@ -53,8 +52,7 @@ public final class Elpies extends Event {
 		{ 20004,  0,  1, 1 }	// Energy Ginseng
 	};
 	
-	private static final int[][] DROPLIST_CRYSTALS =
-	{
+	private static final int[][] DROPLIST_CRYSTALS = {
 		{ 1458, 80, 50, 100 },	// Crystal D-Grade
 		{ 1459, 60, 40,  80 },	// Crystal C-Grade
 		{ 1460, 40, 30,  60 },	// Crystal B-Grade
diff --git a/src/main/java/com/l2jserver/datapack/custom/service/discord/DiscordBot.java b/src/main/java/com/l2jserver/datapack/custom/service/discord/DiscordBot.java
new file mode 100644
index 0000000000..a89a211b4f
--- /dev/null
+++ b/src/main/java/com/l2jserver/datapack/custom/service/discord/DiscordBot.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright © 2004-2020 L2J DataPack
+ *
+ * This file is part of L2J DataPack.
+ *
+ * L2J DataPack is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * L2J DataPack is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.l2jserver.datapack.custom.service.discord;
+
+import static com.l2jserver.gameserver.config.Configuration.discord;
+
+import java.awt.Color;
+
+import javax.security.auth.login.LoginException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import net.dv8tion.jda.api.EmbedBuilder;
+import net.dv8tion.jda.api.JDA;
+import net.dv8tion.jda.api.JDABuilder;
+import net.dv8tion.jda.api.OnlineStatus;
+import net.dv8tion.jda.api.entities.Activity;
+import net.dv8tion.jda.api.entities.MessageChannel;
+import net.dv8tion.jda.api.requests.GatewayIntent;
+import net.dv8tion.jda.api.utils.ChunkingFilter;
+import net.dv8tion.jda.api.utils.MemberCachePolicy;
+
+/**
+ * Main class of Discord Bot.
+ * @author Stalitsa
+ * @version 2.6.2.0
+ */
+public class DiscordBot {
+	
+	private static final Logger LOG = LoggerFactory.getLogger(DiscordBot.class);
+	
+	private static JDA jda;
+	
+	public static void main(String[] args) {
+		if (!discord().enableBot()) {
+			LOG.info("Discord Bot is Disabled.");
+			return;
+		}
+		try {
+			jda = JDABuilder.createDefault(discord().getBotToken()) //
+				.setAutoReconnect(true) //
+				.addEventListeners(new StartListener()) //
+				.enableIntents(GatewayIntent.GUILD_MEMBERS) //
+				.enableIntents(GatewayIntent.GUILD_MESSAGES) //
+				.setMemberCachePolicy(MemberCachePolicy.ALL) //
+				.setChunkingFilter(ChunkingFilter.ALL) //
+				// Login to Discord now that we are all setup.
+				.build() //
+				.awaitReady(); // Blocking guarantees that JDA will be completely loaded.
+			jda.getPresence().setPresence(OnlineStatus.ONLINE, Activity.listening(": -- L2J"));
+			LOG.info("Discord Bot Started.");
+		} catch (InterruptedException | LoginException ex) {
+			LOG.error("Failed to start the Discord Bot!", ex);
+		}
+	}
+	
+	/**
+	 * Send a message in the specified channel
+	 * @param msg the message to send. This will be shown as a description of an embed.
+	 * @param channelId the channel to send the msg. // planned to be used by console logs
+	 */
+	public void sendMessageTo(String msg, String channelId) {
+		sendMessageTo(new EmbedBuilder().setColor(Color.GREEN).setDescription(msg), channelId);
+	}
+	
+	/**
+	 * Send a message in the specified channel
+	 * @param ed the embed message to send. (The embed build(); is done here.)
+	 * @param channelId the channel to send the embed. // planned to be used by console logs
+	 */
+	public void sendMessageTo(EmbedBuilder ed, String channelId) {
+		MessageChannel channel = jda.getTextChannelById(channelId);
+		if (channel != null) {
+			channel.sendMessage(ed.build()).queue(); // this actually sends the information to discord.
+		}
+	}
+}
diff --git a/src/main/java/com/l2jserver/datapack/custom/service/discord/StartListener.java b/src/main/java/com/l2jserver/datapack/custom/service/discord/StartListener.java
new file mode 100644
index 0000000000..0cf1da4971
--- /dev/null
+++ b/src/main/java/com/l2jserver/datapack/custom/service/discord/StartListener.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright © 2004-2020 L2J DataPack
+ *
+ * This file is part of L2J DataPack.
+ *
+ * L2J DataPack is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * L2J DataPack is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.l2jserver.datapack.custom.service.discord;
+
+import static com.l2jserver.gameserver.config.Configuration.discord;
+
+import java.awt.Color;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.l2jserver.gameserver.data.xml.impl.AdminData;
+import com.l2jserver.gameserver.model.L2World;
+
+import net.dv8tion.jda.api.EmbedBuilder;
+import net.dv8tion.jda.api.events.DisconnectEvent;
+import net.dv8tion.jda.api.events.ReadyEvent;
+import net.dv8tion.jda.api.events.ReconnectedEvent;
+import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
+import net.dv8tion.jda.api.hooks.ListenerAdapter;
+
+/**
+ * Basic command Listener
+ * @author Stalitsa
+ * @version 2.6.2.0
+ */
+public class StartListener extends ListenerAdapter {
+	private static final Logger LOG = LoggerFactory.getLogger(DiscordBot.class);
+	
+	@Override
+	public void onReady(ReadyEvent event) {
+		LOG.info("Joined Guilds: " + event.getGuildTotalCount());
+	}
+	
+	@Override
+	public void onDisconnect(DisconnectEvent event) {
+		if (event.isClosedByServer()) {
+			LOG.info(event.getJDA().getSelfUser().getName() + " disconnected (closed by the server) with code: " + event.getServiceCloseFrame().getCloseCode() + " " + event.getCloseCode());
+		}
+	}
+	
+	@Override
+	public void onReconnect(ReconnectedEvent event) {
+		LOG.info(event.getJDA().getSelfUser().getName() + " has reconnected.");
+	}
+	
+	@Override
+	public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
+		if (event.getAuthor().isBot()) {
+			return;
+		}
+		
+		final int playersCount = L2World.getInstance().getAllPlayersCount();
+		final int gmCount = AdminData.getInstance().getAllGms(true).size();
+		// Basic command that the bot listens to and responds in an embed with online players and Gms
+		if (event.getMessage().getContentRaw().startsWith(discord().getPrefix() + "online")) {
+			EmbedBuilder eb = new EmbedBuilder().setColor(Color.CYAN);
+			eb.setTitle(event.getAuthor().getName());
+			eb.addField("Online Players", String.valueOf(playersCount), false);
+			eb.addBlankField(false);
+			eb.addField("Online GM's", String.valueOf(gmCount), false);
+			event.getChannel().sendMessage(eb.build()).queue(); // this actually sends the information to discord.
+		}
+	}
+}
diff --git a/src/main/resources/data/scripts.cfg b/src/main/resources/data/scripts.cfg
index ed637af580..1e10f9add1 100644
--- a/src/main/resources/data/scripts.cfg
+++ b/src/main/resources/data/scripts.cfg
@@ -9,6 +9,7 @@ com/l2jserver/datapack/features/SkillTransfer/SkillTransfer.java
 # Custom
 com/l2jserver/datapack/custom/Validators/SubClassSkills.java
 com/l2jserver/datapack/custom/service/buffer/BufferService.java
+com/l2jserver/datapack/custom/service/discord/DiscordBot.java
 
 # Custom Events
 com/l2jserver/datapack/custom/events/Elpies/Elpies.java
-- 
GitLab