Basic one-message dice roller?

2019-08-27 21:27发布

问题:

This is the same discord bot I've asked about in the last question I posted here, and I want to add a simple dice rolling function that doesn't take up multiple messages so I don't spam the server I'm in.

So far, I have the barebones code for the dice roller itself working here:

if (message.content.toLowerCase().includes("rei!d100")) {
    var response = [Math.floor(Math.random() * ((100 - 1) + 1) + 1)];

   message.channel.send(response).then().catch(console.error);
}

And as of right now it just spits out the number like

96

which is... very out of character for this bot I've given so much personality. What I want is for there to be text before and after the number it spits out, like so.

You got... 96!

If I put something like this into the code it has partially the same effect, it just sends really awkwardly and in two different messages, which isn't what I want.

if (message.content.toLowerCase().includes("rei!d100")) {
    message.channel.send("You got...");
    var response = [Math.floor(Math.random() * ((100 - 1) + 1) + 1)];

   message.channel.send(response).then().catch(console.error);
}

Any help troubleshooting is appreciated! Thanks!

回答1:

I think you are essentially asking how to concatenate strings together. That is done with the plus sign operator. If any of the operands are strings, it treats all the variables as strings:

if (message.content.toLowerCase().includes("rei!d100")) {
    var response = [Math.floor(Math.random() * ((100 - 1) + 1) + 1)];

   message.channel.send("You got... " + response + "!").then().catch(console.error);  // "You got... 96!"
}

Alternatively, you can use template params like so (those are backticks, not quotes):

message.channel.send(`You got... ${response}!`);