【问题标题】:C# Discord Bot - get Userinput on a command parameterC# Discord Bot - 在命令参数上获取用户输入
【发布时间】:2017-06-25 22:59:13
【问题描述】:

我想教我的 DiscordBot 在编写命令后如何处理输入。

我以这种方式创建命令:

private void CreateCommand(string commandName, string parameterName, ParameterType parameterType , string commandValue) // Register this command by the name and the answer value
        {
            commands.CreateCommand(commandName).Parameter(parameterName, parameterType).Do(async (e) =>
            {
                await e.Channel.SendMessage(commandValue); // Bots answer
            });
        }

我用这个方法来缩短我的下一个方法的代码:

private void Add(string commandName, string commandValue, string commandDescription) // Add a simple command to the List
        {
            singleCommandList.Add(new Tuple<string, string, string>(commandName, commandValue, commandDescription));
        }

        private void Add(string commandName, string parameterName, ParameterType parameterType, string commandValue, string commandDescription) // Add commands with Parameters to the List
        {
            parameterCommandList.Add(new Tuple<string, string, ParameterType, string, string>(commandName, parameterName, parameterType, commandValue, commandDescription));
        }

这是填充我的 CommandList 的方法

 private void FillCommandList() // Add all the commands to the List
        {
            Add("test", "success", "test"); // simple Command
            Add("search", "onlineSearch", ParameterType.Multiple, Search("text to look for"), "Google it");
        }

我的问题是我不知道如何填写方法Search()的参数。我必须在那里传递什么? e.User ..?

【问题讨论】:

    标签: c# bots discord


    【解决方案1】:

    使用e.GetArg("parameterName") 获取名为“parameterName”的参数。如果ParameterTypeOptionalRequired,则此方法有效。

    如果您的ParameterTypeMultiple,请尝试:

        string search = string.Join(" ", e.Args)
    

    获取整个“在线搜索”参数。

    【讨论】:

    • 其实整个API都变了。
    【解决方案2】:

    我使用这个非官方的 Discord C# Wrapper 创建了自己的 Discord.Net 机器人:

    An unofficial .NET API Wrapper for the Discord client
    Their Discord Server

    文档中给出的示例并不总是最新的,因为它们对其 API 进行了巨大且代码破坏的重写,但使用异步模式确实做得很好。
    这应该会给你更多关于如何继续你的想法的例子和想法。

    确保使用 1.0 版本 - dev 分支。 (截至目前)

    其他所有内容都在他们的 Github 上进行了描述。如果您仍然需要答案,请加入他们的不和谐。他们很有帮助。

    【讨论】:

      猜你喜欢
      • 2020-12-26
      • 2020-12-22
      • 2018-06-25
      • 2020-10-04
      • 1970-01-01
      • 2021-04-11
      • 2019-02-12
      • 2018-01-23
      相关资源
      最近更新 更多