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