Archived
1
0
Fork 0
This repository has been archived on 2024-05-21. You can view files and clone it, but cannot push or open issues or pull requests.
maki/Maki/DiscordRole.cs

80 lines
2.7 KiB
C#

using Maki.Rest;
using Maki.Structures.Rest;
using Maki.Structures.Roles;
using System;
namespace Maki
{
public class DiscordRole
{
public readonly ulong Id;
public readonly DiscordServer Server;
public readonly DiscordColour Colour;
private readonly Discord client;
public string Name { get; internal set; }
public DiscordPermission Perms { get; internal set; }
public bool IsHoisted { get; internal set; }
public bool IsMentionable { get; internal set; }
public int Position { get; internal set; }
public override string ToString() => $@"<@&{Id}>";
internal DiscordRole(Discord discord, Role role, DiscordServer server)
{
client = discord;
Id = role.Id;
Name = role.Name;
Perms = (DiscordPermission)role.Permissions;
IsHoisted = role.IsHoisted == true;
IsMentionable = role.IsMentionable == true;
Position = role.Position ?? 0;
Server = server;
Colour = new DiscordColour(role.Colour ?? int.MinValue);
}
public void Delete()
{
using (WebRequest wr = new WebRequest(HttpMethod.DELETE, RestEndpoints.GuildRole(Server.Id, Id)))
{
wr.Perform();
if (wr.Status != 204)
throw new DiscordException($"Failed to delete role {Id} in guild {Server.Id}");
}
}
public void Edit(string name = null, DiscordPermission? perms = null, DiscordColour colour = null, bool? hoist = null, bool? mentionable = null)
{
Role? role;
using (WebRequest wr = new WebRequest(HttpMethod.PATCH, RestEndpoints.GuildRole(Server.Id, Id)))
{
wr.AddJson(new RoleCreate
{
Name = name ?? Name,
Perms = perms ?? Perms,
Colour = colour == null ? 0 : colour.Raw,
Hoist = hoist ?? IsHoisted,
Mentionable = mentionable ?? IsMentionable
});
wr.Perform();
if (wr.Status != 200 || wr.ResponseString.Length < 1)
// TODO: elaborate
throw new DiscordException("Failed to edit role");
role = wr.ResponseJson<Role>();
}
if (!role.HasValue)
throw new DiscordException("Empty response?");
Name = role.Value.Name;
Perms = (DiscordPermission)role.Value.Permissions;
Colour.Raw = role.Value.Colour ?? 0;
IsHoisted = role.Value.IsHoisted == true;
IsMentionable = role.Value.IsMentionable == true;
}
}
}