| @@ -0,0 +1,8 @@ | |||
| using System; | |||
| namespace Discord.Commands | |||
| { | |||
| public class InAttribute : Attribute | |||
| { | |||
| } | |||
| } | |||
| @@ -6,6 +6,6 @@ namespace Discord.Commands | |||
| [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true, Inherited = true)] | |||
| public abstract class PreconditionAttribute : Attribute | |||
| { | |||
| public abstract Task<PreconditionResult> CheckPermissions(IUserMessage context, Command executingCommand, object moduleInstance); | |||
| public abstract Task<PreconditionResult> CheckPermissions(CommandContext context, Command executingCommand, object moduleInstance); | |||
| } | |||
| } | |||
| @@ -21,7 +21,7 @@ namespace Discord.Commands | |||
| Contexts = contexts; | |||
| } | |||
| public override Task<PreconditionResult> CheckPermissions(IUserMessage context, Command executingCommand, object moduleInstance) | |||
| public override Task<PreconditionResult> CheckPermissions(CommandContext context, Command executingCommand, object moduleInstance) | |||
| { | |||
| bool isValid = false; | |||
| @@ -20,9 +20,9 @@ namespace Discord.Commands | |||
| GuildPermission = null; | |||
| } | |||
| public override Task<PreconditionResult> CheckPermissions(IUserMessage context, Command executingCommand, object moduleInstance) | |||
| public override Task<PreconditionResult> CheckPermissions(CommandContext context, Command executingCommand, object moduleInstance) | |||
| { | |||
| var guildUser = context.Author as IGuildUser; | |||
| var guildUser = context.User as IGuildUser; | |||
| if (GuildPermission.HasValue) | |||
| { | |||
| @@ -16,7 +16,7 @@ namespace Discord.Commands | |||
| private static readonly ConcurrentDictionary<Type, Func<IEnumerable<object>, object>> _arrayConverters = new ConcurrentDictionary<Type, Func<IEnumerable<object>, object>>(); | |||
| private readonly object _instance; | |||
| private readonly Func<IUserMessage, IReadOnlyList<object>, Task> _action; | |||
| private readonly Func<CommandContext, IReadOnlyList<object>, Task> _action; | |||
| public MethodInfo Source { get; } | |||
| public Module Module { get; } | |||
| @@ -85,7 +85,7 @@ namespace Discord.Commands | |||
| } | |||
| } | |||
| public async Task<PreconditionResult> CheckPreconditions(IUserMessage context) | |||
| public async Task<PreconditionResult> CheckPreconditions(CommandContext context) | |||
| { | |||
| foreach (PreconditionAttribute precondition in Module.Preconditions) | |||
| { | |||
| @@ -104,7 +104,7 @@ namespace Discord.Commands | |||
| return PreconditionResult.FromSuccess(); | |||
| } | |||
| public async Task<ParseResult> Parse(IUserMessage context, SearchResult searchResult, PreconditionResult? preconditionResult = null) | |||
| public async Task<ParseResult> Parse(CommandContext context, SearchResult searchResult, PreconditionResult? preconditionResult = null) | |||
| { | |||
| if (!searchResult.IsSuccess) | |||
| return ParseResult.FromError(searchResult); | |||
| @@ -125,7 +125,7 @@ namespace Discord.Commands | |||
| return await CommandParser.ParseArgs(this, context, input, 0).ConfigureAwait(false); | |||
| } | |||
| public Task<ExecuteResult> Execute(IUserMessage context, ParseResult parseResult) | |||
| public Task<ExecuteResult> Execute(CommandContext context, ParseResult parseResult) | |||
| { | |||
| if (!parseResult.IsSuccess) | |||
| return Task.FromResult(ExecuteResult.FromError(parseResult)); | |||
| @@ -148,7 +148,7 @@ namespace Discord.Commands | |||
| return Execute(context, argList, paramList); | |||
| } | |||
| public async Task<ExecuteResult> Execute(IUserMessage context, IEnumerable<object> argList, IEnumerable<object> paramList) | |||
| public async Task<ExecuteResult> Execute(CommandContext context, IEnumerable<object> argList, IEnumerable<object> paramList) | |||
| { | |||
| try | |||
| { | |||
| @@ -209,7 +209,7 @@ namespace Discord.Commands | |||
| } | |||
| return paramBuilder.ToImmutable(); | |||
| } | |||
| private Func<IUserMessage, IReadOnlyList<object>, Task> BuildAction(MethodInfo methodInfo) | |||
| private Func<CommandContext, IReadOnlyList<object>, Task> BuildAction(MethodInfo methodInfo) | |||
| { | |||
| if (methodInfo.ReturnType != typeof(Task)) | |||
| throw new InvalidOperationException("Commands must return a non-generic Task."); | |||
| @@ -0,0 +1,18 @@ | |||
| namespace Discord.Commands | |||
| { | |||
| public struct CommandContext | |||
| { | |||
| public IGuild Guild { get; } | |||
| public IMessageChannel Channel { get; } | |||
| public IUser User { get; } | |||
| public IUserMessage Message { get; } | |||
| internal CommandContext(IGuild guild, IMessageChannel channel, IUser user, IUserMessage msg) | |||
| { | |||
| Guild = guild; | |||
| Channel = channel; | |||
| User = user; | |||
| Message = msg; | |||
| } | |||
| } | |||
| } | |||
| @@ -32,7 +32,7 @@ namespace Discord.Commands | |||
| DefaultValue = defaultValue; | |||
| } | |||
| public async Task<TypeReaderResult> Parse(IUserMessage context, string input) | |||
| public async Task<TypeReaderResult> Parse(CommandContext context, string input) | |||
| { | |||
| return await _reader.Read(context, input).ConfigureAwait(false); | |||
| } | |||
| @@ -13,7 +13,7 @@ namespace Discord.Commands | |||
| QuotedParameter | |||
| } | |||
| public static async Task<ParseResult> ParseArgs(Command command, IUserMessage context, string input, int startPos) | |||
| public static async Task<ParseResult> ParseArgs(Command command, CommandContext context, string input, int startPos) | |||
| { | |||
| CommandParameter curParam = null; | |||
| StringBuilder argBuilder = new StringBuilder(input.Length); | |||
| @@ -176,8 +176,8 @@ namespace Discord.Commands | |||
| return false; | |||
| } | |||
| public SearchResult Search(IUserMessage message, int argPos) => Search(message, message.Content.Substring(argPos)); | |||
| public SearchResult Search(IUserMessage message, string input) | |||
| public SearchResult Search(CommandContext context, int argPos) => Search(context, context.Message.Content.Substring(argPos)); | |||
| public SearchResult Search(CommandContext context, string input) | |||
| { | |||
| string lowerInput = input.ToLowerInvariant(); | |||
| var matches = _map.GetCommands(input).OrderByDescending(x => x.Priority).ToImmutableArray(); | |||
| @@ -188,18 +188,18 @@ namespace Discord.Commands | |||
| return SearchResult.FromError(CommandError.UnknownCommand, "Unknown command."); | |||
| } | |||
| public Task<IResult> Execute(IUserMessage message, int argPos, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception) | |||
| => Execute(message, message.Content.Substring(argPos), multiMatchHandling); | |||
| public async Task<IResult> Execute(IUserMessage message, string input, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception) | |||
| public Task<IResult> Execute(CommandContext context, int argPos, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception) | |||
| => Execute(context, context.Message.Content.Substring(argPos), multiMatchHandling); | |||
| public async Task<IResult> Execute(CommandContext context, string input, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception) | |||
| { | |||
| var searchResult = Search(message, input); | |||
| var searchResult = Search(context, input); | |||
| if (!searchResult.IsSuccess) | |||
| return searchResult; | |||
| var commands = searchResult.Commands; | |||
| for (int i = commands.Count - 1; i >= 0; i--) | |||
| { | |||
| var preconditionResult = await commands[i].CheckPreconditions(message); | |||
| var preconditionResult = await commands[i].CheckPreconditions(context); | |||
| if (!preconditionResult.IsSuccess) | |||
| { | |||
| if (commands.Count == 1) | |||
| @@ -208,7 +208,7 @@ namespace Discord.Commands | |||
| continue; | |||
| } | |||
| var parseResult = await commands[i].Parse(message, searchResult, preconditionResult); | |||
| var parseResult = await commands[i].Parse(context, searchResult, preconditionResult); | |||
| if (!parseResult.IsSuccess) | |||
| { | |||
| if (parseResult.Error == CommandError.MultipleMatches) | |||
| @@ -233,7 +233,7 @@ namespace Discord.Commands | |||
| } | |||
| } | |||
| return await commands[i].Execute(message, parseResult); | |||
| return await commands[i].Execute(context, parseResult); | |||
| } | |||
| return SearchResult.FromError(CommandError.UnknownCommand, "This input does not match any overload."); | |||
| @@ -9,23 +9,21 @@ namespace Discord.Commands | |||
| internal class ChannelTypeReader<T> : TypeReader | |||
| where T : class, IChannel | |||
| { | |||
| public override async Task<TypeReaderResult> Read(IUserMessage context, string input) | |||
| public override async Task<TypeReaderResult> Read(CommandContext context, string input) | |||
| { | |||
| var guild = (context.Channel as IGuildChannel)?.Guild; | |||
| if (guild != null) | |||
| if (context.Guild != null) | |||
| { | |||
| var results = new Dictionary<ulong, TypeReaderValue>(); | |||
| var channels = await guild.GetChannelsAsync().ConfigureAwait(false); | |||
| var channels = await context.Guild.GetChannelsAsync().ConfigureAwait(false); | |||
| ulong id; | |||
| //By Mention (1.0) | |||
| if (MentionUtils.TryParseChannel(input, out id)) | |||
| AddResult(results, await guild.GetChannelAsync(id).ConfigureAwait(false) as T, 1.00f); | |||
| AddResult(results, await context.Guild.GetChannelAsync(id).ConfigureAwait(false) as T, 1.00f); | |||
| //By Id (0.9) | |||
| if (ulong.TryParse(input, NumberStyles.None, CultureInfo.InvariantCulture, out id)) | |||
| AddResult(results, await guild.GetChannelAsync(id).ConfigureAwait(false) as T, 0.90f); | |||
| AddResult(results, await context.Guild.GetChannelAsync(id).ConfigureAwait(false) as T, 0.90f); | |||
| //By Name (0.7-0.8) | |||
| foreach (var channel in channels.Where(x => string.Equals(input, x.Name, StringComparison.OrdinalIgnoreCase))) | |||
| @@ -42,7 +42,7 @@ namespace Discord.Commands | |||
| _enumsByValue = byValueBuilder.ToImmutable(); | |||
| } | |||
| public override Task<TypeReaderResult> Read(IUserMessage context, string input) | |||
| public override Task<TypeReaderResult> Read(CommandContext context, string input) | |||
| { | |||
| T baseValue; | |||
| object enumValue; | |||
| @@ -6,19 +6,19 @@ namespace Discord.Commands | |||
| internal class MessageTypeReader<T> : TypeReader | |||
| where T : class, IMessage | |||
| { | |||
| public override Task<TypeReaderResult> Read(IUserMessage context, string input) | |||
| public override async Task<TypeReaderResult> Read(CommandContext context, string input) | |||
| { | |||
| ulong id; | |||
| //By Id (1.0) | |||
| if (ulong.TryParse(input, NumberStyles.None, CultureInfo.InvariantCulture, out id)) | |||
| { | |||
| var msg = context.Channel.GetCachedMessage(id) as T; | |||
| var msg = await context.Channel.GetMessageAsync(id, CacheMode.CacheOnly).ConfigureAwait(false) as T; | |||
| if (msg != null) | |||
| return Task.FromResult(TypeReaderResult.FromSuccess(msg)); | |||
| return TypeReaderResult.FromSuccess(msg); | |||
| } | |||
| return Task.FromResult(TypeReaderResult.FromError(CommandError.ObjectNotFound, "Message not found.")); | |||
| return TypeReaderResult.FromError(CommandError.ObjectNotFound, "Message not found."); | |||
| } | |||
| } | |||
| } | |||
| @@ -9,23 +9,22 @@ namespace Discord.Commands | |||
| internal class RoleTypeReader<T> : TypeReader | |||
| where T : class, IRole | |||
| { | |||
| public override Task<TypeReaderResult> Read(IUserMessage context, string input) | |||
| public override Task<TypeReaderResult> Read(CommandContext context, string input) | |||
| { | |||
| var guild = (context.Channel as IGuildChannel)?.Guild; | |||
| ulong id; | |||
| if (guild != null) | |||
| if (context.Guild != null) | |||
| { | |||
| var results = new Dictionary<ulong, TypeReaderValue>(); | |||
| var roles = guild.Roles; | |||
| var roles = context.Guild.Roles; | |||
| //By Mention (1.0) | |||
| if (MentionUtils.TryParseRole(input, out id)) | |||
| AddResult(results, guild.GetRole(id) as T, 1.00f); | |||
| AddResult(results, context.Guild.GetRole(id) as T, 1.00f); | |||
| //By Id (0.9) | |||
| if (ulong.TryParse(input, NumberStyles.None, CultureInfo.InvariantCulture, out id)) | |||
| AddResult(results, guild.GetRole(id) as T, 0.90f); | |||
| AddResult(results, context.Guild.GetRole(id) as T, 0.90f); | |||
| //By Name (0.7-0.8) | |||
| foreach (var role in roles.Where(x => string.Equals(input, x.Name, StringComparison.OrdinalIgnoreCase))) | |||
| @@ -11,7 +11,7 @@ namespace Discord.Commands | |||
| _tryParse = PrimitiveParsers.Get<T>(); | |||
| } | |||
| public override Task<TypeReaderResult> Read(IUserMessage context, string input) | |||
| public override Task<TypeReaderResult> Read(CommandContext context, string input) | |||
| { | |||
| T value; | |||
| if (_tryParse(input, out value)) | |||
| @@ -4,6 +4,6 @@ namespace Discord.Commands | |||
| { | |||
| public abstract class TypeReader | |||
| { | |||
| public abstract Task<TypeReaderResult> Read(IUserMessage context, string input); | |||
| public abstract Task<TypeReaderResult> Read(CommandContext context, string input); | |||
| } | |||
| } | |||
| @@ -9,22 +9,21 @@ namespace Discord.Commands | |||
| internal class UserTypeReader<T> : TypeReader | |||
| where T : class, IUser | |||
| { | |||
| public override async Task<TypeReaderResult> Read(IUserMessage context, string input) | |||
| public override async Task<TypeReaderResult> Read(CommandContext context, string input) | |||
| { | |||
| var results = new Dictionary<ulong, TypeReaderValue>(); | |||
| var guild = (context.Channel as IGuildChannel)?.Guild; | |||
| IReadOnlyCollection<IUser> channelUsers = await context.Channel.GetUsersAsync().ConfigureAwait(false); | |||
| IReadOnlyCollection<IUser> channelUsers = (await context.Channel.GetUsersAsync().Flatten().ConfigureAwait(false)).ToArray(); //TODO: must be a better way? | |||
| IReadOnlyCollection<IGuildUser> guildUsers = null; | |||
| ulong id; | |||
| if (guild != null) | |||
| guildUsers = await guild.GetUsersAsync().ConfigureAwait(false); | |||
| if (context.Guild != null) | |||
| guildUsers = await context.Guild.GetUsersAsync().ConfigureAwait(false); | |||
| //By Mention (1.0) | |||
| if (MentionUtils.TryParseUser(input, out id)) | |||
| { | |||
| if (guild != null) | |||
| AddResult(results, await guild.GetUserAsync(id).ConfigureAwait(false) as T, 1.00f); | |||
| if (context.Guild != null) | |||
| AddResult(results, await context.Guild.GetUserAsync(id).ConfigureAwait(false) as T, 1.00f); | |||
| else | |||
| AddResult(results, await context.Channel.GetUserAsync(id).ConfigureAwait(false) as T, 1.00f); | |||
| } | |||
| @@ -32,8 +31,8 @@ namespace Discord.Commands | |||
| //By Id (0.9) | |||
| if (ulong.TryParse(input, NumberStyles.None, CultureInfo.InvariantCulture, out id)) | |||
| { | |||
| if (guild != null) | |||
| AddResult(results, await guild.GetUserAsync(id).ConfigureAwait(false) as T, 0.90f); | |||
| if (context.Guild != null) | |||
| AddResult(results, await context.Guild.GetUserAsync(id).ConfigureAwait(false) as T, 0.90f); | |||
| else | |||
| AddResult(results, await context.Channel.GetUserAsync(id).ConfigureAwait(false) as T, 0.90f); | |||
| } | |||
| @@ -13,18 +13,14 @@ | |||
| } | |||
| }, | |||
| "buildOptions": { | |||
| "allowUnsafe": true, | |||
| "warningsAsErrors": false, | |||
| "xmlDoc": true | |||
| }, | |||
| "configurations": { | |||
| "Release": { | |||
| "buildOptions": { | |||
| "define": [ "RELEASE" ], | |||
| "nowarn": [ "CS1573", "CS1591" ], | |||
| "optimize": true | |||
| "optimize": true, | |||
| "warningsAsErrors": true, | |||
| "xmlDoc": true | |||
| } | |||
| } | |||
| }, | |||
| @@ -856,27 +856,6 @@ namespace Discord.API | |||
| } | |||
| catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound) { return null; } | |||
| } | |||
| public async Task<User> GetUserAsync(string username, string discriminator, RequestOptions options = null) | |||
| { | |||
| Preconditions.NotNullOrEmpty(username, nameof(username)); | |||
| Preconditions.NotNullOrEmpty(discriminator, nameof(discriminator)); | |||
| options = RequestOptions.CreateOrClone(options); | |||
| try | |||
| { | |||
| var models = await QueryUsersAsync($"{username}#{discriminator}", 1, options: options).ConfigureAwait(false); | |||
| return models.FirstOrDefault(); | |||
| } | |||
| catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound) { return null; } | |||
| } | |||
| public async Task<IReadOnlyCollection<User>> QueryUsersAsync(string query, int limit, RequestOptions options = null) | |||
| { | |||
| Preconditions.NotNullOrEmpty(query, nameof(query)); | |||
| Preconditions.AtLeast(limit, 0, nameof(limit)); | |||
| options = RequestOptions.CreateOrClone(options); | |||
| return await SendAsync<IReadOnlyCollection<User>>("GET", $"users?q={Uri.EscapeDataString(query)}&limit={limit}", options: options).ConfigureAwait(false); | |||
| } | |||
| //Current User/DMs | |||
| public async Task<User> GetMyUserAsync(RequestOptions options = null) | |||
| @@ -12,6 +12,7 @@ namespace Discord | |||
| DateTimeOffset? JoinedAt { get; } | |||
| /// <summary> Gets the nickname for this user. </summary> | |||
| string Nickname { get; } | |||
| GuildPermissions GuildPermissions { get; } | |||
| /// <summary> Gets the id of the guild for this user. </summary> | |||
| ulong GuildId { get; } | |||
| @@ -19,18 +19,18 @@ namespace Discord | |||
| Task<IApplication> GetApplicationInfoAsync(); | |||
| Task<IChannel> GetChannelAsync(ulong id); | |||
| Task<IReadOnlyCollection<IPrivateChannel>> GetPrivateChannelsAsync(); | |||
| Task<IChannel> GetChannelAsync(ulong id, CacheMode mode = CacheMode.AllowDownload); | |||
| Task<IReadOnlyCollection<IPrivateChannel>> GetPrivateChannelsAsync(CacheMode mode = CacheMode.AllowDownload); | |||
| Task<IReadOnlyCollection<IConnection>> GetConnectionsAsync(); | |||
| Task<IGuild> GetGuildAsync(ulong id); | |||
| Task<IReadOnlyCollection<IGuild>> GetGuildsAsync(); | |||
| Task<IGuild> GetGuildAsync(ulong id, CacheMode mode = CacheMode.AllowDownload); | |||
| Task<IReadOnlyCollection<IGuild>> GetGuildsAsync(CacheMode mode = CacheMode.AllowDownload); | |||
| Task<IGuild> CreateGuildAsync(string name, IVoiceRegion region, Stream jpegIcon = null); | |||
| Task<IInvite> GetInviteAsync(string inviteId); | |||
| Task<IUser> GetUserAsync(ulong id); | |||
| Task<IUser> GetUserAsync(ulong id, CacheMode mode = CacheMode.AllowDownload); | |||
| Task<IUser> GetUserAsync(string username, string discriminator); | |||
| Task<IReadOnlyCollection<IVoiceRegion>> GetVoiceRegionsAsync(); | |||
| @@ -129,9 +129,9 @@ namespace Discord.Rest | |||
| Task<IApplication> IDiscordClient.GetApplicationInfoAsync() { throw new NotSupportedException(); } | |||
| Task<IChannel> IDiscordClient.GetChannelAsync(ulong id) | |||
| Task<IChannel> IDiscordClient.GetChannelAsync(ulong id, CacheMode mode) | |||
| => Task.FromResult<IChannel>(null); | |||
| Task<IReadOnlyCollection<IPrivateChannel>> IDiscordClient.GetPrivateChannelsAsync() | |||
| Task<IReadOnlyCollection<IPrivateChannel>> IDiscordClient.GetPrivateChannelsAsync(CacheMode mode) | |||
| => Task.FromResult<IReadOnlyCollection<IPrivateChannel>>(ImmutableArray.Create<IPrivateChannel>()); | |||
| Task<IReadOnlyCollection<IConnection>> IDiscordClient.GetConnectionsAsync() | |||
| @@ -140,13 +140,13 @@ namespace Discord.Rest | |||
| Task<IInvite> IDiscordClient.GetInviteAsync(string inviteId) | |||
| => Task.FromResult<IInvite>(null); | |||
| Task<IGuild> IDiscordClient.GetGuildAsync(ulong id) | |||
| Task<IGuild> IDiscordClient.GetGuildAsync(ulong id, CacheMode mode) | |||
| => Task.FromResult<IGuild>(null); | |||
| Task<IReadOnlyCollection<IGuild>> IDiscordClient.GetGuildsAsync() | |||
| Task<IReadOnlyCollection<IGuild>> IDiscordClient.GetGuildsAsync(CacheMode mode) | |||
| => Task.FromResult<IReadOnlyCollection<IGuild>>(ImmutableArray.Create<IGuild>()); | |||
| Task<IGuild> IDiscordClient.CreateGuildAsync(string name, IVoiceRegion region, Stream jpegIcon) { throw new NotSupportedException(); } | |||
| Task<IUser> IDiscordClient.GetUserAsync(ulong id) | |||
| Task<IUser> IDiscordClient.GetUserAsync(ulong id, CacheMode mode) | |||
| => Task.FromResult<IUser>(null); | |||
| Task<IUser> IDiscordClient.GetUserAsync(string username, string discriminator) | |||
| => Task.FromResult<IUser>(null); | |||
| @@ -94,12 +94,12 @@ namespace Discord.Rest | |||
| return RestUser.Create(client, model); | |||
| return null; | |||
| } | |||
| public static async Task<RestUser> GetUserAsync(BaseDiscordClient client, | |||
| string username, string discriminator) | |||
| public static async Task<RestGuildUser> GetGuildUserAsync(BaseDiscordClient client, | |||
| ulong guildId, ulong id) | |||
| { | |||
| var model = await client.ApiClient.GetUserAsync(username, discriminator).ConfigureAwait(false); | |||
| var model = await client.ApiClient.GetGuildMemberAsync(guildId, id).ConfigureAwait(false); | |||
| if (model != null) | |||
| return RestUser.Create(client, model); | |||
| return RestGuildUser.Create(client, new RestGuild(client, guildId), model); | |||
| return null; | |||
| } | |||
| @@ -1,5 +1,6 @@ | |||
| using Discord.Net.Queue; | |||
| using System.Collections.Generic; | |||
| using System.Collections.Immutable; | |||
| using System.IO; | |||
| using System.Threading.Tasks; | |||
| @@ -60,8 +61,8 @@ namespace Discord.Rest | |||
| public Task<RestUser> GetUserAsync(ulong id) | |||
| => ClientHelper.GetUserAsync(this, id); | |||
| /// <inheritdoc /> | |||
| public Task<RestUser> GetUserAsync(string username, string discriminator) | |||
| => ClientHelper.GetUserAsync(this, username, discriminator); | |||
| public Task<RestGuildUser> GetGuildUserAsync(ulong guildId, ulong id) | |||
| => ClientHelper.GetGuildUserAsync(this, guildId, id); | |||
| /// <inheritdoc /> | |||
| public Task<IReadOnlyCollection<RestVoiceRegion>> GetVoiceRegionsAsync() | |||
| @@ -74,10 +75,20 @@ namespace Discord.Rest | |||
| async Task<IApplication> IDiscordClient.GetApplicationInfoAsync() | |||
| => await GetApplicationInfoAsync().ConfigureAwait(false); | |||
| async Task<IChannel> IDiscordClient.GetChannelAsync(ulong id) | |||
| => await GetChannelAsync(id); | |||
| async Task<IReadOnlyCollection<IPrivateChannel>> IDiscordClient.GetPrivateChannelsAsync() | |||
| => await GetPrivateChannelsAsync(); | |||
| async Task<IChannel> IDiscordClient.GetChannelAsync(ulong id, CacheMode mode) | |||
| { | |||
| if (mode == CacheMode.AllowDownload) | |||
| return await GetChannelAsync(id); | |||
| else | |||
| return null; | |||
| } | |||
| async Task<IReadOnlyCollection<IPrivateChannel>> IDiscordClient.GetPrivateChannelsAsync(CacheMode mode) | |||
| { | |||
| if (mode == CacheMode.AllowDownload) | |||
| return await GetPrivateChannelsAsync(); | |||
| else | |||
| return ImmutableArray.Create<IPrivateChannel>(); | |||
| } | |||
| async Task<IReadOnlyCollection<IConnection>> IDiscordClient.GetConnectionsAsync() | |||
| => await GetConnectionsAsync().ConfigureAwait(false); | |||
| @@ -85,17 +96,30 @@ namespace Discord.Rest | |||
| async Task<IInvite> IDiscordClient.GetInviteAsync(string inviteId) | |||
| => await GetInviteAsync(inviteId).ConfigureAwait(false); | |||
| async Task<IGuild> IDiscordClient.GetGuildAsync(ulong id) | |||
| => await GetGuildAsync(id).ConfigureAwait(false); | |||
| async Task<IReadOnlyCollection<IGuild>> IDiscordClient.GetGuildsAsync() | |||
| => await GetGuildsAsync().ConfigureAwait(false); | |||
| async Task<IGuild> IDiscordClient.GetGuildAsync(ulong id, CacheMode mode) | |||
| { | |||
| if (mode == CacheMode.AllowDownload) | |||
| return await GetGuildAsync(id).ConfigureAwait(false); | |||
| else | |||
| return null; | |||
| } | |||
| async Task<IReadOnlyCollection<IGuild>> IDiscordClient.GetGuildsAsync(CacheMode mode) | |||
| { | |||
| if (mode == CacheMode.AllowDownload) | |||
| return await GetGuildsAsync().ConfigureAwait(false); | |||
| else | |||
| return ImmutableArray.Create<IGuild>(); | |||
| } | |||
| async Task<IGuild> IDiscordClient.CreateGuildAsync(string name, IVoiceRegion region, Stream jpegIcon) | |||
| => await CreateGuildAsync(name, region, jpegIcon).ConfigureAwait(false); | |||
| async Task<IUser> IDiscordClient.GetUserAsync(ulong id) | |||
| => await GetUserAsync(id).ConfigureAwait(false); | |||
| async Task<IUser> IDiscordClient.GetUserAsync(string username, string discriminator) | |||
| => await GetUserAsync(username, discriminator).ConfigureAwait(false); | |||
| async Task<IUser> IDiscordClient.GetUserAsync(ulong id, CacheMode mode) | |||
| { | |||
| if (mode == CacheMode.AllowDownload) | |||
| return await GetUserAsync(id).ConfigureAwait(false); | |||
| else | |||
| return null; | |||
| } | |||
| async Task<IReadOnlyCollection<IVoiceRegion>> IDiscordClient.GetVoiceRegionsAsync() | |||
| => await GetVoiceRegionsAsync().ConfigureAwait(false); | |||
| @@ -163,19 +163,19 @@ namespace Discord.Rest | |||
| } | |||
| //Users | |||
| public static async Task<RestGuildUser> GetUserAsync(IGuildChannel channel, BaseDiscordClient client, | |||
| public static async Task<RestGuildUser> GetUserAsync(IGuildChannel channel, IGuild guild, BaseDiscordClient client, | |||
| ulong id) | |||
| { | |||
| var model = await client.ApiClient.GetGuildMemberAsync(channel.GuildId, id); | |||
| if (model == null) | |||
| return null; | |||
| var user = RestGuildUser.Create(client, model); | |||
| var user = RestGuildUser.Create(client, guild, model); | |||
| if (!user.GetPermissions(channel).ReadMessages) | |||
| return null; | |||
| return user; | |||
| } | |||
| public static IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync(IGuildChannel channel, BaseDiscordClient client, | |||
| public static IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync(IGuildChannel channel, IGuild guild, BaseDiscordClient client, | |||
| ulong? froUserId = null, uint? limit = DiscordConfig.MaxUsersPerBatch) | |||
| { | |||
| return new PagedAsyncEnumerable<RestGuildUser>( | |||
| @@ -188,8 +188,11 @@ namespace Discord.Rest | |||
| }; | |||
| if (info.Position != null) | |||
| args.AfterUserId = info.Position.Value; | |||
| var models = await client.ApiClient.GetGuildMembersAsync(channel.GuildId, args); | |||
| return models.Select(x => RestGuildUser.Create(client, x)).ToImmutableArray(); ; | |||
| var models = await guild.Discord.ApiClient.GetGuildMembersAsync(guild.Id, args); | |||
| return models | |||
| .Select(x => RestGuildUser.Create(client, guild, x)) | |||
| .Where(x => x.GetPermissions(channel).ReadMessages) | |||
| .ToImmutableArray(); | |||
| }, | |||
| nextPage: (info, lastPage) => | |||
| { | |||
| @@ -16,24 +16,25 @@ namespace Discord.Rest | |||
| public IReadOnlyCollection<Overwrite> PermissionOverwrites => _overwrites; | |||
| public ulong GuildId { get; } | |||
| internal IGuild Guild { get; } | |||
| public ulong GuildId => Guild.Id; | |||
| public string Name { get; private set; } | |||
| public int Position { get; private set; } | |||
| internal RestGuildChannel(BaseDiscordClient discord, ulong id, ulong guildId) | |||
| internal RestGuildChannel(BaseDiscordClient discord, IGuild guild, ulong id) | |||
| : base(discord, id) | |||
| { | |||
| GuildId = guildId; | |||
| Guild = guild; | |||
| } | |||
| internal new static RestGuildChannel Create(BaseDiscordClient discord, Model model) | |||
| internal static RestGuildChannel Create(BaseDiscordClient discord, IGuild guild, Model model) | |||
| { | |||
| switch (model.Type) | |||
| { | |||
| case ChannelType.Text: | |||
| return RestTextChannel.Create(discord, model); | |||
| return RestTextChannel.Create(discord, guild, model); | |||
| case ChannelType.Voice: | |||
| return RestVoiceChannel.Create(discord, model); | |||
| return RestVoiceChannel.Create(discord, guild, model); | |||
| default: | |||
| throw new InvalidOperationException("Unknown guild channel type"); | |||
| } | |||
| @@ -17,13 +17,13 @@ namespace Discord.Rest | |||
| public string Mention => MentionUtils.MentionChannel(Id); | |||
| internal RestTextChannel(BaseDiscordClient discord, ulong id, ulong guildId) | |||
| : base(discord, id, guildId) | |||
| internal RestTextChannel(BaseDiscordClient discord, IGuild guild, ulong id) | |||
| : base(discord, guild, id) | |||
| { | |||
| } | |||
| internal new static RestTextChannel Create(BaseDiscordClient discord, Model model) | |||
| internal new static RestTextChannel Create(BaseDiscordClient discord, IGuild guild, Model model) | |||
| { | |||
| var entity = new RestTextChannel(discord, model.Id, model.GuildId.Value); | |||
| var entity = new RestTextChannel(discord, guild, model.Id); | |||
| entity.Update(model); | |||
| return entity; | |||
| } | |||
| @@ -39,9 +39,9 @@ namespace Discord.Rest | |||
| => ChannelHelper.ModifyAsync(this, Discord, func); | |||
| public Task<RestGuildUser> GetUserAsync(ulong id) | |||
| => ChannelHelper.GetUserAsync(this, Discord, id); | |||
| => ChannelHelper.GetUserAsync(this, Guild, Discord, id); | |||
| public IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync() | |||
| => ChannelHelper.GetUsersAsync(this, Discord); | |||
| => ChannelHelper.GetUsersAsync(this, Guild, Discord); | |||
| public Task<RestMessage> GetMessageAsync(ulong id) | |||
| => ChannelHelper.GetMessageAsync(this, Discord, id); | |||
| @@ -16,13 +16,13 @@ namespace Discord.Rest | |||
| public int Bitrate { get; private set; } | |||
| public int UserLimit { get; private set; } | |||
| internal RestVoiceChannel(BaseDiscordClient discord, ulong id, ulong guildId) | |||
| : base(discord, id, guildId) | |||
| internal RestVoiceChannel(BaseDiscordClient discord, IGuild guild, ulong id) | |||
| : base(discord, guild, id) | |||
| { | |||
| } | |||
| internal new static RestVoiceChannel Create(BaseDiscordClient discord, Model model) | |||
| internal new static RestVoiceChannel Create(BaseDiscordClient discord, IGuild guild, Model model) | |||
| { | |||
| var entity = new RestVoiceChannel(discord, model.Id, model.GuildId.Value); | |||
| var entity = new RestVoiceChannel(discord, guild, model.Id); | |||
| entity.Update(model); | |||
| return entity; | |||
| } | |||
| @@ -81,13 +81,13 @@ namespace Discord.Rest | |||
| { | |||
| var model = await client.ApiClient.GetChannelAsync(guild.Id, id).ConfigureAwait(false); | |||
| if (model != null) | |||
| return RestGuildChannel.Create(client, model); | |||
| return RestGuildChannel.Create(client, guild, model); | |||
| return null; | |||
| } | |||
| public static async Task<IReadOnlyCollection<RestGuildChannel>> GetChannelsAsync(IGuild guild, BaseDiscordClient client) | |||
| { | |||
| var models = await client.ApiClient.GetGuildChannelsAsync(guild.Id).ConfigureAwait(false); | |||
| return models.Select(x => RestGuildChannel.Create(client, x)).ToImmutableArray(); | |||
| return models.Select(x => RestGuildChannel.Create(client, guild, x)).ToImmutableArray(); | |||
| } | |||
| public static async Task<RestTextChannel> CreateTextChannelAsync(IGuild guild, BaseDiscordClient client, | |||
| string name) | |||
| @@ -96,7 +96,7 @@ namespace Discord.Rest | |||
| var args = new CreateGuildChannelParams(name, ChannelType.Text); | |||
| var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args).ConfigureAwait(false); | |||
| return RestTextChannel.Create(client, model); | |||
| return RestTextChannel.Create(client, guild, model); | |||
| } | |||
| public static async Task<RestVoiceChannel> CreateVoiceChannelAsync(IGuild guild, BaseDiscordClient client, | |||
| string name) | |||
| @@ -105,7 +105,7 @@ namespace Discord.Rest | |||
| var args = new CreateGuildChannelParams(name, ChannelType.Voice); | |||
| var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args).ConfigureAwait(false); | |||
| return RestVoiceChannel.Create(client, model); | |||
| return RestVoiceChannel.Create(client, guild, model); | |||
| } | |||
| //Integrations | |||
| @@ -155,7 +155,7 @@ namespace Discord.Rest | |||
| { | |||
| var model = await client.ApiClient.GetGuildMemberAsync(guild.Id, id).ConfigureAwait(false); | |||
| if (model != null) | |||
| return RestGuildUser.Create(client, model); | |||
| return RestGuildUser.Create(client, guild, model); | |||
| return null; | |||
| } | |||
| public static async Task<RestGuildUser> GetCurrentUserAsync(IGuild guild, BaseDiscordClient client) | |||
| @@ -166,7 +166,7 @@ namespace Discord.Rest | |||
| { | |||
| var args = new GetGuildMembersParams(); | |||
| var models = await client.ApiClient.GetGuildMembersAsync(guild.Id, args).ConfigureAwait(false); | |||
| return models.Select(x => RestGuildUser.Create(client, x)).ToImmutableArray(); | |||
| return models.Select(x => RestGuildUser.Create(client, guild, x)).ToImmutableArray(); | |||
| } | |||
| public static async Task<int> PruneUsersAsync(IGuild guild, BaseDiscordClient client, | |||
| int days = 30, bool simulate = false) | |||
| @@ -30,6 +30,7 @@ namespace Discord.Rest | |||
| public string VoiceRegionId { get; private set; } | |||
| public string IconId { get; private set; } | |||
| public string SplashId { get; private set; } | |||
| internal bool Available { get; private set; } | |||
| public ulong DefaultChannelId => Id; | |||
| public string IconUrl => API.CDN.GetGuildIconUrl(Id, IconId); | |||
| @@ -87,6 +88,8 @@ namespace Discord.Rest | |||
| roles[model.Roles[i].Id] = RestRole.Create(Discord, model.Roles[i]); | |||
| } | |||
| _roles = roles.ToImmutable(); | |||
| Available = true; | |||
| } | |||
| //General | |||
| @@ -169,7 +172,7 @@ namespace Discord.Rest | |||
| => GuildHelper.PruneUsersAsync(this, Discord, days, simulate); | |||
| //IGuild | |||
| bool IGuild.Available => true; | |||
| bool IGuild.Available => Available; | |||
| IAudioClient IGuild.AudioClient => null; | |||
| IRole IGuild.EveryoneRole => EveryoneRole; | |||
| IReadOnlyCollection<IRole> IGuild.Roles => Roles; | |||
| @@ -15,19 +15,30 @@ namespace Discord.Rest | |||
| private ImmutableArray<ulong> _roleIds; | |||
| public string Nickname { get; private set; } | |||
| public ulong GuildId { get; private set; } | |||
| internal IGuild Guild { get; private set; } | |||
| public ulong GuildId => Guild.Id; | |||
| public GuildPermissions GuildPermissions | |||
| { | |||
| get | |||
| { | |||
| if (!Guild.Available) | |||
| throw new InvalidOperationException("Resolving permissions requires the parent guild to be downloaded."); | |||
| return new GuildPermissions(Permissions.ResolveGuild(Guild, this)); | |||
| } | |||
| } | |||
| public IReadOnlyCollection<ulong> RoleIds => _roleIds; | |||
| public DateTimeOffset? JoinedAt => DateTimeUtils.FromTicks(_joinedAtTicks); | |||
| internal RestGuildUser(BaseDiscordClient discord, ulong id) | |||
| internal RestGuildUser(BaseDiscordClient discord, IGuild guild, ulong id) | |||
| : base(discord, id) | |||
| { | |||
| Guild = guild; | |||
| } | |||
| internal static RestGuildUser Create(BaseDiscordClient discord, Model model) | |||
| internal static RestGuildUser Create(BaseDiscordClient discord, IGuild guild, Model model) | |||
| { | |||
| var entity = new RestGuildUser(discord, model.User.Id); | |||
| var entity = new RestGuildUser(discord, guild, model.User.Id); | |||
| entity.Update(model); | |||
| return entity; | |||
| } | |||
| @@ -41,7 +52,7 @@ namespace Discord.Rest | |||
| private void UpdateRoles(ulong[] roleIds) | |||
| { | |||
| var roles = ImmutableArray.CreateBuilder<ulong>(roleIds.Length + 1); | |||
| roles.Add(GuildId); | |||
| roles.Add(Guild.Id); | |||
| for (int i = 0; i < roleIds.Length; i++) | |||
| roles.Add(roleIds[i]); | |||
| _roleIds = roles.ToImmutable(); | |||
| @@ -56,12 +67,10 @@ namespace Discord.Rest | |||
| public ChannelPermissions GetPermissions(IGuildChannel channel) | |||
| { | |||
| throw new NotImplementedException(); //TODO: Impl | |||
| var guildPerms = GuildPermissions; | |||
| return new ChannelPermissions(Permissions.ResolveChannel(Guild, this, channel, guildPerms.RawValue)); | |||
| } | |||
| //IGuildUser | |||
| IReadOnlyCollection<ulong> IGuildUser.RoleIds => RoleIds; | |||
| //IVoiceState | |||
| bool IVoiceState.IsDeafened => false; | |||
| bool IVoiceState.IsMuted => false; | |||
| @@ -1612,9 +1612,9 @@ namespace Discord.WebSocket | |||
| async Task<IApplication> IDiscordClient.GetApplicationInfoAsync() | |||
| => await GetApplicationInfoAsync().ConfigureAwait(false); | |||
| Task<IChannel> IDiscordClient.GetChannelAsync(ulong id) | |||
| Task<IChannel> IDiscordClient.GetChannelAsync(ulong id, CacheMode mode) | |||
| => Task.FromResult<IChannel>(GetChannel(id)); | |||
| Task<IReadOnlyCollection<IPrivateChannel>> IDiscordClient.GetPrivateChannelsAsync() | |||
| Task<IReadOnlyCollection<IPrivateChannel>> IDiscordClient.GetPrivateChannelsAsync(CacheMode mode) | |||
| => Task.FromResult<IReadOnlyCollection<IPrivateChannel>>(PrivateChannels); | |||
| async Task<IReadOnlyCollection<IConnection>> IDiscordClient.GetConnectionsAsync() | |||
| @@ -1623,14 +1623,14 @@ namespace Discord.WebSocket | |||
| async Task<IInvite> IDiscordClient.GetInviteAsync(string inviteId) | |||
| => await GetInviteAsync(inviteId); | |||
| Task<IGuild> IDiscordClient.GetGuildAsync(ulong id) | |||
| Task<IGuild> IDiscordClient.GetGuildAsync(ulong id, CacheMode mode) | |||
| => Task.FromResult<IGuild>(GetGuild(id)); | |||
| Task<IReadOnlyCollection<IGuild>> IDiscordClient.GetGuildsAsync() | |||
| Task<IReadOnlyCollection<IGuild>> IDiscordClient.GetGuildsAsync(CacheMode mode) | |||
| => Task.FromResult<IReadOnlyCollection<IGuild>>(Guilds); | |||
| async Task<IGuild> IDiscordClient.CreateGuildAsync(string name, IVoiceRegion region, Stream jpegIcon) | |||
| => await CreateGuildAsync(name, region, jpegIcon); | |||
| Task<IUser> IDiscordClient.GetUserAsync(ulong id) | |||
| Task<IUser> IDiscordClient.GetUserAsync(ulong id, CacheMode mode) | |||
| => Task.FromResult<IUser>(GetUser(id)); | |||
| Task<IUser> IDiscordClient.GetUserAsync(string username, string discriminator) | |||
| => Task.FromResult<IUser>(GetUser(username, discriminator)); | |||
| @@ -23,6 +23,7 @@ namespace Discord.WebSocket | |||
| public override ushort DiscriminatorValue { get { return GlobalUser.DiscriminatorValue; } internal set { GlobalUser.DiscriminatorValue = value; } } | |||
| public override string AvatarId { get { return GlobalUser.AvatarId; } internal set { GlobalUser.AvatarId = value; } } | |||
| internal override SocketPresence Presence { get { return GlobalUser.Presence; } set { GlobalUser.Presence = value; } } | |||
| public GuildPermissions GuildPermissions => new GuildPermissions(Permissions.ResolveGuild(Guild, this)); | |||
| public IReadOnlyCollection<ulong> RoleIds => _roleIds; | |||
| public SocketVoiceState? VoiceState => Guild.GetVoiceState(Id); | |||