C# 将整个消息字符串作为参数
我希望我的机器人将命令后的整个消息作为一个完整的参数。但现在的情况是这样的:C# 将整个消息字符串作为参数,c#,string,arguments,discord,discord.net,C#,String,Arguments,Discord,Discord.net,我希望我的机器人将命令后的整个消息作为一个完整的参数。但现在的情况是这样的:当我输入“!math 1+3”时,它只接受“1”作为参数。我希望bot将整个字符串“1+3”作为参数 这是我的代码: [Command("math")] public async Task Calculate(string equation) { string result = new DataTable().Compute(equa
当我输入“!math 1+3”时,它只接受“1”作为参数。我希望bot将整个字符串“1+3”作为参数 这是我的代码:
[Command("math")]
public async Task Calculate(string equation)
{
string result = new DataTable().Compute(equation, null).ToString();
//Basically to calculate from the string to find the result
if (result == "NaN")
{
await Context.Channel.SendMessageAsync("Infinity or undefined");
}
else
{
await Context.Channel.SendMessageAsync(result);
}
}
我目前正在使用Discord.NET v1.0通常,您应该将包含空格的参数括在引号中。例如:
application.exe“包含空格的某些参数”通常,您应该将包含空格的参数括在引号中。例如:
application.exe“一些包含空格的参数”就像Claudiu说的,您可以将参数括在引号中,或者 像这样使用[Requires]属性
[Command("math")]
public async Task Calculate([Remainder]string equation)
{
// Now equation will be everything after !math
// Your code here
}
附言:在我们的网站(查找#dotnet_Discord-Net)中询问future Discord.Net问题,你会更快得到答案。就像克劳迪乌所说的,你可以用引号括住论点,或者 像这样使用[Requires]属性
[Command("math")]
public async Task Calculate([Remainder]string equation)
{
// Now equation will be everything after !math
// Your code here
}
附言:在我们的网站(查找#dotnet#u Discord-Net)中询问未来的Discord.Net问题,你会更快得到答案。如果其他人不使用Discord.Net
public async Task Calc(CommandContext ctx, [RemainingText] string equation)
如果其他人不使用Discord.NET
public async Task Calc(CommandContext ctx, [RemainingText] string equation)
解释一下为什么这样做可能是明智的;RemainderAttribute表示参数不应在下一个空格处停止读取,而应一直读取到命令末尾;RemainderAttribute表示参数不应在下一个空格处停止读取,而应一直读取到命令末尾。