diff --git a/src/main/java/com/l2jserver/datapack/custom/service/discord/AbstractCommand.java b/src/main/java/com/l2jserver/datapack/custom/service/discord/AbstractCommand.java new file mode 100644 index 0000000000000000000000000000000000000000..e7c897460384eec666999aa247c82e816ff29415 --- /dev/null +++ b/src/main/java/com/l2jserver/datapack/custom/service/discord/AbstractCommand.java @@ -0,0 +1,61 @@ +/* + * 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 net.dv8tion.jda.api.entities.ChannelType; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; + +import java.util.ArrayList; +import java.util.List; + +import static com.l2jserver.gameserver.config.Configuration.discord; + +/** + * Abstract Command. + * @author Stalitsa + * @version 2.6.2.0 + */ +public abstract class AbstractCommand extends ListenerAdapter { + + public abstract List<String> getCommands(); + + public abstract void executeCommand(MessageReceivedEvent event, String[] args, String prefix); + + @Override + public void onMessageReceived(MessageReceivedEvent event) { + if (event.getAuthor().isBot() || event.getChannelType().equals(ChannelType.PRIVATE)) + { + return; + } + String[] args = event.getMessage().getContentRaw().split(" "); + if (isCommand(args, discord().getPrefix())) { + executeCommand(event, args, discord().getPrefix()); + } + } + + private boolean isCommand(String[] args, String prefix) { + List<String> commands = new ArrayList<>(); + for (String cmd : getCommands()) { + commands.add(prefix + cmd); + } + return commands.contains(args[0]); + } + +} 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 index a89a211b4f85ce408d47d327a4471d2dddabd64a..12c30135e5fe1c79172006aaac0d73c07a2572c9 100644 --- a/src/main/java/com/l2jserver/datapack/custom/service/discord/DiscordBot.java +++ b/src/main/java/com/l2jserver/datapack/custom/service/discord/DiscordBot.java @@ -24,6 +24,7 @@ import java.awt.Color; import javax.security.auth.login.LoginException; +import com.l2jserver.datapack.custom.service.discord.commands.OnlineCommand; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,7 +48,12 @@ public class DiscordBot { private static final Logger LOG = LoggerFactory.getLogger(DiscordBot.class); private static JDA jda; - + + private static final Object[] COMMANDS = { + new StartListener(), + new OnlineCommand(), + }; + public static void main(String[] args) { if (!discord().enableBot()) { LOG.info("Discord Bot is Disabled."); @@ -56,7 +62,7 @@ public class DiscordBot { try { jda = JDABuilder.createDefault(discord().getBotToken()) // .setAutoReconnect(true) // - .addEventListeners(new StartListener()) // + .addEventListeners(COMMANDS) // .enableIntents(GatewayIntent.GUILD_MEMBERS) // .enableIntents(GatewayIntent.GUILD_MESSAGES) // .setMemberCachePolicy(MemberCachePolicy.ALL) // 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 index 0cf1da4971a6e8f85abfdad389e76c5a29d16cc3..83733b07789fcab45482b544b4635a55c21a0aad 100644 --- a/src/main/java/com/l2jserver/datapack/custom/service/discord/StartListener.java +++ b/src/main/java/com/l2jserver/datapack/custom/service/discord/StartListener.java @@ -18,25 +18,16 @@ */ 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 + * Basic Listener * @author Stalitsa * @version 2.6.2.0 */ @@ -59,23 +50,4 @@ public class StartListener extends ListenerAdapter { 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/java/com/l2jserver/datapack/custom/service/discord/commands/OnlineCommand.java b/src/main/java/com/l2jserver/datapack/custom/service/discord/commands/OnlineCommand.java new file mode 100644 index 0000000000000000000000000000000000000000..d1081c50ea8dcab0d5c48308d40f43b700d3aa00 --- /dev/null +++ b/src/main/java/com/l2jserver/datapack/custom/service/discord/commands/OnlineCommand.java @@ -0,0 +1,66 @@ +/* + * 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.commands; + +import com.l2jserver.datapack.custom.service.discord.AbstractCommand; +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.message.MessageReceivedEvent; + +import java.awt.Color; +import java.util.ArrayList; +import java.util.List; + +/** + * Online Command. + * @author Stalitsa + * @version 2.6.2.0 + */ +public class OnlineCommand extends AbstractCommand { + + @Override + public List<String> getCommands() { + List<String> commands = new ArrayList<>(); + commands.add("online"); + commands.add("on"); + return commands; + } + + @Override + public void executeCommand(MessageReceivedEvent event, String[] args, String prefix) { + + if(args.length > 1) { + event.getTextChannel().sendMessage(new EmbedBuilder().setColor(Color.RED).setDescription("Please use the command without any Arguments").build()).queue(); + event.getMessage().addReaction("\u274C").queue(); // Bot reacts with X mark. + return; + } + event.getMessage().addReaction("\u2705").queue(); // Bot reacts with check mark. + final int playersCount = L2World.getInstance().getAllPlayersCount(); + final int gmCount = AdminData.getInstance().getAllGms(true).size(); + // A command that the bot listens to and responds in an embed with online players and Gms + EmbedBuilder eb = new EmbedBuilder().setColor(Color.CYAN); + eb.setTitle(event.getAuthor().getName(), event.getAuthor().getEffectiveAvatarUrl()); + eb.setDescription("***___GAME INFO___***"); + 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. + } +}