2023-01-18 17:39:24 +03:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
|
using System.Text.Json;
|
2023-01-24 10:36:53 +03:00
|
|
|
|
using Discord;
|
2023-01-18 17:39:24 +03:00
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
|
|
|
|
|
namespace Boyfriend.Data;
|
|
|
|
|
|
|
|
|
|
public record GuildData {
|
|
|
|
|
public static readonly Dictionary<string, string> DefaultPreferences = new() {
|
|
|
|
|
{ "Prefix", "!" },
|
|
|
|
|
{ "Lang", "en" },
|
|
|
|
|
{ "ReceiveStartupMessages", "false" },
|
|
|
|
|
{ "WelcomeMessage", "default" },
|
|
|
|
|
{ "SendWelcomeMessages", "true" },
|
|
|
|
|
{ "PublicFeedbackChannel", "0" },
|
|
|
|
|
{ "PrivateFeedbackChannel", "0" },
|
|
|
|
|
{ "StarterRole", "0" },
|
|
|
|
|
{ "MuteRole", "0" },
|
|
|
|
|
{ "RemoveRolesOnMute", "false" },
|
|
|
|
|
{ "ReturnRolesOnRejoin", "false" },
|
|
|
|
|
{ "EventStartedReceivers", "interested,role" },
|
|
|
|
|
{ "EventNotificationRole", "0" },
|
|
|
|
|
{ "EventNotificationChannel", "0" },
|
|
|
|
|
{ "EventEarlyNotificationOffset", "0" },
|
|
|
|
|
{ "AutoStartEvents", "false" }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public static readonly ConcurrentDictionary<ulong, GuildData> GuildDataDictionary = new();
|
|
|
|
|
|
|
|
|
|
private static readonly JsonSerializerOptions Options = new() {
|
|
|
|
|
IncludeFields = true,
|
|
|
|
|
WriteIndented = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private readonly string _configurationFile;
|
|
|
|
|
|
|
|
|
|
private readonly ulong _id;
|
|
|
|
|
|
|
|
|
|
public readonly List<ulong> EarlyNotifications = new();
|
|
|
|
|
|
|
|
|
|
public readonly Dictionary<ulong, MemberData> MemberData;
|
|
|
|
|
|
|
|
|
|
public readonly Dictionary<string, string> Preferences;
|
|
|
|
|
|
|
|
|
|
private SocketRole? _cachedMuteRole;
|
|
|
|
|
|
|
|
|
|
[SuppressMessage("Performance", "CA1853:Unnecessary call to \'Dictionary.ContainsKey(key)\'")]
|
|
|
|
|
// https://github.com/dotnet/roslyn-analyzers/issues/6377
|
|
|
|
|
private GuildData(SocketGuild guild) {
|
2023-02-02 21:05:28 +03:00
|
|
|
|
var downloaderTask = guild.DownloadUsersAsync();
|
2023-01-18 17:39:24 +03:00
|
|
|
|
_id = guild.Id;
|
|
|
|
|
var idString = $"{_id}";
|
|
|
|
|
var memberDataDir = $"{_id}/MemberData";
|
|
|
|
|
_configurationFile = $"{_id}/Configuration.json";
|
|
|
|
|
if (!Directory.Exists(idString)) Directory.CreateDirectory(idString);
|
|
|
|
|
if (!Directory.Exists(memberDataDir)) Directory.CreateDirectory(memberDataDir);
|
|
|
|
|
if (!File.Exists(_configurationFile)) File.WriteAllText(_configurationFile, "{}");
|
|
|
|
|
Preferences
|
2023-01-23 18:29:44 +03:00
|
|
|
|
= JsonSerializer.Deserialize<Dictionary<string, string>>(File.ReadAllText(_configurationFile))
|
|
|
|
|
?? new Dictionary<string, string>();
|
2023-01-18 17:39:24 +03:00
|
|
|
|
|
|
|
|
|
if (Preferences.Keys.Count < DefaultPreferences.Keys.Count)
|
|
|
|
|
foreach (var key in DefaultPreferences.Keys.Where(key => !Preferences.ContainsKey(key)))
|
|
|
|
|
Preferences.Add(key, DefaultPreferences[key]);
|
|
|
|
|
if (Preferences.Keys.Count > DefaultPreferences.Keys.Count)
|
|
|
|
|
foreach (var key in Preferences.Keys.Where(key => !DefaultPreferences.ContainsKey(key)))
|
|
|
|
|
Preferences.Remove(key);
|
|
|
|
|
Preferences.TrimExcess();
|
|
|
|
|
|
|
|
|
|
MemberData = new Dictionary<ulong, MemberData>();
|
|
|
|
|
foreach (var data in Directory.GetFiles(memberDataDir)) {
|
|
|
|
|
var deserialised
|
|
|
|
|
= JsonSerializer.Deserialize<MemberData>(File.ReadAllText(data), Options);
|
|
|
|
|
MemberData.Add(deserialised!.Id, deserialised);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-02 21:05:28 +03:00
|
|
|
|
downloaderTask.Wait();
|
|
|
|
|
foreach (var member in guild.Users) {
|
2023-01-18 17:39:24 +03:00
|
|
|
|
if (MemberData.TryGetValue(member.Id, out var memberData)) {
|
2023-01-23 18:29:44 +03:00
|
|
|
|
if (!memberData.IsInGuild
|
|
|
|
|
&& DateTimeOffset.Now.ToUnixTimeSeconds()
|
|
|
|
|
- Math.Max(
|
|
|
|
|
memberData.LeftAt.Last().ToUnixTimeSeconds(),
|
|
|
|
|
memberData.BannedUntil?.ToUnixTimeSeconds() ?? 0)
|
|
|
|
|
> 60 * 60 * 24 * 30) {
|
2023-01-18 17:39:24 +03:00
|
|
|
|
File.Delete($"{_id}/MemberData/{memberData.Id}.json");
|
|
|
|
|
MemberData.Remove(memberData.Id);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-24 10:36:53 +03:00
|
|
|
|
if (memberData.MutedUntil is null) {
|
|
|
|
|
memberData.Roles = ((IGuildUser)member).RoleIds.ToList();
|
|
|
|
|
memberData.Roles.Remove(guild.Id);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-18 17:39:24 +03:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MemberData.Add(member.Id, new MemberData(member));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MemberData.TrimExcess();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SocketRole? MuteRole {
|
|
|
|
|
get {
|
|
|
|
|
if (Preferences["MuteRole"] is "0") return null;
|
|
|
|
|
return _cachedMuteRole ??= Boyfriend.Client.GetGuild(_id).Roles
|
2023-01-23 19:06:04 +03:00
|
|
|
|
.Single(x => x.Id == ulong.Parse(Preferences["MuteRole"]));
|
2023-01-18 17:39:24 +03:00
|
|
|
|
}
|
|
|
|
|
set => _cachedMuteRole = value;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-23 19:06:04 +03:00
|
|
|
|
public SocketTextChannel? PublicFeedbackChannel
|
|
|
|
|
=> Boyfriend.Client.GetGuild(_id)
|
|
|
|
|
.GetTextChannel(
|
|
|
|
|
ulong.Parse(Preferences["PublicFeedbackChannel"]));
|
2023-01-18 17:39:24 +03:00
|
|
|
|
|
2023-01-23 18:29:44 +03:00
|
|
|
|
public SocketTextChannel? PrivateFeedbackChannel => Boyfriend.Client.GetGuild(_id)
|
2023-01-23 19:06:04 +03:00
|
|
|
|
.GetTextChannel(
|
|
|
|
|
ulong.Parse(
|
|
|
|
|
Preferences["PrivateFeedbackChannel"]));
|
2023-01-18 17:39:24 +03:00
|
|
|
|
|
|
|
|
|
public static GuildData Get(SocketGuild guild) {
|
|
|
|
|
if (GuildDataDictionary.TryGetValue(guild.Id, out var stored)) return stored;
|
|
|
|
|
var newData = new GuildData(guild);
|
|
|
|
|
while (!GuildDataDictionary.ContainsKey(guild.Id)) GuildDataDictionary.TryAdd(guild.Id, newData);
|
|
|
|
|
return newData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Save(bool saveMemberData) {
|
|
|
|
|
Preferences.TrimExcess();
|
2023-01-23 18:29:44 +03:00
|
|
|
|
await File.WriteAllTextAsync(
|
|
|
|
|
_configurationFile,
|
2023-01-18 17:39:24 +03:00
|
|
|
|
JsonSerializer.Serialize(Preferences));
|
|
|
|
|
if (saveMemberData)
|
|
|
|
|
foreach (var data in MemberData.Values)
|
2023-01-23 18:29:44 +03:00
|
|
|
|
await File.WriteAllTextAsync(
|
|
|
|
|
$"{_id}/MemberData/{data.Id}.json",
|
2023-01-18 17:39:24 +03:00
|
|
|
|
JsonSerializer.Serialize(data, Options));
|
|
|
|
|
}
|
|
|
|
|
}
|