【问题标题】:Taking whole message string as an argument将整个消息字符串作为参数
【发布时间】:2017-06-22 13:41:50
【问题描述】:

我希望我的机器人将命令之后的整个消息作为一个整体参数。但现在情况是这样:
当我输入“!math 1 + 3”时,它只需要“1”作为参数。我希望机器人将“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

【问题讨论】:

    标签: c# string arguments discord discord.net


    【解决方案1】:

    就像 Claudiu 所说,您可以将参数括在引号中,或者...

    像这样使用[Remainder]属性,

    [Command("math")]
    public async Task Calculate([Remainder]string equation)
    {
        // Now equation will be everything after !math
        // Your code here
    }
    

    P.S.:在我们的Discord Server(查找#dotnet_discord-net)中询问未来的 Discord.Net 问题,您会更快得到答案。

    【讨论】:

    • 解释为什么这可能是明智的; RemainderAttribute 表示参数不应在下一个空格处停止读取,而是读取到命令的末尾。
    【解决方案2】:

    通常您应该将包含空格的参数括在引号中。例如:

    application.exe "一些包含空格的参数"

    【讨论】:

      【解决方案3】:

      如果其他人不使用 Discord.NET

      public async Task Calc(CommandContext ctx, [RemainingText] string equation)
      

      【讨论】:

        猜你喜欢
        • 2013-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-04
        • 2011-05-09
        • 2022-11-13
        • 1970-01-01
        相关资源
        最近更新 更多