Merge pull request #7 from NoiTheCat/dev/new-usernames

New username system support
This commit is contained in:
Noi 2024-01-07 15:43:18 -08:00 committed by GitHub
commit e3bf767c25
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 332 additions and 24 deletions

View file

@ -1,5 +1,7 @@
using Discord; using Discord;
using RegexBot.Data;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@ -81,9 +83,41 @@ public static class Utilities {
try { try {
var entityTry = new EntityName(input!, EntityType.User); var entityTry = new EntityName(input!, EntityType.User);
var issueq = bot.EcQueryUser(entityTry.Id!.Value.ToString()); 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}`"; else result = $"Unknown user with ID `{entityTry.Id!.Value}`";
} catch (Exception) { } } catch (Exception) { }
return result ?? input; 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}";
}
}
} }

View file

@ -25,10 +25,15 @@ public class CachedUser {
public string Username { get; set; } = null!; public string Username { get; set; } = null!;
/// <summary> /// <summary>
/// Gets the user's discriminator value. /// Gets the user's discriminator value. A value of "0000" means the user is on the new username system.
/// </summary> /// </summary>
public string Discriminator { get; set; } = null!; public string Discriminator { get; set; } = null!;
/// <summary>
/// Gets the user's display name. A user <em>may</em> have a global name if they are on the new username system.
/// </summary>
public string? GlobalName { get; set; } = null!;
/// <summary> /// <summary>
/// Gets the avatar URL, if any, for the associated user. /// Gets the avatar URL, if any, for the associated user.
/// </summary> /// </summary>

View file

@ -0,0 +1,239 @@
// <auto-generated />
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using RegexBot.Data;
#nullable disable
namespace RegexBot.Data.Migrations
{
[DbContext(typeof(BotDatabaseContext))]
[Migration("20231115032040_NewUsernames")]
partial class NewUsernames
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.7")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "mod_log_type", new[] { "other", "note", "warn", "timeout", "kick", "ban" });
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("RegexBot.Data.CachedGuildMessage", b =>
{
b.Property<long>("MessageId")
.HasColumnType("bigint")
.HasColumnName("message_id");
b.Property<List<string>>("AttachmentNames")
.IsRequired()
.HasColumnType("text[]")
.HasColumnName("attachment_names");
b.Property<long>("AuthorId")
.HasColumnType("bigint")
.HasColumnName("author_id");
b.Property<long>("ChannelId")
.HasColumnType("bigint")
.HasColumnName("channel_id");
b.Property<string>("Content")
.HasColumnType("text")
.HasColumnName("content");
b.Property<DateTimeOffset>("CreatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasColumnName("created_at")
.HasDefaultValueSql("now()");
b.Property<DateTimeOffset?>("EditedAt")
.HasColumnType("timestamp with time zone")
.HasColumnName("edited_at");
b.Property<long>("GuildId")
.HasColumnType("bigint")
.HasColumnName("guild_id");
b.HasKey("MessageId")
.HasName("pk_cache_guildmessages");
b.HasIndex("AuthorId")
.HasDatabaseName("ix_cache_guildmessages_author_id");
b.ToTable("cache_guildmessages", (string)null);
});
modelBuilder.Entity("RegexBot.Data.CachedGuildUser", b =>
{
b.Property<long>("GuildId")
.HasColumnType("bigint")
.HasColumnName("guild_id");
b.Property<long>("UserId")
.HasColumnType("bigint")
.HasColumnName("user_id");
b.Property<DateTimeOffset>("FirstSeenTime")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasColumnName("first_seen_time")
.HasDefaultValueSql("now()");
b.Property<DateTimeOffset>("GULastUpdateTime")
.HasColumnType("timestamp with time zone")
.HasColumnName("gu_last_update_time");
b.Property<string>("Nickname")
.HasColumnType("text")
.HasColumnName("nickname");
b.HasKey("GuildId", "UserId")
.HasName("pk_cache_usersinguild");
b.HasIndex("UserId")
.HasDatabaseName("ix_cache_usersinguild_user_id");
b.ToTable("cache_usersinguild", (string)null);
});
modelBuilder.Entity("RegexBot.Data.CachedUser", b =>
{
b.Property<long>("UserId")
.HasColumnType("bigint")
.HasColumnName("user_id");
b.Property<string>("AvatarUrl")
.HasColumnType("text")
.HasColumnName("avatar_url");
b.Property<string>("Discriminator")
.IsRequired()
.HasMaxLength(4)
.HasColumnType("character(4)")
.HasColumnName("discriminator")
.IsFixedLength();
b.Property<string>("GlobalName")
.HasColumnType("text")
.HasColumnName("global_name");
b.Property<DateTimeOffset>("ULastUpdateTime")
.HasColumnType("timestamp with time zone")
.HasColumnName("u_last_update_time");
b.Property<string>("Username")
.IsRequired()
.HasColumnType("text")
.HasColumnName("username");
b.HasKey("UserId")
.HasName("pk_cache_users");
b.ToTable("cache_users", (string)null);
});
modelBuilder.Entity("RegexBot.Data.ModLogEntry", b =>
{
b.Property<int>("LogId")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasColumnName("log_id");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("LogId"));
b.Property<long>("GuildId")
.HasColumnType("bigint")
.HasColumnName("guild_id");
b.Property<string>("IssuedBy")
.IsRequired()
.HasColumnType("text")
.HasColumnName("issued_by");
b.Property<int>("LogType")
.HasColumnType("integer")
.HasColumnName("log_type");
b.Property<string>("Message")
.HasColumnType("text")
.HasColumnName("message");
b.Property<DateTimeOffset>("Timestamp")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasColumnName("timestamp")
.HasDefaultValueSql("now()");
b.Property<long>("UserId")
.HasColumnType("bigint")
.HasColumnName("user_id");
b.HasKey("LogId")
.HasName("pk_modlogs");
b.HasIndex("GuildId", "UserId")
.HasDatabaseName("ix_modlogs_guild_id_user_id");
b.ToTable("modlogs", (string)null);
});
modelBuilder.Entity("RegexBot.Data.CachedGuildMessage", b =>
{
b.HasOne("RegexBot.Data.CachedUser", "Author")
.WithMany("GuildMessages")
.HasForeignKey("AuthorId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired()
.HasConstraintName("fk_cache_guildmessages_cache_users_author_id");
b.Navigation("Author");
});
modelBuilder.Entity("RegexBot.Data.CachedGuildUser", b =>
{
b.HasOne("RegexBot.Data.CachedUser", "User")
.WithMany("Guilds")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired()
.HasConstraintName("fk_cache_usersinguild_cache_users_user_id");
b.Navigation("User");
});
modelBuilder.Entity("RegexBot.Data.ModLogEntry", b =>
{
b.HasOne("RegexBot.Data.CachedGuildUser", "User")
.WithMany("Logs")
.HasForeignKey("GuildId", "UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired()
.HasConstraintName("fk_modlogs_cache_usersinguild_user_temp_id");
b.Navigation("User");
});
modelBuilder.Entity("RegexBot.Data.CachedGuildUser", b =>
{
b.Navigation("Logs");
});
modelBuilder.Entity("RegexBot.Data.CachedUser", b =>
{
b.Navigation("GuildMessages");
b.Navigation("Guilds");
});
#pragma warning restore 612, 618
}
}
}

View file

@ -0,0 +1,25 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace RegexBot.Data.Migrations
{
public partial class NewUsernames : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "global_name",
table: "cache_users",
type: "text",
nullable: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "global_name",
table: "cache_users");
}
}
}

View file

@ -120,6 +120,10 @@ namespace RegexBot.Data.Migrations
.HasColumnName("discriminator") .HasColumnName("discriminator")
.IsFixedLength(); .IsFixedLength();
b.Property<string>("GlobalName")
.HasColumnType("text")
.HasColumnName("global_name");
b.Property<DateTimeOffset>("ULastUpdateTime") b.Property<DateTimeOffset>("ULastUpdateTime")
.HasColumnType("timestamp with time zone") .HasColumnType("timestamp with time zone")
.HasColumnName("u_last_update_time"); .HasColumnName("u_last_update_time");

View file

@ -1,7 +1,7 @@
using System.Text; using RegexBot.Common;
using System.Text;
namespace RegexBot.Modules.EntryRole; namespace RegexBot.Modules.EntryRole;
/// <summary> /// <summary>
/// Automatically sets a role onto users entering the guild after a predefined amount of time. /// Automatically sets a role onto users entering the guild after a predefined amount of time.
/// </summary> /// </summary>
@ -138,7 +138,7 @@ internal sealed class EntryRole : RegexbotModule, IDisposable {
var failList = new StringBuilder(); var failList = new StringBuilder();
var count = 0; var count = 0;
foreach (var item in failedUserList) { foreach (var item in failedUserList) {
failList.Append($", {item.Username}#{item.Discriminator}"); failList.Append($", {item.GetDisplayableUsername()}");
count++; count++;
if (count > 5) { if (count > 5) {
failList.Append($"and {count} other(s)."); failList.Append($"and {count} other(s).");

View file

@ -58,7 +58,7 @@ class ShowModLogs : CommandConfig {
var resultList = new EmbedBuilder() { var resultList = new EmbedBuilder() {
Author = new EmbedAuthorBuilder() { Author = new EmbedAuthorBuilder() {
Name = $"{query.User.Username}#{query.User.Discriminator}", Name = $"{query.User.GetDisplayableUsername()}",
IconUrl = query.User.AvatarUrl IconUrl = query.User.AvatarUrl
}, },
Footer = new EmbedFooterBuilder() { Footer = new EmbedFooterBuilder() {

View file

@ -29,13 +29,13 @@ class Unban : CommandConfig {
var query = Module.Bot.EcQueryUser(targetstr); var query = Module.Bot.EcQueryUser(targetstr);
if (query != null) { if (query != null) {
targetId = (ulong)query.UserId; targetId = (ulong)query.UserId;
targetDisplay = $"{query.Username}#{query.Discriminator}"; targetDisplay = $"**{query.GetDisplayableUsername()}**";
} else { } else {
if (!ulong.TryParse(targetstr, out targetId)) { if (!ulong.TryParse(targetstr, out targetId)) {
await SendUsageMessageAsync(msg.Channel, TargetNotFound); await SendUsageMessageAsync(msg.Channel, TargetNotFound);
return; return;
} }
targetDisplay = $"with ID {targetId}"; targetDisplay = $"with ID **{targetId}**";
} }
// Do the action // Do the action

View file

@ -24,7 +24,7 @@ internal partial class ModLogs {
var issuedDisplay = Utilities.TryFromEntityNameString(entry.IssuedBy, Bot); var issuedDisplay = Utilities.TryFromEntityNameString(entry.IssuedBy, Bot);
string targetDisplay; string targetDisplay;
var targetq = Bot.EcQueryUser(entry.UserId.ToString()); 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}`"; else targetDisplay = $"User with ID `{entry.UserId}`";
var logEmbed = new EmbedBuilder() var logEmbed = new EmbedBuilder()

View file

@ -1,5 +1,6 @@
using Discord; using Discord;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using RegexBot.Common;
using RegexBot.Data; using RegexBot.Data;
using System.Text; using System.Text;
@ -51,7 +52,7 @@ internal partial class ModLogs {
}; };
} else { } else {
reportEmbed.Author = new EmbedAuthorBuilder() { reportEmbed.Author = new EmbedAuthorBuilder() {
Name = $"{cachedMsg.Author.Username}#{cachedMsg.Author.Discriminator}", Name = $"{cachedMsg.Author.GetDisplayableUsername()}",
IconUrl = cachedMsg.Author.AvatarUrl ?? GetDefaultAvatarUrl(cachedMsg.Author.Discriminator) IconUrl = cachedMsg.Author.AvatarUrl ?? GetDefaultAvatarUrl(cachedMsg.Author.Discriminator)
}; };
} }
@ -87,7 +88,7 @@ internal partial class ModLogs {
.WithFooter($"Message ID: {newMsg.Id}"); .WithFooter($"Message ID: {newMsg.Id}");
reportEmbed.Author = new EmbedAuthorBuilder() { reportEmbed.Author = new EmbedAuthorBuilder() {
Name = $"{newMsg.Author.Username}#{newMsg.Author.Discriminator}", Name = $"{newMsg.Author.GetDisplayableUsername()}",
IconUrl = newMsg.Author.GetAvatarUrl() ?? newMsg.Author.GetDefaultAvatarUrl() IconUrl = newMsg.Author.GetAvatarUrl() ?? newMsg.Author.GetDefaultAvatarUrl()
}; };
@ -132,7 +133,7 @@ internal partial class ModLogs {
string userDisplay; string userDisplay;
if (userId.HasValue) { if (userId.HasValue) {
var q = Bot.EcQueryUser(userId.Value.ToString()); 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 user with ID `{userId}`";
} else { } else {
userDisplay = "Unknown"; userDisplay = "Unknown";

View file

@ -113,8 +113,8 @@ class ResponseExecutor {
} }
) )
.WithAuthor( .WithAuthor(
name: $"{_msg.Author.Username}#{_msg.Author.Discriminator} said:", name: $"{_user.GetDisplayableUsername()} said:",
iconUrl: _msg.Author.GetAvatarUrl(), iconUrl: _user.GetAvatarUrl(),
url: _msg.GetJumpUrl() url: _msg.GetJumpUrl()
) )
.WithDescription(invokingLine) .WithDescription(invokingLine)
@ -127,7 +127,7 @@ class ResponseExecutor {
try { try {
await reportTarget.SendMessageAsync(embed: resultEmbed); await reportTarget.SendMessageAsync(embed: resultEmbed);
} catch (Discord.Net.HttpException ex) when (ex.HttpCode == System.Net.HttpStatusCode.Forbidden) { } 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}'."); if (targetCh == null) return FromError($"Unable to find channel '{name.Name}'.");
isUser = false; isUser = false;
} else if (name.Type == EntityType.User) { } else if (name.Type == EntityType.User) {
if (name.Name == "_") targetCh = await _msg.Author.CreateDMChannelAsync(); if (name.Name == "_") targetCh = await _user.CreateDMChannelAsync();
else { else {
var searchedUser = name.FindUserIn(_guild); var searchedUser = name.FindUserIn(_guild);
if (searchedUser == null) return FromError($"Unable to find user '{name.Name}'."); if (searchedUser == null) return FromError($"Unable to find user '{name.Name}'.");

View file

@ -13,7 +13,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" /> <PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="Discord.Net" Version="3.8.1" /> <PackageReference Include="Discord.Net" Version="3.12.0" />
<PackageReference Include="EFCore.NamingConventions" Version="6.0.0" /> <PackageReference Include="EFCore.NamingConventions" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.7" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.7"> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.7">

View file

@ -98,8 +98,7 @@ public class BanKickResult {
if (_rptTargetId != 0) { if (_rptTargetId != 0) {
var user = bot.EcQueryUser(_rptTargetId.ToString()); var user = bot.EcQueryUser(_rptTargetId.ToString());
if (user != null) { if (user != null) {
// TODO sanitize possible formatting characters in display name msg += $" user **{user.GetDisplayableUsername()}**";
msg += $" user **{user.Username}#{user.Discriminator}**";
} else { } else {
msg += $" user with ID **{_rptTargetId}**"; msg += $" user with ID **{_rptTargetId}**";
} }

View file

@ -68,7 +68,7 @@ partial class RegexbotClient {
// Attempt warning message // Attempt warning message
var userSearch = _svcEntityCache.QueryUserCache(targetUser.ToString()); var userSearch = _svcEntityCache.QueryUserCache(targetUser.ToString());
var userDisp = userSearch != null var userDisp = userSearch != null
? $"**{userSearch.Username}#{userSearch.Discriminator}**" ? $"**{userSearch.GetDisplayableUsername()}**"
: $"user with ID **{targetUser}**"; : $"user with ID **{targetUser}**";
var targetGuildUser = guild.GetUser(targetUser); var targetGuildUser = guild.GetUser(targetUser);
if (targetGuildUser == null) return (entry, new LogAppendResult( if (targetGuildUser == null) return (entry, new LogAppendResult(

View file

@ -15,10 +15,10 @@ public class TimeoutSetResult : IOperationResult {
public Exception? Error { get; } public Exception? Error { get; }
/// <inheritdoc/> /// <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/> /// <inheritdoc/>
public bool ErrorForbidden => (Error as HttpException)?.HttpCode == System.Net.HttpStatusCode.Forbidden; public bool ErrorForbidden => Error is HttpException { HttpCode: System.Net.HttpStatusCode.Forbidden };
/// <inheritdoc/> /// <inheritdoc/>
public bool NotificationSuccess { get; } public bool NotificationSuccess { get; }
@ -32,7 +32,7 @@ public class TimeoutSetResult : IOperationResult {
/// <inheritdoc/> /// <inheritdoc/>
public string ToResultString() { public string ToResultString() {
if (Success) { 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.)"; if (!NotificationSuccess) msg += "\n(User was unable to receive notification message.)";
return msg; return msg;
} else { } else {

View file

@ -60,6 +60,7 @@ class UserCachingSubservice {
uinfo.Username = user.Username; uinfo.Username = user.Username;
uinfo.Discriminator = user.Discriminator; uinfo.Discriminator = user.Discriminator;
uinfo.GlobalName = user.GlobalName;
uinfo.AvatarUrl = user.GetAvatarUrl(size: 512); uinfo.AvatarUrl = user.GetAvatarUrl(size: 512);
uinfo.ULastUpdateTime = DateTimeOffset.UtcNow; uinfo.ULastUpdateTime = DateTimeOffset.UtcNow;
} }