61 lines
2.6 KiB
C#
61 lines
2.6 KiB
C#
using Maki.Rest;
|
|
using Maki.Structures.Guilds;
|
|
using Maki.Structures.Rest;
|
|
using Maki.Structures.Roles;
|
|
using System;
|
|
using System.Linq;
|
|
|
|
namespace Maki
|
|
{
|
|
public class DiscordServer
|
|
{
|
|
public readonly ulong Id;
|
|
private readonly Discord client;
|
|
|
|
public string Name { get; internal set; }
|
|
public ulong OwnerId { get; internal set; }
|
|
internal string IconHash;
|
|
|
|
public DiscordMember[] Members => client.members.Where(x => x.Server == this).ToArray();
|
|
public DiscordMember Owner => Members.Where(x => x.User.Id == OwnerId).FirstOrDefault();
|
|
public DiscordMember Me => Members.Where(x => x.User == client.Me).FirstOrDefault();
|
|
public DiscordChannel[] TextChannels => client.channels.Where(x => x.Server == this && x.Type == DiscordChannelType.Text).OrderByDescending(x => x.Position).ToArray();
|
|
public DiscordChannel[] VoiceChannels => client.channels.Where(x => x.Server == this && x.Type == DiscordChannelType.Voice).OrderByDescending(x => x.Position).ToArray();
|
|
public DiscordRole[] Roles => client.roles.Where(x => x.Server == this).OrderByDescending(x => x.Position).ToArray();
|
|
public string Icon(string ext = @"png", int size = 128) => RestEndpoints.CDN_URL + $@"/icons/{Id}/{IconHash}.{ext}?size={size}";
|
|
|
|
internal DiscordServer(Discord discord, Guild guild)
|
|
{
|
|
client = discord;
|
|
Id = guild.Id;
|
|
Name = guild.Name;
|
|
OwnerId = guild.OwnerId ?? ulong.MinValue;
|
|
IconHash = guild.IconHash;
|
|
}
|
|
|
|
public DiscordRole CreateRole(string name = null, DiscordPermission perms = DiscordPermission.None, DiscordColour colour = null, bool hoist = false, bool mentionable = false)
|
|
{
|
|
RestResponse<Role> roleResp = client.RestClient.Request<Role>(RestRequestMethod.POST, RestEndpoints.GuildRoles(Id), new RoleCreate
|
|
{
|
|
Name = name,
|
|
Perms = perms,
|
|
Colour = colour == null ? 0 : colour.Raw,
|
|
Hoist = hoist,
|
|
Mentionable = mentionable
|
|
});
|
|
|
|
if (roleResp.ErrorCode != RestErrorCode.Ok)
|
|
throw new DiscordException($"{roleResp.ErrorCode}: {roleResp.ErrorMessage}");
|
|
|
|
DiscordRole role = client.roles.Where(x => x.Id == roleResp.Response.Id).FirstOrDefault();
|
|
|
|
if (role == default(DiscordRole))
|
|
{
|
|
role = new DiscordRole(client, roleResp.Response, this);
|
|
client.roles.Add(role);
|
|
}
|
|
|
|
return role;
|
|
}
|
|
}
|
|
}
|