Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit dca41a348e36a9b4e7006ef3a76377eb32aad276
Author: quin lynch <[email protected]>
Date:   Thu Sep 23 07:02:19 2021 -0300

    Autocomplete commands
  • Loading branch information
quinchs committed Sep 23, 2021
1 parent 5987af7 commit 016c1dc
Show file tree
Hide file tree
Showing 20 changed files with 681 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/Discord.Net.Core/Discord.Net.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="../../Discord.Net.targets" />
<Import Project="../../StyleAnalyzer.targets" />
<PropertyGroup>
Expand Down
154 changes: 131 additions & 23 deletions src/Discord.Net.Core/Discord.Net.Core.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ApplicationCommandOptionProperties
private string _description;

/// <summary>
/// The name of this option.
/// Gets or sets the name of this option.
/// </summary>
public string Name
{
Expand All @@ -37,7 +37,7 @@ public string Name
}

/// <summary>
/// The description of this option.
/// Gets or sets the description of this option.
/// </summary>
public string Description
{
Expand All @@ -53,27 +53,31 @@ public string Description
}

/// <summary>
/// The type of this option.
/// Gets or sets the type of this option.
/// </summary>
public ApplicationCommandOptionType Type { get; set; }

/// <summary>
/// The first required option for the user to complete. only one option can be default.
/// Gets or sets whether or not this options is the first required option for the user to complete. only one option can be default.
/// </summary>
public bool? Default { get; set; }

/// <summary>
/// <see langword="true"/> if this option is required for this command, otherwise <see langword="false"/>.
/// Gets or sets if the option is required.
/// </summary>
public bool? Required { get; set; }

/// <summary>
/// choices for string and int types for the user to pick from.
/// Gets or sets whether or not this option supports autocomplete.
/// </summary>
public bool Autocomplete { get; set; }
/// <summary>
/// Gets or sets the choices for string and int types for the user to pick from.
/// </summary>
public List<ApplicationCommandOptionChoiceProperties> Choices { get; set; }

/// <summary>
/// If the option is a subcommand or subcommand group type, this nested options will be the parameters.
/// Gets or sets if this option is a subcommand or subcommand group type, these nested options will be the parameters.
/// </summary>
public List<ApplicationCommandOptionProperties> Options { get; set; }

Expand Down
42 changes: 42 additions & 0 deletions src/Discord.Net.Core/Entities/Interactions/AutocompleteOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
/// <summary>
/// Represents an autocomplete option.
/// </summary>
public class AutocompleteOption
{
/// <summary>
/// Gets the type of this option
/// </summary>
public ApplicationCommandOptionType Type { get; }

/// <summary>
/// Gets the name of the option.
/// </summary>
public string Name { get; }

/// <summary>
/// Gets the value of the option.
/// </summary>
public object Value { get; }

/// <summary>
/// Gets whether or not this option is focused by the executing user.
/// </summary>
public bool Focused { get; }

internal AutocompleteOption(ApplicationCommandOptionType type, string name, object value, bool focused)
{
this.Type = type;
this.Name = name;
this.Value = value;
this.Focused = focused;
}
}
}
90 changes: 90 additions & 0 deletions src/Discord.Net.Core/Entities/Interactions/AutocompleteResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
/// <summary>
/// Represents a result to an autocomplete interaction.
/// </summary>
public class AutocompleteResult
{
private object _value { get; set; }
private string _name { get; set; }

/// <summary>
/// Gets or sets the name of the result.
/// </summary>
/// <remarks>
/// Name cannot be null and has to be between 1-100 characters in length.
/// </remarks>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="ArgumentException"/>
public string Name
{
get => _name;
set
{
if (value == null)
throw new ArgumentException("Name cannot be null!");
if (value.Length > 100)
throw new ArgumentException("Name length must be less than or equal to 100 characters in length!");
if (value.Length < 1)
throw new ArgumentException("Name length must at least 1 character in length!");
_name = value;
}
}

/// <summary>
/// Gets or sets the value of the result.
/// </summary>
/// <remarks>
/// Only <see cref="string"/>, <see cref="int"/>, and <see cref="double"/> are allowed for a value.
/// </remarks>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="ArgumentException"/>
public object Value
{
get => _value;
set
{
if (value == null)
throw new ArgumentNullException("Value cannot be null");

switch (value)
{
case string str:
_value = str;
break;
case int integer:
_value = integer;
break;
case double number:
_value = number;
break;

default:
throw new ArgumentException($"Type {value.GetType().Name} cannot be set as a value! Only string, int, and double allowed!");
}
}
}

/// <summary>
/// Creates a new <see cref="AutocompleteResult"/>.
/// </summary>
public AutocompleteResult() { }

/// <summary>
/// Creates a new <see cref="AutocompleteResult"/> with the passed in <paramref name="name"/> and <paramref name="value"/>.
/// </summary>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="ArgumentException"/>
public AutocompleteResult(string name, object value)
{
this.Name = name;
this.Value = value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,18 @@ public enum InteractionResponseType : byte
DeferredChannelMessageWithSource = 5,

/// <summary>
/// for components: ACK an interaction and edit the original message later; the user does not see a loading state
/// For components: ACK an interaction and edit the original message later; the user does not see a loading state
/// </summary>
DeferredUpdateMessage = 6,

/// <summary>
/// for components: edit the message the component was attached to
/// For components: edit the message the component was attached to
/// </summary>
UpdateMessage = 7
UpdateMessage = 7,

/// <summary>
/// Respond with a set of choices to a autocomplete interaction
/// </summary>
ApplicationCommandAutocompleteResult = 8
}
}
5 changes: 5 additions & 0 deletions src/Discord.Net.Core/Entities/Interactions/InteractionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,10 @@ public enum InteractionType : byte
/// A <see cref="IMessageComponent"/> sent from discord.
/// </summary>
MessageComponent = 3,

/// <summary>
/// An autocomplete request sent from discord.
/// </summary>
ApplicationCommandAutocomplete = 4,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public SlashCommandBuilder WithDefaultPermission(bool value)
/// <param name="choices">The choices of this option.</param>
/// <returns>The current builder.</returns>
public SlashCommandBuilder AddOption(string name, ApplicationCommandOptionType type,
string description, bool required = true, bool isDefault = false, List<SlashCommandOptionBuilder> options = null, params ApplicationCommandOptionChoiceProperties[] choices)
string description, bool required = true, bool isDefault = false, bool isAutocomplete = false, List<SlashCommandOptionBuilder> options = null, params ApplicationCommandOptionChoiceProperties[] choices)
{
// Make sure the name matches the requirements from discord
Preconditions.NotNullOrEmpty(name, nameof(name));
Expand Down Expand Up @@ -197,6 +197,7 @@ public SlashCommandBuilder AddOption(string name, ApplicationCommandOptionType t
option.Default = isDefault;
option.Options = options;
option.Type = type;
option.Autocomplete = isAutocomplete;
option.Choices = choices != null ? new List<ApplicationCommandOptionChoiceProperties>(choices) : null;

return AddOption(option);
Expand Down Expand Up @@ -288,7 +289,7 @@ public class SlashCommandOptionBuilder
private string _description;

/// <summary>
/// The name of this option.
/// Gets or sets the name of this option.
/// </summary>
public string Name
{
Expand All @@ -309,7 +310,7 @@ public string Name
}

/// <summary>
/// The description of this option.
/// Gets or sets the description of this option.
/// </summary>
public string Description
{
Expand All @@ -326,27 +327,32 @@ public string Description
}

/// <summary>
/// The type of this option.
/// Gets or sets the type of this option.
/// </summary>
public ApplicationCommandOptionType Type { get; set; }

/// <summary>
/// The first required option for the user to complete. only one option can be default.
/// Gets or sets whether or not this options is the first required option for the user to complete. only one option can be default.
/// </summary>
public bool? Default { get; set; }

/// <summary>
/// <see langword="true"/> if this option is required for this command, otherwise <see langword="false"/>.
/// Gets or sets if the option is required.
/// </summary>
public bool Required { get; set; }

/// <summary>
/// choices for string and int types for the user to pick from.
/// Gets or sets whether or not this option supports autocomplete.
/// </summary>
public bool Autocomplete { get; set; }

/// <summary>
/// Gets or sets the choices for string and int types for the user to pick from.
/// </summary>
public List<ApplicationCommandOptionChoiceProperties> Choices { get; set; }

/// <summary>
/// If the option is a subcommand or subcommand group type, this nested options will be the parameters.
/// Gets or sets if this option is a subcommand or subcommand group type, these nested options will be the parameters.
/// </summary>
public List<SlashCommandOptionBuilder> Options { get; set; }

Expand All @@ -372,7 +378,8 @@ public ApplicationCommandOptionProperties Build()
Required = this.Required,
Type = this.Type,
Options = this.Options?.Count > 0 ? new List<ApplicationCommandOptionProperties>(this.Options.Select(x => x.Build())) : null,
Choices = this.Choices
Choices = this.Choices,
Autocomplete = this.Autocomplete
};
}

Expand Down
4 changes: 4 additions & 0 deletions src/Discord.Net.Rest/API/Common/ApplicationCommandOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ internal class ApplicationCommandOption
[JsonProperty("options")]
public Optional<ApplicationCommandOption[]> Options { get; set; }

[JsonProperty("autocomplete")]
public Optional<bool> Autocomplete { get; set; }

public ApplicationCommandOption() { }

public ApplicationCommandOption(IApplicationCommandOption cmd)
Expand Down Expand Up @@ -78,6 +81,7 @@ public ApplicationCommandOption(Discord.ApplicationCommandOptionProperties optio
this.Name = option.Name;
this.Type = option.Type;
this.Description = option.Description;
this.Autocomplete = option.Autocomplete;
}
}
}
27 changes: 27 additions & 0 deletions src/Discord.Net.Rest/API/Common/AutocompleteInteractionData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord.API
{
internal class AutocompleteInteractionData : IDiscordInteractionData
{
[JsonProperty("id")]
public ulong Id { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("type")]
public ApplicationCommandType Type { get; set; }

[JsonProperty("version")]
public ulong Version { get; set; }

[JsonProperty("options")]
public AutocompleteInteractionDataOption[] Options { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord.API
{
internal class AutocompleteInteractionDataOption
{
[JsonProperty("type")]
public ApplicationCommandOptionType Type { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("value")]
public object Value { get; set; }

[JsonProperty("focused")]
public bool Focused { get; set; }
}
}
3 changes: 3 additions & 0 deletions src/Discord.Net.Rest/API/Common/InteractionCallbackData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ internal class InteractionCallbackData

[JsonProperty("components")]
public Optional<API.ActionRowComponent[]> Components { get; set; }

[JsonProperty("choices")]
public Optional<API.ApplicationCommandOptionChoice[]> Choices { get; set; }
}
}
Loading

0 comments on commit 016c1dc

Please sign in to comment.