Skip to content

Commit 627b38b

Browse files
committed
Fix: Discriminator Use, replace with a UserNameReference Method for a common format
1 parent b0bafa3 commit 627b38b

File tree

6 files changed

+42
-15
lines changed

6 files changed

+42
-15
lines changed

DiscordBot/Extensions/UserExtensions.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,29 @@ public static bool HasRoleGroup(this IUser user, ulong roleId)
1717
{
1818
return user is SocketGuildUser guildUser && guildUser.Roles.Any(x => x.Id == roleId);
1919
}
20+
21+
public static bool IsNickAndNameEqual(this IUser user)
22+
{
23+
return user is SocketGuildUser guildUser && string.Equals(guildUser.Nickname, guildUser.Username, StringComparison.CurrentCultureIgnoreCase);
24+
}
25+
26+
// Returns a simple string formatted as: "**user.Username** (aka **user.Nickname**)"
27+
// Nickname is only included if it's different from the username
28+
public static string UserNameReferenceForEmbed(this IUser user)
29+
{
30+
var reference = $"**{user.Username}**";
31+
if (!user.IsNickAndNameEqual())
32+
reference += $" (aka **{user}**)";
33+
return reference;
34+
}
35+
36+
// Returns a simple string formatted as: "user.Username (aka user.Nickname)"
37+
// Nickname is only included if it's different from the username
38+
public static string UserNameReference(this IUser user)
39+
{
40+
var reference = $"{user.Username}";
41+
if (!user.IsNickAndNameEqual())
42+
reference += $" (aka {user})";
43+
return reference;
44+
}
2045
}

DiscordBot/Modules/UserModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public async Task QuoteMessage(ulong messageId, IMessageChannel channel = null)
131131
.WithFooter(footer =>
132132
{
133133
footer
134-
.WithText($"Quoted by {Context.User.Username}#{Context.User.Discriminator} • From channel {message.Channel.Name}")
134+
.WithText($"Quoted by {Context.User.UserNameReference()} • From channel {message.Channel.Name}")
135135
.WithIconUrl(Context.User.GetAvatarUrl());
136136
})
137137
.WithAuthor(author =>

DiscordBot/Modules/UserSlashModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public async Task ModalResponse(ulong id, ReportMessageModal modal)
170170
.WithFooter(footer =>
171171
{
172172
footer
173-
.WithText($"Reported by {Context.User.Username}#{Context.User.Discriminator} • From channel {reportedMessage.Channel.Name}")
173+
.WithText($"Reported by {Context.User.UserNameReference()} • From channel {reportedMessage.Channel.Name}")
174174
.WithIconUrl(Context.User.GetAvatarUrl());
175175
})
176176
.WithAuthor(author =>

DiscordBot/Services/DatabaseService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public async Task AddNewUser(SocketGuildUser socketUser)
134134
await Query().InsertUser(user);
135135

136136
await _logging.LogAction(
137-
$"User {socketUser.Username}#{socketUser.DiscriminatorValue.ToString()} successfully added to the database.",
137+
$"User {socketUser.UserNameReference()} successfully added to the database.",
138138
true,
139139
false);
140140
}

DiscordBot/Services/ModerationService.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ private async Task MessageDeleted(Cacheable<IMessage, ulong> message, Cacheable<
2525
if (content.Length > 800)
2626
content = content.Substring(0, 800);
2727

28+
var user = message.Value.Author;
2829
var builder = new EmbedBuilder()
2930
.WithColor(new Color(200, 128, 128))
3031
.WithTimestamp(message.Value.Timestamp)
@@ -36,13 +37,13 @@ private async Task MessageDeleted(Cacheable<IMessage, ulong> message, Cacheable<
3637
.WithAuthor(author =>
3738
{
3839
author
39-
.WithName($"{message.Value.Author.Username}");
40+
.WithName($"{user.Username}");
4041
})
4142
.AddField("Deleted message", content);
4243
var embed = builder.Build();
43-
44+
4445
await _loggingService.LogAction(
45-
$"User {message.Value.Author.Username}#{message.Value.Author.DiscriminatorValue} has " +
46+
$"User {user.UserNameReference()} has " +
4647
$"deleted the message\n{content}\n from channel #{(await channel.GetOrDownloadAsync()).Name}", true, false);
4748
await _loggingService.LogAction(" ", false, true, embed);
4849
}

DiscordBot/Services/UserService.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,13 @@ private async Task UserLeft(SocketGuild guild, SocketUser user)
165165
var timeStayed = DateTime.Now - joinDate;
166166
await _loggingService.LogAction(
167167
$"User Left - After {(timeStayed.Days > 1 ? Math.Floor((double)timeStayed.Days) + " days" : " ")}" +
168-
$" {Math.Floor((double)timeStayed.Hours).ToString(CultureInfo.InvariantCulture)} hours {user.Mention} - `{user.Username}#{user.DiscriminatorValue}` - ID : `{user.Id}`");
168+
$" {Math.Floor((double)timeStayed.Hours).ToString(CultureInfo.InvariantCulture)} hours {user.Mention} - `{guildUser.UserNameReference()}` - ID : `{user.Id}`");
169169
}
170170
// If bot is to slow to get user info, we just say they left at current time.
171171
else
172172
{
173173
await _loggingService.LogAction(
174-
$"User `{user.Username}#{user.DiscriminatorValue}` - ID : `{user.Id}` - Left at {DateTime.Now}");
174+
$"User `{guildUser.UserNameReference()}` - ID : `{user.Id}` - Left at {DateTime.Now}");
175175
}
176176
}
177177

@@ -336,7 +336,7 @@ public async Task<string> GenerateProfileCard(IUser user)
336336
MaxXpShown = maxXpShown,
337337
Nickname = ((IGuildUser)user).Nickname,
338338
UserId = ulong.Parse(userData.UserID),
339-
Username = user.Username,
339+
Username = user.UserNameReference(),
340340
XpHigh = xpHigh,
341341
XpLow = xpLow,
342342
XpPercentage = percentage,
@@ -412,9 +412,10 @@ public Embed WelcomeMessage(SocketGuildUser user)
412412
{
413413
string icon = user.GetAvatarUrl();
414414
icon = string.IsNullOrEmpty(icon) ? "https://cdn.discordapp.com/embed/avatars/0.png" : icon;
415-
415+
416+
string welcomeString = $"Welcome to Unity Developer Community {user.UserNameReferenceForEmbed()}!";
416417
var builder = new EmbedBuilder()
417-
.WithDescription($"Welcome to Unity Developer Community **{user.Username}#{user.Discriminator}**!")
418+
.WithDescription(welcomeString)
418419
.WithColor(_welcomeColour)
419420
.WithAuthor(author =>
420421
{
@@ -652,7 +653,7 @@ private async Task UserJoined(SocketGuildUser user)
652653
{
653654
await user.AddRoleAsync(socketTextChannel?.Guild.GetRole(_settings.MutedRoleId));
654655
await _loggingService.LogAction(
655-
$"Currently muted user rejoined - {user.Mention} - `{user.Username}#{user.DiscriminatorValue}` - ID : `{user.Id}`");
656+
$"Currently muted user rejoined - {user.Mention} - `{user.UserNameReference()}` - ID : `{user.Id}`");
656657
if (socketTextChannel != null)
657658
await socketTextChannel.SendMessageAsync(
658659
$"{user.Mention} tried to rejoin the server to avoid their mute. Mute time increased by 72 hours.");
@@ -662,7 +663,7 @@ await socketTextChannel.SendMessageAsync(
662663
}
663664

664665
await _loggingService.LogAction(
665-
$"User Joined - {user.Mention} - `{user.Username}#{user.DiscriminatorValue}` - ID : `{user.Id}`");
666+
$"User Joined - {user.Mention} - `{user.UserNameReference()}` - ID : `{user.Id}`");
666667

667668
// We check if they're already in the welcome list, if they are we don't add them again to avoid double posts
668669
if (_welcomeNoticeUsers.Count == 0 || !_welcomeNoticeUsers.Exists(u => u.id == user.Id))
@@ -767,8 +768,8 @@ private async Task UserUpdated(Cacheable<SocketGuildUser, ulong> oldUserCached,
767768
if (oldUser.Nickname != user.Nickname)
768769
{
769770
await _loggingService.LogAction(
770-
$"User {oldUser.Nickname ?? oldUser.Username}#{oldUser.DiscriminatorValue} changed his " +
771-
$"username to {user.Nickname ?? user.Username}#{user.DiscriminatorValue}");
771+
$"User {oldUser.UserNameReference()} changed his " +
772+
$"username to {user.UserNameReference()}");
772773
}
773774
}
774775

0 commit comments

Comments
 (0)