Skip to content
Snippets Groups Projects
Commit 044fccdb authored by Stalitsa's avatar Stalitsa
Browse files

Add AbstractCommand and move (online) command from main Listener.

parent c9ae4d2b
No related branches found
No related tags found
No related merge requests found
/*
* 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]);
}
}
...@@ -24,6 +24,7 @@ import java.awt.Color; ...@@ -24,6 +24,7 @@ import java.awt.Color;
import javax.security.auth.login.LoginException; import javax.security.auth.login.LoginException;
import com.l2jserver.datapack.custom.service.discord.commands.OnlineCommand;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -47,7 +48,12 @@ public class DiscordBot { ...@@ -47,7 +48,12 @@ public class DiscordBot {
private static final Logger LOG = LoggerFactory.getLogger(DiscordBot.class); private static final Logger LOG = LoggerFactory.getLogger(DiscordBot.class);
private static JDA jda; private static JDA jda;
private static final Object[] COMMANDS = {
new StartListener(),
new OnlineCommand(),
};
public static void main(String[] args) { public static void main(String[] args) {
if (!discord().enableBot()) { if (!discord().enableBot()) {
LOG.info("Discord Bot is Disabled."); LOG.info("Discord Bot is Disabled.");
...@@ -56,7 +62,7 @@ public class DiscordBot { ...@@ -56,7 +62,7 @@ public class DiscordBot {
try { try {
jda = JDABuilder.createDefault(discord().getBotToken()) // jda = JDABuilder.createDefault(discord().getBotToken()) //
.setAutoReconnect(true) // .setAutoReconnect(true) //
.addEventListeners(new StartListener()) // .addEventListeners(COMMANDS) //
.enableIntents(GatewayIntent.GUILD_MEMBERS) // .enableIntents(GatewayIntent.GUILD_MEMBERS) //
.enableIntents(GatewayIntent.GUILD_MESSAGES) // .enableIntents(GatewayIntent.GUILD_MESSAGES) //
.setMemberCachePolicy(MemberCachePolicy.ALL) // .setMemberCachePolicy(MemberCachePolicy.ALL) //
......
...@@ -18,25 +18,16 @@ ...@@ -18,25 +18,16 @@
*/ */
package com.l2jserver.datapack.custom.service.discord; 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.Logger;
import org.slf4j.LoggerFactory; 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.DisconnectEvent;
import net.dv8tion.jda.api.events.ReadyEvent; import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.api.events.ReconnectedEvent; import net.dv8tion.jda.api.events.ReconnectedEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
/** /**
* Basic command Listener * Basic Listener
* @author Stalitsa * @author Stalitsa
* @version 2.6.2.0 * @version 2.6.2.0
*/ */
...@@ -59,23 +50,4 @@ public class StartListener extends ListenerAdapter { ...@@ -59,23 +50,4 @@ public class StartListener extends ListenerAdapter {
public void onReconnect(ReconnectedEvent event) { public void onReconnect(ReconnectedEvent event) {
LOG.info(event.getJDA().getSelfUser().getName() + " has reconnected."); 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.
}
}
} }
/*
* 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.
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment