【问题标题】:if string is empty in discord bot如果不和谐机器人中的字符串为空
【发布时间】:2019-05-16 10:19:26
【问题描述】:

我正在尝试检查输入命令是否包含给定的字符串,如果没有,它应该返回一个示例。

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

遗憾的是,if statement 并不能解决问题。

这是我的代码 sn-p。

 [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);
    }

【问题讨论】:

  • message 包含什么?
  • 要么从if 内部返回,要么使用else 块。

标签: c# bots discord


【解决方案1】:

当你检测到 options 为 null 或 empty 时,你不需要返回吗?

[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);
}

【讨论】:

    猜你喜欢
    • 2017-10-15
    • 2021-04-18
    • 2018-03-20
    • 2021-04-28
    • 2021-02-28
    • 2020-07-03
    • 2020-09-23
    • 2021-10-30
    • 2021-08-05
    相关资源
    最近更新 更多