2021-11-22 21:40:08 +00:00
|
|
|
|
using System.Text;
|
2020-04-02 18:27:55 +00:00
|
|
|
|
|
2021-10-18 23:58:22 +00:00
|
|
|
|
namespace BirthdayBot;
|
|
|
|
|
static class Common {
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Formats a user's name to a consistent, readable format which makes use of their nickname.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static string FormatName(SocketGuildUser member, bool ping) {
|
|
|
|
|
if (ping) return member.Mention;
|
2020-04-02 18:27:55 +00:00
|
|
|
|
|
2021-10-18 23:58:22 +00:00
|
|
|
|
static string escapeFormattingCharacters(string input) {
|
|
|
|
|
var result = new StringBuilder();
|
|
|
|
|
foreach (var c in input) {
|
2023-09-11 05:13:51 +00:00
|
|
|
|
if (c is '\\' or '_' or '~' or '*' or '@' or '`') {
|
2021-10-18 23:58:22 +00:00
|
|
|
|
result.Append('\\');
|
|
|
|
|
}
|
|
|
|
|
result.Append(c);
|
2020-04-02 18:27:55 +00:00
|
|
|
|
}
|
2021-10-18 23:58:22 +00:00
|
|
|
|
return result.ToString();
|
2020-04-02 18:27:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-04 23:58:45 +00:00
|
|
|
|
if (member.DiscriminatorValue == 0) {
|
2023-09-11 05:13:51 +00:00
|
|
|
|
var username = escapeFormattingCharacters(member.GlobalName ?? member.Username);
|
2024-04-28 08:51:58 +00:00
|
|
|
|
if (member.Nickname != null) {
|
|
|
|
|
return $"{escapeFormattingCharacters(member.Nickname)} ({username})";
|
2023-09-04 23:58:45 +00:00
|
|
|
|
}
|
2023-09-11 05:13:51 +00:00
|
|
|
|
return username;
|
2023-09-04 23:58:45 +00:00
|
|
|
|
} else {
|
|
|
|
|
var username = escapeFormattingCharacters(member.Username);
|
|
|
|
|
if (member.Nickname != null) {
|
2023-09-11 05:13:51 +00:00
|
|
|
|
return $"{escapeFormattingCharacters(member.Nickname)} ({username}#{member.Discriminator})";
|
2023-09-04 23:58:45 +00:00
|
|
|
|
}
|
2023-09-11 05:13:51 +00:00
|
|
|
|
return $"{username}#{member.Discriminator}";
|
2021-10-18 23:58:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-02 18:27:55 +00:00
|
|
|
|
|
2021-10-18 23:58:22 +00:00
|
|
|
|
public static Dictionary<int, string> MonthNames { get; } = new() {
|
|
|
|
|
{ 1, "Jan" }, { 2, "Feb" }, { 3, "Mar" }, { 4, "Apr" }, { 5, "May" }, { 6, "Jun" },
|
|
|
|
|
{ 7, "Jul" }, { 8, "Aug" }, { 9, "Sep" }, { 10, "Oct" }, { 11, "Nov" }, { 12, "Dec" }
|
|
|
|
|
};
|
2021-12-06 01:20:05 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// An alternative to <see cref="SocketGuild.HasAllMembers"/>.
|
|
|
|
|
/// Returns true if *most* members have been downloaded.
|
|
|
|
|
/// Used as a workaround check due to Discord.Net occasionally unable to actually download all members.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static bool HasMostMembersDownloaded(SocketGuild guild) {
|
|
|
|
|
if (guild.HasAllMembers) return true;
|
|
|
|
|
if (guild.MemberCount > 30) {
|
|
|
|
|
// For guilds of size over 30, require 85% or more of the members to be known
|
|
|
|
|
// (26/30, 42/50, 255/300, etc)
|
2022-09-01 04:09:10 +00:00
|
|
|
|
var threshold = (int)(guild.MemberCount * 0.85);
|
2021-12-06 01:20:05 +00:00
|
|
|
|
return guild.DownloadedMemberCount >= threshold;
|
|
|
|
|
} else {
|
|
|
|
|
// For smaller guilds, fail if two or more members are missing
|
|
|
|
|
return guild.MemberCount - guild.DownloadedMemberCount <= 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-02 18:27:55 +00:00
|
|
|
|
}
|