From 4ca89c6bf25a4dfc04c3907347f142cb1bb903bc Mon Sep 17 00:00:00 2001 From: ZeroEpoch1969 <13510767+ZeroEpoch1969@users.noreply.github.com> Date: Thu, 28 Nov 2019 02:08:36 -0700 Subject: [PATCH] probably broke something --- .idea/compiler.xml | 1 + .idea/modules.xml | 2 +- pom.xml | 2 +- .../totalfreedommod/ChatManager.java | 3 + .../totalfreedommod/config/ConfigEntry.java | 2 + .../totalfreedommod/discord/Discord.java | 114 +++++++++++++----- .../discord/PrivateMessageListener.java | 8 +- src/main/resources/config.yml | 4 + 8 files changed, 103 insertions(+), 33 deletions(-) diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 8c6ab777..6467f80c 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -7,6 +7,7 @@ + diff --git a/.idea/modules.xml b/.idea/modules.xml index bf19ff78..8915fc5f 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 8235f229..44ea6a7a 100644 --- a/pom.xml +++ b/pom.xml @@ -170,7 +170,7 @@ net.dv8tion JDA - 3.8.0_423 + 4.0.0_39 provided diff --git a/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java b/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java index 269b4305..0a7ef8ad 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java @@ -130,6 +130,9 @@ public class ChatManager extends FreedomService // Set format event.setFormat(format); + + // Send to discord + plugin.dc.messageChatChannel(player.getName() + " \u00BB " + ChatColor.stripColor(message)); } public ChatColor getColor(Admin admin, Displayable display) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/config/ConfigEntry.java b/src/main/java/me/totalfreedom/totalfreedommod/config/ConfigEntry.java index 4a6994a1..99a36f2e 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/config/ConfigEntry.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/config/ConfigEntry.java @@ -56,6 +56,7 @@ public enum ConfigEntry // DISCORD_TOKEN(String.class, "discord.token"), DISCORD_REPORT_CHANNEL_ID(String.class, "discord.report_channel_id"), + DISCORD_CHAT_CHANNEL_ID(String.class, "discord.chat_channel_id"), DISCORD_ROLE_SYNC(Boolean.class, "discord.role_sync"), DISCORD_SERVER_ID(String.class, "discord.server_id"), DISCORD_MASTER_BUILDER_ROLE_ID(String.class, "discord.master_builder_role_id"), @@ -63,6 +64,7 @@ public enum ConfigEntry DISCORD_TELNET_ROLE_ID(String.class, "discord.telnet_role_id"), DISCORD_SENIOR_ROLE_ID(String.class, "discord.senior_role_id"), DISCORD_DEVELOPER_ROLE_ID(String.class, "discord.developer_role_id"), + DISCORD_ASSISTANT_EXECUTIVE_ROLE_ID(String.class, "discord.assistant_executive_role_id"), DISCORD_EXECUTIVE_ROLE_ID(String.class, "discord.executive_role_id"), DISCORD_SERVER_OWNER_ROLE_ID(String.class, "discord.server_owner_role_id"), // diff --git a/src/main/java/me/totalfreedom/totalfreedommod/discord/Discord.java b/src/main/java/me/totalfreedom/totalfreedommod/discord/Discord.java index be506ddf..4f55c997 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/discord/Discord.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/discord/Discord.java @@ -15,18 +15,24 @@ import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.playerverification.VPlayer; import me.totalfreedom.totalfreedommod.rank.Rank; import me.totalfreedom.totalfreedommod.util.FLog; -import net.dv8tion.jda.core.AccountType; -import net.dv8tion.jda.core.EmbedBuilder; -import net.dv8tion.jda.core.JDA; -import net.dv8tion.jda.core.JDABuilder; -import net.dv8tion.jda.core.entities.Guild; -import net.dv8tion.jda.core.entities.Member; -import net.dv8tion.jda.core.entities.MessageEmbed; -import net.dv8tion.jda.core.entities.Role; -import net.dv8tion.jda.core.entities.TextChannel; -import net.dv8tion.jda.core.managers.GuildController; +import net.dv8tion.jda.api.AccountType; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.JDABuilder; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.MessageEmbed; +import net.dv8tion.jda.api.entities.Role; +import net.dv8tion.jda.api.entities.TextChannel; +import net.dv8tion.jda.api.events.ReadyEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; import org.apache.commons.lang.WordUtils; import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.entity.PlayerDeathEvent; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; public class Discord extends FreedomService { @@ -57,22 +63,68 @@ public class Discord extends FreedomService } try { - bot = new JDABuilder(AccountType.BOT).setToken(ConfigEntry.DISCORD_TOKEN.getString()).addEventListener(new PrivateMessageListener()).setAudioEnabled(false).setAutoReconnect(true).buildBlocking(); + + bot = new JDABuilder(AccountType.BOT) + .setToken(ConfigEntry.DISCORD_TOKEN.getString()) + .addEventListeners(new PrivateMessageListener()) + .addEventListeners(new DiscordToMinecraftListener()) + .setAutoReconnect(true) + .addEventListeners(new ListenerAdapter() + { + @Override + public void onReady(ReadyEvent event) + { + new StartEvent(event.getJDA()).start(); + } + }).build(); FLog.info("Discord verification bot has successfully enabled!"); } catch (LoginException e) { FLog.warning("An invalid token for the discord verification bot, the bot will not enable."); } - catch (IllegalArgumentException | InterruptedException e) + catch (IllegalArgumentException e) { FLog.warning("Discord verification bot failed to start."); } catch (NoClassDefFoundError e) { FLog.warning("The JDA plugin is not installed, therefore the bot cannot start."); - FLog.warning("To resolve this error, please download JDA from: https://github.com/TFPatches/Minecraft-JDA/releases"); + FLog.warning("To resolve this error, please download the latest JDA from: https://github.com/TFPatches/Minecraft-JDA/releases"); } + + } + + // Do no ask why this is here. I spent two hours trying to make a simple thing work + public class StartEvent { + private final JDA api; + + public StartEvent(JDA api) { + this.api = api; + } + + public void start() + { + messageChatChannel("**Server has started**"); + } + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onPlayerJoin(PlayerJoinEvent event) + { + messageChatChannel("**" + event.getPlayer().getName() + " joined the server" + "**"); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onPlayerLeave(PlayerQuitEvent event) + { + messageChatChannel("**" + event.getPlayer().getName() + " left the server" + "**"); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onPlayerDeath(PlayerDeathEvent event) + { + messageChatChannel("**" + event.getDeathMessage() + "**"); } @Override @@ -81,6 +133,15 @@ public class Discord extends FreedomService startBot(); } + public void messageChatChannel(String message) + { + String chat_channel_id = ConfigEntry.DISCORD_CHAT_CHANNEL_ID.getString(); + if (enabled && !chat_channel_id.isEmpty()) + { + bot.getTextChannelById(chat_channel_id).sendMessage(message).queue(); + } + } + public static String getCodeForAdmin(Admin admin) { for (String code : LINK_CODES.keySet()) @@ -110,6 +171,7 @@ public class Discord extends FreedomService { if (bot != null) { + messageChatChannel("**Server has stopped**"); bot.shutdown(); } FLog.info("Discord verification bot has successfully shutdown."); @@ -171,8 +233,6 @@ public class Discord extends FreedomService return false; } - GuildController controller = new GuildController(server); - Member member = server.getMemberById(admin.getDiscordID()); if (member == null) { @@ -202,15 +262,15 @@ public class Discord extends FreedomService { if (member.getRoles().contains(superAdminRole)) { - controller.removeRolesFromMember(member, superAdminRole).complete(); + server.removeRoleFromMember(member, superAdminRole).complete(); } if (member.getRoles().contains(telnetAdminRole)) { - controller.removeRolesFromMember(member, telnetAdminRole).complete(); + server.removeRoleFromMember(member, telnetAdminRole).complete(); } if (member.getRoles().contains(seniorAdminRole)) { - controller.removeRolesFromMember(member, seniorAdminRole).complete(); + server.removeRoleFromMember(member, seniorAdminRole).complete(); } return true; } @@ -219,15 +279,15 @@ public class Discord extends FreedomService { if (!member.getRoles().contains(superAdminRole)) { - controller.addRolesToMember(member, superAdminRole).complete(); + server.addRoleToMember(member, superAdminRole).complete(); } if (member.getRoles().contains(telnetAdminRole)) { - controller.removeRolesFromMember(member, telnetAdminRole).complete(); + server.removeRoleFromMember(member, telnetAdminRole).complete(); } if (member.getRoles().contains(seniorAdminRole)) { - controller.removeRolesFromMember(member, seniorAdminRole).complete(); + server.removeRoleFromMember(member, seniorAdminRole).complete(); } return true; } @@ -235,15 +295,15 @@ public class Discord extends FreedomService { if (!member.getRoles().contains(telnetAdminRole)) { - controller.addRolesToMember(member, telnetAdminRole).complete(); + server.addRoleToMember(member, telnetAdminRole).complete(); } if (member.getRoles().contains(superAdminRole)) { - controller.removeRolesFromMember(member, superAdminRole).complete(); + server.removeRoleFromMember(member, superAdminRole).complete(); } if (member.getRoles().contains(seniorAdminRole)) { - controller.removeRolesFromMember(member, seniorAdminRole).complete(); + server.removeRoleFromMember(member, seniorAdminRole).complete(); } return true; } @@ -251,15 +311,15 @@ public class Discord extends FreedomService { if (!member.getRoles().contains(seniorAdminRole)) { - controller.addRolesToMember(member, seniorAdminRole).complete(); + server.addRoleToMember(member, seniorAdminRole).complete(); } if (member.getRoles().contains(superAdminRole)) { - controller.removeRolesFromMember(member, superAdminRole).complete(); + server.removeRoleFromMember(member, superAdminRole).complete(); } if (member.getRoles().contains(telnetAdminRole)) { - controller.removeRolesFromMember(member, telnetAdminRole).complete(); + server.removeRoleFromMember(member, telnetAdminRole).complete(); } return true; } diff --git a/src/main/java/me/totalfreedom/totalfreedommod/discord/PrivateMessageListener.java b/src/main/java/me/totalfreedom/totalfreedommod/discord/PrivateMessageListener.java index 5849ef26..dc1a6836 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/discord/PrivateMessageListener.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/discord/PrivateMessageListener.java @@ -3,8 +3,8 @@ package me.totalfreedom.totalfreedommod.discord; import me.totalfreedom.totalfreedommod.TotalFreedomMod; import me.totalfreedom.totalfreedommod.admin.Admin; import me.totalfreedom.totalfreedommod.playerverification.VPlayer; -import net.dv8tion.jda.core.events.message.priv.PrivateMessageReceivedEvent; -import net.dv8tion.jda.core.hooks.ListenerAdapter; +import net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; public class PrivateMessageListener extends ListenerAdapter { @@ -21,7 +21,7 @@ public class PrivateMessageListener extends ListenerAdapter Admin admin = Discord.LINK_CODES.get(code); admin.setDiscordID(event.getMessage().getAuthor().getId()); Discord.LINK_CODES.remove(code); - event.getChannel().sendMessage("Link successful. Now this Discord account is linked with the Minecraft account `" + admin.getName() + "`.\n" + event.getChannel().sendMessage("Link successful. Now this Discord account is linked with your Minecraft account `" + admin.getName() + "`.\n" + "Now when you are an impostor on the server, you may use `/verify` to verify.").complete(); Discord.syncRoles(admin); } @@ -33,7 +33,7 @@ public class PrivateMessageListener extends ListenerAdapter TotalFreedomMod.plugin().pv.saveVerificationData(player); Discord.PLAYER_LINK_CODES.remove(code); - event.getChannel().sendMessage("Link successful. Now this Discord account is linked with the Minecraft account `" + player.getName() + "`.\n" + event.getChannel().sendMessage("Link successful. Now this Discord account is linked with your Minecraft account `" + player.getName() + "`.\n" + "Now when you are an impostor on the server, you may use `/verify` to verify.").complete(); } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index f415b389..a375956b 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -50,6 +50,8 @@ discord: server_id: '' # Channel to send /report messages to report_channel_id: '' + # Channel for discord to minecraft and vice-versa + chat_channel_id: '' # Do you want roles to sync with the server? role_sync: false # Master Admin role ID @@ -62,6 +64,8 @@ discord: senior_role_id: '' # Developer role ID developer_role_id: '' + # Assistant Executive Admin role ID + assistant_executive_role_id: '' # Executive Admin role ID executive_role_id: '' # Owner role ID