简体   繁体   中英

discord bot ref (C#)

I kind of suck att everything C# but what I am trying to do is that you will write "b!roll [number]" and it will generate a random number from 0 to [number]. But I don't understand how to use the number you input at the command in other parts so that I can use "number" as it says in my code in other parts like a var.

     commands.CreateCommand("!random")
            .Parameter("number", ParameterType.Required)
            .Do(async (e) =>
            {
                Random random = new Random();

                int randomNumber = random.Next(0, number);

                await e.Channel.SendMessage(":game_die: You rolled your **" + number "** sided die and got the number **" + randomNumber + "**!");
            });

This is probably really simple but I can't seem to understand but I would love if someone helped so that I can understand C# better.

You can extract the parameter with e.GetArg(parameterName) :

int number;
if (!int.TryParse(e.GetArg("number"), out number)
{
    // handle error
}

// rest of your logics...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM