JarFile
JarFile

Reputation: 318

C# Discord.NET Commands How to Have a String Array as an Argument

I am using Discord.NET 1.0.2 and this question is specific to Discord.NET.

I am using Discord.Commands for how I work my commands and I initialize them as so

var msg = message as SocketUserMessage;
var context = new SocketCommandContext(_client, msg);
int argPos = 0;
if(msg.HasCharPrefix('>', ref argPos))
{
    var result = await _service.ExecuteAsync(context, argPos);
}

Then in my separate class for a specific command I have

public class Command : ModuleBase<SocketCommandContext>
{
    [Command("test")]
    public async Task balanceCommmand(String[] stringArray)
    {
        // code
    }
}

However, when running the program, I receive a System.InvalidOperationException "Additional information: No type reader found for type String[], one must be specified"

I have used parameters under the async task before, but it does not seem to want to have an array of Strings as a parameter.

How would I be able to pass an array of strings as an argument to a command using Discord.Commands version 1.0.2.

If there is not a way to do this, is there a way I could mimic this by using an alternative?

I have looked around on google and haven't found someone with a similar problem as myself or even similar to it.

Upvotes: 1

Views: 4093

Answers (1)

Unknown
Unknown

Reputation: 799

Instead of just String[] you need to put params beore it like so :

public class Command : ModuleBase<SocketCommandContext>
{
    [Command("test")]
    public async Task balanceCommmand(params String[] stringArray)
    {
        // code
    }
}

params keyword : https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/params

Upvotes: 6

Related Questions