Have done some changes for logging.

This commit is contained in:
Joel 2021-03-15 16:55:35 +01:00
parent 93357ecb4e
commit 4180b25348
6 changed files with 144 additions and 77 deletions

View File

@ -35,7 +35,6 @@ public class MGZBot {
commandClientBuilder.addCommand(new SuggestCommand());
commandClientBuilder.addCommand(new SetActivityCommand());
commandClientBuilder.addCommand(new ToggleNewsRoleCommand());
commandClientBuilder.addCommand(new InviteCommand());
commandClientBuilder.addCommand(new MessageCommand());
commandClientBuilder.addCommand(new EditMessageCommand());
@ -49,7 +48,7 @@ public class MGZBot {
.enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_EMOJIS)
.addEventListeners(
commandClientBuilder.build(),
new GuildsListener(this),
new GuildsListener(),
new MainGuildListener(this))
.build();
jda.awaitReady();

View File

@ -1,47 +0,0 @@
package zone.themcgamer.discordbot.command.impl;
import com.jagrosh.jdautilities.command.CommandEvent;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
import zone.themcgamer.discordbot.command.BaseCommand;
import zone.themcgamer.discordbot.utilities.EmbedUtils;
import java.util.Collections;
import java.util.List;
public class ToggleNewsRoleCommand extends BaseCommand {
public ToggleNewsRoleCommand() {
name = "togglenews";
aliases = new String[]{"news"};
help = "Turn on or off news role.";
guildOnly = true;
guilds = Collections.singletonList(zone.themcgamer.discordbot.guild.Guild.MAIN);
}
@Override
protected void execute(CommandEvent event, List<String> args) {
Member member = event.getMember();
Guild guild = event.getGuild();
if (member == null)
return;
Role newsRole = guild.getRoleById(812440883898875914L);
if (newsRole == null)
return;
if (member.getRoles().contains(newsRole)) {
guild.removeRoleFromMember(member.getIdLong(), newsRole).queue();
EmbedBuilder embedBuilder = EmbedUtils.successEmbed();
embedBuilder.setDescription("You have successfully removed the " + newsRole.getAsMention() + " role from your account!");
event.reply(embedBuilder.build());
} else {
guild.addRoleToMember(member.getIdLong(), newsRole).queue();
EmbedBuilder embedBuilder = EmbedUtils.successEmbed();
embedBuilder.setDescription("You have successfully added the " + newsRole.getAsMention() + " role to your account!");
event.reply(embedBuilder.build());
}
}
}

View File

@ -1,22 +1,30 @@
package zone.themcgamer.discordbot.events;
import lombok.RequiredArgsConstructor;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.Role;
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.events.guild.member.GuildMemberRoleAddEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleRemoveEvent;
import net.dv8tion.jda.api.events.role.update.RoleUpdateColorEvent;
import net.dv8tion.jda.api.events.role.update.RoleUpdateNameEvent;
import net.dv8tion.jda.api.events.role.update.RoleUpdatePermissionsEvent;
import net.dv8tion.jda.api.events.role.update.RoleUpdatePositionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import zone.themcgamer.discordbot.BotConstants;
import zone.themcgamer.discordbot.MGZBot;
import org.jetbrains.annotations.NotNull;
import zone.themcgamer.discordbot.utilities.EmbedUtils;
import zone.themcgamer.discordbot.utilities.MessageUtils;
import javax.annotation.Nonnull;
import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@RequiredArgsConstructor
public class GuildsListener extends ListenerAdapter {
private final MGZBot mgzBot;
@Override
public void onGuildMemberJoin(@Nonnull GuildMemberJoinEvent event) {
User user = event.getUser();
@ -24,15 +32,11 @@ public class GuildsListener extends ListenerAdapter {
if (user.isBot())
return;
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();
EmbedBuilder embedBuilder = EmbedUtils.successEmbed();
embedBuilder.setTitle("Member Joined");
embedBuilder.addField("Member", user.getAsTag() + " (" + user.getId() + ")", true);
embedBuilder.addField("Guild", guild.getName(), true);
MessageUtils.sendLogMessage(embedBuilder);
}
@Override
@ -41,15 +45,103 @@ public class GuildsListener extends ListenerAdapter {
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();
MessageUtils.sendLogMessage(embedBuilder);
}
@Override
public void onGuildMemberRoleAdd(@NotNull GuildMemberRoleAddEvent event) {
Guild guild = event.getGuild();
List<Role> roles = event.getRoles();
EmbedBuilder embedBuilder = EmbedUtils.successEmbed();
embedBuilder.setTitle("Member Role Added");
embedBuilder.setDescription(event.getUser().getAsTag());
embedBuilder.addField("Role", roles.stream().map(Role::getName).collect(Collectors.joining("&7, &f")), false);
embedBuilder.addField("Guild", guild.getName(), true);
MessageUtils.sendLogMessage(embedBuilder);
}
@Override
public void onGuildMemberRoleRemove(@NotNull GuildMemberRoleRemoveEvent event) {
Guild guild = event.getGuild();
List<Role> roles = event.getRoles();
if (roles.size() == 0)
return;
EmbedBuilder embedBuilder = EmbedUtils.successEmbed();
embedBuilder.setTitle("Member Role Added");
embedBuilder.setDescription(event.getUser().getAsTag());
embedBuilder.addField("Role", roles.stream().map(Role::getName).collect(Collectors.joining(", ")), false);
embedBuilder.addField("Guild", guild.getName(), true);
MessageUtils.sendLogMessage(embedBuilder);
}
@Override
public void onRoleUpdateColor(@NotNull RoleUpdateColorEvent event) {
Guild guild = event.getGuild();
Role role = event.getRole();
EmbedBuilder embedBuilder = EmbedUtils.successEmbed();
embedBuilder.setTitle("Role Color Update");
embedBuilder.setColor(role.getColor());
embedBuilder.addField("Role", role.getAsMention(), false);
embedBuilder.addField("Id", role.getId(), false);
embedBuilder.addField("Color", Objects.requireNonNull(role.getColor()).toString(), false);
embedBuilder.addField("Guild", guild.getName(), true);
MessageUtils.sendLogMessage(embedBuilder);
}
@Override
public void onRoleUpdateName(@NotNull RoleUpdateNameEvent event) {
Guild guild = event.getGuild();
Role role = event.getRole();
EmbedBuilder embedBuilder = EmbedUtils.successEmbed();
embedBuilder.setTitle("Role Name Update");
embedBuilder.setColor(role.getColor());
embedBuilder.setDescription(event.getOldName() + " **->** " + event.getNewName());
embedBuilder.addField("Role", role.getAsMention(), false);
embedBuilder.addField("Id", role.getId(), false);
embedBuilder.addField("Color", Objects.requireNonNull(role.getColor()).toString(), false);
embedBuilder.addField("Guild", guild.getName(), true);
MessageUtils.sendLogMessage(embedBuilder);
}
@Override
public void onRoleUpdatePermissions(@NotNull RoleUpdatePermissionsEvent event) {
Guild guild = event.getGuild();
Role role = event.getRole();
EnumSet<Permission> permission = event.getNewPermissions();
EnumSet<Permission> oldPermission = event.getOldPermissions();
EmbedBuilder embedBuilder = EmbedUtils.successEmbed();
embedBuilder.setTitle("Role Permission Update");
embedBuilder.setColor(role.getColor());
embedBuilder.addField("Role", role.getAsMention(), false);
embedBuilder.addField("Id", role.getId(), false);
embedBuilder.addField("Permissions Added", permission.stream().map(Permission::getName).collect(Collectors.joining(", ")), false);
embedBuilder.addField("Permissions Removed", oldPermission.stream().map(Permission::getName).collect(Collectors.joining(", ")), false);
embedBuilder.addField("Guild", guild.getName(), true);
MessageUtils.sendLogMessage(embedBuilder);
}
@Override
public void onRoleUpdatePosition(@NotNull RoleUpdatePositionEvent event) {
Guild guild = event.getGuild();
Role role = event.getRole();
EmbedBuilder embedBuilder = EmbedUtils.successEmbed();
embedBuilder.setTitle("Role Position Update");
embedBuilder.setColor(role.getColor());
embedBuilder.setDescription(event.getOldPosition() + " -> " + event.getNewPosition());
embedBuilder.addField("Role", role.getAsMention(), false);
embedBuilder.addField("Id", role.getId(), false);
embedBuilder.addField("Color", Objects.requireNonNull(role.getColor()).toString(), false);
embedBuilder.addField("Guild", guild.getName(), true);
MessageUtils.sendLogMessage(embedBuilder);
}
}

View File

@ -44,11 +44,15 @@ public class MainGuildListener extends ListenerAdapter {
GuildUtils.toggleRole(guild, member, memberRole);
Role newsRole = guild.getRoleById(812440883898875914L);
GuildUtils.toggleRole(guild, member, newsRole);
Role pollsRole = guild.getRoleById(813139198428839976L);
GuildUtils.toggleRole(guild, member, pollsRole);
Role eventsRole = guild.getRoleById(813140631705878549L);
GuildUtils.toggleRole(guild, member, eventsRole);
user.openPrivateChannel().queue(privateChannel -> {
EmbedBuilder embedBuilder = EmbedUtils.defaultEmbed();
embedBuilder.setThumbnail(mgzBot.getJda().getSelfUser().getAvatarUrl());
embedBuilder.setDescription("Welcome to **McGamerZone** you have received News & Member role.");
embedBuilder.setDescription("Welcome to **McGamerZone**! You have have the default roles applied your account.\nYou can toggle them in #roles channel!");
privateChannel.sendMessage(embedBuilder.build()).queue();
}, error -> {
TextChannel textChannelById = guild.getTextChannelById(767396615299923998L);

View File

@ -48,16 +48,11 @@ public class GuildUtils {
textChannelById.sendMessage(embedBuilder.build()).queue(message -> message.delete().queueAfter(2, TimeUnit.SECONDS));
});
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();
MessageUtils.sendLogMessage(embedBuilder);
}
}

View File

@ -1,9 +1,14 @@
package zone.themcgamer.discordbot.utilities;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.TextChannel;
import zone.themcgamer.discordbot.BotConstants;
import zone.themcgamer.discordbot.MGZBot;
import zone.themcgamer.discordbot.command.BaseCommand;
import java.util.Objects;
public class MessageUtils {
public static void sendUsageMessage(TextChannel textChannel, BaseCommand command) {
@ -12,4 +17,23 @@ public class MessageUtils {
.build()
).queue();
}
public static void sendLogMessage(Message message) {
Objects.requireNonNull(getLogChannel()).sendMessage(message).queue();
}
public static void sendLogMessage(EmbedBuilder embedBuilder) {
Objects.requireNonNull(getLogChannel()).sendMessage(embedBuilder.build()).queue();
}
private static TextChannel getLogChannel() {
TextChannel logChannel = MGZBot.getInstance().getJda().getTextChannelById(BotConstants.HAROLD_LOG);
if (logChannel == null) {
Objects.requireNonNull(MGZBot.getInstance().getJda().getUserById("504069946528104471"))
.openPrivateChannel().queue(privateChannel ->
privateChannel.sendMessage("There was an error while sending a log message, the channel id is invalid or does not exist.").queue());
return null;
}
return logChannel;
}
}