Converted enough to send the system card
This commit is contained in:
45
Myriad/Extensions/CacheExtensions.cs
Normal file
45
Myriad/Extensions/CacheExtensions.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
@@ -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}>";
|
||||
}
|
||||
}
|
7
Myriad/Extensions/GuildExtensions.cs
Normal file
7
Myriad/Extensions/GuildExtensions.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Myriad.Extensions
|
||||
{
|
||||
public static class GuildExtensions
|
||||
{
|
||||
|
||||
}
|
||||
}
|
@@ -1,7 +1,6 @@
|
||||
namespace Myriad.Extensions
|
||||
{
|
||||
public class MessageExtensions
|
||||
public static class MessageExtensions
|
||||
{
|
||||
|
||||
}
|
||||
}
|
@@ -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;
|
||||
|
@@ -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";
|
||||
}
|
||||
|
Reference in New Issue
Block a user