Browse Source

Merge pull request #375 from LassieME/embed/fix-urls-add-timestamp

Adds Timestamps to embeds, and removes Image and Thumbnail-Builders
tags/1.0-rc
RogueException GitHub 8 years ago
parent
commit
08d85cd2d9
5 changed files with 47 additions and 81 deletions
  1. +3
    -0
      src/Discord.Net.Core/API/Common/Embed.cs
  2. +1
    -1
      src/Discord.Net.Core/API/DiscordRestApiClient.cs
  3. +30
    -72
      src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs
  4. +3
    -1
      src/Discord.Net.Core/Entities/Messages/IEmbed.cs
  5. +10
    -7
      src/Discord.Net.Rest/Entities/Messages/Embed.cs

+ 3
- 0
src/Discord.Net.Core/API/Common/Embed.cs View File

@@ -1,4 +1,5 @@
#pragma warning disable CS1591 #pragma warning disable CS1591
using System;
using Newtonsoft.Json; using Newtonsoft.Json;


namespace Discord.API namespace Discord.API
@@ -15,6 +16,8 @@ namespace Discord.API
public string Url { get; set; } public string Url { get; set; }
[JsonProperty("color")] [JsonProperty("color")]
public uint? Color { get; set; } public uint? Color { get; set; }
[JsonProperty("timestamp")]
public DateTimeOffset? Timestamp { get; set; }
[JsonProperty("author")] [JsonProperty("author")]
public Optional<EmbedAuthor> Author { get; set; } public Optional<EmbedAuthor> Author { get; set; }
[JsonProperty("footer")] [JsonProperty("footer")]


+ 1
- 1
src/Discord.Net.Core/API/DiscordRestApiClient.cs View File

@@ -49,7 +49,7 @@ namespace Discord.API
{ {
_restClientProvider = restClientProvider; _restClientProvider = restClientProvider;
_userAgent = userAgent; _userAgent = userAgent;
_serializer = serializer ?? new JsonSerializer { ContractResolver = new DiscordContractResolver() };
_serializer = serializer ?? new JsonSerializer { DateFormatString = "yyyy-MM-ddTHH:mm:ssZ", ContractResolver = new DiscordContractResolver() };
RequestQueue = requestQueue; RequestQueue = requestQueue;
FetchCurrentUser = true; FetchCurrentUser = true;




+ 30
- 72
src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs View File

@@ -20,14 +20,15 @@ namespace Discord
_fields = new List<Field>(); _fields = new List<Field>();
} }


public string Title { get { return _model.Title; } set { _model.Title = value; } }
public string Title { get { return _model.Title; } set { _model.Title = value; } }
public string Description { get { return _model.Description; } set { _model.Description = value; } } public string Description { get { return _model.Description; } set { _model.Description = value; } }
public string Url { get { return _model.Url; } set { _model.Url = value; } } public string Url { get { return _model.Url; } set { _model.Url = value; } }
public string ThumbnailUrl { get; set; }
public string ImageUrl { get; set; }
public DateTimeOffset? Timestamp { get; set; }
public Color? Color { get { return _model.Color.HasValue ? new Color(_model.Color.Value) : (Color?)null; } set { _model.Color = value?.RawValue; } } public Color? Color { get { return _model.Color.HasValue ? new Color(_model.Color.Value) : (Color?)null; } set { _model.Color = value?.RawValue; } }
public EmbedAuthorBuilder Author { get; set; } public EmbedAuthorBuilder Author { get; set; }
public EmbedFooterBuilder Footer { get; set; } public EmbedFooterBuilder Footer { get; set; }
public EmbedThumbnailBuilder Thumbnail { get; set; }
public EmbedImageBuilder Image { get; set; }


public EmbedBuilder WithTitle(string title) public EmbedBuilder WithTitle(string title)
{ {
@@ -44,6 +45,26 @@ namespace Discord
Url = url; Url = url;
return this; return this;
} }
public EmbedBuilder WithThumbnailUrl(string thumbnailUrl)
{
ThumbnailUrl = thumbnailUrl;
return this;
}
public EmbedBuilder WithImageUrl(string imageUrl)
{
ImageUrl = ImageUrl;
return this;
}
public EmbedBuilder WithCurrentTimestamp()
{
Timestamp = DateTimeOffset.UtcNow;
return this;
}
public EmbedBuilder WithTimestamp(DateTimeOffset dateTimeOffset)
{
Timestamp = dateTimeOffset;
return this;
}
public EmbedBuilder WithColor(Color color) public EmbedBuilder WithColor(Color color)
{ {
Color = color; Color = color;
@@ -74,30 +95,6 @@ namespace Discord
Footer = footer; Footer = footer;
return this; return this;
} }
public EmbedBuilder WithThumbnail(EmbedThumbnailBuilder thumbnail)
{
Thumbnail = thumbnail;
return this;
}
public EmbedBuilder WithThumbnail(Action<EmbedThumbnailBuilder> action)
{
var thumbnail = new EmbedThumbnailBuilder();
action(thumbnail);
Thumbnail = thumbnail;
return this;
}
public EmbedBuilder WithImage(EmbedImageBuilder image)
{
Image = image;
return this;
}
public EmbedBuilder WithImage(Action<EmbedImageBuilder> action)
{
var image = new EmbedImageBuilder();
action(image);
Image = image;
return this;
}


public EmbedBuilder AddField(Action<EmbedFieldBuilder> action) public EmbedBuilder AddField(Action<EmbedFieldBuilder> action)
{ {
@@ -111,8 +108,9 @@ namespace Discord
{ {
_model.Author = Author?.ToModel(); _model.Author = Author?.ToModel();
_model.Footer = Footer?.ToModel(); _model.Footer = Footer?.ToModel();
_model.Thumbnail = Thumbnail?.ToModel();
_model.Image = Image?.ToModel();
_model.Timestamp = Timestamp?.ToUniversalTime();
_model.Thumbnail = ThumbnailUrl != null ? new Thumbnail { Url = ThumbnailUrl } : null;
_model.Image = ImageUrl != null ? new Image { Url = ImageUrl } : null;
_model.Fields = _fields.ToArray(); _model.Fields = _fields.ToArray();
return _model; return _model;
} }
@@ -120,7 +118,7 @@ namespace Discord


public class EmbedFieldBuilder public class EmbedFieldBuilder
{ {
private Field _model;
private readonly Field _model;


public string Name { get { return _model.Name; } set { _model.Name = value; } } public string Name { get { return _model.Name; } set { _model.Name = value; } }
public string Value { get { return _model.Value; } set { _model.Value = value; } } public string Value { get { return _model.Value; } set { _model.Value = value; } }
@@ -152,7 +150,7 @@ namespace Discord


public class EmbedAuthorBuilder public class EmbedAuthorBuilder
{ {
private Author _model;
private readonly Author _model;


public string Name { get { return _model.Name; } set { _model.Name = value; } } public string Name { get { return _model.Name; } set { _model.Name = value; } }
public string Url { get { return _model.Url; } set { _model.Url = value; } } public string Url { get { return _model.Url; } set { _model.Url = value; } }
@@ -184,7 +182,7 @@ namespace Discord


public class EmbedFooterBuilder public class EmbedFooterBuilder
{ {
private Footer _model;
private readonly Footer _model;


public string Text { get { return _model.Text; } set { _model.Text = value; } } public string Text { get { return _model.Text; } set { _model.Text = value; } }
public string IconUrl { get { return _model.IconUrl; } set { _model.IconUrl = value; } } public string IconUrl { get { return _model.IconUrl; } set { _model.IconUrl = value; } }
@@ -207,44 +205,4 @@ namespace Discord


internal Footer ToModel() => _model; internal Footer ToModel() => _model;
} }

public class EmbedThumbnailBuilder
{
private Thumbnail _model;

public string Url { get { return _model.Url; } set { _model.Url = value; } }

public EmbedThumbnailBuilder()
{
_model = new Thumbnail();
}

public EmbedThumbnailBuilder WithUrl(string url)
{
Url = url;
return this;
}

internal Thumbnail ToModel() => _model;
}

public class EmbedImageBuilder
{
private Image _model;

public string Url { get { return _model.Url; } set { _model.Url = value; } }

public EmbedImageBuilder()
{
_model = new Image();
}

public EmbedImageBuilder WithUrl(string url)
{
Url = url;
return this;
}

internal Image ToModel() => _model;
}
} }

+ 3
- 1
src/Discord.Net.Core/Entities/Messages/IEmbed.cs View File

@@ -1,4 +1,5 @@
using System.Collections.Immutable;
using System;
using System.Collections.Immutable;


namespace Discord namespace Discord
{ {
@@ -8,6 +9,7 @@ namespace Discord
string Type { get; } string Type { get; }
string Title { get; } string Title { get; }
string Description { get; } string Description { get; }
DateTimeOffset? Timestamp { get; }
Color? Color { get; } Color? Color { get; }
EmbedImage? Image { get; } EmbedImage? Image { get; }
EmbedVideo? Video { get; } EmbedVideo? Video { get; }


+ 10
- 7
src/Discord.Net.Rest/Entities/Messages/Embed.cs View File

@@ -13,6 +13,7 @@ namespace Discord
public string Url { get; } public string Url { get; }
public string Title { get; } public string Title { get; }
public string Type { get; } public string Type { get; }
public DateTimeOffset? Timestamp { get; }
public Color? Color { get; } public Color? Color { get; }
public EmbedImage? Image { get; } public EmbedImage? Image { get; }
public EmbedVideo? Video { get; } public EmbedVideo? Video { get; }
@@ -23,10 +24,11 @@ namespace Discord
public ImmutableArray<EmbedField> Fields { get; } public ImmutableArray<EmbedField> Fields { get; }


internal Embed(string type, internal Embed(string type,
string title,
string description,
string url,
Color? color,
string title,
string description,
string url,
DateTimeOffset? timestamp,
Color? color,
EmbedImage? image, EmbedImage? image,
EmbedVideo? video, EmbedVideo? video,
EmbedAuthor? author, EmbedAuthor? author,
@@ -40,6 +42,7 @@ namespace Discord
Description = description; Description = description;
Url = url; Url = url;
Color = color; Color = color;
Timestamp = timestamp;
Image = image; Image = image;
Video = video; Video = video;
Author = author; Author = author;
@@ -50,15 +53,15 @@ namespace Discord
} }
internal static Embed Create(Model model) internal static Embed Create(Model model)
{ {
return new Embed(model.Type, model.Title, model.Description, model.Url,
model.Color.HasValue ? new Color(model.Color.Value) : (Color?)null,
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.Image.IsSpecified ? EmbedImage.Create(model.Image.Value) : (EmbedImage?)null,
model.Video.IsSpecified ? EmbedVideo.Create(model.Video.Value) : (EmbedVideo?)null, model.Video.IsSpecified ? EmbedVideo.Create(model.Video.Value) : (EmbedVideo?)null,
model.Author.IsSpecified ? EmbedAuthor.Create(model.Author.Value) : (EmbedAuthor?)null, model.Author.IsSpecified ? EmbedAuthor.Create(model.Author.Value) : (EmbedAuthor?)null,
model.Footer.IsSpecified ? EmbedFooter.Create(model.Footer.Value) : (EmbedFooter?)null, model.Footer.IsSpecified ? EmbedFooter.Create(model.Footer.Value) : (EmbedFooter?)null,
model.Provider.IsSpecified ? EmbedProvider.Create(model.Provider.Value) : (EmbedProvider?)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(x => EmbedField.Create(x)).ToImmutableArray() : ImmutableArray.Create<EmbedField>());
model.Fields.IsSpecified ? model.Fields.Value.Select(EmbedField.Create).ToImmutableArray() : ImmutableArray.Create<EmbedField>());
} }


public override string ToString() => Title; public override string ToString() => Title;


Loading…
Cancel
Save