From 55b68e8f070f1480b934997b9240827333b103a5 Mon Sep 17 00:00:00 2001 From: Joel Date: Sun, 21 Feb 2021 22:05:22 +0100 Subject: [PATCH] Added Join & Quit Log --- .../themcgamer/discordbot/BotConstants.java | 1 + .../events/GuildMemberJoinQuitListener.java | 29 +++++++++++++++++++ .../discordbot/events/ReactionListener.java | 14 ++------- .../discordbot/utilities/GuildUtils.java | 28 ++++++++++++++++++ 4 files changed, 60 insertions(+), 12 deletions(-) diff --git a/discordbot/src/main/java/zone/themcgamer/discordbot/BotConstants.java b/discordbot/src/main/java/zone/themcgamer/discordbot/BotConstants.java index 34df8c5..b81e384 100644 --- a/discordbot/src/main/java/zone/themcgamer/discordbot/BotConstants.java +++ b/discordbot/src/main/java/zone/themcgamer/discordbot/BotConstants.java @@ -24,5 +24,6 @@ public class BotConstants { public static final String COPYRIGHT = "© McGamerZone - " + Calendar.getInstance().get(Calendar.YEAR); // Channels + public static final String HAROLD_LOG = "813151182758608936"; public static final String SUGGESTIONS = "802304706701426730"; // TODO: 2/15/2021 Change this to the main guild's suggestions channel when the bot is on the main guild. } \ No newline at end of file diff --git a/discordbot/src/main/java/zone/themcgamer/discordbot/events/GuildMemberJoinQuitListener.java b/discordbot/src/main/java/zone/themcgamer/discordbot/events/GuildMemberJoinQuitListener.java index 3c8f412..cdf8bd8 100644 --- a/discordbot/src/main/java/zone/themcgamer/discordbot/events/GuildMemberJoinQuitListener.java +++ b/discordbot/src/main/java/zone/themcgamer/discordbot/events/GuildMemberJoinQuitListener.java @@ -7,6 +7,7 @@ import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.entities.TextChannel; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; +import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import zone.themcgamer.discordbot.BotConstants; import zone.themcgamer.discordbot.MGZBot; @@ -61,6 +62,34 @@ public class GuildMemberJoinQuitListener extends ListenerAdapter { textChannelById.sendMessage(embedBuilder.build()).queue(message -> { message.addReaction(":MGZ_Icon_M:791020631042162729").queue(); }); + + TextChannel logChannel = mgzBot.getJda().getTextChannelById(BotConstants.HAROLD_LOG); + if (logChannel == null) + return; + EmbedBuilder MemberJoinedEmbed = EmbedUtils.successEmbed(); + MemberJoinedEmbed.setTitle("Member Joined"); + MemberJoinedEmbed.addField("Member", user.getAsTag() + " (" + user.getId() + ")", true); + MemberJoinedEmbed.addField("Guild", guild.getName(), true); + MemberJoinedEmbed.addField("Status", "Success", false); + logChannel.sendMessage(MemberJoinedEmbed.build()).queue(); } } + + @Override + public void onGuildMemberRemove(@Nonnull GuildMemberRemoveEvent event) { + User user = event.getUser(); + Guild guild = event.getGuild(); + if (user.isBot()) + return; + TextChannel logChannel = mgzBot.getJda().getTextChannelById(BotConstants.HAROLD_LOG); + if (logChannel == null) + return; + + EmbedBuilder embedBuilder = EmbedUtils.errorEmbed(); + embedBuilder.setTitle("Member Left"); + embedBuilder.addField("Member", user.getAsTag() + " (" + user.getId() + ")", true); + embedBuilder.addField("Guild", guild.getName(), true); + embedBuilder.addField("Status", "Success", false); + logChannel.sendMessage(embedBuilder.build()).queue(); + } } \ No newline at end of file diff --git a/discordbot/src/main/java/zone/themcgamer/discordbot/events/ReactionListener.java b/discordbot/src/main/java/zone/themcgamer/discordbot/events/ReactionListener.java index 0a081d7..5ccdc7e 100644 --- a/discordbot/src/main/java/zone/themcgamer/discordbot/events/ReactionListener.java +++ b/discordbot/src/main/java/zone/themcgamer/discordbot/events/ReactionListener.java @@ -11,6 +11,8 @@ import zone.themcgamer.discordbot.utilities.EmbedUtils; import javax.annotation.Nonnull; +import static zone.themcgamer.discordbot.utilities.GuildUtils.toggleRole; + public class ReactionListener extends ListenerAdapter { @Override @@ -42,16 +44,4 @@ public class ReactionListener extends ListenerAdapter { event.getReaction().removeReaction(member.getUser()).queue(); } } - - protected void toggleRole(Guild guild, Member member, Role role) { - if (member.getRoles().contains(role)) - guild.removeRoleFromMember(member.getIdLong(), role).queue(); - else guild.addRoleToMember(member.getIdLong(), role).queue(); - - member.getUser().openPrivateChannel().queue(privateChannel -> { - privateChannel.sendMessage(EmbedUtils.successEmbed().setDescription("Succesfully toggled " + role.getName() + " " + (!member.getRoles().contains(role) ? "On" : "Off")).build()).queue(); - }, error -> { - - }); - } } \ No newline at end of file diff --git a/discordbot/src/main/java/zone/themcgamer/discordbot/utilities/GuildUtils.java b/discordbot/src/main/java/zone/themcgamer/discordbot/utilities/GuildUtils.java index f4054be..e813802 100644 --- a/discordbot/src/main/java/zone/themcgamer/discordbot/utilities/GuildUtils.java +++ b/discordbot/src/main/java/zone/themcgamer/discordbot/utilities/GuildUtils.java @@ -1,6 +1,11 @@ package zone.themcgamer.discordbot.utilities; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.Role; +import net.dv8tion.jda.api.entities.TextChannel; import zone.themcgamer.discordbot.BotConstants; +import zone.themcgamer.discordbot.MGZBot; import zone.themcgamer.discordbot.guild.Guild; /** @@ -18,4 +23,27 @@ public class GuildUtils { } return null; } + + public static void toggleRole(net.dv8tion.jda.api.entities.Guild guild, Member member, Role role) { + if (member.getRoles().contains(role)) + guild.removeRoleFromMember(member.getIdLong(), role).queue(); + else guild.addRoleToMember(member.getIdLong(), role).queue(); + + member.getUser().openPrivateChannel().queue(privateChannel -> { + privateChannel.sendMessage(EmbedUtils.successEmbed().setDescription("Succesfully toggled " + role.getName() + " " + (!member.getRoles().contains(role) ? "On" : "Off")).build()).queue(); + }, error -> { + }); + + TextChannel textChannelById = MGZBot.getInstance().getJda().getTextChannelById(BotConstants.HAROLD_LOG); + if (textChannelById == null) + return; + + EmbedBuilder embedBuilder = EmbedUtils.defaultEmbed(); + embedBuilder.setTitle("Role Log"); + embedBuilder.addField("Member", member.getUser().getAsTag() + " (" + member.getId() + ")", true); + embedBuilder.addField("Role", role.getName() + " " + (!member.getRoles().contains(role) ? "(Added)" : "(Removed)"), true); + embedBuilder.addField("Status", "Success", false); + textChannelById.sendMessage(embedBuilder.build()).queue(); + + } } \ No newline at end of file