示例#1
0
        /// <summary>
        ///     Respond to an interaction with a <see cref="IModal"/>.
        /// </summary>
        /// <typeparam name="T">Type of the <see cref="IModal"/> implementation.</typeparam>
        /// <param name="interaction">The interaction to respond to.</param>
        /// <param name="modifyModal">Delegate that can be used to modify the modal.</param>
        /// <param name="options">The request options for this <see langword="async"/> request.</param>
        /// <returns>A task that represents the asynchronous operation of responding to the interaction.</returns>
        public static async Task RespondWithModalAsync <T>(this IDiscordInteraction interaction, string customId, RequestOptions options = null, Action <ModalBuilder> modifyModal = null)
            where T : class, IModal
        {
            if (!ModalUtils.TryGet <T>(out var modalInfo))
            {
                throw new ArgumentException($"{typeof(T).FullName} isn't referenced by any registered Modal Interaction Command and doesn't have a cached {typeof(ModalInfo)}");
            }

            var builder = new ModalBuilder(modalInfo.Title, customId);

            foreach (var input in modalInfo.Components)
            {
                switch (input)
                {
                case TextInputComponentInfo textComponent:
                    builder.AddTextInput(textComponent.Label, textComponent.CustomId, textComponent.Style, textComponent.Placeholder, textComponent.IsRequired ? textComponent.MinLength : null,
                                         textComponent.MaxLength, textComponent.IsRequired, textComponent.InitialValue);
                    break;

                default:
                    throw new InvalidOperationException($"{input.GetType().FullName} isn't a valid component info class");
                }
            }

            if (modifyModal is not null)
            {
                modifyModal(builder);
            }

            await interaction.RespondWithModalAsync(builder.Build(), options).ConfigureAwait(false);
        }
示例#2
0
 public static async Task<RestInteractionMessage> GetOriginalResponseAsync(BaseDiscordClient client, IMessageChannel channel,
     IDiscordInteraction interaction, RequestOptions options = null)
 {
     var model = await client.ApiClient.GetInteractionResponseAsync(interaction.Token, options).ConfigureAwait(false);
     if(model != null)
         return RestInteractionMessage.Create(client, model, interaction.Token, channel);
     return null;
 }
 /// <summary>
 ///     Initializes a new <see cref="SocketInteractionContext{TInteraction}"/>.
 /// </summary>
 /// <param name="client">The underlying client.</param>
 /// <param name="interaction">The underlying interaction.</param>
 /// <param name="channel"><see cref="IMessageChannel"/> the command originated from.</param>
 public InteractionContext(IDiscordClient client, IDiscordInteraction interaction, IMessageChannel channel = null)
 {
     Client      = client;
     Interaction = interaction;
     Channel     = channel;
     Guild       = (interaction.User as IGuildUser)?.Guild;
     User        = interaction.User;
     Interaction = interaction;
 }
示例#4
0
        public static API.Message ToMessage(this API.InteractionResponse model, IDiscordInteraction interaction)
        {
            if (model.Data.IsSpecified)
            {
                var data         = model.Data.Value;
                var messageModel = new API.Message
                {
                    IsTextToSpeech  = data.TTS,
                    Content         = (data.Content.IsSpecified && data.Content.Value == null) ? Optional <string> .Unspecified : data.Content,
                    Embeds          = data.Embeds,
                    AllowedMentions = data.AllowedMentions,
                    Components      = data.Components,
                    Flags           = data.Flags,
                };

                if (interaction is IApplicationCommandInteraction command)
                {
                    messageModel.Interaction = new API.MessageInteraction
                    {
                        Id   = command.Id,
                        Name = command.Data.Name,
                        Type = InteractionType.ApplicationCommand,
                        User = new API.User
                        {
                            Username      = command.User.Username,
                            Avatar        = command.User.AvatarId,
                            Bot           = command.User.IsBot,
                            Discriminator = command.User.Discriminator,
                            PublicFlags   = command.User.PublicFlags.HasValue ? command.User.PublicFlags.Value : Optional <UserProperties> .Unspecified,
                            Id            = command.User.Id,
                        }
                    };
                }

                return(messageModel);
            }

            return(new API.Message
            {
                Id = interaction.Id,
            });
        }
示例#5
0
        void ISlashCommandModule.SetContext(IDiscordInteraction interaction)
        {
            var newValue = interaction as T;

            Interaction = newValue ?? throw new InvalidOperationException($"Invalid interaction type. Expected {typeof(T).Name}, got {interaction.GetType().Name}.");
        }
 public static async Task DeleteInteractionResponseAsync(BaseDiscordClient client, IDiscordInteraction interaction, RequestOptions options = null)
 => await client.ApiClient.DeleteInteractionResponseAsync(interaction.Token, options);
 public static async Task SendInteractionResponseAsync(BaseDiscordClient client, UploadInteractionFileParams response,
                                                       IDiscordInteraction interaction, IMessageChannel channel = null, RequestOptions options = null)
 {
     await client.ApiClient.CreateInteractionResponseAsync(response, interaction.Id, interaction.Token, options).ConfigureAwait(false);
 }
 public static bool CanRespondOrFollowup(IDiscordInteraction interaction)
 {
     return((DateTime.UtcNow - interaction.CreatedAt).TotalMinutes <= ResponseAndFollowupLimit);
 }
 public static bool CanSendResponse(IDiscordInteraction interaction)
 {
     return((DateTime.UtcNow - interaction.CreatedAt).TotalSeconds < ResponseTimeLimit);
 }
示例#10
0
 internal SlashCommandContext(IDiscordInteraction interaction)
 {
     this.Interaction = (T)interaction;
 }
示例#11
0
 internal Task ExecuteInternalAsync(IDiscordInteraction interaction)
 {
     // try catch?
     return(this.CommandExecuted?.Invoke(new SlashCommandContext <T>(interaction)));
 }