Converted enough to send the system card

This commit is contained in:
Ske
2020-12-22 16:55:13 +01:00
parent a6fbd869be
commit 05334f0d25
28 changed files with 343 additions and 198 deletions

View File

@@ -17,12 +17,12 @@ namespace Myriad.Cache
public ValueTask RemoveUser(ulong userId);
public ValueTask RemoveRole(ulong guildId, ulong roleId);
public ValueTask<Guild?> GetGuild(ulong guildId);
public ValueTask<Channel?> GetChannel(ulong channelId);
public ValueTask<User?> GetUser(ulong userId);
public ValueTask<Role?> GetRole(ulong roleId);
public bool TryGetGuild(ulong guildId, out Guild guild);
public bool TryGetChannel(ulong channelId, out Channel channel);
public bool TryGetUser(ulong userId, out User user);
public bool TryGetRole(ulong roleId, out Role role);
public IAsyncEnumerable<Guild> GetAllGuilds();
public ValueTask<IEnumerable<Channel>> GetGuildChannels(ulong guildId);
public IEnumerable<Channel> GetGuildChannels(ulong guildId);
}
}

View File

@@ -110,13 +110,26 @@ namespace Myriad.Cache
return default;
}
public ValueTask<Guild?> GetGuild(ulong guildId) => new(_guilds.GetValueOrDefault(guildId)?.Guild);
public bool TryGetGuild(ulong guildId, out Guild guild)
{
if (_guilds.TryGetValue(guildId, out var cg))
{
guild = cg.Guild;
return true;
}
public ValueTask<Channel?> GetChannel(ulong channelId) => new(_channels.GetValueOrDefault(channelId));
guild = null!;
return false;
}
public ValueTask<User?> GetUser(ulong userId) => new(_users.GetValueOrDefault(userId));
public bool TryGetChannel(ulong channelId, out Channel channel) =>
_channels.TryGetValue(channelId, out channel!);
public ValueTask<Role?> GetRole(ulong roleId) => new(_roles.GetValueOrDefault(roleId));
public bool TryGetUser(ulong userId, out User user) =>
_users.TryGetValue(userId, out user!);
public bool TryGetRole(ulong roleId, out Role role) =>
_roles.TryGetValue(roleId, out role!);
public async IAsyncEnumerable<Guild> GetAllGuilds()
{
@@ -124,12 +137,12 @@ namespace Myriad.Cache
yield return guild.Guild;
}
public ValueTask<IEnumerable<Channel>> GetGuildChannels(ulong guildId)
public IEnumerable<Channel> GetGuildChannels(ulong guildId)
{
if (!_guilds.TryGetValue(guildId, out var guild))
throw new ArgumentException("Guild not found", nameof(guildId));
return new ValueTask<IEnumerable<Channel>>(guild.Channels.Keys.Select(c => _channels[c]));
return guild.Channels.Keys.Select(c => _channels[c]);
}
private CachedGuild SaveGuildRaw(Guild guild) =>

View File

@@ -0,0 +1,45 @@
using System.Collections.Generic;
using Myriad.Cache;
using Myriad.Types;
namespace Myriad.Extensions
{
public static class CacheExtensions
{
public static Guild GetGuild(this IDiscordCache cache, ulong guildId)
{
if (!cache.TryGetGuild(guildId, out var guild))
throw new KeyNotFoundException($"Guild {guildId} not found in cache");
return guild;
}
public static Channel GetChannel(this IDiscordCache cache, ulong channelId)
{
if (!cache.TryGetChannel(channelId, out var channel))
throw new KeyNotFoundException($"Channel {channelId} not found in cache");
return channel;
}
public static Channel? GetChannelOrNull(this IDiscordCache cache, ulong channelId)
{
if (cache.TryGetChannel(channelId, out var channel))
return channel;
return null;
}
public static User GetUser(this IDiscordCache cache, ulong userId)
{
if (!cache.TryGetUser(userId, out var user))
throw new KeyNotFoundException($"User {userId} not found in cache");
return user;
}
public static Role GetRole(this IDiscordCache cache, ulong roleId)
{
if (!cache.TryGetRole(roleId, out var role))
throw new KeyNotFoundException($"User {roleId} not found in cache");
return role;
}
}
}

View File

@@ -1,7 +1,9 @@
namespace Myriad.Extensions
using Myriad.Types;
namespace Myriad.Extensions
{
public static class ChannelExtensions
{
public static string Mention(this Channel channel) => $"<#{channel.Id}>";
}
}

View File

@@ -0,0 +1,7 @@
namespace Myriad.Extensions
{
public static class GuildExtensions
{
}
}

View File

@@ -1,7 +1,6 @@
namespace Myriad.Extensions
{
public class MessageExtensions
public static class MessageExtensions
{
}
}

View File

@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Myriad.Cache;
using Myriad.Gateway;
using Myriad.Types;
@@ -9,17 +11,39 @@ 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);
public static PermissionSet PermissionsFor(this IDiscordCache cache, ulong channelId, GuildMember member) =>
PermissionsFor(cache, channelId, member.User.Id, member.Roles);
public static PermissionSet PermissionsFor(this IDiscordCache cache, ulong channelId, ulong userId, GuildMemberPartial member) =>
PermissionsFor(cache, channelId, userId, member.Roles);
public static PermissionSet PermissionsFor(this IDiscordCache cache, ulong channelId, ulong userId, ICollection<ulong>? userRoles)
{
var channel = cache.GetChannel(channelId);
if (channel.GuildId == null)
return PermissionSet.Dm;
var guild = cache.GetGuild(channel.GuildId.Value);
return PermissionsFor(guild, channel, userId, userRoles);
}
public static PermissionSet EveryonePermissions(this Guild guild) =>
guild.Roles.FirstOrDefault(r => r.Id == guild.Id)?.Permissions ?? PermissionSet.Dm;
public static PermissionSet PermissionsFor(Guild guild, Channel channel, MessageCreateEvent msg) =>
PermissionsFor(guild, channel, msg.Author.Id, msg.Member!.Roles);
PermissionsFor(guild, channel, msg.Author.Id, msg.Member?.Roles);
public static PermissionSet PermissionsFor(Guild guild, Channel channel, ulong userId,
ICollection<ulong> roleIds)
ICollection<ulong>? roleIds)
{
if (channel.Type == Channel.ChannelType.Dm)
return PermissionSet.Dm;
if (roleIds == null)
throw new ArgumentException($"User roles must be specified for guild channels");
var perms = GuildPermissions(guild, userId, roleIds);
perms = ApplyChannelOverwrites(perms, channel, userId, roleIds);
@@ -36,9 +60,6 @@ namespace Myriad.Extensions
return perms;
}
public static bool Has(this PermissionSet value, PermissionSet flag) =>
(value & flag) == flag;
public static PermissionSet GuildPermissions(this Guild guild, ulong userId, ICollection<ulong> roleIds)
{
if (guild.OwnerId == userId)
@@ -51,7 +72,7 @@ namespace Myriad.Extensions
perms |= role.Permissions;
}
if (perms.Has(PermissionSet.Administrator))
if (perms.HasFlag(PermissionSet.Administrator))
return PermissionSet.All;
return perms;

View File

@@ -4,6 +4,8 @@ namespace Myriad.Extensions
{
public static class UserExtensions
{
public static string Mention(this User user) => $"<@{user.Id}>";
public static string AvatarUrl(this User user) =>
$"https://cdn.discordapp.com/avatars/{user.Id}/{user.Avatar}.png";
}

View File

@@ -11,9 +11,9 @@ namespace Myriad.Rest.Types
Everyone
}
public List<ParseType>? Parse { get; set; }
public List<ulong>? Users { get; set; }
public List<ulong>? Roles { get; set; }
public ParseType[]? Parse { get; set; }
public ulong[]? Users { get; set; }
public ulong[]? Roles { get; set; }
public bool RepliedUser { get; set; }
}
}

View File

@@ -8,6 +8,6 @@ namespace Myriad.Rest.Types.Requests
public object? Nonce { get; set; }
public bool Tts { get; set; }
public AllowedMentions AllowedMentions { get; set; }
public Embed? Embeds { get; set; }
public Embed? Embed { get; set; }
}
}

View File

@@ -20,7 +20,7 @@
public string? Name { get; init; }
public string? Topic { get; init; }
public bool? Nsfw { get; init; }
public long? ParentId { get; init; }
public ulong? ParentId { get; init; }
public Overwrite[]? PermissionOverwrites { get; init; }
public record Overwrite