Skip to content

Commit

Permalink
Add Image property to Guild Scheduled Events (#2151)
Browse files Browse the repository at this point in the history
* Add Image property to create and modify events

* Add CDN routes to get cover image

* Update banner names

* Update CDN.cs

* Update IGuildScheduledEvent.cs
  • Loading branch information
quinchs committed Mar 2, 2022
1 parent 202554f commit 1dc473c
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/Discord.Net.Core/CDN.cs
Expand Up @@ -208,6 +208,18 @@ public static string GetSpotifyDirectUrl(string trackId)
public static string GetStickerUrl(ulong stickerId, StickerFormatType format = StickerFormatType.Png)
=> $"{DiscordConfig.CDNUrl}stickers/{stickerId}.{FormatToExtension(format)}";

/// <summary>
/// Returns an events cover image url.
/// </summary>
/// <param name="guildId">The guild id that the event is in.</param>
/// <param name="eventId">The id of the event.</param>
/// <param name="assetId">The id of the cover image asset.</param>
/// <param name="format">The format of the image.</param>
/// <param name="size">The size of the image.</param>
/// <returns></returns>
public static string GetEventCoverImageUrl(ulong guildId, ulong eventId, string assetId, ImageFormat format = ImageFormat.Auto, ushort size = 1024)
=> $"{DiscordConfig.CDNUrl}guild-events/{guildId}/{eventId}/{assetId}.{FormatToExtension(format, assetId)}?size={size}";

private static string FormatToExtension(StickerFormatType format)
{
return format switch
Expand Down
Expand Up @@ -54,5 +54,10 @@ public class GuildScheduledEventsProperties
/// Gets or sets the status of the event.
/// </summary>
public Optional<GuildScheduledEventStatus> Status { get; set; }

/// <summary>
/// Gets or sets the banner image of the event.
/// </summary>
public Optional<Image?> CoverImage { get; set; }
}
}
2 changes: 2 additions & 0 deletions src/Discord.Net.Core/Entities/Guilds/IGuild.cs
Expand Up @@ -1105,6 +1105,7 @@ public interface IGuild : IDeletable, ISnowflakeEntity
/// </param>
/// <param name="speakers">A collection of speakers for the event.</param>
/// <param name="location">The location of the event; links are supported</param>
/// <param name="coverImage">The optional banner image for the event.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous create operation.
Expand All @@ -1118,6 +1119,7 @@ public interface IGuild : IDeletable, ISnowflakeEntity
DateTimeOffset? endTime = null,
ulong? channelId = null,
string location = null,
Image? coverImage = null,
RequestOptions options = null);

/// <summary>
Expand Down
13 changes: 13 additions & 0 deletions src/Discord.Net.Core/Entities/Guilds/IGuildScheduledEvent.cs
Expand Up @@ -39,6 +39,11 @@ public interface IGuildScheduledEvent : IEntity<ulong>
/// </remarks>
string Description { get; }

/// <summary>
/// Gets the banner asset id of the event.
/// </summary>
string CoverImageId { get; }

/// <summary>
/// Gets the start time of the event.
/// </summary>
Expand Down Expand Up @@ -80,6 +85,14 @@ public interface IGuildScheduledEvent : IEntity<ulong>
/// </summary>
int? UserCount { get; }

/// <summary>
/// Gets this events banner image url.
/// </summary>
/// <param name="format">The format to return.</param>
/// <param name="size">The size of the image to return in. This can be any power of two between 16 and 2048.
/// <returns>The cover images url.</returns>
string GetCoverImageUrl(ImageFormat format = ImageFormat.Auto, ushort size = 1024);

/// <summary>
/// Starts the event.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Discord.Net.Rest/API/Common/GuildScheduledEvent.cs
Expand Up @@ -39,5 +39,7 @@ internal class GuildScheduledEvent
public Optional<User> Creator { get; set; }
[JsonProperty("user_count")]
public Optional<int> UserCount { get; set; }
[JsonProperty("image")]
public string Image { get; set; }
}
}
Expand Up @@ -25,5 +25,7 @@ internal class CreateGuildScheduledEventParams
public Optional<string> Description { get; set; }
[JsonProperty("entity_type")]
public GuildScheduledEventType Type { get; set; }
[JsonProperty("image")]
public Optional<Image> Image { get; set; }
}
}
Expand Up @@ -27,5 +27,7 @@ internal class ModifyGuildScheduledEventParams
public Optional<GuildScheduledEventType> Type { get; set; }
[JsonProperty("status")]
public Optional<GuildScheduledEventStatus> Status { get; set; }
[JsonProperty("image")]
public Optional<Image?> Image { get; set; }
}
}
12 changes: 10 additions & 2 deletions src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
Expand Up @@ -799,7 +799,12 @@ public static async Task<IReadOnlyCollection<RestUser>> GetEventUsersAsync(BaseD
PrivacyLevel = args.PrivacyLevel,
StartTime = args.StartTime,
Status = args.Status,
Type = args.Type
Type = args.Type,
Image = args.CoverImage.IsSpecified
? args.CoverImage.Value.HasValue
? args.CoverImage.Value.Value.ToModel()
: null
: Optional<ImageModel?>.Unspecified
};

if(args.Location.IsSpecified)
Expand Down Expand Up @@ -839,6 +844,7 @@ public static async Task<IReadOnlyCollection<RestGuildEvent>> GetGuildEventsAsyn
DateTimeOffset? endTime = null,
ulong? channelId = null,
string location = null,
Image? bannerImage = null,
RequestOptions options = null)
{
if(location != null)
Expand All @@ -864,6 +870,7 @@ public static async Task<IReadOnlyCollection<RestGuildEvent>> GetGuildEventsAsyn
if (endTime != null && endTime <= startTime)
throw new ArgumentOutOfRangeException(nameof(endTime), $"{nameof(endTime)} cannot be before the start time");


var apiArgs = new CreateGuildScheduledEventParams()
{
ChannelId = channelId ?? Optional<ulong>.Unspecified,
Expand All @@ -872,7 +879,8 @@ public static async Task<IReadOnlyCollection<RestGuildEvent>> GetGuildEventsAsyn
Name = name,
PrivacyLevel = privacyLevel,
StartTime = startTime,
Type = type
Type = type,
Image = bannerImage.HasValue ? bannerImage.Value.ToModel() : Optional<ImageModel>.Unspecified
};

if(location != null)
Expand Down
8 changes: 5 additions & 3 deletions src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
Expand Up @@ -1167,6 +1167,7 @@ public Task<IReadOnlyCollection<RestGuildEvent>> GetEventsAsync(RequestOptions o
/// </param>
/// <param name="speakers">A collection of speakers for the event.</param>
/// <param name="location">The location of the event; links are supported</param>
/// <param name="coverImage">The optional banner image for the event.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous create operation.
Expand All @@ -1180,8 +1181,9 @@ public Task<IReadOnlyCollection<RestGuildEvent>> GetEventsAsync(RequestOptions o
DateTimeOffset? endTime = null,
ulong? channelId = null,
string location = null,
Image? coverImage = null,
RequestOptions options = null)
=> GuildHelper.CreateGuildEventAsync(Discord, this, name, privacyLevel, startTime, type, description, endTime, channelId, location, options);
=> GuildHelper.CreateGuildEventAsync(Discord, this, name, privacyLevel, startTime, type, description, endTime, channelId, location, coverImage, options);

#endregion

Expand All @@ -1198,8 +1200,8 @@ public Task<IReadOnlyCollection<RestGuildEvent>> GetEventsAsync(RequestOptions o
IReadOnlyCollection<ICustomSticker> IGuild.Stickers => Stickers;

/// <inheritdoc />
async Task<IGuildScheduledEvent> IGuild.CreateEventAsync(string name, DateTimeOffset startTime, GuildScheduledEventType type, GuildScheduledEventPrivacyLevel privacyLevel, string description, DateTimeOffset? endTime, ulong? channelId, string location, RequestOptions options)
=> await CreateEventAsync(name, startTime, type, privacyLevel, description, endTime, channelId, location, options).ConfigureAwait(false);
async Task<IGuildScheduledEvent> IGuild.CreateEventAsync(string name, DateTimeOffset startTime, GuildScheduledEventType type, GuildScheduledEventPrivacyLevel privacyLevel, string description, DateTimeOffset? endTime, ulong? channelId, string location, Image? coverImage, RequestOptions options)
=> await CreateEventAsync(name, startTime, type, privacyLevel, description, endTime, channelId, location, coverImage, options).ConfigureAwait(false);

/// <inheritdoc />
async Task<IGuildScheduledEvent> IGuild.GetEventAsync(ulong id, RequestOptions options)
Expand Down
8 changes: 8 additions & 0 deletions src/Discord.Net.Rest/Entities/Guilds/RestGuildEvent.cs
Expand Up @@ -28,6 +28,9 @@ public class RestGuildEvent : RestEntity<ulong>, IGuildScheduledEvent
/// <inheritdoc/>
public string Description { get; private set; }

/// <inheritdoc/>
public string CoverImageId { get; private set; }

/// <inheritdoc/>
public DateTimeOffset StartTime { get; private set; }

Expand Down Expand Up @@ -98,8 +101,13 @@ internal void Update(Model model)
EntityId = model.EntityId;
Location = model.EntityMetadata?.Location.GetValueOrDefault();
UserCount = model.UserCount.ToNullable();
CoverImageId = model.Image;
}

/// <inheritdoc/>
public string GetCoverImageUrl(ImageFormat format = ImageFormat.Auto, ushort size = 1024)
=> CDN.GetEventCoverImageUrl(Guild.Id, Id, CoverImageId, format, size);

/// <inheritdoc/>
public Task StartAsync(RequestOptions options = null)
=> ModifyAsync(x => x.Status = GuildScheduledEventStatus.Active);
Expand Down
8 changes: 5 additions & 3 deletions src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs
Expand Up @@ -1295,6 +1295,7 @@ public Task<IReadOnlyCollection<RestGuildEvent>> GetEventsAsync(RequestOptions o
/// </param>
/// <param name="speakers">A collection of speakers for the event.</param>
/// <param name="location">The location of the event; links are supported</param>
/// <param name="coverImage">The optional banner image for the event.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous create operation.
Expand All @@ -1308,6 +1309,7 @@ public Task<IReadOnlyCollection<RestGuildEvent>> GetEventsAsync(RequestOptions o
DateTimeOffset? endTime = null,
ulong? channelId = null,
string location = null,
Image? coverImage = null,
RequestOptions options = null)
{
// requirements taken from https://discord.com/developers/docs/resources/guild-scheduled-event#guild-scheduled-event-permissions-requirements
Expand All @@ -1324,7 +1326,7 @@ public Task<IReadOnlyCollection<RestGuildEvent>> GetEventsAsync(RequestOptions o
break;
}

return GuildHelper.CreateGuildEventAsync(Discord, this, name, privacyLevel, startTime, type, description, endTime, channelId, location, options);
return GuildHelper.CreateGuildEventAsync(Discord, this, name, privacyLevel, startTime, type, description, endTime, channelId, location, coverImage, options);
}


Expand Down Expand Up @@ -1803,8 +1805,8 @@ internal async Task RepopulateAudioStreamsAsync()
/// <inheritdoc />
IReadOnlyCollection<ICustomSticker> IGuild.Stickers => Stickers;
/// <inheritdoc />
async Task<IGuildScheduledEvent> IGuild.CreateEventAsync(string name, DateTimeOffset startTime, GuildScheduledEventType type, GuildScheduledEventPrivacyLevel privacyLevel, string description, DateTimeOffset? endTime, ulong? channelId, string location, RequestOptions options)
=> await CreateEventAsync(name, startTime, type, privacyLevel, description, endTime, channelId, location, options).ConfigureAwait(false);
async Task<IGuildScheduledEvent> IGuild.CreateEventAsync(string name, DateTimeOffset startTime, GuildScheduledEventType type, GuildScheduledEventPrivacyLevel privacyLevel, string description, DateTimeOffset? endTime, ulong? channelId, string location, Image? coverImage, RequestOptions options)
=> await CreateEventAsync(name, startTime, type, privacyLevel, description, endTime, channelId, location, coverImage, options).ConfigureAwait(false);
/// <inheritdoc />
async Task<IGuildScheduledEvent> IGuild.GetEventAsync(ulong id, RequestOptions options)
=> await GetEventAsync(id, options).ConfigureAwait(false);
Expand Down
8 changes: 8 additions & 0 deletions src/Discord.Net.WebSocket/Entities/Guilds/SocketGuildEvent.cs
Expand Up @@ -35,6 +35,9 @@ public class SocketGuildEvent : SocketEntity<ulong>, IGuildScheduledEvent
/// <inheritdoc/>
public string Description { get; private set; }

/// <inheritdoc/>
public string CoverImageId { get; private set; }

/// <inheritdoc/>
public DateTimeOffset StartTime { get; private set; }

Expand Down Expand Up @@ -109,8 +112,13 @@ internal void Update(Model model)
StartTime = model.ScheduledStartTime;
Status = model.Status;
UserCount = model.UserCount.ToNullable();
CoverImageId = model.Image;
}

/// <inheritdoc/>
public string GetCoverImageUrl(ImageFormat format = ImageFormat.Auto, ushort size = 1024)
=> CDN.GetEventCoverImageUrl(Guild.Id, Id, CoverImageId, format, size);

/// <inheritdoc/>
public Task DeleteAsync(RequestOptions options = null)
=> GuildHelper.DeleteEventAsync(Discord, this, options);
Expand Down

0 comments on commit 1dc473c

Please sign in to comment.