| @@ -13,33 +13,34 @@ | |||
| public const string Channels = "channels"; | |||
| public static string Channel(string channelId) => $"channels/{channelId}"; | |||
| public static string ChannelTyping(string channelId) => $"channels/{channelId}/typing"; | |||
| public static string ChannelInvites(string channelId) => $"channels/{channelId}/invites"; | |||
| public static string ChannelMessages(string channelId) => $"channels/{channelId}/messages"; | |||
| public static string ChannelMessages(string channelId, int limit) => $"channels/{channelId}/messages?limit={limit}"; | |||
| public static string ChannelMessages(string channelId, int limit, string beforeId) => $"channels/{channelId}/messages?limit={limit}&before={beforeId}"; | |||
| public static string ChannelMessage(string channelId, string msgId) => $"channels/{channelId}/messages/{msgId}"; | |||
| public static string ChannelMessageAck(string channelId, string msgId) => $"channels/{channelId}/messages/{msgId}/ack"; | |||
| public static string ChannelInvites(string channelId) => $"channels/{channelId}/invites"; | |||
| public static string ChannelPermission(string channelId, string userOrRoleId) => $"channels/{channelId}/permissions/{userOrRoleId}"; | |||
| public static string ChannelTyping(string channelId) => $"channels/{channelId}/typing"; | |||
| public const string Servers = "guilds"; | |||
| public static string Server(string serverId) => $"guilds/{serverId}"; | |||
| public static string ServerBan(string serverId, string userId) => $"guilds/{serverId}/bans/{userId}"; | |||
| public static string ServerChannels(string serverId) => $"guilds/{serverId}/channels"; | |||
| public static string ServerInvites(string serverId) => $"guilds/{serverId}/invites"; | |||
| public static string ServerMember(string serverId, string userId) => $"guilds/{serverId}/members/{userId}"; | |||
| public static string ServerBan(string serverId, string userId) => $"guilds/{serverId}/bans/{userId}"; | |||
| public static string ServerPrune(string serverId, int days) => $"guilds/{serverId}/prune?days={days}"; | |||
| public static string ServerRoles(string serverId) => $"guilds/{serverId}/roles"; | |||
| public static string ServerRole(string serverId, string roleId) => $"guilds/{serverId}/roles/{roleId}"; | |||
| public static string ServerPrune(string serverId, int days) => $"guilds/{serverId}/prune?days={days}"; | |||
| public const string Invites = "invite"; | |||
| public static string Invite(string inviteId) => $"invite/{inviteId}"; | |||
| public static string InviteUrl(string inviteId) => $"https://discord.gg/{inviteId}"; | |||
| public const string Users = "users"; | |||
| public static string UserMe => $"users/@me"; | |||
| public static string UserChannels(string userId) => $"users/{userId}/channels"; | |||
| public static string UserAvatar(string userId, string avatarId) => $"users/{userId}/avatars/{avatarId}.jpg"; | |||
| public static string UserChannels(string userId) => $"users/{userId}/channels"; | |||
| public static string UserMe => $"users/@me"; | |||
| public const string Voice = "voice"; | |||
| public const string VoiceRegions = "voice/regions"; | |||
| //public const string VoiceIce = "voice/ice"; | |||
| @@ -4,6 +4,7 @@ | |||
| using Newtonsoft.Json; | |||
| using System; | |||
| using System.Collections.Generic; | |||
| namespace Discord.API | |||
| { | |||
| @@ -53,6 +54,7 @@ namespace Discord.API | |||
| //Get | |||
| public class GetInviteResponse : InviteReference { } | |||
| public class GetInvitesResponse : List<InviteReference> { } | |||
| //Accept | |||
| public class AcceptInviteResponse : InviteReference { } | |||
| @@ -126,6 +126,12 @@ namespace Discord | |||
| return _rest.Get<GetInviteResponse>(Endpoints.Invite(inviteIdOrXkcd)); | |||
| } | |||
| public Task<GetInvitesResponse> GetInvites(string serverId) | |||
| { | |||
| if (serverId == null) throw new ArgumentNullException(nameof(serverId)); | |||
| return _rest.Get<GetInvitesResponse>(Endpoints.ServerInvites(serverId)); | |||
| } | |||
| public Task<AcceptInviteResponse> AcceptInvite(string inviteId) | |||
| { | |||
| if (inviteId == null) throw new ArgumentNullException(nameof(inviteId)); | |||
| @@ -1,4 +1,5 @@ | |||
| using System; | |||
| using System.Linq; | |||
| using System.Net; | |||
| using System.Threading.Tasks; | |||
| @@ -10,8 +11,6 @@ namespace Discord | |||
| /// <remarks> Supported formats: inviteCode, xkcdCode, https://discord.gg/inviteCode, https://discord.gg/xkcdCode </remarks> | |||
| public async Task<Invite> GetInvite(string inviteIdOrXkcd) | |||
| { | |||
| //This doesn't work well if it's an invite to a different server! | |||
| if (inviteIdOrXkcd == null) throw new ArgumentNullException(nameof(inviteIdOrXkcd)); | |||
| CheckReady(); | |||
| @@ -30,6 +29,22 @@ namespace Discord | |||
| return invite; | |||
| } | |||
| /// <summary> Gets all active (non-expired) invites to a provided server. </summary> | |||
| public async Task<Invite[]> GetInvites(Server server) | |||
| { | |||
| if (server == null) throw new ArgumentNullException(nameof(server)); | |||
| CheckReady(); | |||
| var response = await _api.GetInvites(server.Id).ConfigureAwait(false); | |||
| return response.Select(x => | |||
| { | |||
| var invite = new Invite(this, x.Code, x.XkcdPass, x.Guild.Id, x.Inviter?.Id, x.Channel?.Id); | |||
| invite.Cache(); //Builds references | |||
| invite.Update(x); | |||
| return invite; | |||
| }).ToArray(); | |||
| } | |||
| /// <summary> Creates a new invite to the default channel of the provided server. </summary> | |||
| /// <param name="maxAge"> Time (in seconds) until the invite expires. Set to 0 to never expire. </param> | |||
| /// <param name="tempMembership"> If true, a user accepting this invite will be kicked from the server after closing their client. </param> | |||
| @@ -55,11 +55,6 @@ namespace Discord | |||
| public IEnumerable<Channel> VoiceChannels => _channels.Select(x => x.Value).Where(x => x.Type == ChannelType.Voice); | |||
| private ConcurrentDictionary<string, Channel> _channels; | |||
| /// <summary> Returns a collection of all invites to this server. </summary> | |||
| [JsonIgnore] | |||
| public IEnumerable<Invite> Invites => _invites.Values; | |||
| private ConcurrentDictionary<string, Invite> _invites; | |||
| /// <summary> Returns a collection of all users within this server with their server-specific data. </summary> | |||
| [JsonIgnore] | |||
| public IEnumerable<User> Members => _members.Select(x => x.Value); | |||
| @@ -85,7 +80,6 @@ namespace Discord | |||
| //Local Cache | |||
| _bans = new ConcurrentDictionary<string, bool>(); | |||
| _invites = new ConcurrentDictionary<string, Invite>(); | |||
| } | |||
| internal override void LoadReferences() | |||
| { | |||
| @@ -113,11 +107,6 @@ namespace Discord | |||
| roles.Clear(); | |||
| //Local Cache | |||
| var invites = _invites; | |||
| foreach (var invite in invites) | |||
| invite.Value.Uncache(); | |||
| invites.Clear(); | |||
| _bans.Clear(); | |||
| _afkChannel.Unload(); | |||
| @@ -218,9 +207,6 @@ namespace Discord | |||
| _channels.TryRemove(channel.Id, out channel); | |||
| } | |||
| internal void AddInvite(Invite invite) => _invites.TryAdd(invite.Id, invite); | |||
| internal void RemoveInvite(Invite invite) => _invites.TryRemove(invite.Id, out invite); | |||
| internal void AddMember(User user) | |||
| { | |||
| if (_members.TryAdd(user.Id, user)) | |||