Previously, any errors in guild data load will cause the bot to be unusable in that guild. It didn't help that the end users had no information that something was wrong! Now, any errors will be logged better (with the full path to the file that couldn't be loaded), and the users will receive a message saying that functionality is degraded The old way to save objects was to serialize them directly into streams opened by `File#Create`. This can cause problems if the serialization isn't completed, because `File#Create` overwrites the file with an empty one on the spot. Now, objects are first deserialized into a temporary file, then the original is replaced by the temporary, then the temporary is deleted. Errors during guild data load would sometimes cause the bot to replace the corrupted file with a default one whenever a save is triggered. Now, guilds with load errors won't have their data saved to aid in debugging --------- Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com> Signed-off-by: mctaylors <mctaylxrs@outlook.com>
47 lines
1.5 KiB
C#
47 lines
1.5 KiB
C#
using System.Text.Json.Nodes;
|
|
using Remora.Rest.Core;
|
|
|
|
namespace Octobot.Data;
|
|
|
|
/// <summary>
|
|
/// Stores information about a guild. This information is not accessible via the Discord API.
|
|
/// </summary>
|
|
/// <remarks>This information is stored on disk as a JSON file.</remarks>
|
|
public sealed class GuildData
|
|
{
|
|
public readonly Dictionary<ulong, MemberData> MemberData;
|
|
public readonly string MemberDataPath;
|
|
|
|
public readonly Dictionary<ulong, ScheduledEventData> ScheduledEvents;
|
|
public readonly string ScheduledEventsPath;
|
|
public readonly JsonNode Settings;
|
|
public readonly string SettingsPath;
|
|
|
|
public readonly bool DataLoadFailed;
|
|
|
|
public GuildData(
|
|
JsonNode settings, string settingsPath,
|
|
Dictionary<ulong, ScheduledEventData> scheduledEvents, string scheduledEventsPath,
|
|
Dictionary<ulong, MemberData> memberData, string memberDataPath, bool dataLoadFailed)
|
|
{
|
|
Settings = settings;
|
|
SettingsPath = settingsPath;
|
|
ScheduledEvents = scheduledEvents;
|
|
ScheduledEventsPath = scheduledEventsPath;
|
|
MemberData = memberData;
|
|
MemberDataPath = memberDataPath;
|
|
DataLoadFailed = dataLoadFailed;
|
|
}
|
|
|
|
public MemberData GetOrCreateMemberData(Snowflake memberId)
|
|
{
|
|
if (MemberData.TryGetValue(memberId.Value, out var existing))
|
|
{
|
|
return existing;
|
|
}
|
|
|
|
var newData = new MemberData(memberId.Value);
|
|
MemberData.Add(memberId.Value, newData);
|
|
return newData;
|
|
}
|
|
}
|