コード例 #1
0
        /// <summary>
        ///     Sends a followup message for this interaction.
        /// </summary>
        /// <param name="text">The text of the message to be sent</param>
        /// <param name="embeds">A <see cref="Embed"/> to send with this response.</param>
        /// <param name="isTTS"><see langword="true"/> if the message should be read out by a text-to-speech reader, otherwise <see langword="false"/>.</param>
        /// <param name="allowedMentions">The allowed mentions for this response.</param>
        /// <param name="flags"></param>
        /// <param name="options">The request options for this response.</param>
        /// <returns>
        ///     The sent message.
        /// </returns>
        public async Task <IMessage> FollowupEmbedsAsync(IEnumerable <Embed> embeds, string text = null, bool isTTS = false, AllowedMentions allowedMentions = null,
                                                         int?flags = null, RequestOptions options = null)
        {
            if (!IsValidToken)
            {
                throw new InvalidOperationException("Interaction token is no longer valid");
            }

            var args = new CreateWebhookMessageParams(text)
            {
                IsTTS = isTTS
            };

            if (embeds != null)
            {
                args.Embeds = embeds.Select(x => x.ToModel()).ToArray();
            }
            if (allowedMentions != null)
            {
                args.AllowedMentions = allowedMentions.ToModel();
            }
            if (flags.HasValue)
            {
                args.Flags = flags.Value;
            }

            return(await InteractionHelper.SendFollowupAsync(Discord.Rest, args, Token, Channel, options).ConfigureAwait(false));
        }
コード例 #2
0
        private async Task <IMessage> FollowupFileAsyncInternal(Stream stream, string filename, string text, bool isTTS, IEnumerable <Embed> embeds,
                                                                string username, string avatarUrl, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler, int?flags)
        {
            var args = new UploadWebhookFileParams(stream)
            {
                Filename = filename, Content = text, IsTTS = isTTS, IsSpoiler = isSpoiler
            };

            if (username != null)
            {
                args.Username = username;
            }
            if (avatarUrl != null)
            {
                args.AvatarUrl = avatarUrl;
            }
            if (embeds != null)
            {
                args.Embeds = embeds.Select(x => x.ToModel()).ToArray();
            }
            if (allowedMentions != null)
            {
                args.AllowedMentions = allowedMentions.ToModel();
            }
            if (flags.HasValue)
            {
                args.Flags = flags.Value;
            }

            return(await InteractionHelper.SendFollowupFileAsync(Discord.Rest, args, Token, Channel, options).ConfigureAwait(false));
        }
コード例 #3
0
        public static async Task <ulong> SendFileAsync(DiscordWebhookClient client, Stream stream, string filename, string text, bool isTTS,
                                                       IEnumerable <Embed> embeds, string username, string avatarUrl, RequestOptions options, bool isSpoiler, AllowedMentions allowedMentions, InteractionRow[] components)
        {
            UploadWebhookFileParams args = new UploadWebhookFileParams(stream)
            {
                Filename = filename, Content = text, IsTTS = isTTS, IsSpoiler = isSpoiler
            };

            if (username != null)
            {
                args.Username = username;
            }
            if (avatarUrl != null)
            {
                args.AvatarUrl = avatarUrl;
            }
            if (embeds != null)
            {
                args.Embeds = embeds.Select(x => x.ToModel()).ToArray();
            }
            if (allowedMentions != null)
            {
                args.AllowedMentions = allowedMentions.ToModel();
            }
            if (components != null)
            {
                args.Components = components.Select(x => x.ToModel()).ToArray();
            }

            API.MessageJson msg = await client.ApiClient.UploadWebhookFileAsync(client.Webhook.Id, args, options).ConfigureAwait(false);

            return(msg.Id);
        }
コード例 #4
0
        /// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
        public static async Task <RestUserMessage> SendMessageAsync(IMessageChannel channel, BaseDiscordClient client,
                                                                    string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, RequestOptions options)
        {
            Preconditions.AtMost(allowedMentions?.RoleIds?.Count ?? 0, 100, nameof(allowedMentions.RoleIds), "A max of 100 role Ids are allowed.");
            Preconditions.AtMost(allowedMentions?.UserIds?.Count ?? 0, 100, nameof(allowedMentions.UserIds), "A max of 100 user Ids are allowed.");

            // check that user flag and user Id list are exclusive, same with role flag and role Id list
            if (allowedMentions != null && allowedMentions.AllowedTypes.HasValue)
            {
                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Users) &&
                    allowedMentions.UserIds != null && allowedMentions.UserIds.Count > 0)
                {
                    throw new ArgumentException("The Users flag is mutually exclusive with the list of User Ids.", nameof(allowedMentions));
                }

                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Roles) &&
                    allowedMentions.RoleIds != null && allowedMentions.RoleIds.Count > 0)
                {
                    throw new ArgumentException("The Roles flag is mutually exclusive with the list of Role Ids.", nameof(allowedMentions));
                }
            }

            var args = new CreateMessageParams(text)
            {
                IsTTS = isTTS, Embed = embed?.ToModel(), AllowedMentions = allowedMentions?.ToModel()
            };
            var model = await client.ApiClient.CreateMessageAsync(channel.Id, args, options).ConfigureAwait(false);

            return(RestUserMessage.Create(client, channel, client.CurrentUser, model));
        }
コード例 #5
0
        public static async Task <ulong> SendMessageAsync(DiscordWebhookClient client,
                                                          string text, bool isTTS, IEnumerable <Embed> embeds, string username, string avatarUrl, RequestOptions options, AllowedMentions allowedMentions, InteractionRow[] components)
        {
            CreateWebhookMessageParams args = new CreateWebhookMessageParams(text)
            {
                IsTTS = isTTS
            };

            if (embeds != null)
            {
                args.Embeds = embeds.Select(x => x.ToModel()).ToArray();
            }
            if (username != null)
            {
                args.Username = username;
            }
            if (avatarUrl != null)
            {
                args.AvatarUrl = avatarUrl;
            }
            if (allowedMentions != null)
            {
                args.AllowedMentions = allowedMentions.ToModel();
            }
            if (components != null)
            {
                args.Components = components.Select(x => x.ToModel()).ToArray();
            }

            API.MessageJson model = await client.ApiClient.CreateWebhookMessageAsync(client.Webhook.Id, args, options : options).ConfigureAwait(false);

            return(model.Id);
        }
コード例 #6
0
ファイル: WebhookClientHelper.cs プロジェクト: Nensec/FRTools
        public static async Task <ulong> SendFileAsync(DiscordWebhookClient client, IEnumerable <KeyValuePair <string, Stream> > files, string text, bool isTTS,
                                                       IEnumerable <Embed> embeds, string username, string avatarUrl, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler)
        {
            var args = new UploadWebhookFileParams(files)
            {
                Content = text, IsTTS = isTTS, IsSpoiler = isSpoiler
            };

            if (username != null)
            {
                args.Username = username;
            }
            if (avatarUrl != null)
            {
                args.AvatarUrl = avatarUrl;
            }
            if (embeds != null)
            {
                args.Embeds = embeds.Select(x => x.ToModel()).ToArray();
            }
            if (allowedMentions != null)
            {
                args.AllowedMentions = allowedMentions.ToModel();
            }
            var msg = await client.ApiClient.UploadWebhookFileAsync(client.Webhook.Id, args, options).ConfigureAwait(false);

            return(msg.Id);
        }
コード例 #7
0
        /// <exception cref="InvalidOperationException">Only the author of a message may modify the message.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
        public static async Task <Model> ModifyAsync(IMessage msg, BaseDiscordClient client, Action <MessageProperties> func,
                                                     RequestOptions options, AllowedMentions allowedMentions)
        {
            if (msg.Author.Id != client.CurrentUser.Id)
            {
                throw new InvalidOperationException("Only the author of a message may modify the message.");
            }

            MessageProperties args = new MessageProperties();

            func(args);

            bool hasText  = args.Content.IsSpecified ? !string.IsNullOrEmpty(args.Content.Value) : !string.IsNullOrEmpty(msg.Content);
            bool hasEmbed = args.Embed.IsSpecified ? args.Embed.Value != null : msg.Embeds.Any();

            if (!hasText && !hasEmbed)
            {
                Preconditions.NotNullOrEmpty(args.Content.IsSpecified ? args.Content.Value : string.Empty, nameof(args.Content));
            }

            ModifyMessageParams apiArgs = new API.Rest.ModifyMessageParams
            {
                Content = args.Content,
                Embed   = args.Embed.IsSpecified ? args.Embed.Value.ToModel() : Optional.Create <API.EmbedJson>()
            };

            if (allowedMentions != null)
            {
                apiArgs.AllowedMentions = allowedMentions.ToModel();
            }

            return(await client.ApiClient.ModifyMessageAsync(msg.Channel.Id, msg.Id, apiArgs, options).ConfigureAwait(false));
        }
コード例 #8
0
        /// <summary>
        /// Responds to an Interaction.
        /// </summary>
        /// <param name="text">The text of the message to be sent.</param>
        /// <param name="isTTS"><see langword="true"/> if the message should be read out by a text-to-speech reader, otherwise <see langword="false"/>.</param>
        /// <param name="embed">A <see cref="Embed"/> to send with this response.</param>
        /// <param name="type">The type of response to this Interaction.</param>
        /// <param name="allowedMentions">The allowed mentions for this response.</param>
        /// <param name="flags"></param>
        /// <param name="options">The request options for this response.</param>
        /// <returns>
        ///     The <see cref="IMessage"/> sent as the response. If this is the first acknowledgement, it will return null.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
        /// <exception cref="InvalidOperationException">The parameters provided were invalid or the token was invalid.</exception>
        public async Task <IMessage> RespondAsync(string text = null, bool isTTS = false, Embed embed = null, InteractionResponseType type = InteractionResponseType.ChannelMessageWithSource,
                                                  AllowedMentions allowedMentions = null, int?flags = null, RequestOptions options = null)
        {
            if (type == InteractionResponseType.Pong)
            {
                throw new InvalidOperationException($"Cannot use {Type} on a send message function");
            }
            if (!IsValidToken)
            {
                throw new InvalidOperationException("Interaction token is no longer valid");
            }

            Preconditions.AtMost(allowedMentions?.RoleIds?.Count ?? 0, 100, nameof(allowedMentions.RoleIds), "A max of 100 role Ids are allowed.");
            Preconditions.AtMost(allowedMentions?.UserIds?.Count ?? 0, 100, nameof(allowedMentions.UserIds), "A max of 100 user Ids are allowed.");

            // check that user flag and user Id list are exclusive, same with role flag and role Id list
            if (allowedMentions?.AllowedTypes != null)
            {
                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Users) &&
                    allowedMentions.UserIds != null && allowedMentions.UserIds.Count > 0)
                {
                    throw new ArgumentException("The Users flag is mutually exclusive with the list of User Ids.", nameof(allowedMentions));
                }

                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Roles) &&
                    allowedMentions.RoleIds != null && allowedMentions.RoleIds.Count > 0)
                {
                    throw new ArgumentException("The Roles flag is mutually exclusive with the list of Role Ids.", nameof(allowedMentions));
                }
            }

            var callbackData = new InteractionApplicationCommandCallbackData(text)
            {
                TTS = isTTS
            };

            if (allowedMentions != null)
            {
                callbackData.AllowedMentions = allowedMentions.ToModel();
            }
            if (embed != null)
            {
                callbackData.Embeds = new[] { embed.ToModel() }
            }
            ;
            if (flags.HasValue)
            {
                callbackData.Flags = flags.Value;
            }

            var response = new InteractionResponse
            {
                Type = type,
                Data = callbackData
            };
            await Discord.Rest.ApiClient.CreateInteractionResponse(response, Id, Token, options).ConfigureAwait(false);

            return(null);
        }
コード例 #9
0
        /// <summary>
        /// Responds to an Interaction.
        /// <para>
        ///     If you have <see cref="DiscordSocketConfig.AlwaysAcknowledgeInteractions"/> set to <see langword="true"/>, You should use
        ///     <see cref="FollowupAsync(string, bool, Embed, InteractionResponseType, AllowedMentions, RequestOptions)"/> instead.
        /// </para>
        /// </summary>
        /// <param name="text">The text of the message to be sent</param>
        /// <param name="isTTS"><see langword="true"/> if the message should be read out by a text-to-speech reader, otherwise <see langword="false"/></param>
        /// <param name="embed">A <see cref="Embed"/> to send with this response</param>
        /// <param name="Type">The type of response to this Interaction</param>
        /// <param name="allowedMentions">The allowed mentions for this response</param>
        /// <param name="options">The request options for this response</param>
        /// <returns>
        ///     The <see cref="IMessage"/> sent as the response. If this is the first acknowledgement, it will return null;
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
        /// <exception cref="InvalidOperationException">The parameters provided were invalid or the token was invalid</exception>

        public async Task <IMessage> RespondAsync(string text = null, bool isTTS = false, Embed embed = null, InteractionResponseType Type = InteractionResponseType.ChannelMessageWithSource, AllowedMentions allowedMentions = null, RequestOptions options = null)
        {
            if (Type == InteractionResponseType.Pong)
            {
                throw new InvalidOperationException($"Cannot use {Type} on a send message function");
            }

            if (!IsValidToken)
            {
                throw new InvalidOperationException("Interaction token is no longer valid");
            }

            if (Discord.AlwaysAcknowledgeInteractions)
            {
                return(await FollowupAsync());
            }

            Preconditions.AtMost(allowedMentions?.RoleIds?.Count ?? 0, 100, nameof(allowedMentions.RoleIds), "A max of 100 role Ids are allowed.");
            Preconditions.AtMost(allowedMentions?.UserIds?.Count ?? 0, 100, nameof(allowedMentions.UserIds), "A max of 100 user Ids are allowed.");

            // check that user flag and user Id list are exclusive, same with role flag and role Id list
            if (allowedMentions != null && allowedMentions.AllowedTypes.HasValue)
            {
                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Users) &&
                    allowedMentions.UserIds != null && allowedMentions.UserIds.Count > 0)
                {
                    throw new ArgumentException("The Users flag is mutually exclusive with the list of User Ids.", nameof(allowedMentions));
                }

                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Roles) &&
                    allowedMentions.RoleIds != null && allowedMentions.RoleIds.Count > 0)
                {
                    throw new ArgumentException("The Roles flag is mutually exclusive with the list of Role Ids.", nameof(allowedMentions));
                }
            }


            var response = new API.InteractionResponse()
            {
                Type = Type,
                Data = new API.InteractionApplicationCommandCallbackData(text)
                {
                    AllowedMentions = allowedMentions?.ToModel(),
                    Embeds          = embed != null
                        ? new API.Embed[] { embed.ToModel() }
                        : Optional <API.Embed[]> .Unspecified,
                    TTS = isTTS
                }
            };

            await Discord.Rest.ApiClient.CreateInteractionResponse(response, this.Id, Token, options);

            return(null);
        }
コード例 #10
0
        public static async Task <ulong> SendFilesAsync(DiscordWebhookClient client,
                                                        IEnumerable <FileAttachment> attachments, string text, bool isTTS, IEnumerable <Embed> embeds, string username,
                                                        string avatarUrl, AllowedMentions allowedMentions, MessageComponent components, RequestOptions options,
                                                        MessageFlags flags)
        {
            embeds ??= Array.Empty <Embed>();

            Preconditions.AtMost(allowedMentions?.RoleIds?.Count ?? 0, 100, nameof(allowedMentions.RoleIds), "A max of 100 role Ids are allowed.");
            Preconditions.AtMost(allowedMentions?.UserIds?.Count ?? 0, 100, nameof(allowedMentions.UserIds), "A max of 100 user Ids are allowed.");
            Preconditions.AtMost(embeds.Count(), 10, nameof(embeds), "A max of 10 embeds are allowed.");

            foreach (var attachment in attachments)
            {
                Preconditions.NotNullOrEmpty(attachment.FileName, nameof(attachment.FileName), "File Name must not be empty or null");
            }

            // check that user flag and user Id list are exclusive, same with role flag and role Id list
            if (allowedMentions != null && allowedMentions.AllowedTypes.HasValue)
            {
                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Users) &&
                    allowedMentions.UserIds != null && allowedMentions.UserIds.Count > 0)
                {
                    throw new ArgumentException("The Users flag is mutually exclusive with the list of User Ids.", nameof(allowedMentions));
                }

                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Roles) &&
                    allowedMentions.RoleIds != null && allowedMentions.RoleIds.Count > 0)
                {
                    throw new ArgumentException("The Roles flag is mutually exclusive with the list of Role Ids.", nameof(allowedMentions));
                }
            }

            if (flags is not MessageFlags.None and not MessageFlags.SuppressEmbeds)
            {
                throw new ArgumentException("The only valid MessageFlags are SuppressEmbeds and none.", nameof(flags));
            }

            var args = new UploadWebhookFileParams(attachments.ToArray())
            {
                AvatarUrl         = avatarUrl,
                Username          = username, Content = text,
                IsTTS             = isTTS,
                Embeds            = embeds.Any() ? embeds.Select(x => x.ToModel()).ToArray() : Optional <API.Embed[]> .Unspecified,
                AllowedMentions   = allowedMentions?.ToModel() ?? Optional <API.AllowedMentions> .Unspecified,
                MessageComponents = components?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Optional <API.ActionRowComponent[]> .Unspecified,
                Flags             = flags
            };
            var msg = await client.ApiClient.UploadWebhookFileAsync(client.Webhook.Id, args, options).ConfigureAwait(false);

            return(msg.Id);
        }
コード例 #11
0
        public static async Task <ulong> SendMessageAsync(DiscordWebhookClient client,
                                                          string text, bool isTTS, IEnumerable <Embed> embeds, string username, string avatarUrl,
                                                          AllowedMentions allowedMentions, RequestOptions options, MessageComponent components, MessageFlags flags)
        {
            var args = new CreateWebhookMessageParams
            {
                Content = text,
                IsTTS   = isTTS,
                Flags   = flags
            };

            if (embeds != null)
            {
                args.Embeds = embeds.Select(x => x.ToModel()).ToArray();
            }
            if (username != null)
            {
                args.Username = username;
            }
            if (avatarUrl != null)
            {
                args.AvatarUrl = avatarUrl;
            }
            if (allowedMentions != null)
            {
                args.AllowedMentions = allowedMentions.ToModel();
            }
            if (components != null)
            {
                args.Components = components?.Components.Select(x => new API.ActionRowComponent(x)).ToArray();
            }

            if (flags is not MessageFlags.None and not MessageFlags.SuppressEmbeds)
            {
                throw new ArgumentException("The only valid MessageFlags are SuppressEmbeds and none.", nameof(flags));
            }

            var model = await client.ApiClient.CreateWebhookMessageAsync(client.Webhook.Id, args, options : options).ConfigureAwait(false);

            return(model.Id);
        }
コード例 #12
0
ファイル: ChannelHelper.cs プロジェクト: yebafan/Discord.Net
        /// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
        public static async Task <RestUserMessage> SendFileAsync(IMessageChannel channel, BaseDiscordClient client,
                                                                 Stream stream, string filename, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, MessageReference messageReference, RequestOptions options, bool isSpoiler)
        {
            Preconditions.AtMost(allowedMentions?.RoleIds?.Count ?? 0, 100, nameof(allowedMentions.RoleIds), "A max of 100 role Ids are allowed.");
            Preconditions.AtMost(allowedMentions?.UserIds?.Count ?? 0, 100, nameof(allowedMentions.UserIds), "A max of 100 user Ids are allowed.");

            // check that user flag and user Id list are exclusive, same with role flag and role Id list
            if (allowedMentions != null && allowedMentions.AllowedTypes.HasValue)
            {
                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Users) &&
                    allowedMentions.UserIds != null && allowedMentions.UserIds.Count > 0)
                {
                    throw new ArgumentException("The Users flag is mutually exclusive with the list of User Ids.", nameof(allowedMentions));
                }

                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Roles) &&
                    allowedMentions.RoleIds != null && allowedMentions.RoleIds.Count > 0)
                {
                    throw new ArgumentException("The Roles flag is mutually exclusive with the list of Role Ids.", nameof(allowedMentions));
                }
            }

            var args = new UploadFileParams(stream)
            {
                Filename = filename, Content = text, IsTTS = isTTS, Embed = embed?.ToModel() ?? Optional <API.Embed> .Unspecified, AllowedMentions = allowedMentions?.ToModel() ?? Optional <API.AllowedMentions> .Unspecified, MessageReference = messageReference?.ToModel() ?? Optional <API.MessageReference> .Unspecified, IsSpoiler = isSpoiler
            };
            var model = await client.ApiClient.UploadFileAsync(channel.Id, args, options).ConfigureAwait(false);

            return(RestUserMessage.Create(client, channel, client.CurrentUser, model));
        }
コード例 #13
0
        public static async Task <RestUserMessage> SendInteractionFileAsync(IMessageChannel channel, BaseDiscordClient client,
                                                                            InteractionData interaction, Stream stream, string filename, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler, MessageReferenceParams reference, InteractionMessageType type, bool ghostMessage, InteractionRow[] components)
        {
            if (interaction == null)
            {
                return(await SendFileAsync(channel, client, stream, filename, text, isTTS, embed, allowedMentions, options, isSpoiler, reference, components).ConfigureAwait(false));
            }
            Preconditions.AtMost(allowedMentions?.RoleIds?.Count ?? 0, 100, nameof(allowedMentions.RoleIds), "A max of 100 role Ids are allowed.");
            Preconditions.AtMost(allowedMentions?.UserIds?.Count ?? 0, 100, nameof(allowedMentions.UserIds), "A max of 100 user Ids are allowed.");

            // check that user flag and user Id list are exclusive, same with role flag and role Id list
            if (allowedMentions != null && allowedMentions.AllowedTypes.HasValue)
            {
                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Users) &&
                    allowedMentions.UserIds != null && allowedMentions.UserIds.Count > 0)
                {
                    throw new ArgumentException("The Users flag is mutually exclusive with the list of User Ids.", nameof(allowedMentions));
                }

                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Roles) &&
                    allowedMentions.RoleIds != null && allowedMentions.RoleIds.Count > 0)
                {
                    throw new ArgumentException("The Roles flag is mutually exclusive with the list of Role Ids.", nameof(allowedMentions));
                }
            }
            UploadInteractionFileParams args = new UploadInteractionFileParams
            {
                Type = type,
                Data = new UploadWebhookFileParams(stream)
                {
                    Filename = filename, Content = text, IsTTS = isTTS, AllowedMentions = allowedMentions?.ToModel() ?? Optional <API.AllowedMentions> .Unspecified, IsSpoiler = isSpoiler, Components = components?.Select(x => x.ToModel()).ToArray()
                }
            };

            if (embed != null)
            {
                args.Data.Embeds = new API.EmbedJson[] { embed.ToModel() }
            }
            ;

            if (ghostMessage)
            {
                args.Data.Flags = 64;
            }


            API.MessageJson model = await client.ApiClient.UploadInteractionFileAsync(channel.Id, interaction, args, options).ConfigureAwait(false);

            return(RestUserMessage.Create(client, channel, client.CurrentUser, model));
        }
コード例 #14
0
        public static async Task <RestUserMessage> SendInteractionMessageAsync(IMessageChannel channel, BaseDiscordClient client,
                                                                               InteractionData interaction, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, MessageReferenceParams reference, RequestOptions options, InteractionMessageType type, bool ghostMessage, InteractionRow[] components)
        {
            if (interaction == null)
            {
                return(await SendMessageAsync(channel, client, text, isTTS, embed, allowedMentions, reference, options, components).ConfigureAwait(false));
            }

            Preconditions.AtMost(allowedMentions?.RoleIds?.Count ?? 0, 100, nameof(allowedMentions.RoleIds), "A max of 100 role Ids are allowed.");
            Preconditions.AtMost(allowedMentions?.UserIds?.Count ?? 0, 100, nameof(allowedMentions.UserIds), "A max of 100 user Ids are allowed.");

            // check that user flag and user Id list are exclusive, same with role flag and role Id list
            if (allowedMentions != null && allowedMentions.AllowedTypes.HasValue)
            {
                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Users) &&
                    allowedMentions.UserIds != null && allowedMentions.UserIds.Count > 0)
                {
                    throw new ArgumentException("The Users flag is mutually exclusive with the list of User Ids.", nameof(allowedMentions));
                }

                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Roles) &&
                    allowedMentions.RoleIds != null && allowedMentions.RoleIds.Count > 0)
                {
                    throw new ArgumentException("The Roles flag is mutually exclusive with the list of Role Ids.", nameof(allowedMentions));
                }
            }

            CreateInteractionMessageParams args = new CreateInteractionMessageParams()
            {
                Type = type
            };

            switch (type)
            {
            case InteractionMessageType.ChannelMessageWithSource:
                args.Data = new CreateWebhookMessageParams(text)
                {
                    IsTTS           = isTTS,
                    AllowedMentions = allowedMentions?.ToModel()
                };
                break;
            }
            if (components != null)
            {
                args.Data.Components = components?.Select(x => x.ToModel()).ToArray();
            }

            if (embed != null)
            {
                args.Data.Embeds = new API.EmbedJson[] { embed.ToModel() }
            }
            ;

            if (ghostMessage)
            {
                args.Data.Flags = 64;
            }
            API.MessageJson model = await client.ApiClient.CreateInteractionMessageAsync(channel.Id, interaction, args, options).ConfigureAwait(false);

            if (model == null)
            {
                return(null);
            }
            return(RestUserMessage.Create(client, channel, client.CurrentUser, model));
        }
コード例 #15
0
        /// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
        public static async Task <RestUserMessage> SendMessageAsync(IMessageChannel channel, BaseDiscordClient client,
                                                                    string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, MessageReferenceParams reference, RequestOptions options, InteractionRow[] components)
        {
            Preconditions.AtMost(allowedMentions?.RoleIds?.Count ?? 0, 100, nameof(allowedMentions.RoleIds), "A max of 100 role Ids are allowed.");
            Preconditions.AtMost(allowedMentions?.UserIds?.Count ?? 0, 100, nameof(allowedMentions.UserIds), "A max of 100 user Ids are allowed.");

            // check that user flag and user Id list are exclusive, same with role flag and role Id list
            if (allowedMentions != null && allowedMentions.AllowedTypes.HasValue)
            {
                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Users) &&
                    allowedMentions.UserIds != null && allowedMentions.UserIds.Count > 0)
                {
                    throw new ArgumentException("The Users flag is mutually exclusive with the list of User Ids.", nameof(allowedMentions));
                }

                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Roles) &&
                    allowedMentions.RoleIds != null && allowedMentions.RoleIds.Count > 0)
                {
                    throw new ArgumentException("The Roles flag is mutually exclusive with the list of Role Ids.", nameof(allowedMentions));
                }
            }

            CreateMessageParams args = new CreateMessageParams(text)
            {
                IsTTS            = isTTS, Embed = embed?.ToModel(), AllowedMentions = allowedMentions?.ToModel(),
                MessageReference = reference?.ToModel(),
                Components       = components?.Select(x => x.ToModel()).ToArray()
            };

            //Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(args, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { ContractResolver = new DiscordContractResolver() }));
            API.MessageJson model = await client.ApiClient.CreateMessageAsync(channel.Id, args, options).ConfigureAwait(false);

            return(RestUserMessage.Create(client, channel, client.CurrentUser, model));
        }