C# Discord.NET 命令如何将字符串数组作为参数
C# Discord.NET Commands How to Have a String Array as an Argument
我正在使用 Discord.NET 1.0.2,这个问题特定于 Discord.NET。
我正在使用 Discord.Commands
来处理我的命令并按原样初始化它们
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);
}
然后在我单独的 class 中我有一个特定的命令
public class Command : ModuleBase<SocketCommandContext>
{
[Command("test")]
public async Task balanceCommmand(String[] stringArray)
{
// code
}
}
但是,当运行这个程序时,我收到一个System.InvalidOperationException
"Additional information: No type reader found for type String[], one must be specified"
以前在async任务下用过参数,但是好像不想用String数组作为参数。
如何使用 Discord.Commands 版本 1.0.2.
将字符串数组作为参数传递给命令
如果没有办法做到这一点,我是否可以通过使用替代方法来模仿它?
我在 google 上看了一圈,没有找到和我有类似问题甚至类似问题的人。
而不是 String[]
你需要像这样在它前面加上 params
:
public class Command : ModuleBase<SocketCommandContext>
{
[Command("test")]
public async Task balanceCommmand(params String[] stringArray)
{
// code
}
}
参数关键字:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/params
我正在使用 Discord.NET 1.0.2,这个问题特定于 Discord.NET。
我正在使用 Discord.Commands
来处理我的命令并按原样初始化它们
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);
}
然后在我单独的 class 中我有一个特定的命令
public class Command : ModuleBase<SocketCommandContext>
{
[Command("test")]
public async Task balanceCommmand(String[] stringArray)
{
// code
}
}
但是,当运行这个程序时,我收到一个System.InvalidOperationException
"Additional information: No type reader found for type String[], one must be specified"
以前在async任务下用过参数,但是好像不想用String数组作为参数。
如何使用 Discord.Commands 版本 1.0.2.
将字符串数组作为参数传递给命令如果没有办法做到这一点,我是否可以通过使用替代方法来模仿它?
我在 google 上看了一圈,没有找到和我有类似问题甚至类似问题的人。
而不是 String[]
你需要像这样在它前面加上 params
:
public class Command : ModuleBase<SocketCommandContext>
{
[Command("test")]
public async Task balanceCommmand(params String[] stringArray)
{
// code
}
}
参数关键字:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/params