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/DiscordServer.cs

80 lines
2.9 KiB
C#

using Maki.Rest;
using Maki.Structures.Guilds;
using Maki.Structures.Rest;
using Maki.Structures.Roles;
using System;
using System.Collections.Generic;
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; }
public DateTime Created { get; internal set; }
internal string IconHash;
public IEnumerable<DiscordMember> Members => client.MemberManager.Server(this);
public DiscordMember Owner => Members.Where(x => x.User.Id == OwnerId).FirstOrDefault();
public DiscordMember Me => Members.Where(x => x.User == client.Me).FirstOrDefault();
public IEnumerable<DiscordChannel> Channels => client.ChannelManager.Server(this);
public IEnumerable<DiscordChannel> TextChannels => Channels.Where(x => x.Type == DiscordChannelType.Text);
public IEnumerable<DiscordChannel> VoiceChannels => Channels.Where(x => x.Type == DiscordChannelType.Voice);
public IEnumerable<DiscordRole> Roles => client.RoleManager.Server(this);
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;
Created = guild.Created ?? DateTime.MinValue;
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)
{
Role? roleStruct;
using (WebRequest wr = new WebRequest(HttpMethod.POST, RestEndpoints.GuildRoles(Id)))
{
wr.AddJson(new RoleCreate
{
Name = name,
Perms = perms,
Colour = colour == null ? 0 : colour.Raw,
Hoist = hoist,
Mentionable = mentionable
});
wr.Perform();
if (wr.Status != 200 || wr.ResponseString.Length < 1)
throw new DiscordException("Failed to create role");
roleStruct = wr.ResponseJson<Role>();
}
if (!roleStruct.HasValue)
throw new DiscordException("Empty response?");
DiscordRole role = client.RoleManager.Id(roleStruct.Value.Id);
//Debug.Assert(role != null, "role is null");
if (role == null)
{
role = new DiscordRole(client, roleStruct.Value, this);
client.RoleManager.Add(role);
}
return role;
}
}
}