Add username edit logging

This commit is contained in:
Anya 2022-05-21 03:42:11 -07:00
parent 299622ed30
commit ae092d3440

View file

@ -1,22 +1,37 @@
package moe.oko.Kiafumi.listener;
import moe.oko.Kiafumi.Kiafumi;
import moe.oko.Kiafumi.KiafumiConfig;
import moe.oko.Kiafumi.util.EmbedUI;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent;
import net.dv8tion.jda.api.events.user.update.UserUpdateNameEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import static moe.oko.Kiafumi.Kiafumi.error;
import static moe.oko.Kiafumi.Kiafumi.debug;
import static moe.oko.Kiafumi.Kiafumi.info;
import static moe.oko.Kiafumi.Kiafumi.error;
/**
* Skynet Listener
* Handles all event logging (Join/Leaves, Username Change, etc).
* @author oko
*/
public class SkynetListener extends ListenerAdapter {
KiafumiConfig config;
DateTimeFormatter dTF = DateTimeFormatter.ofPattern("MM-dd-yyyy");
/**
* Join/Leave logging
* Requires the server to configure welcomeEnabled & welcomeChannel (optionally joinRole).
*/
@Override
public void onGuildMemberJoin(@NotNull GuildMemberJoinEvent event) {
var server = Kiafumi.instance.getServerManager().getOrCreateServer(event.getGuild());
@ -37,14 +52,15 @@ public class SkynetListener extends ListenerAdapter {
error("Failed to send join message to guild " + event.getGuild().getId() + " as the welcome channel was not found.");
return;
}
var dTF = DateTimeFormatter.ofPattern("MM-dd-yyyy");
// Prepare embed.
var embedBuilder = new EmbedBuilder()
var eb = new EmbedBuilder()
.setColor(EmbedUI.SUCCESS)
.setAuthor(event.getMember().getEffectiveName() + "#" + event.getMember().getUser().getDiscriminator() + " ("
+ event.getMember().getId() + ")", null, event.getUser().getAvatarUrl()).setFooter("User Joined").setTimestamp(OffsetDateTime.now())
.setDescription(event.getMember().getAsMention() + " | **Joined Discord**: " + event.getMember().getTimeCreated().format(dTF));
textChannel.sendMessageEmbeds(embedBuilder.build()).queue();
+ event.getMember().getId() + ")", null, event.getUser().getAvatarUrl())
.setDescription(event.getMember().getAsMention() + " | **Joined Discord**: " + event.getMember().getTimeCreated().format(dTF))
.setFooter("User Joined")
.setTimestamp(OffsetDateTime.now());
textChannel.sendMessageEmbeds(eb.build()).queue();
debug("Guild join message successfully sent.");
}
}
@ -63,14 +79,36 @@ public class SkynetListener extends ListenerAdapter {
return;
}
// Prepare embed.
var dTF = DateTimeFormatter.ofPattern("MM-dd-yyyy");
var embedBuilder = new EmbedBuilder()
var eb = new EmbedBuilder()
.setColor(EmbedUI.FAILURE)
.setAuthor(event.getMember().getEffectiveName() + "#" + event.getMember().getUser().getDiscriminator() + " ("
+ event.getMember().getId() + ")", null, event.getUser().getAvatarUrl()).setFooter("User Left").setTimestamp(OffsetDateTime.now())
.setDescription(event.getMember().getAsMention() + " | **Joined Server**: " + event.getMember().getTimeJoined().format(dTF));
textChannel.sendMessageEmbeds(embedBuilder.build()).queue();
+ event.getMember().getId() + ")", null, event.getUser().getAvatarUrl())
.setDescription(event.getMember().getAsMention() + " | **Joined Server**: " + event.getMember().getTimeJoined().format(dTF))
.setFooter("User Left")
.setTimestamp(OffsetDateTime.now());
textChannel.sendMessageEmbeds(eb.build()).queue();
debug("Guild leave message successfully sent.");
}
}
/**
* Name change logging
* Sent to the central logChannel.
*/
@Override
public void onUserUpdateName(@NotNull UserUpdateNameEvent event) {
var user = event.getUser();
String name = event.getNewName(), oldName = event.getOldName();
info("User [" + name + ':' + user.getId() + "] changed name from \"" + oldName + "\".");
var logChannel = Kiafumi.JDA.getTextChannelById(config.getLogChannel());
var eb = new EmbedBuilder()
.setColor(EmbedUI.INFO)
.setAuthor(user.getName() + '#' + user.getDiscriminator() + " (" + user.getId() + ')', null, user.getAvatarUrl())
.setDescription('`' + oldName + "` → `" + name + '`')
.setFooter("Username Edited")
.setTimestamp(OffsetDateTime.now());
logChannel.sendMessageEmbeds(eb.build()).queue();
debug("Username edit message successfully sent.");
}
}