chore: format with csharpier

This commit is contained in:
sam 2024-10-09 17:35:11 +02:00
parent 2f516dcb73
commit 4f54077c68
59 changed files with 2000 additions and 942 deletions

View file

@ -6,10 +6,21 @@ namespace Catalogger.Backend.Cache.InMemoryCache;
public class AuditLogCache
{
private readonly ConcurrentDictionary<(Snowflake GuildId, Snowflake TargetId), ActionData> _kicks = new();
private readonly ConcurrentDictionary<(Snowflake GuildId, Snowflake TargetId), ActionData> _bans = new();
private readonly ConcurrentDictionary<
(Snowflake GuildId, Snowflake TargetId),
ActionData
> _kicks = new();
private readonly ConcurrentDictionary<
(Snowflake GuildId, Snowflake TargetId),
ActionData
> _bans = new();
public void SetKick(Snowflake guildId, string targetId, Snowflake moderatorId, Optional<string> reason)
public void SetKick(
Snowflake guildId,
string targetId,
Snowflake moderatorId,
Optional<string> reason
)
{
if (!DiscordSnowflake.TryParse(targetId, out var targetUser))
throw new CataloggerError("Target ID was not a valid snowflake");
@ -20,16 +31,21 @@ public class AuditLogCache
public bool TryGetKick(Snowflake guildId, Snowflake targetId, out ActionData data) =>
_kicks.TryGetValue((guildId, targetId), out data);
public void SetBan(Snowflake guildId, string targetId, Snowflake moderatorId, Optional<string> reason)
public void SetBan(
Snowflake guildId,
string targetId,
Snowflake moderatorId,
Optional<string> reason
)
{
if (!DiscordSnowflake.TryParse(targetId, out var targetUser))
throw new CataloggerError("Target ID was not a valid snowflake");
_bans[(guildId, targetUser.Value)] = new ActionData(moderatorId, reason.OrDefault());
}
public bool TryGetBan(Snowflake guildId, Snowflake targetId, out ActionData data) =>
_bans.TryGetValue((guildId, targetId), out data);
public record struct ActionData(Snowflake ModeratorId, string? Reason);
}
}

View file

@ -17,18 +17,21 @@ public class ChannelCache
_channels[channel.ID] = channel;
if (guildId == null)
{
if (!channel.GuildID.TryGet(out var snowflake)) return;
if (!channel.GuildID.TryGet(out var snowflake))
return;
guildId = snowflake;
}
// Add to set of guild channels
_guildChannels.AddOrUpdate(guildId.Value,
_guildChannels.AddOrUpdate(
guildId.Value,
_ => [channel.ID],
(_, l) =>
{
l.Add(channel.ID);
return l;
});
}
);
}
public bool TryGet(Snowflake id, [NotNullWhen(true)] out IChannel? channel) =>
@ -37,13 +40,18 @@ public class ChannelCache
public void Remove(Snowflake? guildId, Snowflake id, out IChannel? channel)
{
_channels.Remove(id, out channel);
if (guildId == null) return;
if (guildId == null)
return;
// Remove from set of guild channels
_guildChannels.AddOrUpdate(guildId.Value, _ => [], (_, s) =>
{
s.Remove(id);
return s;
});
_guildChannels.AddOrUpdate(
guildId.Value,
_ => [],
(_, s) =>
{
s.Remove(id);
return s;
}
);
}
/// <summary>
@ -54,6 +62,8 @@ public class ChannelCache
public IEnumerable<IChannel> GuildChannels(Snowflake guildId) =>
!_guildChannels.TryGetValue(guildId, out var channelIds)
? []
: channelIds.Select(id => _channels.GetValueOrDefault(id))
.Where(c => c != null).Select(c => c!);
}
: channelIds
.Select(id => _channels.GetValueOrDefault(id))
.Where(c => c != null)
.Select(c => c!);
}

View file

@ -12,6 +12,10 @@ public class GuildCache
public int Size => _guilds.Count;
public void Set(IGuild guild) => _guilds[guild.ID] = guild;
public bool Remove(Snowflake id, [NotNullWhen(true)] out IGuild? guild) => _guilds.Remove(id, out guild);
public bool TryGet(Snowflake id, [NotNullWhen(true)] out IGuild? guild) => _guilds.TryGetValue(id, out guild);
}
public bool Remove(Snowflake id, [NotNullWhen(true)] out IGuild? guild) =>
_guilds.Remove(id, out guild);
public bool TryGet(Snowflake id, [NotNullWhen(true)] out IGuild? guild) =>
_guilds.TryGetValue(id, out guild);
}

View file

@ -8,13 +8,14 @@ public class InMemoryInviteCache : IInviteCache
{
private readonly ConcurrentDictionary<Snowflake, IEnumerable<IInvite>> _invites = new();
public Task<IEnumerable<IInvite>> TryGetAsync(Snowflake guildId) => _invites.TryGetValue(guildId, out var invites)
? Task.FromResult(invites)
: Task.FromResult<IEnumerable<IInvite>>([]);
public Task<IEnumerable<IInvite>> TryGetAsync(Snowflake guildId) =>
_invites.TryGetValue(guildId, out var invites)
? Task.FromResult(invites)
: Task.FromResult<IEnumerable<IInvite>>([]);
public Task SetAsync(Snowflake guildId, IEnumerable<IInvite> invites)
{
_invites[guildId] = invites;
return Task.CompletedTask;
}
}
}

View file

@ -19,7 +19,9 @@ public class InMemoryMemberCache : IMemberCache
public Task SetAsync(Snowflake guildId, IGuildMember member)
{
if (!member.User.IsDefined())
throw new CataloggerError("Member with undefined User passed to InMemoryMemberCache.SetAsync");
throw new CataloggerError(
"Member with undefined User passed to InMemoryMemberCache.SetAsync"
);
_members[(guildId, member.User.Value.ID)] = member;
return Task.CompletedTask;
}
@ -36,7 +38,8 @@ public class InMemoryMemberCache : IMemberCache
return Task.CompletedTask;
}
public Task<bool> IsGuildCachedAsync(Snowflake guildId) => Task.FromResult(_guilds.ContainsKey(guildId));
public Task<bool> IsGuildCachedAsync(Snowflake guildId) =>
Task.FromResult(_guilds.ContainsKey(guildId));
public Task MarkAsCachedAsync(Snowflake guildId)
{
@ -49,4 +52,4 @@ public class InMemoryMemberCache : IMemberCache
_guilds.Remove(guildId, out _);
return Task.CompletedTask;
}
}
}

View file

@ -18,4 +18,4 @@ public class InMemoryWebhookCache : IWebhookCache
_cache[channelId] = webhook;
return Task.CompletedTask;
}
}
}

View file

@ -16,13 +16,15 @@ public class RoleCache
{
_roles[role.ID] = role;
// Add to set of guild channels
_guildRoles.AddOrUpdate(guildId,
_guildRoles.AddOrUpdate(
guildId,
_ => [role.ID],
(_, l) =>
{
l.Add(role.ID);
return l;
});
}
);
}
public bool TryGet(Snowflake id, [NotNullWhen(true)] out IRole? role) =>
@ -32,16 +34,21 @@ public class RoleCache
{
_roles.Remove(id, out role);
// Remove from set of guild channels
_guildRoles.AddOrUpdate(guildId, _ => [], (_, s) =>
{
s.Remove(id);
return s;
});
_guildRoles.AddOrUpdate(
guildId,
_ => [],
(_, s) =>
{
s.Remove(id);
return s;
}
);
}
public void RemoveGuild(Snowflake guildId)
{
if (!_guildRoles.TryGetValue(guildId, out var roleIds)) return;
if (!_guildRoles.TryGetValue(guildId, out var roleIds))
return;
foreach (var id in roleIds)
{
_roles.Remove(id, out _);
@ -58,6 +65,8 @@ public class RoleCache
public IEnumerable<IRole> GuildRoles(Snowflake guildId) =>
!_guildRoles.TryGetValue(guildId, out var roleIds)
? []
: roleIds.Select(id => _roles.GetValueOrDefault(id))
.Where(r => r != null).Select(r => r!);
}
: roleIds
.Select(id => _roles.GetValueOrDefault(id))
.Where(r => r != null)
.Select(r => r!);
}

View file

@ -13,13 +13,16 @@ public class UserCache(IDiscordRestUserAPI userApi)
public int Size => _cacheSize;
public async Task<IUser?> GetUserAsync(Snowflake userId) => await _cache.GetOrAddAsync(userId.ToString(),
async () =>
{
var user = await userApi.GetUserAsync(userId).GetOrThrow();
Interlocked.Increment(ref _cacheSize);
return user;
});
public async Task<IUser?> GetUserAsync(Snowflake userId) =>
await _cache.GetOrAddAsync(
userId.ToString(),
async () =>
{
var user = await userApi.GetUserAsync(userId).GetOrThrow();
Interlocked.Increment(ref _cacheSize);
return user;
}
);
public void UpdateUser(IUser user) => _cache.Add(user.ID.ToString(), user);
}
}