Exemplo n.º 1
0
    public override async Task <TypeConverterResult> ReadAsync(
        IInteractionContext context,
        IApplicationCommandInteractionDataOption option,
        IServiceProvider services)
    {
        await Task.Yield();

        // Get color from hex, TryParse will take care of the pound (#) symbol.
        return(await Task.FromResult(SKColor.TryParse((string)option.Value, out var color)
                                     ?TypeConverterResult.FromSuccess(color)
                                     : TypeConverterResult.FromError(
                                         InteractionCommandError.ParseFailed,
                                         "Not a valid hex!! (*/ω\*)")));
    }
Exemplo n.º 2
0
    /// <inheritdoc />
    public override async Task <TypeConverterResult> ReadAsync(
        IInteractionContext context,
        IApplicationCommandInteractionDataOption option,
        IServiceProvider services)
    {
        // By Id (1.0)
        if (ulong.TryParse((string)option.Value, NumberStyles.None, CultureInfo.InvariantCulture, out ulong id) &&
            await context.Channel.GetMessageAsync(id).ConfigureAwait(false) is T msg)
        {
            return(await Task.FromResult(TypeConverterResult.FromSuccess(msg)));
        }

        return(await Task.FromResult(TypeConverterResult.FromError(
                                         InteractionCommandError.BadArgs,
                                         "Message not found.")));
    }
Exemplo n.º 3
0
    public override async Task <TypeConverterResult> ReadAsync(
        IInteractionContext context,
        IApplicationCommandInteractionDataOption option,
        IServiceProvider services)
    {
        // Custom emote.
        if (Emote.TryParse((string)option.Value, out var emote))
        {
            return(await Task.FromResult(TypeConverterResult.FromSuccess(emote as T)));
        }

        // Unicode emoji.
        var emoji = new Emoji((string)option.Value);

        return(await Task.FromResult(string.IsNullOrEmpty(emoji.ToString()) is false
                                     ?TypeConverterResult.FromSuccess(emoji as T)
                                     : TypeConverterResult.FromError(
                                         InteractionCommandError.ParseFailed,
                                         $"Could not parse emote from input `{option.Value}`.")));
    }
        private static (string Name, IReadOnlyList <string> Values) UnpackInteractionParameter
        (
            IApplicationCommandInteractionDataOption option
        )
        {
            if (!option.Value.HasValue)
            {
                throw new InvalidOperationException();
            }

            var values      = new List <string>();
            var optionValue = option.Value.Value !;

            if (optionValue.Value is ICollection collection)
            {
                values.AddRange(collection.Cast <object>().Select(o => o.ToString() ?? string.Empty));
            }
            else
            {
                values.Add(optionValue.Value.ToString() ?? string.Empty);
            }

            return(option.Name, values);
        }
Exemplo n.º 5
0
        public override Task <TypeConverterResult> ReadAsync(IInteractionContext context, IApplicationCommandInteractionDataOption option, IServiceProvider services)
        {
            var value = option.Value as T;

            if (value is not null)
            {
                return(Task.FromResult(TypeConverterResult.FromSuccess(option.Value as T)));
            }
            else
            {
                return(Task.FromResult(TypeConverterResult.FromError(InteractionCommandError.ConvertFailed, $"Provided input cannot be read as {nameof(IChannel)}")));
            }
        }
Exemplo n.º 6
0
        public override Task <TypeConverterResult> ReadAsync(IInteractionContext context, IApplicationCommandInteractionDataOption option, IServiceProvider services)
        {
            var input = option.Value as string;

            if (Emote.TryParse(input, out var emote))
            {
                return(Task.FromResult(TypeConverterResult.FromSuccess(emote)));
            }

            if (NeoSmart.Unicode.Emoji.IsEmoji(input))
            {
                return(Task.FromResult(TypeConverterResult.FromSuccess(new Emoji(input))));
            }

            // Doesn't seem to throw anything/tell me not success...
            // return Task.FromResult(TypeConverterResult.FromError(InteractionCommandError.Unsuccessful, "That is not an emote."));
            return(Task.FromResult(TypeConverterResult.FromSuccess(null)));
        }
 /// <summary>
 ///     Will be used to read the incoming payload before executing the method body.
 /// </summary>
 /// <param name="context">Command exexution context.</param>
 /// <param name="option">Recieved option payload.</param>
 /// <param name="services">Service provider that will be used to initialize the command module.</param>
 /// <returns>The result of the read process.</returns>
 public abstract Task <TypeConverterResult> ReadAsync(IInteractionContext context, IApplicationCommandInteractionDataOption option, IServiceProvider services);
Exemplo n.º 8
0
 public override Task <TypeConverterResult> ReadAsync(IInteractionContext context, IApplicationCommandInteractionDataOption option, IServiceProvider services)
 {
     return((TimeSpan.TryParseExact((option.Value as string).ToLowerInvariant(), Formats, CultureInfo.InvariantCulture, out var timeSpan))
         ? Task.FromResult(TypeConverterResult.FromSuccess(timeSpan))
         : Task.FromResult(TypeConverterResult.FromError(InteractionCommandError.ConvertFailed, "Failed to parse TimeSpan")));
 }
        public override Task <TypeConverterResult> ReadAsync(IInteractionContext context, IApplicationCommandInteractionDataOption option, IServiceProvider services)
        {
            object value;

            if (option.Value is Optional <object> optional)
            {
                value = optional.IsSpecified ? optional.Value : default(T);
            }
            else
            {
                value = option.Value;
            }

            try
            {
                var converted = Convert.ChangeType(value, typeof(T));
                return(Task.FromResult(TypeConverterResult.FromSuccess(converted)));
            }
            catch (InvalidCastException castEx)
            {
                return(Task.FromResult(TypeConverterResult.FromError(castEx)));
            }
        }
Exemplo n.º 10
0
 public override Task <TypeConverterResult> ReadAsync(IInteractionContext context, IApplicationCommandInteractionDataOption option, IServiceProvider services)
 {
     if (Enum.TryParse <T>((string)option.Value, out var result))
     {
         return(Task.FromResult(TypeConverterResult.FromSuccess(result)));
     }
     else
     {
         return(Task.FromResult(TypeConverterResult.FromError(InteractionCommandError.ConvertFailed, $"Value {option.Value} cannot be converted to {nameof(T)}")));
     }
 }
 public override Task <TypeConverterResult> ReadAsync(IInteractionContext context, IApplicationCommandInteractionDataOption option, IServiceProvider services)
 => _typeConverter.ReadAsync(context, option, services);