protected override void OnValidate()
        {
            OptionalGuard.HasValue(Type);

            switch (Type.Value)
            {
            case ApplicationCommandType.Slash:
            {
                OptionalGuard.HasValue(Description, "Slash commands must have descriptions set.");

                // "CHAT_INPUT command names and command option names must match the following regex ^[\w-]{1,32}$ with the unicode flag set.
                // If there is a lowercase variant of any letters used, you must use those.
                // Characters with no lowercase variants and/or uncased letters are still allowed."
                // ☜(゚ヮ゚☜)
                break;
            }

            case ApplicationCommandType.User:
            case ApplicationCommandType.Message:
            {
                OptionalGuard.HasNoValue(Description, "Context menu commands must not have descriptions set.");
                break;
            }
            }

            ContentValidation.ApplicationCommands.ValidateName(Name);
            ContentValidation.ApplicationCommands.ValidateDescription(Description);
            ContentValidation.ApplicationCommands.ValidateOptions(Options);
        }
Beispiel #2
0
        protected override void OnValidate()
        {
            switch (EntityType)
            {
            case GuildEventTargetType.Stage:
            case GuildEventTargetType.Voice:
            {
                OptionalGuard.HasValue(ChannelId, "Stage or Voice events must have a channel ID set.");
                OptionalGuard.HasNoValue(EntityMetadata, "Stage or Voice events must not have entity metadata set.");
                break;
            }

            case GuildEventTargetType.External:
            {
                OptionalGuard.HasNoValue(ChannelId, "External events must not have a channel ID set.");
                OptionalGuard.CheckValue(EntityMetadata, metadata =>
                    {
                        Guard.IsNotNull(metadata);
                        ContentValidation.GuildEvents.Metadata.ValidateLocation(metadata.Location);
                    });
                OptionalGuard.HasValue(ScheduledEndTime, "External events must have an end time set.");
                break;
            }
            }

            ContentValidation.GuildEvents.ValidateName(Name);
            ContentValidation.GuildEvents.ValidateDescription(Description);
        }
        public static ApplicationCommandOptionChoiceJsonModel ToModel(this LocalSlashCommandOptionChoice choice, IJsonSerializer serializer)
        {
            Guard.IsNotNull(choice);
            OptionalGuard.HasValue(choice.Name);

            return(new ApplicationCommandOptionChoiceJsonModel
            {
                Name = choice.Name.Value,
                Value = serializer.GetJsonNode(choice.Value.GetValueOrDefault()) as IJsonValue
            });
        }
        public static ApplicationCommandOptionJsonModel ToModel(this LocalSlashCommandOption option, IJsonSerializer serializer)
        {
            Guard.IsNotNull(option);
            OptionalGuard.HasValue(option.Type);
            OptionalGuard.HasValue(option.Name);
            OptionalGuard.HasValue(option.Description);

            return(new ApplicationCommandOptionJsonModel
            {
                Type = option.Type.Value,
                Name = option.Name.Value,
                Description = option.Description.Value,
                Required = option.IsRequired,
                Choices = Optional.Convert(option.Choices, choices => choices?.Select(choice => choice?.ToModel(serializer)).ToArray()),
                Options = Optional.Convert(option.Options, options => options?.Select(option => option?.ToModel(serializer)).ToArray()),
                ChannelTypes = Optional.Convert(option.ChannelTypes, channelTypes => channelTypes?.ToArray())
            });
        }
        public override void Validate()
        {
            base.Validate();

            foreach (var attachment in Attachments)
            {
                OptionalGuard.HasValue(attachment.Stream);
                OptionalGuard.HasValue(attachment.FileName);

                var stream = attachment.Stream.Value;
                Guard.CanRead(stream);

                if (stream.CanSeek && stream.Length != 0 && stream.Position == stream.Length)
                {
                    Throw.InvalidDataException("The attachment stream's position is the same as its length. Did you forget to rewind it?");
                }

                // See CheckStreamType for more info.
                // Arguably this isn't "correct" to call it here as this isn't JSON serialization
                // but it doesn't matter and warning the user is more important to me.
                var streamConverter = (_serializer?.UnderlyingSerializer.ContractResolver as ContractResolver)?._streamConverter;
                streamConverter?.CheckStreamType(stream);
            }
        }