mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-05-03 04:29:54 +03:00
General code refactor (and a few breaking config changes)
This commit is contained in:
parent
0e144db2e2
commit
552c575dd2
15 changed files with 1164 additions and 879 deletions
|
@ -12,10 +12,9 @@ using Humanizer.Localisation;
|
|||
namespace Boyfriend;
|
||||
|
||||
public static class Utils {
|
||||
public static readonly Random Random = new();
|
||||
private static readonly Dictionary<string, string> ReflectionMessageCache = new();
|
||||
|
||||
private static readonly Dictionary<string, CultureInfo> CultureInfoCache = new() {
|
||||
public static readonly Dictionary<string, CultureInfo> CultureInfoCache = new() {
|
||||
{ "ru", new CultureInfo("ru-RU") },
|
||||
{ "en", new CultureInfo("en-US") },
|
||||
{ "mctaylors-ru", new CultureInfo("tt-RU") }
|
||||
|
@ -28,16 +27,16 @@ public static class Utils {
|
|||
};
|
||||
|
||||
public static string GetBeep(int i = -1) {
|
||||
return GetMessage($"Beep{(i < 0 ? Random.Next(3) + 1 : ++i)}");
|
||||
return GetMessage($"Beep{(i < 0 ? Random.Shared.Next(3) + 1 : ++i)}");
|
||||
}
|
||||
|
||||
public static SocketTextChannel? GetAdminLogChannel(ulong id) {
|
||||
public static SocketTextChannel? GetBotLogChannel(ulong id) {
|
||||
return Boyfriend.Client.GetGuild(id)
|
||||
.GetTextChannel(ParseMention(Boyfriend.GetGuildConfig(id)["AdminLogChannel"]));
|
||||
.GetTextChannel(ParseMention(Boyfriend.GetGuildConfig(id)["BotLogChannel"]));
|
||||
}
|
||||
|
||||
public static string? Wrap(string? original, bool limitedSpace = false) {
|
||||
if (original == null) return null;
|
||||
if (original is null) return null;
|
||||
var maxChars = limitedSpace ? 970 : 1940;
|
||||
if (original.Length > maxChars) original = original[..maxChars];
|
||||
var style = original.Contains('\n') ? "```" : "`";
|
||||
|
@ -52,29 +51,22 @@ public static class Utils {
|
|||
return ulong.TryParse(Regex.Replace(mention, "[^0-9]", ""), out var id) ? id : 0;
|
||||
}
|
||||
|
||||
public static SocketUser? ParseUser(string mention) {
|
||||
var user = Boyfriend.Client.GetUser(ParseMention(mention));
|
||||
return user;
|
||||
}
|
||||
|
||||
public static async Task SendDirectMessage(SocketUser user, string toSend) {
|
||||
try { await user.SendMessageAsync(toSend); } catch (HttpException e) {
|
||||
if (e.DiscordCode != DiscordErrorCode.CannotSendMessageToUser) throw;
|
||||
if (e.DiscordCode is not DiscordErrorCode.CannotSendMessageToUser) throw;
|
||||
}
|
||||
}
|
||||
|
||||
public static SocketRole? GetMuteRole(SocketGuild guild) {
|
||||
var id = ulong.Parse(Boyfriend.GetGuildConfig(guild.Id)["MuteRole"]);
|
||||
if (MuteRoleCache.TryGetValue(id, out var cachedMuteRole)) return cachedMuteRole;
|
||||
SocketRole? role = null;
|
||||
foreach (var x in guild.Roles) {
|
||||
if (x.Id != id) continue;
|
||||
role = x;
|
||||
MuteRoleCache.Add(id, role);
|
||||
break;
|
||||
MuteRoleCache.Add(id, x);
|
||||
return x;
|
||||
}
|
||||
|
||||
return role;
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void RemoveMuteRoleFromCache(ulong id) {
|
||||
|
@ -82,7 +74,7 @@ public static class Utils {
|
|||
}
|
||||
|
||||
public static async Task SilentSendAsync(SocketTextChannel? channel, string text, bool allowRoles = false) {
|
||||
if (channel == null || text.Length is 0 or > 2000)
|
||||
if (channel is null || text.Length is 0 or > 2000)
|
||||
throw new Exception($"Message length is out of range: {text.Length}");
|
||||
|
||||
await channel.SendMessageAsync(text, false, null, null, allowRoles ? AllowRoles : AllowedMentions.None);
|
||||
|
@ -102,8 +94,8 @@ public static class Utils {
|
|||
var toReturn =
|
||||
typeof(Messages).GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Static)?.GetValue(null)
|
||||
?.ToString();
|
||||
if (toReturn == null) {
|
||||
Console.WriteLine($@"Could not find localized property: {propertyName}");
|
||||
if (toReturn is null) {
|
||||
Console.Error.WriteLine($@"Could not find localized property: {propertyName}");
|
||||
return name;
|
||||
}
|
||||
|
||||
|
@ -113,11 +105,11 @@ public static class Utils {
|
|||
|
||||
public static async Task
|
||||
SendFeedbackAsync(string feedback, ulong guildId, string mention, bool sendPublic = false) {
|
||||
var adminChannel = GetAdminLogChannel(guildId);
|
||||
var adminChannel = GetBotLogChannel(guildId);
|
||||
var systemChannel = Boyfriend.Client.GetGuild(guildId).SystemChannel;
|
||||
var toSend = string.Format(Messages.FeedbackFormat, mention, feedback);
|
||||
if (adminChannel != null) await SilentSendAsync(adminChannel, toSend);
|
||||
if (sendPublic && systemChannel != null) await SilentSendAsync(systemChannel, toSend);
|
||||
var toSend = $"*[{mention}: {feedback}]*";
|
||||
if (adminChannel is not null) await SilentSendAsync(adminChannel, toSend);
|
||||
if (sendPublic && systemChannel is not null) await SilentSendAsync(systemChannel, toSend);
|
||||
}
|
||||
|
||||
public static string GetHumanizedTimeOffset(TimeSpan span) {
|
||||
|
@ -131,7 +123,7 @@ public static class Utils {
|
|||
}
|
||||
|
||||
public static void SafeAppendToBuilder(StringBuilder appendTo, string appendWhat, SocketTextChannel? channel) {
|
||||
if (channel == null) return;
|
||||
if (channel is null) return;
|
||||
if (appendTo.Length + appendWhat.Length > 2000) {
|
||||
_ = SilentSendAsync(channel, appendTo.ToString());
|
||||
appendTo.Clear();
|
||||
|
@ -169,15 +161,19 @@ public static class Utils {
|
|||
var eventConfig = Boyfriend.GetGuildConfig(guild.Id);
|
||||
|
||||
var receivers = eventConfig["EventStartedReceivers"];
|
||||
var role = guild.GetRole(Convert.ToUInt64(eventConfig["EventNotifyReceiverRole"]));
|
||||
var role = guild.GetRole(ulong.Parse(eventConfig["EventNotificationRole"]));
|
||||
var mentions = Boyfriend.StringBuilder;
|
||||
|
||||
if (receivers.Contains("role") && role != null) mentions.Append($"{role.Mention} ");
|
||||
if (receivers.Contains("role") && role is not null) mentions.Append($"{role.Mention} ");
|
||||
if (receivers.Contains("users") || receivers.Contains("interested"))
|
||||
mentions = (await scheduledEvent.GetUsersAsync(15)).Aggregate(mentions,
|
||||
(current, user) => current.Append($"{user.Mention} "));
|
||||
await channel?.SendMessageAsync(string.Format(Messages.EventEarlyNotification, mentions,
|
||||
Wrap(scheduledEvent.Name), scheduledEvent.StartTime.ToUnixTimeSeconds()))!;
|
||||
Wrap(scheduledEvent.Name), scheduledEvent.StartTime.ToUnixTimeSeconds().ToString()))!;
|
||||
mentions.Clear();
|
||||
}
|
||||
|
||||
public static SocketTextChannel? GetEventNotificationChannel(SocketGuild guild) {
|
||||
return guild.GetTextChannel(ParseMention(Boyfriend.GetGuildConfig(guild.Id)["EventCreatedChannel"]));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue