基本的一条消息骰子滚轮?

2022-01-10 00:00:00 discord node.js javascript discord.js

这是我在这里,我想添加一个简单的骰子滚动功能,它不会占用多条消息,所以我不会向我所在的服务器发送垃圾邮件.

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.

你得到了... 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!

推荐答案

我认为您本质上是在问如何将字符串连接在一起.这是通过加号运算符完成的.如果任何操作数是字符串,它将所有变量都视为字符串:

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

相关文章