0

At this time, i'm trying to code a rp bot on discord and i'm creating a command for creating character profile. And I am blocking on the part when I ask how old is the character. If the message is an int, no problem, but when it's a str or something else, there is an error and I don't know how I can send a message to say like "you can only type number" when a user put something that is not a number. Thank you for understanding

  • Sending a message is as simple as `await bot.send_message(message.channel, "...")`. You'll want to have that in a loop that `try`s to do the conversion, and only breaks out of the loop if there's no error. See [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Patrick Haugh Sep 02 '18 at 14:22
  • So I tried what you said, but I think the `try` and the `except` in the `while` loop work only with the `input` and not with the `await bot.wait_for_message(author=user)` – Maxime Bores Sep 02 '18 at 22:24
  • Sure you can. Note that the `input` isn't what potentially raises the error, the call to `int` is. – Patrick Haugh Sep 02 '18 at 23:18

1 Answers1

0

Well strictly speaking the response will always be a string because that's the object type returned. What I do is extend the String prototype with a static function.

String.prototype.isNumber = function() {
    return /^\d+$/.test(this);
};

then you can do this on any string:

if(message.isNumber()){
    //do stuff
}
Astrydax
  • 463
  • 3
  • 15