示例#1
0
 public static MessageReferenceParams ToModel(this MessageReferenceParams entity)
 {
     return(new MessageReferenceParams
     {
         ChannelId = entity.ChannelId,
         MessageId = entity.MessageId
     });
 }
 async Task <IUserMessage> IMessageChannel.SendInteractionFileAsync(InteractionData interaction, Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler, AllowedMentions allowedMentions, MessageReferenceParams reference, InteractionMessageType type, bool ghostMessage, InteractionRow[] components)
 => await SendInteractionFileAsync(interaction, stream, filename, text, isTTS, embed, options, isSpoiler, allowedMentions, reference, type, ghostMessage, components).ConfigureAwait(false);
 /// <inheritdoc />
 async Task <IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, Embed embed, RequestOptions options, AllowedMentions allowedMentions, MessageReferenceParams reference, InteractionRow[] components)
 => await SendMessageAsync(text, isTTS, embed, options, allowedMentions, reference, components).ConfigureAwait(false);
 /// <inheritdoc />
 public Task <RestUserMessage> SendFileAsync(Stream stream, string filename, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null, MessageReferenceParams reference = null, InteractionRow[] components = null)
 => ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, embed, allowedMentions, options, isSpoiler, reference, components);
 /// <inheritdoc />
 /// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
 public Task <RestUserMessage> SendInteractionFileAsync(InteractionData interaction, Stream stream, string filename, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null, MessageReferenceParams reference = null, InteractionMessageType type = InteractionMessageType.ChannelMessageWithSource, bool ghostMessage = false, InteractionRow[] components = null)
 => ChannelHelper.SendInteractionFileAsync(this, Discord, interaction, stream, filename, text, isTTS, embed, allowedMentions, options, isSpoiler, reference, type, ghostMessage, components);
 /// <inheritdoc />
 /// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
 public Task <RestUserMessage> SendMessageAsync(string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, AllowedMentions allowedMentions = null, MessageReferenceParams reference = null, InteractionRow[] components = null)
 => ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, embed, allowedMentions, reference, options, components);
示例#7
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> SendFileAsync(IMessageChannel channel, BaseDiscordClient client,
                                                                 Stream stream, string filename, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler, MessageReferenceParams reference, 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));
                }
            }

            UploadFileParams args = new UploadFileParams(stream)
            {
                Filename = filename, Content = text, IsTTS = isTTS, Embed = embed?.ToModel() ?? Optional <API.EmbedJson> .Unspecified, AllowedMentions = allowedMentions?.ToModel() ?? Optional <API.AllowedMentions> .Unspecified, IsSpoiler = isSpoiler, Components = components?.Select(x => x.ToModel()).ToArray()
            };

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

            return(RestUserMessage.Create(client, channel, client.CurrentUser, model));
        }
示例#8
0
        /// <exception cref="ArgumentException">
        /// <paramref name="filePath" /> is a zero-length string, contains only white space, or contains one or more
        /// invalid characters as defined by <see cref="System.IO.Path.GetInvalidPathChars"/>.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="filePath" /> is <c>null</c>.
        /// </exception>
        /// <exception cref="PathTooLongException">
        /// The specified path, file name, or both exceed the system-defined maximum length. For example, on
        /// Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260
        /// characters.
        /// </exception>
        /// <exception cref="DirectoryNotFoundException">
        /// The specified path is invalid, (for example, it is on an unmapped drive).
        /// </exception>
        /// <exception cref="UnauthorizedAccessException">
        /// <paramref name="filePath" /> specified a directory.-or- The caller does not have the required permission.
        /// </exception>
        /// <exception cref="FileNotFoundException">
        /// The file specified in <paramref name="filePath" /> was not found.
        /// </exception>
        /// <exception cref="NotSupportedException"><paramref name="filePath" /> is in an invalid format.</exception>
        /// <exception cref="IOException">An I/O error occurred while opening the file.</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 <RestUserMessage> SendFileAsync(IMessageChannel channel, BaseDiscordClient client,
                                                                 string filePath, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler, MessageReferenceParams reference, InteractionRow[] components)
        {
            string filename = Path.GetFileName(filePath);

            using (FileStream file = File.OpenRead(filePath))
                return(await SendFileAsync(channel, client, file, filename, text, isTTS, embed, allowedMentions, options, isSpoiler, reference, components).ConfigureAwait(false));
        }
示例#9
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));
        }
示例#10
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));
        }