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

81 lines
2.7 KiB
C#
Raw Normal View History

2017-05-14 12:02:51 +00:00
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; }
2017-05-21 01:08:34 +00:00
public int Position { get; internal set; }
2017-05-14 12:02:51 +00:00
2017-05-17 22:33:09 +00:00
public override string ToString() => $@"<@&{Id}>";
2017-05-14 12:02:51 +00:00
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;
2017-05-21 01:08:34 +00:00
Position = role.Position ?? 0;
2017-05-14 12:02:51 +00:00
Server = server;
2017-05-27 08:48:11 +00:00
Colour = new DiscordColour(role.Colour ?? int.MinValue);
2017-05-14 12:02:51 +00:00
}
public void Delete()
{
2017-05-31 21:02:38 +00:00
using (WebRequest wr = new WebRequest(HttpMethod.DELETE, RestEndpoints.GuildRole(Server.Id, Id)))
{
wr.Perform();
2017-05-14 12:02:51 +00:00
2017-05-31 21:02:38 +00:00
if (wr.Status != 204)
throw new DiscordException($"Failed to delete role {Id} in guild {Server.Id}");
}
2017-05-14 12:02:51 +00:00
}
public void Edit(string name = null, DiscordPermission? perms = null, DiscordColour colour = null, bool? hoist = null, bool? mentionable = null)
{
2017-05-31 21:02:38 +00:00
Role? role;
using (WebRequest wr = new WebRequest(HttpMethod.PATCH, RestEndpoints.GuildRole(Server.Id, Id)))
2017-05-14 12:02:51 +00:00
{
2017-05-31 21:02:38 +00:00
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)
2017-05-31 21:02:38 +00:00
// TODO: elaborate
throw new DiscordException("Failed to edit role");
role = wr.ResponseJson<Role>();
}
2017-05-14 12:02:51 +00:00
2017-05-31 21:02:38 +00:00
if (!role.HasValue)
throw new DiscordException("Empty response?");
2017-05-14 12:02:51 +00:00
2017-05-31 21:02:38 +00:00
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;
2017-05-14 12:02:51 +00:00
}
}
}