1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-05-06 22:16:29 +03:00

Begin adapting code to new guild data storage

This commit is contained in:
Octol1ttle 2022-12-30 00:47:02 +05:00
parent f5a81fba35
commit 163e3ac46b
Signed by: Octol1ttle
GPG key ID: B77C34313AEE1FFF
8 changed files with 56 additions and 167 deletions

View file

@ -1,11 +1,10 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using Discord.WebSocket;
using Newtonsoft.Json;
namespace Boyfriend.Data;
public struct GuildData {
public record GuildData {
public static readonly Dictionary<string, string> DefaultConfiguration = new() {
{ "Prefix", "!" },
{ "Lang", "en" },
@ -26,81 +25,46 @@ public struct GuildData {
private static readonly Dictionary<ulong, GuildData> GuildDataDictionary = new();
public readonly Dictionary<string, string> GuildConfiguration;
public readonly Dictionary<ulong, MemberData> MemberData;
/*public static Dictionary<string, string> GetGuildConfig(ulong id) {
if (GuildConfigDictionary.TryGetValue(id, out var cfg)) return cfg;
public readonly Dictionary<string, string> Preferences;
var path = $"config_{id}.json";
private SocketRole? _cachedMuteRole;
if (!File.Exists(path)) File.Create(path).Dispose();
private ulong _id;
var json = File.ReadAllText(path);
var config = JsonConvert.DeserializeObject<Dictionary<string, string>>(json)
?? new Dictionary<string, string>();
if (config.Keys.Count < GuildData.DefaultConfiguration.Keys.Count) {
// ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator
// Conversion will result in a lot of memory allocations
foreach (var key in GuildData.DefaultConfiguration.Keys)
if (!config.ContainsKey(key))
config.Add(key, GuildData.DefaultConfiguration[key]);
} else if (config.Keys.Count > GuildData.DefaultConfiguration.Keys.Count) {
foreach (var key in config.Keys.Where(key => !GuildData.DefaultConfiguration.ContainsKey(key))) config.Remove(key);
}
GuildConfigDictionary.Add(id, config);
return config;
}*/
/*public static async Task WriteGuildConfigAsync(ulong id) {
await File.WriteAllTextAsync($"config_{id}.json",
JsonConvert.SerializeObject(GuildConfigDictionary[id], Formatting.Indented));
if (RemovedRolesDictionary.TryGetValue(id, out var removedRoles))
await File.WriteAllTextAsync($"removedroles_{id}.json",
JsonConvert.SerializeObject(removedRoles, Formatting.Indented));
}*/
[SuppressMessage("Performance", "CA1853:Unnecessary call to \'Dictionary.ContainsKey(key)\'")]
// https://github.com/dotnet/roslyn-analyzers/issues/6377
public GuildData(SocketGuild guild) {
private GuildData(SocketGuild guild) {
var id = guild.Id;
if (GuildDataDictionary.TryGetValue(id, out var stored)) {
this = stored;
return;
}
if (!Directory.Exists($"{id}")) Directory.CreateDirectory($"{id}");
if (!Directory.Exists($"{id}/MemberData")) Directory.CreateDirectory($"{id}/MemberData");
if (!File.Exists($"{id}/Configuration.json")) File.Create($"{id}/Configuration.json").Dispose();
GuildConfiguration = JsonConvert.DeserializeObject<Dictionary<string, string>>($"{id}/Configuration.json") ??
new Dictionary<string, string>();
Preferences
= JsonSerializer.Deserialize<Dictionary<string, string>>(File.ReadAllText($"{id}/Configuration.json")) ??
new Dictionary<string, string>();
// ReSharper disable twice ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator
if (GuildConfiguration.Keys.Count < DefaultConfiguration.Keys.Count)
if (Preferences.Keys.Count < DefaultConfiguration.Keys.Count)
foreach (var key in DefaultConfiguration.Keys)
if (!GuildConfiguration.ContainsKey(key))
GuildConfiguration.Add(key, DefaultConfiguration[key]);
if (GuildConfiguration.Keys.Count > DefaultConfiguration.Keys.Count)
foreach (var key in GuildConfiguration.Keys)
if (!Preferences.ContainsKey(key))
Preferences.Add(key, DefaultConfiguration[key]);
if (Preferences.Keys.Count > DefaultConfiguration.Keys.Count)
foreach (var key in Preferences.Keys)
if (!DefaultConfiguration.ContainsKey(key))
GuildConfiguration.Remove(key);
GuildConfiguration.TrimExcess();
Preferences.Remove(key);
Preferences.TrimExcess();
MemberData = new Dictionary<ulong, MemberData>();
foreach (var data in Directory.GetFiles($"{id}/MemberData")) {
var deserialised = JsonConvert.DeserializeObject<MemberData>($"{id}/MemberData/{data}.json") ??
throw new UnreachableException();
MemberData.Add(deserialised.Id, deserialised);
var deserialised = JsonSerializer.Deserialize<MemberData>(File.ReadAllText($"{id}/MemberData/{data}.json"));
MemberData.Add(deserialised!.Id, deserialised);
}
if (guild.MemberCount > MemberData.Count)
foreach (var member in guild.Users) {
if (MemberData.TryGetValue(member.Id, out var memberData)) {
if (memberData is { IsInGuild: false } &&
if (!memberData.IsInGuild &&
DateTimeOffset.Now.ToUnixTimeSeconds() -
Math.Max(memberData.LeftAt.Last(), memberData.BannedUntil) >
60 * 60 * 24 * 30) {
@ -114,9 +78,24 @@ public struct GuildData {
var data = new MemberData(member);
MemberData.Add(member.Id, data);
File.WriteAllText($"{id}/MemberData/{data.Id}.json",
JsonConvert.SerializeObject(data, Formatting.Indented));
JsonSerializer.Serialize(data));
}
GuildDataDictionary.Add(id, this);
}
public SocketRole? MuteRole {
get => _cachedMuteRole ??= Boyfriend.Client.GetGuild(_id).Roles
.Single(x => x.Id == ulong.Parse(Preferences["MuteRole"]));
set => _cachedMuteRole = value;
}
public static GuildData FromSocketGuild(SocketGuild guild) {
if (GuildDataDictionary.TryGetValue(guild.Id, out var stored)) return stored;
var newData = new GuildData(guild) {
_id = guild.Id
};
GuildDataDictionary.Add(guild.Id, newData);
return newData;
}
}

View file

@ -1,6 +1,6 @@
namespace Boyfriend.Data;
public struct Reminder {
public DateTimeOffset RemindAt;
public long RemindAt;
public string ReminderText;
}