Use new extension method for displaying username
This commit is contained in:
parent
aad54f9fa7
commit
97654a6f29
10 changed files with 55 additions and 21 deletions
|
@ -1,5 +1,7 @@
|
|||
using Discord;
|
||||
using RegexBot.Data;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
|
@ -81,9 +83,41 @@ public static class Utilities {
|
|||
try {
|
||||
var entityTry = new EntityName(input!, EntityType.User);
|
||||
var issueq = bot.EcQueryUser(entityTry.Id!.Value.ToString());
|
||||
if (issueq != null) result = $"<@{issueq.UserId}> - {issueq.Username}#{issueq.Discriminator} `{issueq.UserId}`";
|
||||
if (issueq != null) result = $"<@{issueq.UserId}> - {issueq.GetDisplayableUsername()} `{issueq.UserId}`";
|
||||
else result = $"Unknown user with ID `{entityTry.Id!.Value}`";
|
||||
} catch (Exception) { }
|
||||
return result ?? input;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="GetDisplayableUsernameCommon"/>
|
||||
public static string GetDisplayableUsername(this SocketUser user)
|
||||
=> GetDisplayableUsernameCommon(user.Username, user.Discriminator, user.GlobalName);
|
||||
|
||||
/// <inheritdoc cref="GetDisplayableUsernameCommon"/>
|
||||
public static string GetDisplayableUsername(this CachedUser user)
|
||||
=> GetDisplayableUsernameCommon(user.Username, user.Discriminator, user.GlobalName);
|
||||
|
||||
/// <summary>
|
||||
/// Returns a string representation of the user's username suitable for display purposes.
|
||||
/// For the sake of consistency, it is preferable using this instead of any other means, including Discord.Net's ToString.
|
||||
/// </summary>
|
||||
private static string GetDisplayableUsernameCommon(string username, string discriminator, string? global) {
|
||||
static string escapeFormattingCharacters(string input) {
|
||||
var result = new StringBuilder();
|
||||
foreach (var c in input) {
|
||||
if (c is '\\' or '_' or '~' or '*' or '@' or '`') {
|
||||
result.Append('\\');
|
||||
}
|
||||
result.Append(c);
|
||||
}
|
||||
return result.ToString();
|
||||
}
|
||||
|
||||
if (discriminator == "0000") {
|
||||
if (global != null) return $"{escapeFormattingCharacters(global)} ({username})";
|
||||
return username;
|
||||
} else {
|
||||
return $"{escapeFormattingCharacters(username)}#{discriminator}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
using System.Text;
|
||||
using RegexBot.Common;
|
||||
using System.Text;
|
||||
|
||||
namespace RegexBot.Modules.EntryRole;
|
||||
|
||||
/// <summary>
|
||||
/// Automatically sets a role onto users entering the guild after a predefined amount of time.
|
||||
/// </summary>
|
||||
|
@ -138,7 +138,7 @@ internal sealed class EntryRole : RegexbotModule, IDisposable {
|
|||
var failList = new StringBuilder();
|
||||
var count = 0;
|
||||
foreach (var item in failedUserList) {
|
||||
failList.Append($", {item.Username}#{item.Discriminator}");
|
||||
failList.Append($", {item.GetDisplayableUsername()}");
|
||||
count++;
|
||||
if (count > 5) {
|
||||
failList.Append($"and {count} other(s).");
|
||||
|
|
|
@ -58,7 +58,7 @@ class ShowModLogs : CommandConfig {
|
|||
|
||||
var resultList = new EmbedBuilder() {
|
||||
Author = new EmbedAuthorBuilder() {
|
||||
Name = $"{query.User.Username}#{query.User.Discriminator}",
|
||||
Name = $"{query.User.GetDisplayableUsername()}",
|
||||
IconUrl = query.User.AvatarUrl
|
||||
},
|
||||
Footer = new EmbedFooterBuilder() {
|
||||
|
|
|
@ -29,13 +29,13 @@ class Unban : CommandConfig {
|
|||
var query = Module.Bot.EcQueryUser(targetstr);
|
||||
if (query != null) {
|
||||
targetId = (ulong)query.UserId;
|
||||
targetDisplay = $"{query.Username}#{query.Discriminator}";
|
||||
targetDisplay = $"**{query.GetDisplayableUsername()}**";
|
||||
} else {
|
||||
if (!ulong.TryParse(targetstr, out targetId)) {
|
||||
await SendUsageMessageAsync(msg.Channel, TargetNotFound);
|
||||
return;
|
||||
}
|
||||
targetDisplay = $"with ID {targetId}";
|
||||
targetDisplay = $"with ID **{targetId}**";
|
||||
}
|
||||
|
||||
// Do the action
|
||||
|
|
|
@ -24,7 +24,7 @@ internal partial class ModLogs {
|
|||
var issuedDisplay = Utilities.TryFromEntityNameString(entry.IssuedBy, Bot);
|
||||
string targetDisplay;
|
||||
var targetq = Bot.EcQueryUser(entry.UserId.ToString());
|
||||
if (targetq != null) targetDisplay = $"<@{targetq.UserId}> - {targetq.Username}#{targetq.Discriminator} `{targetq.UserId}`";
|
||||
if (targetq != null) targetDisplay = $"<@{targetq.UserId}> - {targetq.GetDisplayableUsername()} `{targetq.UserId}`";
|
||||
else targetDisplay = $"User with ID `{entry.UserId}`";
|
||||
|
||||
var logEmbed = new EmbedBuilder()
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
using Discord;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using RegexBot.Common;
|
||||
using RegexBot.Data;
|
||||
using System.Text;
|
||||
|
||||
|
@ -50,7 +51,7 @@ internal partial class ModLogs {
|
|||
};
|
||||
} else {
|
||||
reportEmbed.Author = new EmbedAuthorBuilder() {
|
||||
Name = $"{cachedMsg.Author.Username}#{cachedMsg.Author.Discriminator}",
|
||||
Name = $"{cachedMsg.Author.GetDisplayableUsername()}",
|
||||
IconUrl = cachedMsg.Author.AvatarUrl ?? GetDefaultAvatarUrl(cachedMsg.Author.Discriminator)
|
||||
};
|
||||
}
|
||||
|
@ -85,7 +86,7 @@ internal partial class ModLogs {
|
|||
.WithFooter($"Message ID: {newMsg.Id}");
|
||||
|
||||
reportEmbed.Author = new EmbedAuthorBuilder() {
|
||||
Name = $"{newMsg.Author.Username}#{newMsg.Author.Discriminator}",
|
||||
Name = $"{newMsg.Author.GetDisplayableUsername()}",
|
||||
IconUrl = newMsg.Author.GetAvatarUrl() ?? newMsg.Author.GetDefaultAvatarUrl()
|
||||
};
|
||||
|
||||
|
@ -130,7 +131,7 @@ internal partial class ModLogs {
|
|||
string userDisplay;
|
||||
if (userId.HasValue) {
|
||||
var q = Bot.EcQueryUser(userId.Value.ToString());
|
||||
if (q != null) userDisplay = $"<@{q.UserId}> - {q.Username}#{q.Discriminator} `{q.UserId}`";
|
||||
if (q != null) userDisplay = $"<@{q.UserId}> - {q.GetDisplayableUsername()} `{q.UserId}`";
|
||||
else userDisplay = $"Unknown user with ID `{userId}`";
|
||||
} else {
|
||||
userDisplay = "Unknown";
|
||||
|
|
|
@ -113,8 +113,8 @@ class ResponseExecutor {
|
|||
}
|
||||
)
|
||||
.WithAuthor(
|
||||
name: $"{_msg.Author.Username}#{_msg.Author.Discriminator} said:",
|
||||
iconUrl: _msg.Author.GetAvatarUrl(),
|
||||
name: $"{_user.GetDisplayableUsername()} said:",
|
||||
iconUrl: _user.GetAvatarUrl(),
|
||||
url: _msg.GetJumpUrl()
|
||||
)
|
||||
.WithDescription(invokingLine)
|
||||
|
@ -127,7 +127,7 @@ class ResponseExecutor {
|
|||
try {
|
||||
await reportTarget.SendMessageAsync(embed: resultEmbed);
|
||||
} catch (Discord.Net.HttpException ex) when (ex.HttpCode == System.Net.HttpStatusCode.Forbidden) {
|
||||
Log("Encountered 403 error when attempting to send report.");
|
||||
Log("Encountered error 403 when attempting to send report.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -218,7 +218,7 @@ class ResponseExecutor {
|
|||
if (targetCh == null) return FromError($"Unable to find channel '{name.Name}'.");
|
||||
isUser = false;
|
||||
} else if (name.Type == EntityType.User) {
|
||||
if (name.Name == "_") targetCh = await _msg.Author.CreateDMChannelAsync();
|
||||
if (name.Name == "_") targetCh = await _user.CreateDMChannelAsync();
|
||||
else {
|
||||
var searchedUser = name.FindUserIn(_guild);
|
||||
if (searchedUser == null) return FromError($"Unable to find user '{name.Name}'.");
|
||||
|
|
|
@ -98,8 +98,7 @@ public class BanKickResult {
|
|||
if (_rptTargetId != 0) {
|
||||
var user = bot.EcQueryUser(_rptTargetId.ToString());
|
||||
if (user != null) {
|
||||
// TODO sanitize possible formatting characters in display name
|
||||
msg += $" user **{user.Username}#{user.Discriminator}**";
|
||||
msg += $" user **{user.GetDisplayableUsername()}**";
|
||||
} else {
|
||||
msg += $" user with ID **{_rptTargetId}**";
|
||||
}
|
||||
|
|
|
@ -68,7 +68,7 @@ partial class RegexbotClient {
|
|||
// Attempt warning message
|
||||
var userSearch = _svcEntityCache.QueryUserCache(targetUser.ToString());
|
||||
var userDisp = userSearch != null
|
||||
? $"**{userSearch.Username}#{userSearch.Discriminator}**"
|
||||
? $"**{userSearch.GetDisplayableUsername()}**"
|
||||
: $"user with ID **{targetUser}**";
|
||||
var targetGuildUser = guild.GetUser(targetUser);
|
||||
if (targetGuildUser == null) return (entry, new LogAppendResult(
|
||||
|
|
|
@ -15,10 +15,10 @@ public class TimeoutSetResult : IOperationResult {
|
|||
public Exception? Error { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool ErrorNotFound => (_target == null) || ((Error as HttpException)?.HttpCode == System.Net.HttpStatusCode.NotFound);
|
||||
public bool ErrorNotFound => (_target == null) || Error is HttpException { HttpCode: System.Net.HttpStatusCode.NotFound };
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool ErrorForbidden => (Error as HttpException)?.HttpCode == System.Net.HttpStatusCode.Forbidden;
|
||||
public bool ErrorForbidden => Error is HttpException { HttpCode: System.Net.HttpStatusCode.Forbidden };
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool NotificationSuccess { get; }
|
||||
|
@ -32,7 +32,7 @@ public class TimeoutSetResult : IOperationResult {
|
|||
/// <inheritdoc/>
|
||||
public string ToResultString() {
|
||||
if (Success) {
|
||||
var msg = $":white_check_mark: Timeout set for **{_target!.Username}#{_target.Discriminator}**.";
|
||||
var msg = $":white_check_mark: Timeout set for **{_target!.GetDisplayableUsername()}**.";
|
||||
if (!NotificationSuccess) msg += "\n(User was unable to receive notification message.)";
|
||||
return msg;
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue