問題描述
這是我在這里,我想添加一個簡單的骰子滾動功能,它不會占用多條消息,所以我不會向我所在的服務器發送垃圾郵件.
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}!`);
這篇關于基本的一條消息骰子滾輪?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!