From c1e3abce57280ca0c5ad406ceea1f9443a436e53 Mon Sep 17 00:00:00 2001 From: Octol1ttle Date: Mon, 23 Jan 2023 20:29:44 +0500 Subject: [PATCH] Do not cache public & private feedback channels for no reason Signed-off-by: Octol1ttle --- Commands/SettingsCommand.cs | 29 ++++++++-------------- Data/GuildData.cs | 48 ++++++++++++++++--------------------- 2 files changed, 30 insertions(+), 47 deletions(-) diff --git a/Commands/SettingsCommand.cs b/Commands/SettingsCommand.cs index 7ed012c..e056cdc 100644 --- a/Commands/SettingsCommand.cs +++ b/Commands/SettingsCommand.cs @@ -34,7 +34,7 @@ public sealed class SettingsCommand : ICommand { } currentSettings.Append($"{Utils.GetMessage($"Settings{setting.Key}")} (`{setting.Key}`): ") - .AppendFormat(format, currentValue).AppendLine(); + .AppendFormat(format, currentValue).AppendLine(); } cmd.Reply(currentSettings.ToString(), ReplyEmojis.SettingsList); @@ -63,8 +63,9 @@ public sealed class SettingsCommand : ICommand { if (value is null) return Task.CompletedTask; if (selectedSetting is "EventStartedReceivers") { value = value.Replace(" ", "").ToLower(); - if (value.StartsWith(",") || value.Count(x => x is ',') > 1 || - (!value.Contains("interested") && !value.Contains("users") && !value.Contains("role"))) { + if (value.StartsWith(",") + || value.Count(x => x is ',') > 1 + || (!value.Contains("interested") && !value.Contains("users") && !value.Contains("role"))) { cmd.Reply(Messages.InvalidSettingValue, ReplyEmojis.Error); return Task.CompletedTask; } @@ -73,9 +74,7 @@ public sealed class SettingsCommand : ICommand { if (IsBool(GuildData.DefaultPreferences[selectedSetting]) && !IsBool(value)) { value = value switch { - "y" or "yes" or "д" or "да" => "true", - "n" or "no" or "н" or "нет" => "false", - _ => value + "y" or "yes" or "д" or "да" => "true", "n" or "no" or "н" or "нет" => "false", _ => value }; if (!IsBool(value)) { cmd.Reply(Messages.InvalidSettingValue, ReplyEmojis.Error); @@ -95,7 +94,7 @@ public sealed class SettingsCommand : ICommand { } var formattedValue = selectedSetting switch { - "WelcomeMessage" => Utils.Wrap(Messages.DefaultWelcomeMessage), + "WelcomeMessage" => Utils.Wrap(Messages.DefaultWelcomeMessage), "EventStartedReceivers" => Utils.Wrap(GuildData.DefaultPreferences[selectedSetting])!, _ => value is "reset" or "default" ? Messages.SettingNotDefined : IsBool(value) ? YesOrNo(value is "true") @@ -106,7 +105,8 @@ public sealed class SettingsCommand : ICommand { config[selectedSetting] = GuildData.DefaultPreferences[selectedSetting]; } else { if (value == config[selectedSetting]) { - cmd.Reply(string.Format(Messages.SettingsNothingChanged, localizedSelectedSetting, formattedValue), + cmd.Reply( + string.Format(Messages.SettingsNothingChanged, localizedSelectedSetting, formattedValue), ReplyEmojis.Error); return Task.CompletedTask; } @@ -135,17 +135,8 @@ public sealed class SettingsCommand : ICommand { return Task.CompletedTask; } - switch (selectedSetting) { - case "MuteRole": - data.MuteRole = guild.GetRole(mention); - break; - case "PublicFeedbackChannel": - data.PublicFeedbackChannel = guild.GetTextChannel(mention); - break; - case "PrivateFeedbackChannel": - data.PrivateFeedbackChannel = guild.GetTextChannel(mention); - break; - } + if (selectedSetting is "MuteRole") + data.MuteRole = guild.GetRole(mention); config[selectedSetting] = value; } diff --git a/Data/GuildData.cs b/Data/GuildData.cs index 9d00525..53d7114 100644 --- a/Data/GuildData.cs +++ b/Data/GuildData.cs @@ -43,8 +43,6 @@ public record GuildData { public readonly Dictionary Preferences; private SocketRole? _cachedMuteRole; - private SocketTextChannel? _cachedPrivateFeedbackChannel; - private SocketTextChannel? _cachedPublicFeedbackChannel; [SuppressMessage("Performance", "CA1853:Unnecessary call to \'Dictionary.ContainsKey(key)\'")] // https://github.com/dotnet/roslyn-analyzers/issues/6377 @@ -57,8 +55,8 @@ public record GuildData { if (!Directory.Exists(memberDataDir)) Directory.CreateDirectory(memberDataDir); if (!File.Exists(_configurationFile)) File.WriteAllText(_configurationFile, "{}"); Preferences - = JsonSerializer.Deserialize>(File.ReadAllText(_configurationFile)) ?? - new Dictionary(); + = JsonSerializer.Deserialize>(File.ReadAllText(_configurationFile)) + ?? new Dictionary(); if (Preferences.Keys.Count < DefaultPreferences.Keys.Count) foreach (var key in DefaultPreferences.Keys.Where(key => !Preferences.ContainsKey(key))) @@ -78,11 +76,12 @@ public record GuildData { guild.DownloadUsersAsync().Wait(); foreach (var member in guild.Users.Where(user => !user.IsBot)) { if (MemberData.TryGetValue(member.Id, out var memberData)) { - if (!memberData.IsInGuild && - DateTimeOffset.Now.ToUnixTimeSeconds() - - Math.Max(memberData.LeftAt.Last().ToUnixTimeSeconds(), - memberData.BannedUntil?.ToUnixTimeSeconds() ?? 0) > - 60 * 60 * 24 * 30) { + if (!memberData.IsInGuild + && DateTimeOffset.Now.ToUnixTimeSeconds() + - Math.Max( + memberData.LeftAt.Last().ToUnixTimeSeconds(), + memberData.BannedUntil?.ToUnixTimeSeconds() ?? 0) + > 60 * 60 * 24 * 30) { File.Delete($"{_id}/MemberData/{memberData.Id}.json"); MemberData.Remove(memberData.Id); } @@ -100,28 +99,19 @@ public record GuildData { get { if (Preferences["MuteRole"] is "0") return null; return _cachedMuteRole ??= Boyfriend.Client.GetGuild(_id).Roles - .Single(x => x.Id == ulong.Parse(Preferences["MuteRole"])); + .Single(x => x.Id == ulong.Parse(Preferences["MuteRole"])); } set => _cachedMuteRole = value; } - public SocketTextChannel? PublicFeedbackChannel { - get { - if (Preferences["PublicFeedbackChannel"] is "0") return null; - return _cachedPublicFeedbackChannel ??= Boyfriend.Client.GetGuild(_id).TextChannels - .Single(x => x.Id == ulong.Parse(Preferences["PublicFeedbackChannel"])); - } - set => _cachedPublicFeedbackChannel = value; - } + public SocketTextChannel? PublicFeedbackChannel => Boyfriend.Client.GetGuild(_id) + .GetTextChannel( + ulong.Parse(Preferences["PublicFeedbackChannel"])); - public SocketTextChannel? PrivateFeedbackChannel { - get { - if (Preferences["PublicFeedbackChannel"] is "0") return null; - return _cachedPrivateFeedbackChannel ??= Boyfriend.Client.GetGuild(_id).TextChannels - .Single(x => x.Id == ulong.Parse(Preferences["PrivateFeedbackChannel"])); - } - set => _cachedPrivateFeedbackChannel = value; - } + public SocketTextChannel? PrivateFeedbackChannel => Boyfriend.Client.GetGuild(_id) + .GetTextChannel( + ulong.Parse( + Preferences["PrivateFeedbackChannel"])); public static GuildData Get(SocketGuild guild) { if (GuildDataDictionary.TryGetValue(guild.Id, out var stored)) return stored; @@ -132,11 +122,13 @@ public record GuildData { public async Task Save(bool saveMemberData) { Preferences.TrimExcess(); - await File.WriteAllTextAsync(_configurationFile, + await File.WriteAllTextAsync( + _configurationFile, JsonSerializer.Serialize(Preferences)); if (saveMemberData) foreach (var data in MemberData.Values) - await File.WriteAllTextAsync($"{_id}/MemberData/{data.Id}.json", + await File.WriteAllTextAsync( + $"{_id}/MemberData/{data.Id}.json", JsonSerializer.Serialize(data, Options)); } }