1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 09:09:00 +03:00

Do not cache public & private feedback channels for no reason

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2023-01-23 20:29:44 +05:00
parent 0bdf2cd33e
commit c1e3abce57
Signed by: Octol1ttle
GPG key ID: B77C34313AEE1FFF
2 changed files with 30 additions and 47 deletions

View file

@ -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);
@ -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":
if (selectedSetting is "MuteRole")
data.MuteRole = guild.GetRole(mention);
break;
case "PublicFeedbackChannel":
data.PublicFeedbackChannel = guild.GetTextChannel(mention);
break;
case "PrivateFeedbackChannel":
data.PrivateFeedbackChannel = guild.GetTextChannel(mention);
break;
}
config[selectedSetting] = value;
}

View file

@ -43,8 +43,6 @@ public record GuildData {
public readonly Dictionary<string, string> 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<Dictionary<string, string>>(File.ReadAllText(_configurationFile)) ??
new Dictionary<string, string>();
= JsonSerializer.Deserialize<Dictionary<string, string>>(File.ReadAllText(_configurationFile))
?? new Dictionary<string, string>();
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);
}
@ -105,23 +104,14 @@ public record GuildData {
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));
}
}