Merge branch 'dev' into feature/reactions

This commit is contained in:
RogueException
2016-11-27 00:30:46 -04:00
112 changed files with 2808 additions and 1191 deletions

View File

@@ -6,6 +6,7 @@ using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Model = Discord.API.Channel;
using UserModel = Discord.API.User;
namespace Discord.Rest
{
@@ -43,13 +44,13 @@ namespace Discord.Rest
}
//Invites
public static async Task<IReadOnlyCollection<RestInviteMetadata>> GetInvitesAsync(IChannel channel, BaseDiscordClient client,
public static async Task<IReadOnlyCollection<RestInviteMetadata>> GetInvitesAsync(IGuildChannel channel, BaseDiscordClient client,
RequestOptions options)
{
var models = await client.ApiClient.GetChannelInvitesAsync(channel.Id, options).ConfigureAwait(false);
return models.Select(x => RestInviteMetadata.Create(client, null, channel, x)).ToImmutableArray();
}
public static async Task<RestInviteMetadata> CreateInviteAsync(IChannel channel, BaseDiscordClient client,
public static async Task<RestInviteMetadata> CreateInviteAsync(IGuildChannel channel, BaseDiscordClient client,
int? maxAge, int? maxUses, bool isTemporary, RequestOptions options)
{
var args = new CreateChannelInviteParams { IsTemporary = isTemporary };
@@ -62,18 +63,24 @@ namespace Discord.Rest
}
//Messages
public static async Task<RestMessage> GetMessageAsync(IChannel channel, BaseDiscordClient client,
ulong id, IGuild guild, RequestOptions options)
public static async Task<RestMessage> GetMessageAsync(IMessageChannel channel, BaseDiscordClient client,
ulong id, RequestOptions options)
{
var guildId = (channel as IGuildChannel)?.GuildId;
var guild = guildId != null ? await (client as IDiscordClient).GetGuildAsync(guildId.Value, CacheMode.CacheOnly).ConfigureAwait(false) : null;
var model = await client.ApiClient.GetChannelMessageAsync(channel.Id, id, options).ConfigureAwait(false);
return RestMessage.Create(client, guild, model);
var author = GetAuthor(client, guild, model.Author.Value);
return RestMessage.Create(client, channel, author, model);
}
public static IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(IChannel channel, BaseDiscordClient client,
ulong? fromMessageId, Direction dir, int limit, IGuild guild, RequestOptions options)
public static IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(IMessageChannel channel, BaseDiscordClient client,
ulong? fromMessageId, Direction dir, int limit, RequestOptions options)
{
if (dir == Direction.Around)
throw new NotImplementedException(); //TODO: Impl
var guildId = (channel as IGuildChannel)?.GuildId;
var guild = guildId != null ? (client as IDiscordClient).GetGuildAsync(guildId.Value, CacheMode.CacheOnly).Result : null;
return new PagedAsyncEnumerable<RestMessage>(
DiscordConfig.MaxMessagesPerBatch,
async (info, ct) =>
@@ -85,8 +92,15 @@ namespace Discord.Rest
};
if (info.Position != null)
args.RelativeMessageId = info.Position.Value;
var models = await client.ApiClient.GetChannelMessagesAsync(channel.Id, args, options).ConfigureAwait(false);
return models.Select(x => RestMessage.Create(client, guild, x)).ToImmutableArray();
var builder = ImmutableArray.CreateBuilder<RestMessage>();
foreach (var model in models)
{
var author = GetAuthor(client, guild, model.Author.Value);
builder.Add(RestMessage.Create(client, channel, author, model));
}
return builder.ToImmutable();
},
nextPage: (info, lastPage) =>
{
@@ -102,37 +116,45 @@ namespace Discord.Rest
count: limit
);
}
public static async Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(IChannel channel, BaseDiscordClient client,
IGuild guild, RequestOptions options)
public static async Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(IMessageChannel channel, BaseDiscordClient client,
RequestOptions options)
{
var guildId = (channel as IGuildChannel)?.GuildId;
var guild = guildId != null ? await (client as IDiscordClient).GetGuildAsync(guildId.Value, CacheMode.CacheOnly).ConfigureAwait(false) : null;
var models = await client.ApiClient.GetPinsAsync(channel.Id, options).ConfigureAwait(false);
return models.Select(x => RestMessage.Create(client, guild, x)).ToImmutableArray();
var builder = ImmutableArray.CreateBuilder<RestMessage>();
foreach (var model in models)
{
var author = GetAuthor(client, guild, model.Author.Value);
builder.Add(RestMessage.Create(client, channel, author, model));
}
return builder.ToImmutable();
}
public static async Task<RestUserMessage> SendMessageAsync(IChannel channel, BaseDiscordClient client,
string text, bool isTTS, IGuild guild, RequestOptions options)
public static async Task<RestUserMessage> SendMessageAsync(IMessageChannel channel, BaseDiscordClient client,
string text, bool isTTS, EmbedBuilder embed, RequestOptions options)
{
var args = new CreateMessageParams(text) { IsTTS = isTTS };
var args = new CreateMessageParams(text) { IsTTS = isTTS, Embed = embed?.Build() };
var model = await client.ApiClient.CreateMessageAsync(channel.Id, args, options).ConfigureAwait(false);
return RestUserMessage.Create(client, guild, model);
return RestUserMessage.Create(client, channel, client.CurrentUser, model);
}
public static async Task<RestUserMessage> SendFileAsync(IChannel channel, BaseDiscordClient client,
string filePath, string text, bool isTTS, IGuild guild, RequestOptions options)
public static async Task<RestUserMessage> SendFileAsync(IMessageChannel channel, BaseDiscordClient client,
string filePath, string text, bool isTTS, RequestOptions options)
{
string filename = Path.GetFileName(filePath);
using (var file = File.OpenRead(filePath))
return await SendFileAsync(channel, client, file, filename, text, isTTS, guild, options).ConfigureAwait(false);
return await SendFileAsync(channel, client, file, filename, text, isTTS, options).ConfigureAwait(false);
}
public static async Task<RestUserMessage> SendFileAsync(IChannel channel, BaseDiscordClient client,
Stream stream, string filename, string text, bool isTTS, IGuild guild, RequestOptions options)
public static async Task<RestUserMessage> SendFileAsync(IMessageChannel channel, BaseDiscordClient client,
Stream stream, string filename, string text, bool isTTS, RequestOptions options)
{
var args = new UploadFileParams(stream) { Filename = filename, Content = text, IsTTS = isTTS };
var model = await client.ApiClient.UploadFileAsync(channel.Id, args, options).ConfigureAwait(false);
return RestUserMessage.Create(client, guild, model);
return RestUserMessage.Create(client, channel, client.CurrentUser, model);
}
public static async Task DeleteMessagesAsync(IChannel channel, BaseDiscordClient client,
public static async Task DeleteMessagesAsync(IMessageChannel channel, BaseDiscordClient client,
IEnumerable<IMessage> messages, RequestOptions options)
{
var args = new DeleteMessagesParams(messages.Select(x => x.Id).ToArray());
@@ -216,5 +238,16 @@ namespace Discord.Rest
public static IDisposable EnterTypingState(IMessageChannel channel, BaseDiscordClient client,
RequestOptions options)
=> new TypingNotifier(client, channel, options);
//Helpers
private static IUser GetAuthor(BaseDiscordClient client, IGuild guild, UserModel model)
{
IUser author = null;
if (guild != null)
author = guild.GetUserAsync(model.Id, CacheMode.CacheOnly).Result;
if (author == null)
author = RestUser.Create(client, model);
return author;
}
}
}

View File

@@ -7,7 +7,7 @@ namespace Discord.Rest
public interface IRestMessageChannel : IMessageChannel
{
/// <summary> Sends a message to this message channel. </summary>
new Task<RestUserMessage> SendMessageAsync(string text, bool isTTS = false, RequestOptions options = null);
new Task<RestUserMessage> SendMessageAsync(string text, bool isTTS = false, EmbedBuilder embed = null, RequestOptions options = null);
/// <summary> Sends a file to this text channel, with an optional caption. </summary>
new Task<RestUserMessage> SendFileAsync(string filePath, string text = null, bool isTTS = false, RequestOptions options = null);
/// <summary> Sends a file to this text channel, with an optional caption. </summary>

View File

@@ -53,22 +53,22 @@ namespace Discord.Rest
}
public Task<RestMessage> GetMessageAsync(ulong id, RequestOptions options = null)
=> ChannelHelper.GetMessageAsync(this, Discord, id, null, options);
=> ChannelHelper.GetMessageAsync(this, Discord, id, options);
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
=> ChannelHelper.GetMessagesAsync(this, Discord, null, Direction.Before, limit, null, options);
=> ChannelHelper.GetMessagesAsync(this, Discord, null, Direction.Before, limit, options);
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessageId, dir, limit, null, options);
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessageId, dir, limit, options);
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessage.Id, dir, limit, null, options);
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessage.Id, dir, limit, options);
public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(RequestOptions options = null)
=> ChannelHelper.GetPinnedMessagesAsync(this, Discord, null, options);
=> ChannelHelper.GetPinnedMessagesAsync(this, Discord, options);
public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS = false, RequestOptions options = null)
=> ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, null, options);
public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS = false, EmbedBuilder embed = null, RequestOptions options = null)
=> ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, embed, options);
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, RequestOptions options = null)
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, null, options);
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, options);
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, RequestOptions options = null)
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, null, options);
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, options);
public Task DeleteMessagesAsync(IEnumerable<IMessage> messages, RequestOptions options = null)
=> ChannelHelper.DeleteMessagesAsync(this, Discord, messages, options);
@@ -126,8 +126,8 @@ namespace Discord.Rest
=> await SendFileAsync(filePath, text, isTTS, options).ConfigureAwait(false);
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, RequestOptions options)
=> await SendFileAsync(stream, filename, text, isTTS, options).ConfigureAwait(false);
async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, RequestOptions options)
=> await SendMessageAsync(text, isTTS, options).ConfigureAwait(false);
async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, EmbedBuilder embed, RequestOptions options)
=> await SendMessageAsync(text, isTTS, embed, options).ConfigureAwait(false);
IDisposable IMessageChannel.EnterTypingState(RequestOptions options)
=> EnterTypingState(options);

View File

@@ -66,22 +66,22 @@ namespace Discord.Rest
}
public Task<RestMessage> GetMessageAsync(ulong id, RequestOptions options = null)
=> ChannelHelper.GetMessageAsync(this, Discord, id, null, options);
=> ChannelHelper.GetMessageAsync(this, Discord, id, options);
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
=> ChannelHelper.GetMessagesAsync(this, Discord, null, Direction.Before, limit, null, options);
=> ChannelHelper.GetMessagesAsync(this, Discord, null, Direction.Before, limit, options);
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessageId, dir, limit, null, options);
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessageId, dir, limit, options);
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessage.Id, dir, limit, null, options);
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessage.Id, dir, limit, options);
public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(RequestOptions options = null)
=> ChannelHelper.GetPinnedMessagesAsync(this, Discord, null, options);
=> ChannelHelper.GetPinnedMessagesAsync(this, Discord, options);
public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS = false, RequestOptions options = null)
=> ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, null, options);
public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS = false, EmbedBuilder embed = null, RequestOptions options = null)
=> ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, embed, options);
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, RequestOptions options = null)
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, null, options);
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, options);
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, RequestOptions options = null)
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, null, options);
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, options);
public Task DeleteMessagesAsync(IEnumerable<IMessage> messages, RequestOptions options = null)
=> ChannelHelper.DeleteMessagesAsync(this, Discord, messages, options);
@@ -136,8 +136,8 @@ namespace Discord.Rest
=> await SendFileAsync(filePath, text, isTTS, options).ConfigureAwait(false);
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, RequestOptions options)
=> await SendFileAsync(stream, filename, text, isTTS, options).ConfigureAwait(false);
async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, RequestOptions options)
=> await SendMessageAsync(text, isTTS, options).ConfigureAwait(false);
async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, EmbedBuilder embed, RequestOptions options)
=> await SendMessageAsync(text, isTTS, embed, options).ConfigureAwait(false);
IDisposable IMessageChannel.EnterTypingState(RequestOptions options)
=> EnterTypingState(options);

View File

@@ -45,22 +45,22 @@ namespace Discord.Rest
=> ChannelHelper.GetUsersAsync(this, Guild, Discord, null, null, options);
public Task<RestMessage> GetMessageAsync(ulong id, RequestOptions options = null)
=> ChannelHelper.GetMessageAsync(this, Discord, id, null, options);
=> ChannelHelper.GetMessageAsync(this, Discord, id, options);
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
=> ChannelHelper.GetMessagesAsync(this, Discord, null, Direction.Before, limit, null, options);
=> ChannelHelper.GetMessagesAsync(this, Discord, null, Direction.Before, limit, options);
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessageId, dir, limit, null, options);
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessageId, dir, limit, options);
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessage.Id, dir, limit, null, options);
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessage.Id, dir, limit, options);
public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(RequestOptions options = null)
=> ChannelHelper.GetPinnedMessagesAsync(this, Discord, null, options);
=> ChannelHelper.GetPinnedMessagesAsync(this, Discord, options);
public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS = false, RequestOptions options = null)
=> ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, null, options);
public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS = false, EmbedBuilder embed = null, RequestOptions options = null)
=> ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, embed, options);
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, RequestOptions options = null)
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, null, options);
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, options);
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, RequestOptions options = null)
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, null, options);
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, options);
public Task DeleteMessagesAsync(IEnumerable<IMessage> messages, RequestOptions options = null)
=> ChannelHelper.DeleteMessagesAsync(this, Discord, messages, options);
@@ -108,8 +108,8 @@ namespace Discord.Rest
=> await SendFileAsync(filePath, text, isTTS, options).ConfigureAwait(false);
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, RequestOptions options)
=> await SendFileAsync(stream, filename, text, isTTS, options).ConfigureAwait(false);
async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, RequestOptions options)
=> await SendMessageAsync(text, isTTS, options).ConfigureAwait(false);
async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, EmbedBuilder embed, RequestOptions options)
=> await SendMessageAsync(text, isTTS, embed, options).ConfigureAwait(false);
IDisposable IMessageChannel.EnterTypingState(RequestOptions options)
=> EnterTypingState(options);

View File

@@ -23,22 +23,22 @@ namespace Discord.Rest
}
public Task<RestMessage> GetMessageAsync(ulong id, RequestOptions options = null)
=> ChannelHelper.GetMessageAsync(this, Discord, id, null, options);
=> ChannelHelper.GetMessageAsync(this, Discord, id, options);
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
=> ChannelHelper.GetMessagesAsync(this, Discord, null, Direction.Before, limit, null, options);
=> ChannelHelper.GetMessagesAsync(this, Discord, null, Direction.Before, limit, options);
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessageId, dir, limit, null, options);
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessageId, dir, limit, options);
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessage.Id, dir, limit, null, options);
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessage.Id, dir, limit, options);
public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(RequestOptions options = null)
=> ChannelHelper.GetPinnedMessagesAsync(this, Discord, null, options);
=> ChannelHelper.GetPinnedMessagesAsync(this, Discord, options);
public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS, RequestOptions options = null)
=> ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, null, options);
public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS, EmbedBuilder embed = null, RequestOptions options = null)
=> ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, embed, options);
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS, RequestOptions options = null)
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, null, options);
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, options);
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS, RequestOptions options = null)
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, null, options);
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, options);
public Task DeleteMessagesAsync(IEnumerable<IMessage> messages, RequestOptions options = null)
=> ChannelHelper.DeleteMessagesAsync(this, Discord, messages, options);
@@ -86,8 +86,8 @@ namespace Discord.Rest
=> await SendFileAsync(filePath, text, isTTS, options);
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, RequestOptions options)
=> await SendFileAsync(stream, filename, text, isTTS, options);
async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, RequestOptions options)
=> await SendMessageAsync(text, isTTS, options);
async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, EmbedBuilder embed, RequestOptions options)
=> await SendMessageAsync(text, isTTS, embed, options);
IDisposable IMessageChannel.EnterTypingState(RequestOptions options)
=> EnterTypingState(options);

View File

@@ -142,7 +142,7 @@ namespace Discord.Rest
if (name == null) throw new ArgumentNullException(nameof(name));
var model = await client.ApiClient.CreateGuildRoleAsync(guild.Id, options).ConfigureAwait(false);
var role = RestRole.Create(client, model);
var role = RestRole.Create(client, guild, model);
await role.ModifyAsync(x =>
{

View File

@@ -87,7 +87,7 @@ namespace Discord.Rest
if (model.Roles != null)
{
for (int i = 0; i < model.Roles.Length; i++)
roles[model.Roles[i].Id] = RestRole.Create(Discord, model.Roles[i]);
roles[model.Roles[i].Id] = RestRole.Create(Discord, this, model.Roles[i]);
}
_roles = roles.ToImmutable();

View File

@@ -1,4 +1,7 @@
using System.Diagnostics;
using System;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using Model = Discord.API.Embed;
namespace Discord
@@ -10,23 +13,55 @@ namespace Discord
public string Url { get; }
public string Title { get; }
public string Type { get; }
public DateTimeOffset? Timestamp { get; }
public Color? Color { get; }
public EmbedImage? Image { get; }
public EmbedVideo? Video { get; }
public EmbedAuthor? Author { get; }
public EmbedFooter? Footer { get; }
public EmbedProvider? Provider { get; }
public EmbedThumbnail? Thumbnail { get; }
public ImmutableArray<EmbedField> Fields { get; }
internal Embed(string type, string title, string description, string url, EmbedProvider? provider, EmbedThumbnail? thumbnail)
internal Embed(string type,
string title,
string description,
string url,
DateTimeOffset? timestamp,
Color? color,
EmbedImage? image,
EmbedVideo? video,
EmbedAuthor? author,
EmbedFooter? footer,
EmbedProvider? provider,
EmbedThumbnail? thumbnail,
ImmutableArray<EmbedField> fields)
{
Type = type;
Title = title;
Description = description;
Url = url;
Color = color;
Timestamp = timestamp;
Image = image;
Video = video;
Author = author;
Footer = footer;
Provider = provider;
Thumbnail = thumbnail;
Fields = fields;
}
internal static Embed Create(Model model)
{
return new Embed(model.Type, model.Title, model.Description, model.Url,
return new Embed(model.Type, model.Title, model.Description, model.Url,model.Timestamp,
model.Color.HasValue ? new Color(model.Color.Value) : (Color?)null,
model.Image.IsSpecified ? EmbedImage.Create(model.Image.Value) : (EmbedImage?)null,
model.Video.IsSpecified ? EmbedVideo.Create(model.Video.Value) : (EmbedVideo?)null,
model.Author.IsSpecified ? EmbedAuthor.Create(model.Author.Value) : (EmbedAuthor?)null,
model.Footer.IsSpecified ? EmbedFooter.Create(model.Footer.Value) : (EmbedFooter?)null,
model.Provider.IsSpecified ? EmbedProvider.Create(model.Provider.Value) : (EmbedProvider?)null,
model.Thumbnail.IsSpecified ? EmbedThumbnail.Create(model.Thumbnail.Value) : (EmbedThumbnail?)null);
model.Thumbnail.IsSpecified ? EmbedThumbnail.Create(model.Thumbnail.Value) : (EmbedThumbnail?)null,
model.Fields.IsSpecified ? model.Fields.Value.Select(EmbedField.Create).ToImmutableArray() : ImmutableArray.Create<EmbedField>());
}
public override string ToString() => Title;

View File

@@ -15,7 +15,12 @@ namespace Discord.Rest
{
var args = new ModifyMessageParams();
func(args);
return await client.ApiClient.ModifyMessageAsync(msg.Channel.Id, msg.Id, args, options).ConfigureAwait(false);
var apiArgs = new API.Rest.ModifyMessageParams
{
Content = args.Content,
Embed = args.Embed.IsSpecified ? args.Embed.Value.Build() : Optional.Create<API.Embed>()
};
return await client.ApiClient.ModifyMessageAsync(msg.Channel.Id, msg.Id, apiArgs, options).ConfigureAwait(false);
}
public static async Task DeleteAsync(IMessage msg, BaseDiscordClient client,
RequestOptions options)

View File

@@ -9,11 +9,10 @@ namespace Discord.Rest
{
public abstract class RestMessage : RestEntity<ulong>, IMessage, IUpdateable
{
internal readonly IGuild _guild;
private long _timestampTicks;
public IMessageChannel Channel { get; }
public RestUser Author { get; }
public IUser Author { get; }
public string Content { get; private set; }
@@ -32,19 +31,18 @@ namespace Discord.Rest
public DateTimeOffset Timestamp => DateTimeUtils.FromTicks(_timestampTicks);
internal RestMessage(BaseDiscordClient discord, ulong id, IMessageChannel channel, RestUser author, IGuild guild)
internal RestMessage(BaseDiscordClient discord, ulong id, IMessageChannel channel, IUser author)
: base(discord, id)
{
Channel = channel;
Author = author;
_guild = guild;
}
internal static RestMessage Create(BaseDiscordClient discord, IGuild guild, Model model)
internal static RestMessage Create(BaseDiscordClient discord, IMessageChannel channel, IUser author, Model model)
{
if (model.Type == MessageType.Default)
return RestUserMessage.Create(discord, guild, model);
return RestUserMessage.Create(discord, channel, author, model);
else
return RestSystemMessage.Create(discord, guild, model);
return RestSystemMessage.Create(discord, channel, author, model);
}
internal virtual void Update(Model model)
{

View File

@@ -8,15 +8,13 @@ namespace Discord.Rest
{
public MessageType Type { get; private set; }
internal RestSystemMessage(BaseDiscordClient discord, ulong id, IMessageChannel channel, RestUser author, IGuild guild)
: base(discord, id, channel, author, guild)
internal RestSystemMessage(BaseDiscordClient discord, ulong id, IMessageChannel channel, IUser author)
: base(discord, id, channel, author)
{
}
internal new static RestSystemMessage Create(BaseDiscordClient discord, IGuild guild, Model model)
internal new static RestSystemMessage Create(BaseDiscordClient discord, IMessageChannel channel, IUser author, Model model)
{
var entity = new RestSystemMessage(discord, model.Id,
RestVirtualMessageChannel.Create(discord, model.ChannelId),
RestUser.Create(discord, model.Author.Value), guild);
var entity = new RestSystemMessage(discord, model.Id, channel, author);
entity.Update(model);
return entity;
}

View File

@@ -3,7 +3,6 @@ using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Model = Discord.API.Message;
@@ -32,15 +31,13 @@ namespace Discord.Rest
public override IReadOnlyCollection<ITag> Tags => _tags;
public IReadOnlyDictionary<Emoji, int> Reactions => _reactions.ToDictionary(x => x.Emoji, x => x.Count);
internal RestUserMessage(BaseDiscordClient discord, ulong id, IMessageChannel channel, RestUser author, IGuild guild)
: base(discord, id, channel, author, guild)
internal RestUserMessage(BaseDiscordClient discord, ulong id, IMessageChannel channel, IUser author)
: base(discord, id, channel, author)
{
}
internal new static RestUserMessage Create(BaseDiscordClient discord, IGuild guild, Model model)
internal new static RestUserMessage Create(BaseDiscordClient discord, IMessageChannel channel, IUser author, Model model)
{
var entity = new RestUserMessage(discord, model.Id,
RestVirtualMessageChannel.Create(discord, model.ChannelId),
RestUser.Create(discord, model.Author.Value), guild);
var entity = new RestUserMessage(discord, model.Id, channel, author);
entity.Update(model);
return entity;
}
@@ -122,7 +119,9 @@ namespace Discord.Rest
if (model.Content.IsSpecified)
{
var text = model.Content.Value;
_tags = MessageHelper.ParseTags(text, null, _guild, mentions);
var guildId = (Channel as IGuildChannel)?.GuildId;
var guild = guildId != null ? (Discord as IDiscordClient).GetGuildAsync(guildId.Value, CacheMode.CacheOnly).Result : null;
_tags = MessageHelper.ParseTags(text, null, guild, mentions);
model.Content = text;
}
}
@@ -155,9 +154,12 @@ namespace Discord.Rest
public Task UnpinAsync(RequestOptions options)
=> MessageHelper.UnpinAsync(this, Discord, options);
public string Resolve(int startIndex, TagHandling userHandling = TagHandling.Name, TagHandling channelHandling = TagHandling.Name,
TagHandling roleHandling = TagHandling.Name, TagHandling everyoneHandling = TagHandling.Ignore, TagHandling emojiHandling = TagHandling.Name)
=> MentionUtils.Resolve(this, startIndex, userHandling, channelHandling, roleHandling, everyoneHandling, emojiHandling);
public string Resolve(TagHandling userHandling = TagHandling.Name, TagHandling channelHandling = TagHandling.Name,
TagHandling roleHandling = TagHandling.Name, TagHandling everyoneHandling = TagHandling.Ignore, TagHandling emojiHandling = TagHandling.Name)
=> MentionUtils.Resolve(this, userHandling, channelHandling, roleHandling, everyoneHandling, emojiHandling);
=> MentionUtils.Resolve(this, 0, userHandling, channelHandling, roleHandling, everyoneHandling, emojiHandling);
private string DebuggerDisplay => $"{Author}: {Content} ({Id}{(Attachments.Count > 0 ? $", {Attachments.Count} Attachments" : "")})";
}

View File

@@ -9,7 +9,7 @@ namespace Discord.Rest
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class RestRole : RestEntity<ulong>, IRole
{
public RestGuild Guild { get; }
internal IGuild Guild { get; }
public Color Color { get; private set; }
public bool IsHoisted { get; private set; }
public bool IsManaged { get; private set; }
@@ -22,13 +22,14 @@ namespace Discord.Rest
public bool IsEveryone => Id == Guild.Id;
public string Mention => MentionUtils.MentionRole(Id);
internal RestRole(BaseDiscordClient discord, ulong id)
internal RestRole(BaseDiscordClient discord, IGuild guild, ulong id)
: base(discord, id)
{
Guild = guild;
}
internal static RestRole Create(BaseDiscordClient discord, Model model)
internal static RestRole Create(BaseDiscordClient discord, IGuild guild, Model model)
{
var entity = new RestRole(discord, model.Id);
var entity = new RestRole(discord, guild, model.Id);
entity.Update(model);
return entity;
}
@@ -51,10 +52,20 @@ namespace Discord.Rest
public Task DeleteAsync(RequestOptions options = null)
=> RoleHelper.DeleteAsync(this, Discord, options);
public int CompareTo(IRole role) => this.Compare(role);
public override string ToString() => Name;
private string DebuggerDisplay => $"{Name} ({Id})";
//IRole
IGuild IRole.Guild => Guild;
IGuild IRole.Guild
{
get
{
if (Guild != null)
return Guild;
throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object.");
}
}
}
}