Basic one-message dice roller?

淺唱寂寞╮ 提交于 2019-12-11 15:11:35

问题


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}!`);


来源:https://stackoverflow.com/questions/56012519/basic-one-message-dice-roller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!