Skip to content

Commit

Permalink
Implement default values for select menus.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nihlus committed Oct 29, 2023
1 parent 90320c1 commit 1a3905c
Show file tree
Hide file tree
Showing 26 changed files with 194 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Remora.Discord.API.Abstractions.Objects;
/// Represents a dropdown of selectable channels.
/// </summary>
[PublicAPI]
public interface IChannelSelectComponent : ISelectMenuComponent, IPartialChannelSelectComponent
public interface IChannelSelectComponent : IMentionableSelectComponent, IPartialChannelSelectComponent
{
/// <summary>
/// Gets the channel types to show on a <see cref="ComponentType.ChannelSelect"/> component.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

using System.Collections.Generic;
using JetBrains.Annotations;
using Remora.Rest.Core;

namespace Remora.Discord.API.Abstractions.Objects;

Expand All @@ -30,4 +32,12 @@ namespace Remora.Discord.API.Abstractions.Objects;
[PublicAPI]
public interface IMentionableSelectComponent : ISelectMenuComponent, IPartialMentionableSelectComponent
{
/// <summary>
/// Gets the default values for auto-populated select menu components.
/// </summary>
new Optional<IReadOnlyList<ISelectDefaultValue>> DefaultValues { get; }

/// <inheritdoc/>
Optional<IReadOnlyList<IPartialSelectDefaultValue>> IPartialMentionableSelectComponent.DefaultValues
=> this.DefaultValues.Map<IReadOnlyList<IPartialSelectDefaultValue>>(v => v);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Remora.Discord.API.Abstractions.Objects;
/// Represents a partial dropdown of selectable channels.
/// </summary>
[PublicAPI]
public interface IPartialChannelSelectComponent : IPartialSelectMenuComponent
public interface IPartialChannelSelectComponent : IPartialMentionableSelectComponent
{
/// <inheritdoc cref="IChannelSelectComponent.ChannelTypes" />
Optional<IReadOnlyList<ChannelType>> ChannelTypes { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

using System.Collections.Generic;
using JetBrains.Annotations;
using Remora.Rest.Core;

namespace Remora.Discord.API.Abstractions.Objects;

/// <summary>
/// Represents a partial dropdown of selectable mentionables (users and roles).
/// Represents a partial dropdown of selectable mentionables (users, roles, and channels).
/// </summary>
[PublicAPI]
public interface IPartialMentionableSelectComponent : IPartialSelectMenuComponent
{
/// <inheritdoc cref="IMentionableSelectComponent.DefaultValues"/>
Optional<IReadOnlyList<IPartialSelectDefaultValue>> DefaultValues { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ namespace Remora.Discord.API.Abstractions.Objects;
/// Represents a partial dropdown of selectable roles.
/// </summary>
[PublicAPI]
public interface IPartialRoleSelectComponent : IPartialSelectMenuComponent
public interface IPartialRoleSelectComponent : IPartialMentionableSelectComponent
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// IPartialSelectDefaultValue.cs
//
// Author:
// Jarl Gullberg <[email protected]>
//
// Copyright (c) Jarl Gullberg
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

using Remora.Rest.Core;

namespace Remora.Discord.API.Abstractions.Objects;

/// <summary>
/// Represents a partial default value for a select menu.
/// </summary>
public interface IPartialSelectDefaultValue
{
/// <inheritdoc cref="ISelectDefaultValue.ID"/>
Optional<Snowflake> ID { get; }

/// <inheritdoc cref="ISelectDefaultValue.Type"/>
Optional<string> Type { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ namespace Remora.Discord.API.Abstractions.Objects;
/// Represents a partial dropdown of selectable users.
/// </summary>
[PublicAPI]
public interface IPartialUserSelectComponent : IPartialSelectMenuComponent
public interface IPartialUserSelectComponent : IPartialMentionableSelectComponent
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ namespace Remora.Discord.API.Abstractions.Objects;
/// Represents a dropdown of selectable roles.
/// </summary>
[PublicAPI]
public interface IRoleSelectComponent : ISelectMenuComponent, IPartialRoleSelectComponent
public interface IRoleSelectComponent : IMentionableSelectComponent, IPartialRoleSelectComponent
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// ISelectDefaultValue.cs
//
// Author:
// Jarl Gullberg <[email protected]>
//
// Copyright (c) Jarl Gullberg
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

using Remora.Rest.Core;

namespace Remora.Discord.API.Abstractions.Objects;

/// <summary>
/// Represents a default value for a select menu.
/// </summary>
public interface ISelectDefaultValue : IPartialSelectDefaultValue
{
/// <summary>
/// Gets the ID of the default entity (user, role, or channel).
/// </summary>
new Snowflake ID { get; }

/// <summary>
/// Gets the type of value that <see cref="ID"/> represents. Can be one of "user", "role", or "channel".
/// </summary>
new string Type { get; }

/// <inheritdoc/>
Optional<Snowflake> IPartialSelectDefaultValue.ID => this.ID;

/// <inheritdoc/>
Optional<string> IPartialSelectDefaultValue.Type => this.Type;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ namespace Remora.Discord.API.Abstractions.Objects;
/// Represents a dropdown of selectable users.
/// </summary>
[PublicAPI]
public interface IUserSelectComponent : ISelectMenuComponent, IPartialUserSelectComponent
public interface IUserSelectComponent : IMentionableSelectComponent, IPartialUserSelectComponent
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public record ChannelSelectComponent
Optional<string> Placeholder = default,
Optional<int> MinValues = default,
Optional<int> MaxValues = default,
Optional<bool> IsDisabled = default
Optional<bool> IsDisabled = default,
Optional<IReadOnlyList<ISelectDefaultValue>> DefaultValues = default
) : IChannelSelectComponent
{
/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

using System.Collections.Generic;
using JetBrains.Annotations;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Rest.Core;
Expand All @@ -34,7 +35,8 @@ public record MentionableSelectComponent
Optional<string> Placeholder = default,
Optional<int> MinValues = default,
Optional<int> MaxValues = default,
Optional<bool> IsDisabled = default
Optional<bool> IsDisabled = default,
Optional<IReadOnlyList<ISelectDefaultValue>> DefaultValues = default
) : IMentionableSelectComponent
{
/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public record PartialChannelSelectComponent
Optional<string> Placeholder = default,
Optional<int> MinValues = default,
Optional<int> MaxValues = default,
Optional<bool> IsDisabled = default
Optional<bool> IsDisabled = default,
Optional<IReadOnlyList<IPartialSelectDefaultValue>> DefaultValues = default
) : IPartialChannelSelectComponent
{
/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

using System.Collections.Generic;
using JetBrains.Annotations;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Rest.Core;
Expand All @@ -34,7 +35,8 @@ public record PartialMentionableSelectComponent
Optional<string> Placeholder = default,
Optional<int> MinValues = default,
Optional<int> MaxValues = default,
Optional<bool> IsDisabled = default
Optional<bool> IsDisabled = default,
Optional<IReadOnlyList<IPartialSelectDefaultValue>> DefaultValues = default
) : IPartialMentionableSelectComponent
{
/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

using System.Collections.Generic;
using JetBrains.Annotations;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Rest.Core;
Expand All @@ -34,7 +35,8 @@ public record PartialRoleSelectComponent
Optional<string> Placeholder = default,
Optional<int> MinValues = default,
Optional<int> MaxValues = default,
Optional<bool> IsDisabled = default
Optional<bool> IsDisabled = default,
Optional<IReadOnlyList<IPartialSelectDefaultValue>> DefaultValues = default
) : IPartialRoleSelectComponent
{
/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

using System.Collections.Generic;
using JetBrains.Annotations;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Rest.Core;
Expand All @@ -34,7 +35,8 @@ public record PartialUserSelectComponent
Optional<string> Placeholder = default,
Optional<int> MinValues = default,
Optional<int> MaxValues = default,
Optional<bool> IsDisabled = default
Optional<bool> IsDisabled = default,
Optional<IReadOnlyList<IPartialSelectDefaultValue>> DefaultValues = default
) : IPartialUserSelectComponent
{
/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

using System.Collections.Generic;
using JetBrains.Annotations;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Rest.Core;
Expand All @@ -34,7 +35,8 @@ public record RoleSelectComponent
Optional<string> Placeholder = default,
Optional<int> MinValues = default,
Optional<int> MaxValues = default,
Optional<bool> IsDisabled = default
Optional<bool> IsDisabled = default,
Optional<IReadOnlyList<ISelectDefaultValue>> DefaultValues = default
) : IRoleSelectComponent
{
/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// SelectDefaultValue.cs
//
// Author:
// Jarl Gullberg <[email protected]>
//
// Copyright (c) Jarl Gullberg
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

using Remora.Discord.API.Abstractions.Objects;
using Remora.Rest.Core;

namespace Remora.Discord.API.Objects;

/// <inheritdoc />
public record SelectDefaultValue(Snowflake ID, string Type) : ISelectDefaultValue;
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

using System.Collections.Generic;
using JetBrains.Annotations;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Rest.Core;
Expand All @@ -34,7 +35,8 @@ public record UserSelectComponent
Optional<string> Placeholder = default,
Optional<int> MinValues = default,
Optional<int> MaxValues = default,
Optional<bool> IsDisabled = default
Optional<bool> IsDisabled = default,
Optional<IReadOnlyList<ISelectDefaultValue>> DefaultValues = default
) : IUserSelectComponent
{
/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,8 @@ private static JsonSerializerOptions AddInteractionObjectConverters(this JsonSer
options.AddDataObjectConverter<IPartialSelectOption, PartialSelectOption>()
.WithPropertyName(o => o.IsDefault, "default");

options.AddDataObjectConverter<ISelectDefaultValue, SelectDefaultValue>();

return options;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ public override void Write(Utf8JsonWriter writer, IMessageComponent value, JsonS
JsonSerializer.Serialize(writer, roleSelect, options);
break;
}
case IMentionableSelectComponent mentionableSelect:
case IChannelSelectComponent channelSelect:
{
JsonSerializer.Serialize(writer, mentionableSelect, options);
JsonSerializer.Serialize(writer, channelSelect, options);
break;
}
case IChannelSelectComponent channelSelect:
case IMentionableSelectComponent mentionableSelect:
{
JsonSerializer.Serialize(writer, channelSelect, options);
JsonSerializer.Serialize(writer, mentionableSelect, options);
break;
}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ public override void Write(Utf8JsonWriter writer, IPartialMessageComponent value
JsonSerializer.Serialize(writer, roleSelect, options);
break;
}
case IPartialMentionableSelectComponent mentionableSelect:
case IPartialChannelSelectComponent channelSelect:
{
JsonSerializer.Serialize(writer, mentionableSelect, options);
JsonSerializer.Serialize(writer, channelSelect, options);
break;
}
case IPartialChannelSelectComponent channelSelect:
case IPartialMentionableSelectComponent mentionableSelect:
{
JsonSerializer.Serialize(writer, channelSelect, options);
JsonSerializer.Serialize(writer, mentionableSelect, options);
break;
}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,11 @@
"placeholder": "none",
"min_values": 1,
"max_values": 3,
"disabled": true
"disabled": true,
"default_values": [
{
"id": "999999999999999999",
"type": "channel"
}
]
}
Loading

0 comments on commit 1a3905c

Please sign in to comment.