Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement basic support for GUILD_MEDIA channels. #109

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Disqord.Bot/Bot/Base/Setup/DiscordBotBase.Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ protected virtual ValueTask AddTypeParsers(DefaultTypeParserProvider typeParserP
typeParserProvider.AddParser(new GuildChannelTypeParser<IStageChannel>());
typeParserProvider.AddParser(new GuildChannelTypeParser<IThreadChannel>());
typeParserProvider.AddParser(new GuildChannelTypeParser<IForumChannel>());
typeParserProvider.AddParser(new GuildChannelTypeParser<IMediaChannel>());
typeParserProvider.AddParser(new MemberTypeParser());
typeParserProvider.AddParser(new RoleTypeParser());
return default;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,10 @@
using System;
using System.Collections.Generic;

namespace Disqord;

/// <summary>
/// Represents a guild forum channel.
/// </summary>
public interface IForumChannel : IThreadParentChannel, ITopicChannel, ISlowmodeChannel
public interface IForumChannel : IMediaChannel
{
/// <summary>
/// Gets the ID of the last thread created in this channel.
/// </summary>
Snowflake? LastThreadId { get; }

/// <summary>
/// Gets the available tags that can be applied to threads in this channel.
/// </summary>
IReadOnlyList<IForumTag> Tags { get; }

/// <summary>
/// Gets the emoji that can be reacted with by default to threads in this channel.
/// </summary>
IEmoji? DefaultReactionEmoji { get; }

/// <summary>
/// Gets the default slowmode applied to threads upon their creation in this channel.
/// </summary>
TimeSpan DefaultThreadSlowmode { get; }

/// <summary>
/// Gets the default sort order of posts in this channel.
/// </summary>
ForumSortOrder? DefaultSortOrder { get; }

/// <summary>
/// Gets the default layout of posts in this channel.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Disqord;

/// <summary>
/// Represents a tag that can be applied to threads in a forum channel.
/// Represents a tag that can be applied to threads in a forum or media channel.
/// </summary>
public interface IForumTag : IIdentifiableEntity, INamableEntity, IJsonUpdatable<ForumTagJsonModel>
{
Expand Down
35 changes: 35 additions & 0 deletions src/Disqord.Core/Entities/Core/Channels/Guild/IMediaChannel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;

namespace Disqord;

/// <summary>
/// Represents a guild media channel.
/// </summary>
public interface IMediaChannel : IThreadParentChannel, ITopicChannel, ISlowmodeChannel
{
/// <summary>
/// Gets the ID of the last thread created in this channel.
/// </summary>
Snowflake? LastThreadId { get; }

/// <summary>
/// Gets the available tags that can be applied to threads in this channel.
/// </summary>
IReadOnlyList<IForumTag> Tags { get; }

/// <summary>
/// Gets the emoji that can be reacted with by default to threads in this channel.
/// </summary>
IEmoji? DefaultReactionEmoji { get; }

/// <summary>
/// Gets the default slowmode applied to threads upon their creation in this channel.
/// </summary>
TimeSpan DefaultThreadSlowmode { get; }

/// <summary>
/// Gets the default sort order of posts in this channel.
/// </summary>
ForumSortOrder? DefaultSortOrder { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.ComponentModel;

namespace Disqord;

[EditorBrowsable(EditorBrowsableState.Never)]
public static class LocalForumTagExtensions
{
public static TForumTag WithId<TForumTag>(this TForumTag forumTag, Snowflake id)
where TForumTag : LocalForumTag
{
forumTag.Id = id;
return forumTag;
}

public static TForumTag WithName<TForumTag>(this TForumTag forumTag, string name)
where TForumTag : LocalForumTag
{
forumTag.Name = name;
return forumTag;
}

public static TForumTag WithIsModerated<TForumTag>(this TForumTag forumTag, bool isModerated = true)
where TForumTag : LocalForumTag
{
forumTag.IsModerated = isModerated;
return forumTag;
}

public static TForumTag WithEmoji<TForumTag>(this TForumTag forumTag, LocalEmoji emoji)
where TForumTag : LocalForumTag
{
forumTag.Emoji = emoji;
return forumTag;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
namespace Disqord;

/// <summary>
/// Represents a local forum tag to be created within a forum channel.
/// Represents a local tag to be created within a forum or media channel.
/// </summary>
/// <remarks>
/// Note that when updating a forum channel's tags,
/// Note that when updating a channel's tags,
/// you must provide the previous tags with their original values.
/// You can use <see cref="CreateFrom"/> for that purpose.
/// </remarks>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public override string ToString()
case ChannelType.Stage:
case ChannelType.Directory:
case ChannelType.Forum:
case ChannelType.Media:
return TransientGuildChannel.Create(client, model);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ protected TransientGuildChannel(IClient client, ChannelJsonModel model)

case ChannelType.Forum:
return new TransientForumChannel(client, model);

case ChannelType.Media:
return new TransientMediaChannel(client, model);
}

return new TransientUnknownGuildChannel(client, model);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Disqord.Models;
using Qommon;

namespace Disqord;

/// <inheritdoc cref="IForumChannel"/>
public class TransientForumChannel : TransientMediaChannel, IForumChannel
{
/// <inheritdoc/>
public ForumLayout DefaultLayout => Model.DefaultForumLayout.GetValueOrDefault();

public TransientForumChannel(IClient client, ChannelJsonModel model)
: base(client, model)
{ }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

namespace Disqord;

/// <inheritdoc cref="IForumChannel"/>
public class TransientForumChannel : TransientCategorizableGuildChannel, IForumChannel
/// <inheritdoc cref="IMediaChannel"/>
public class TransientMediaChannel : TransientCategorizableGuildChannel, IMediaChannel
{
/// <inheritdoc/>
public string Topic => Model.Topic.Value;
Expand Down Expand Up @@ -72,10 +72,7 @@ public IEmoji? DefaultReactionEmoji
/// <inheritdoc/>
public ForumSortOrder? DefaultSortOrder => Model.DefaultSortOrder.GetValueOrDefault();

/// <inheritdoc/>
public ForumLayout DefaultLayout => Model.DefaultForumLayout.GetValueOrDefault();

public TransientForumChannel(IClient client, ChannelJsonModel model)
public TransientMediaChannel(IClient client, ChannelJsonModel model)
: base(client, model)
{ }
}
6 changes: 4 additions & 2 deletions src/Disqord.Core/Enums/ChannelType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ public enum ChannelType : byte

Directory = 14,

Forum = 15
}
Forum = 15,

Media = 16
}
7 changes: 6 additions & 1 deletion src/Disqord.Core/Enums/GuildChannelFlags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ public enum GuildChannelFlags
/// <summary>
/// The forum channel requires a tag to be specified for threads created in it.
/// </summary>
RequiresTag = 1 << 4
RequiresTag = 1 << 4,

/// <summary>
/// The media channel hides the embedded media download options for media in the threads created in it.
/// </summary>
HideMediaDownloadOptions = 1 << 15
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,12 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Disqord.Models;
using Qommon;
using Qommon.Collections.ReadOnly;

namespace Disqord.Gateway;

/// <inheritdoc cref="IForumChannel"/>
public class CachedForumChannel : CachedCategorizableGuildChannel, IForumChannel
public class CachedForumChannel : CachedMediaChannel, IForumChannel
{
/// <inheritdoc/>
public string? Topic { get; private set; }

/// <inheritdoc/>
public bool IsAgeRestricted { get; private set; }

/// <inheritdoc/>
public TimeSpan Slowmode { get; private set; }

/// <inheritdoc/>
public TimeSpan DefaultAutomaticArchiveDuration { get; private set; }

/// <inheritdoc/>
public Snowflake? LastThreadId { get; private set; }

/// <inheritdoc/>
public IEmoji? DefaultReactionEmoji { get; private set; }

/// <inheritdoc/>
public IReadOnlyList<IForumTag> Tags { get; private set; } = ReadOnlyList<IForumTag>.Empty;

/// <inheritdoc/>
public TimeSpan DefaultThreadSlowmode { get; private set; }

/// <inheritdoc/>
public ForumSortOrder? DefaultSortOrder { get; private set; }

/// <inheritdoc/>
public ForumLayout DefaultLayout { get; private set; }

Expand All @@ -48,49 +18,6 @@ public CachedForumChannel(IGatewayClient client, ChannelJsonModel model)
public override void Update(ChannelJsonModel model)
{
base.Update(model);

if (model.Topic.HasValue)
Topic = model.Topic.Value;

if (model.Nsfw.HasValue)
IsAgeRestricted = model.Nsfw.Value;

if (model.RateLimitPerUser.HasValue)
Slowmode = TimeSpan.FromSeconds(model.RateLimitPerUser.Value);

DefaultAutomaticArchiveDuration = TimeSpan.FromMinutes(model.DefaultAutoArchiveDuration.GetValueOrDefault(1440));

if (model.LastMessageId.HasValue)
LastThreadId = model.LastMessageId.Value;

if (model.DefaultReactionEmoji.HasValue)
{
IEmoji? defaultReactionEmoji;
var defaultReactionEmojiModel = model.DefaultReactionEmoji.GetValueOrDefault();
if (defaultReactionEmojiModel != null)
{
if (defaultReactionEmojiModel.EmojiId != null)
{
defaultReactionEmoji = new TransientCustomEmoji(defaultReactionEmojiModel.EmojiId.Value);
}
else
{
defaultReactionEmoji = new TransientEmoji(defaultReactionEmojiModel.EmojiName!);
}
}
else
{
defaultReactionEmoji = null;
}

DefaultReactionEmoji = defaultReactionEmoji;
}

if (model.AvailableTags.HasValue)
Tags = model.AvailableTags.Value.ToReadOnlyList(model => new TransientForumTag(model));

DefaultThreadSlowmode = TimeSpan.FromSeconds(model.DefaultThreadRateLimitPerUser.GetValueOrDefault(0));
DefaultSortOrder = model.DefaultSortOrder.GetValueOrDefault();
DefaultLayout = model.DefaultForumLayout.GetValueOrDefault();
}
}
Loading
Loading