Added Join & Quit Log

This commit is contained in:
Joel 2021-02-21 22:05:22 +01:00
parent e53b1feb50
commit 55b68e8f07
4 changed files with 60 additions and 12 deletions

@ -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.
}

@ -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();
}
}

@ -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 -> {
});
}
}

@ -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();
}
}