Preliminary support for APIv9 and threads

This commit is contained in:
Ske
2021-07-15 12:41:19 +02:00
parent 0e7bcb993e
commit 1f2b9f998d
19 changed files with 127 additions and 24 deletions

View File

@@ -75,5 +75,15 @@ namespace Myriad.Extensions
await cache.SaveChannel(restChannel);
return restChannel;
}
public static Channel GetRootChannel(this IDiscordCache cache, ulong channelOrThread)
{
var channel = cache.GetChannel(channelOrThread);
if (!channel.IsThread())
return channel;
var parent = cache.GetChannel(channel.ParentId!.Value);
return parent;
}
}
}

View File

@@ -5,5 +5,12 @@ namespace Myriad.Extensions
public static class ChannelExtensions
{
public static string Mention(this Channel channel) => $"<#{channel.Id}>";
public static bool IsThread(this Channel channel) => channel.Type.IsThread();
public static bool IsThread(this Channel.ChannelType type) =>
type is Channel.ChannelType.GuildPublicThread
or Channel.ChannelType.GuildPrivateThread
or Channel.ChannelType.GuildNewsThread;
}
}

View File

@@ -11,7 +11,7 @@ namespace Myriad.Extensions
public static class PermissionExtensions
{
public static PermissionSet PermissionsFor(this IDiscordCache cache, MessageCreateEvent message) =>
PermissionsFor(cache, message.ChannelId, message.Author.Id, message.Member?.Roles, isWebhook: message.Author.Discriminator == "0000");
PermissionsFor(cache, message.ChannelId, message.Author.Id, message.Member?.Roles, isWebhook: message.WebhookId != null);
public static PermissionSet PermissionsFor(this IDiscordCache cache, ulong channelId, GuildMember member) =>
PermissionsFor(cache, channelId, member.User.Id, member.Roles);
@@ -21,16 +21,21 @@ namespace Myriad.Extensions
public static PermissionSet PermissionsFor(this IDiscordCache cache, ulong channelId, ulong userId, ICollection<ulong>? userRoles, bool isWebhook = false)
{
var channel = cache.GetChannel(channelId);
if (!cache.TryGetChannel(channelId, out var channel))
// todo: handle channel not found better
return PermissionSet.Dm;
if (channel.GuildId == null)
return PermissionSet.Dm;
var rootChannel = cache.GetRootChannel(channelId);
var guild = cache.GetGuild(channel.GuildId.Value);
if (isWebhook)
return EveryonePermissions(guild);
return PermissionsFor(guild, channel, userId, userRoles);
return PermissionsFor(guild, rootChannel, userId, userRoles);
}
public static PermissionSet EveryonePermissions(this Guild guild) =>