Skip to content
Snippets Groups Projects
Commit 36dd6577 authored by Stalitsa's avatar Stalitsa
Browse files

Discord Bot init commit

parent 5a5da57b
No related branches found
No related tags found
No related merge requests found
...@@ -39,8 +39,7 @@ public final class Elpies extends Event { ...@@ -39,8 +39,7 @@ public final class Elpies extends Event {
// Event duration in minutes // Event duration in minutes
private static final int EVENT_DURATION_MINUTES = 2; private static final int EVENT_DURATION_MINUTES = 2;
// @formatter:off // @formatter:off
private static final int[][] DROPLIST_CONSUMABLES = private static final int[][] DROPLIST_CONSUMABLES = {
{
// itemId, chance, min amount, max amount // itemId, chance, min amount, max amount
{ 1540, 80, 10, 15 }, // Quick Healing Potion { 1540, 80, 10, 15 }, // Quick Healing Potion
{ 1538, 60, 5, 10 }, // Blessed Scroll of Escape { 1538, 60, 5, 10 }, // Blessed Scroll of Escape
...@@ -53,8 +52,7 @@ public final class Elpies extends Event { ...@@ -53,8 +52,7 @@ public final class Elpies extends Event {
{ 20004, 0, 1, 1 } // Energy Ginseng { 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 { 1458, 80, 50, 100 }, // Crystal D-Grade
{ 1459, 60, 40, 80 }, // Crystal C-Grade { 1459, 60, 40, 80 }, // Crystal C-Grade
{ 1460, 40, 30, 60 }, // Crystal B-Grade { 1460, 40, 30, 60 }, // Crystal B-Grade
......
/*
* 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.
}
}
}
/*
* 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.
}
}
}
...@@ -9,6 +9,7 @@ com/l2jserver/datapack/features/SkillTransfer/SkillTransfer.java ...@@ -9,6 +9,7 @@ com/l2jserver/datapack/features/SkillTransfer/SkillTransfer.java
# Custom # Custom
com/l2jserver/datapack/custom/Validators/SubClassSkills.java com/l2jserver/datapack/custom/Validators/SubClassSkills.java
com/l2jserver/datapack/custom/service/buffer/BufferService.java com/l2jserver/datapack/custom/service/buffer/BufferService.java
com/l2jserver/datapack/custom/service/discord/DiscordBot.java
# Custom Events # Custom Events
com/l2jserver/datapack/custom/events/Elpies/Elpies.java com/l2jserver/datapack/custom/events/Elpies/Elpies.java
......
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