如果在discord bot中字符串为空

时间:2018-12-15 10:53:25

标签: c# bots discord

我正在尝试检查输入命令是否包含给定的字符串,否则请返回一个示例。

用户键入$random,后跟将随机选择的选项。 如果没有给出选项,则应返回示例。

if statement不能解决问题。

这是我的代码段。

 [Command("random")]
    public async Task random([Remainder]string message)
    {

        var embed = new EmbedBuilder();

        if (String.IsNullOrEmpty(message))
        {
            embed.WithDescription("Example: $random option1|option2|option3");
        }

        string[] options = message.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

        Random randNr = new Random();
        string slection = options[randNr.Next(0, options.Length)];


        embed.WithTitle("Choise for " + Context.User.Username);
        embed.WithDescription(slection);
        embed.WithColor(new Color(Color.DarkGreen.RawValue));


        await Context.Channel.SendMessageAsync("", false, embed);
    }

1 个答案:

答案 0 :(得分:1)

当您检测到选项为null或为空时,不需要返回吗?

[Command("random")]
public async Task random([Remainder]string message)
{

    var embed = new EmbedBuilder();

    if (String.IsNullOrEmpty(message))
    {
        embed.WithDescription("Example: $random option1|option2|option3");
        // miss return proecess? 
    }

    string[] options = message.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

    Random randNr = new Random();
    string slection = options[randNr.Next(0, options.Length)];


    embed.WithTitle("Choise for " + Context.User.Username);
    embed.WithDescription(slection);
    embed.WithColor(new Color(Color.DarkGreen.RawValue));


    await Context.Channel.SendMessageAsync("", false, embed);
}