feat: gateway service

This commit is contained in:
alyssa 2024-09-14 12:19:47 +09:00
parent 1118d8bdf8
commit e4ed354536
50 changed files with 1737 additions and 545 deletions

View file

@ -13,27 +13,13 @@ public static class CacheExtensions
return guild;
}
public static async Task<Channel> GetChannel(this IDiscordCache cache, ulong channelId)
public static async Task<Channel> GetChannel(this IDiscordCache cache, ulong guildId, ulong channelId)
{
if (!(await cache.TryGetChannel(channelId) is Channel channel))
if (!(await cache.TryGetChannel(guildId, channelId) is Channel channel))
throw new KeyNotFoundException($"Channel {channelId} not found in cache");
return channel;
}
public static async Task<User> GetUser(this IDiscordCache cache, ulong userId)
{
if (!(await cache.TryGetUser(userId) is User user))
throw new KeyNotFoundException($"User {userId} not found in cache");
return user;
}
public static async Task<Role> GetRole(this IDiscordCache cache, ulong roleId)
{
if (!(await cache.TryGetRole(roleId) is Role role))
throw new KeyNotFoundException($"Role {roleId} not found in cache");
return role;
}
public static async ValueTask<User?> GetOrFetchUser(this IDiscordCache cache, DiscordApiClient rest,
ulong userId)
{
@ -47,9 +33,9 @@ public static class CacheExtensions
}
public static async ValueTask<Channel?> GetOrFetchChannel(this IDiscordCache cache, DiscordApiClient rest,
ulong channelId)
ulong guildId, ulong channelId)
{
if (await cache.TryGetChannel(channelId) is { } cacheChannel)
if (await cache.TryGetChannel(guildId, channelId) is { } cacheChannel)
return cacheChannel;
var restChannel = await rest.GetChannel(channelId);
@ -58,13 +44,13 @@ public static class CacheExtensions
return restChannel;
}
public static async Task<Channel> GetRootChannel(this IDiscordCache cache, ulong channelOrThread)
public static async Task<Channel> GetRootChannel(this IDiscordCache cache, ulong guildId, ulong channelOrThread)
{
var channel = await cache.GetChannel(channelOrThread);
var channel = await cache.GetChannel(guildId, channelOrThread);
if (!channel.IsThread())
return channel;
var parent = await cache.GetChannel(channel.ParentId!.Value);
var parent = await cache.GetChannel(guildId, channel.ParentId!.Value);
return parent;
}
}

View file

@ -32,23 +32,23 @@ public static class PermissionExtensions
PermissionSet.EmbedLinks;
public static Task<PermissionSet> PermissionsForMCE(this IDiscordCache cache, MessageCreateEvent message) =>
PermissionsFor2(cache, message.ChannelId, message.Author.Id, message.Member, message.WebhookId != null);
PermissionsFor2(cache, message.GuildId ?? 0, message.ChannelId, message.Author.Id, message.Member, message.WebhookId != null);
public static Task<PermissionSet>
PermissionsForMemberInChannel(this IDiscordCache cache, ulong channelId, GuildMember member) =>
PermissionsFor2(cache, channelId, member.User.Id, member);
PermissionsForMemberInChannel(this IDiscordCache cache, ulong guildId, ulong channelId, GuildMember member) =>
PermissionsFor2(cache, guildId, channelId, member.User.Id, member);
public static async Task<PermissionSet> PermissionsFor2(this IDiscordCache cache, ulong channelId, ulong userId,
public static async Task<PermissionSet> PermissionsFor2(this IDiscordCache cache, ulong guildId, ulong channelId, ulong userId,
GuildMemberPartial? member, bool isThread = false)
{
if (!(await cache.TryGetChannel(channelId) is Channel channel))
if (!(await cache.TryGetChannel(guildId, channelId) is Channel channel))
// todo: handle channel not found better
return PermissionSet.Dm;
if (channel.GuildId == null)
return PermissionSet.Dm;
var rootChannel = await cache.GetRootChannel(channelId);
var rootChannel = await cache.GetRootChannel(guildId, channelId);
var guild = await cache.GetGuild(channel.GuildId.Value);