【问题标题】:Creating a Discord-Bot command with "multiple" parameters使用“多个”参数创建 Discord-Bot 命令
【发布时间】:2018-01-23 10:09:10
【问题描述】:

我正在尝试设计一个命令,允许用户使用特定命令调用机器人,并让机器人读取用户传入的整个字符串。

这里的问题是,discord bot 只接受命令后面的第一个单词,而忽略了所有其他单词。

commands.CreateCommand("sarcastify").Parameter("input", ParameterType.Multiple).Do(async (e) =>
        {

            String userInput = e.GetArg("input");
            String output = sarcastify(userInput);

            await e.Channel.SendMessage(output);               

        });

如果你不知道我正在制作一个允许用户请求输入文本的“讽刺”版本的命令,也就是那个海绵宝宝表情包。

反正调用命令的时候,只取第一个词

//In discord chat window
$sarcastify This program works


//Response
tHiS

Discord bot 有没有办法将整个字符串提取出来进行解析?

【问题讨论】:

    标签: c# discord discord.net


    【解决方案1】:

    我不完全确定 Discord.NET API 如何处理参数名称(除了单个参数),但我知道当 discord 读取命令参数时,它会将它们存储在一个名为“Args”的数组中像普通的字符串数组一样访问和使用。

    如果你跑了

    $sarcastify this program works
    

    然后在命令函数内:e.Args[0] = "this", e.Args[1] = "program", e.Args[2] = "works" e.t.c

    因此,如果您希望输入为一个字符串,我们可以在数组上使用 string.Join() 方法,因此您的代码如下所示:

    commands.CreateCommand("sarcastify")
        .Parameter("input",ParameterType.Multiple)
        .Do(async (e) =>
        {
            String userInput = string.Join(" ", e.Args);
            String output = sarcastify(userInput);
            await e.Channel.SendMessage(output);               
        });
    

    使用此代码输入:

    $sarcastify this program works
    

    会产生所需的输出:这个计划工作

    :)

    【讨论】:

      猜你喜欢
      • 2021-02-01
      • 2018-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-25
      • 2018-10-21
      • 2021-03-01
      相关资源
      最近更新 更多