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

81 lines
2.9 KiB
C#
Raw Normal View History

2017-05-14 12:02:51 +00:00
using Maki.Rest;
using Maki.Structures.Guilds;
using Maki.Structures.Rest;
using Maki.Structures.Roles;
using System;
2017-10-13 20:05:50 +00:00
using System.Collections.Generic;
2017-05-14 12:02:51 +00:00
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; }
2017-05-31 21:02:38 +00:00
public DateTime Created { get; internal set; }
2017-05-21 01:50:37 +00:00
internal string IconHash;
2017-05-14 12:02:51 +00:00
2017-10-13 20:05:50 +00:00
public IEnumerable<DiscordMember> Members => client.MemberManager.Server(this);
2017-05-18 23:04:45 +00:00
public DiscordMember Owner => Members.Where(x => x.User.Id == OwnerId).FirstOrDefault();
public DiscordMember Me => Members.Where(x => x.User == client.Me).FirstOrDefault();
2017-10-13 20:05:50 +00:00
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}";
2017-05-14 12:02:51 +00:00
internal DiscordServer(Discord discord, Guild guild)
{
client = discord;
Id = guild.Id;
Name = guild.Name;
2017-05-31 21:02:38 +00:00
Created = guild.Created ?? DateTime.MinValue;
2017-05-14 12:02:51 +00:00
OwnerId = guild.OwnerId ?? ulong.MinValue;
2017-05-21 01:50:37 +00:00
IconHash = guild.IconHash;
2017-05-14 12:02:51 +00:00
}
public DiscordRole CreateRole(string name = null, DiscordPermission perms = DiscordPermission.None, DiscordColour colour = null, bool hoist = false, bool mentionable = false)
{
2017-05-31 21:02:38 +00:00
Role? roleStruct;
using (WebRequest wr = new WebRequest(HttpMethod.POST, RestEndpoints.GuildRoles(Id)))
2017-05-14 12:02:51 +00:00
{
2017-05-31 21:02:38 +00:00
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.Response.Length < 1)
throw new DiscordException("Failed to create role");
2017-05-14 12:02:51 +00:00
2017-05-31 21:02:38 +00:00
roleStruct = wr.ResponseJson<Role>();
}
if (!roleStruct.HasValue)
throw new DiscordException("Empty response?");
2017-05-14 12:02:51 +00:00
2017-10-13 20:05:50 +00:00
DiscordRole role = client.RoleManager.Id(roleStruct.Value.Id);
//Debug.Assert(role != null, "role is null");
2017-05-17 22:33:09 +00:00
2017-10-13 20:05:50 +00:00
if (role == null)
2017-05-17 22:33:09 +00:00
{
2017-05-31 21:02:38 +00:00
role = new DiscordRole(client, roleStruct.Value, this);
2017-10-13 20:05:50 +00:00
client.RoleManager.Add(role);
2017-05-17 22:33:09 +00:00
}
2017-10-13 20:05:50 +00:00
2017-05-14 12:02:51 +00:00
return role;
}
}
}