Skip to content

Commit

Permalink
replace getAsTag() with getName() on user entity (#1016)
Browse files Browse the repository at this point in the history
* replace getAsTag() with getName() on user entity

* problematic changes in RemindRoutine related classes

* addition to problematic changes

* fixed failing tests

---------

Co-authored-by: Zabuzard <[email protected]>
  • Loading branch information
2 people authored and Taz03 committed Mar 6, 2024
1 parent 74a4ad9 commit bc4ecfe
Show file tree
Hide file tree
Showing 21 changed files with 34 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ private static RestAction<Message> handleAlreadyBanned(Guild.Ban ban, Interactio
String reasonText =
reason == null || reason.isBlank() ? "" : " (reason: %s)".formatted(reason);

String message = "The user '%s' is already banned%s.".formatted(ban.getUser().getAsTag(),
reasonText);
String message =
"The user '%s' is already banned%s.".formatted(ban.getUser().getName(), reasonText);
return hook.sendMessage(message).setEphemeral(true);
}

Expand Down Expand Up @@ -123,7 +123,7 @@ private static Optional<RestAction<Message>> handleNotAlreadyBannedResponse(
}
logger.warn(LogMarkers.SENSITIVE,
"Something unexpected went wrong while trying to ban the user '{}'.",
target.getAsTag(), alreadyBannedFailure);
target.getName(), alreadyBannedFailure);
return Optional.of(hook.sendMessage("Failed to ban the user due to an unexpected problem.")
.setEphemeral(true));
}
Expand All @@ -145,7 +145,7 @@ private AuditableRestAction<Void> banUser(User target, Member author,
temporaryData == null ? "permanently" : "for " + temporaryData.duration();
logger.info(LogMarkers.SENSITIVE,
"'{}' ({}) banned the user '{}' ({}) {} from guild '{}' and deleted their message history of the last {} days, for reason '{}'.",
author.getUser().getAsTag(), author.getId(), target.getAsTag(), target.getId(),
author.getUser().getName(), author.getId(), target.getName(), target.getId(),
durationMessage, guild.getName(), deleteHistoryDays, reason);

Instant expiresAt = temporaryData == null ? null : temporaryData.expiresAt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private AuditableRestAction<Void> kickUser(Member target, Member author, String
Guild guild) {
logger.info(LogMarkers.SENSITIVE,
"'{}' ({}) kicked the user '{}' ({}) from guild '{}' for reason '{}'.",
author.getUser().getAsTag(), author.getId(), target.getUser().getAsTag(),
author.getUser().getName(), author.getId(), target.getUser().getName(),
target.getId(), guild.getName(), reason);

actionsStore.addAction(guild.getIdLong(), author.getIdLong(), target.getIdLong(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ static boolean handleReason(CharSequence reason, IReplyCallback event) {
*/
public static boolean handleCanInteractWithTarget(String actionVerb, Member bot, Member author,
Member target, IReplyCallback event) {
String targetTag = target.getUser().getAsTag();
String targetTag = target.getUser().getName();
if (!author.canInteract(target)) {
event
.reply("The user %s is too powerful for you to %s.".formatted(targetTag,
Expand Down Expand Up @@ -279,8 +279,8 @@ static boolean handleHasAuthorPermissions(String actionVerb, Permission permissi
*/
static MessageEmbed createActionResponse(User author, ModerationAction action, User target,
@Nullable String extraMessage, @Nullable String reason) {
String description = "%s **%s** (id: %s).".formatted(action.getVerb(), target.getAsTag(),
target.getId());
String description =
"%s **%s** (id: %s).".formatted(action.getVerb(), target.getName(), target.getId());
if (extraMessage != null && !extraMessage.isBlank()) {
description += "\n" + extraMessage;
}
Expand All @@ -290,7 +290,7 @@ static MessageEmbed createActionResponse(User author, ModerationAction action, U

String avatarOrDefaultUrl = author.getEffectiveAvatarUrl();

return new EmbedBuilder().setAuthor(author.getAsTag(), null, avatarOrDefaultUrl)
return new EmbedBuilder().setAuthor(author.getName(), null, avatarOrDefaultUrl)
.setDescription(description)
.setTimestamp(Instant.now())
.setColor(AMBIENT_COLOR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private AuditableRestAction<Void> muteUser(Member target, Member author,
temporaryData == null ? "permanently" : "for " + temporaryData.duration();
logger.info(LogMarkers.SENSITIVE,
"'{}' ({}) muted the user '{}' ({}) {} in guild '{}' for reason '{}'.",
author.getUser().getAsTag(), author.getId(), target.getUser().getAsTag(),
author.getUser().getName(), author.getId(), target.getUser().getName(),
target.getId(), durationMessage, guild.getName(), reason);

Instant expiresAt = temporaryData == null ? null : temporaryData.expiresAt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private void sendNote(User target, Member author, String content, ISnowflake gui
private void storeNote(User target, Member author, String content, ISnowflake guild) {
logger.info(LogMarkers.SENSITIVE,
"'{}' ({}) wrote a note about the user '{}' ({}) with content '{}'.",
author.getUser().getAsTag(), author.getId(), target.getAsTag(), target.getId(),
author.getUser().getName(), author.getId(), target.getName(), target.getId(),
content);

actionsStore.addAction(guild.getIdLong(), author.getIdLong(), target.getIdLong(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private AuditableRestAction<Void> quarantineUser(Member target, Member author, S
Guild guild) {
logger.info(LogMarkers.SENSITIVE,
"'{}' ({}) quarantined the user '{}' ({}) in guild '{}' for reason '{}'.",
author.getUser().getAsTag(), author.getId(), target.getUser().getAsTag(),
author.getUser().getName(), author.getId(), target.getUser().getName(),
target.getId(), guild.getName(), reason);

actionsStore.addAction(guild.getIdLong(), author.getIdLong(), target.getIdLong(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private static void applyModerationRole(ModerationRole moderationRole, Member me
Guild guild = member.getGuild();
logger.info(LogMarkers.SENSITIVE,
"Reapplied existing {} to user '{}' ({}) in guild '{}' after rejoining.",
moderationRole.actionName, member.getUser().getAsTag(), member.getId(),
moderationRole.actionName, member.getUser().getName(), member.getId(),
guild.getName());

guild.addRoleToMember(member, moderationRole.guildToRole.apply(guild))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private void unban(User target, Member author, String reason, Guild guild,

logger.info(LogMarkers.SENSITIVE,
"'{}' ({}) unbanned the user '{}' ({}) from guild '{}' for reason '{}'.",
author.getUser().getAsTag(), author.getId(), target.getAsTag(), target.getId(),
author.getUser().getName(), author.getId(), target.getName(), target.getId(),
guild.getName(), reason);

actionsStore.addAction(guild.getIdLong(), author.getIdLong(), target.getIdLong(),
Expand All @@ -62,7 +62,7 @@ private void unban(User target, Member author, String reason, Guild guild,
}

private static void handleFailure(Throwable unbanFailure, User target, IReplyCallback event) {
String targetTag = target.getAsTag();
String targetTag = target.getName();
if (unbanFailure instanceof ErrorResponseException errorResponseException) {
if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) {
event.reply("The specified user does not exist.").setEphemeral(true).queue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private AuditableRestAction<Void> unmuteUser(Member target, Member author, Strin
Guild guild) {
logger.info(LogMarkers.SENSITIVE,
"'{}' ({}) unmuted the user '{}' ({}) in guild '{}' for reason '{}'.",
author.getUser().getAsTag(), author.getId(), target.getUser().getAsTag(),
author.getUser().getName(), author.getId(), target.getUser().getName(),
target.getId(), guild.getName(), reason);

actionsStore.addAction(guild.getIdLong(), author.getIdLong(), target.getIdLong(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private AuditableRestAction<Void> unquarantineUser(Member target, Member author,
Guild guild) {
logger.info(LogMarkers.SENSITIVE,
"'{}' ({}) unquarantined the user '{}' ({}) in guild '{}' for reason '{}'.",
author.getUser().getAsTag(), author.getId(), target.getUser().getAsTag(),
author.getUser().getName(), author.getId(), target.getUser().getName(),
target.getId(), guild.getName(), reason);

actionsStore.addAction(guild.getIdLong(), author.getIdLong(), target.getIdLong(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private static RestAction<Boolean> sendDm(User target, String reason, Guild guil

private void warnUser(User target, Member author, String reason, Guild guild) {
logger.info(LogMarkers.SENSITIVE, "'{}' ({}) warned the user '{}' ({}) for reason '{}'.",
author.getUser().getAsTag(), author.getId(), target.getAsTag(), target.getId(),
author.getUser().getName(), author.getId(), target.getName(), target.getId(),
reason);

actionsStore.addAction(guild.getIdLong(), author.getIdLong(), target.getIdLong(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private static EmbedBuilder generateEmbedBuilder(final Interaction event, final

EmbedBuilder embedBuilder = new EmbedBuilder().setThumbnail(user.getEffectiveAvatarUrl())
.setColor(effectiveColor)
.setFooter("Requested by " + event.getUser().getAsTag(),
.setFooter("Requested by " + event.getUser().getName(),
event.getMember().getEffectiveAvatarUrl())
.setTimestamp(Instant.now());

Expand Down Expand Up @@ -175,8 +175,8 @@ private static String possibleBoosterToStringItem(final Member member) {
* @return user readable {@link String}
*/
private static String userIdentificationToStringItem(final User user) {
return "**Mention:** " + user.getAsMention() + "\n**Tag:** " + user.getAsTag()
+ "\n**ID:** " + user.getId();
return "**Mention:** " + user.getAsMention() + "\n**Tag:** " + user.getName() + "\n**ID:** "
+ user.getId();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private List<List<ActionRecord>> groupActionsByPages(List<ActionRecord> actions)
private static EmbedBuilder createSummaryEmbed(User user, Collection<ActionRecord> actions) {
String avatarOrDefaultUrl = user.getEffectiveAvatarUrl();

return new EmbedBuilder().setTitle("Audit log of **%s**".formatted(user.getAsTag()))
return new EmbedBuilder().setTitle("Audit log of **%s**".formatted(user.getName()))
.setAuthor(user.getName(), null, avatarOrDefaultUrl)
.setDescription(createSummaryMessageDescription(actions))
.setColor(ModerationUtils.AMBIENT_COLOR);
Expand Down Expand Up @@ -190,7 +190,7 @@ private RestAction<EmbedBuilder> attachEmbedFields(EmbedBuilder auditEmbed,

private static RestAction<MessageEmbed.Field> actionToField(ActionRecord action, JDA jda) {
return jda.retrieveUserById(action.authorId())
.map(author -> author == null ? "(unknown user)" : author.getAsTag())
.map(author -> author == null ? "(unknown user)" : author.getName())
.map(authorText -> {
String expiresAtFormatted = action.actionExpiresAt() == null ? ""
: "\nTemporary action, expires at: " + formatTime(action.actionExpiresAt());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ String getVerb() {
private record AuditLogMessage(User author, Action action, @Nullable User target,
@Nullable String reason, TemporalAccessor timestamp) {
MessageEmbed toEmbed() {
String targetTag = target == null ? "(user unknown)" : target.getAsTag();
String targetTag = target == null ? "(user unknown)" : target.getName();
String description = "%s **%s**.".formatted(action.getVerb(), targetTag);

if (reason != null && !reason.isBlank()) {
Expand All @@ -343,7 +343,7 @@ MessageEmbed toEmbed() {

String avatarOrDefaultUrl = author.getEffectiveAvatarUrl();

return new EmbedBuilder().setAuthor(author.getAsTag(), null, avatarOrDefaultUrl)
return new EmbedBuilder().setAuthor(author.getName(), null, avatarOrDefaultUrl)
.setDescription(description)
.setTimestamp(timestamp)
.setColor(AMBIENT_COLOR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void write(String title, String description, @Nullable User author,
.setTimestamp(timestamp)
.setColor(EMBED_COLOR);
if (author != null) {
embedBuilder.setAuthor(author.getAsTag(), null, author.getAvatarUrl());
embedBuilder.setAuthor(author.getName(), null, author.getAvatarUrl());
}

MessageCreateAction message =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ private void sendMessage(ModalInteractionEvent event, MessageCreateAction messag
}

private MessageEmbed createModMailMessage(@Nullable User author, String userMessage) {
String authorTag = author == null ? "Anonymous" : author.getAsTag();
String authorTag = author == null ? "Anonymous" : author.getName();
String authorAvatar = author == null ? null : author.getAvatarUrl();
return new EmbedBuilder().setTitle("Modmail")
.setAuthor(authorTag, null, authorAvatar)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ private void reportScamMessage(MessageReceivedEvent event, String reportTitle,
MessageEmbed embed =
new EmbedBuilder().setDescription(event.getMessage().getContentStripped())
.setTitle(reportTitle)
.setAuthor(author.getAsTag(), null, avatarOrDefaultUrl)
.setAuthor(author.getName(), null, avatarOrDefaultUrl)
.setTimestamp(event.getMessage().getTimeCreated())
.setColor(AMBIENT_COLOR)
.setFooter(author.getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private void revokeAction(RevocationGroupIdentifier groupIdentifier) {
private RestAction<Void> executeRevocation(Guild guild, User target,
ModerationAction actionType) {
logger.info(LogMarkers.SENSITIVE, "Revoked temporary action {} against user '{}' ({}).",
actionType, target.getAsTag(), target.getId());
actionType, target.getName(), target.getId());
RevocableModerationAction action = getRevocableActionByType(actionType);

String reason = "Automatic revocation of temporary action.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private void sendReminderViaRoute(RestAction<ReminderRoute> routeAction,

private static MessageEmbed createReminderEmbed(CharSequence content,
TemporalAccessor createdAt, @Nullable User author) {
String authorName = author == null ? "Unknown user" : author.getAsTag();
String authorName = author == null ? "Unknown user" : author.getName();
String authorIconUrl = author == null ? null : author.getAvatarUrl();

return new EmbedBuilder().setAuthor(authorName, null, authorIconUrl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void sendsPendingReminderChannelFoundAuthorFound() {
MessageEmbed lastMessage = getLastMessageFrom(jdaTester.getTextChannelSpy());
assertEquals(reminderContent, lastMessage.getDescription());
assertSimilar(remindAt, lastMessage.getTimestamp().toInstant());
assertEquals(author.getUser().getAsTag(), lastMessage.getAuthor().getName());
assertEquals(author.getUser().getName(), lastMessage.getAuthor().getName());
}

@Test
Expand Down Expand Up @@ -145,7 +145,7 @@ void sendsPendingReminderChannelNotFoundAuthorFound() {
MessageEmbed lastMessage = getLastMessageFrom(jdaTester.getPrivateChannelSpy());
assertEquals(reminderContent, lastMessage.getDescription());
assertSimilar(remindAt, lastMessage.getTimestamp().toInstant());
assertEquals(author.getUser().getAsTag(), lastMessage.getAuthor().getName());
assertEquals(author.getUser().getName(), lastMessage.getAuthor().getName());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ public JdaTester() {
when(jda.getCacheFlags()).thenReturn(EnumSet.noneOf(CacheFlag.class));

SelfUserImpl selfUser = spy(new SelfUserImpl(SELF_USER_ID, jda));
selfUser.setName("Self Tester");
UserImpl user = spy(new UserImpl(USER_ID, jda));
user.setName("John Doe Tester");
guild = spy(new GuildImpl(jda, GUILD_ID));
rateLimiter =
new SequentialRestRateLimiter(new RestRateLimiter.RateLimitConfig(RATE_LIMIT_POOL,
Expand Down

0 comments on commit bc4ecfe

Please sign in to comment.