問(wèn)題描述
這是我在這里,我想添加一個(gè)簡(jiǎn)單的骰子滾動(dòng)功能,它不會(huì)占用多條消息,所以我不會(huì)向我所在的服務(wù)器發(fā)送垃圾郵件.
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.
到目前為止,我的擲骰子本身的準(zhǔn)系統(tǒng)代碼在這里工作:
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);
}
現(xiàn)在它只是吐出像這樣的數(shù)字
And as of right now it just spits out the number like
96
這...對(duì)于我賦予了如此多個(gè)性的這個(gè)機(jī)器人來(lái)說(shuō)非常不合時(shí)宜.我想要的是在它吐出的數(shù)字之前和之后有文字,就像這樣.
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!
如果我將這樣的內(nèi)容放入代碼中,它會(huì)產(chǎn)生部分相同的效果,它只會(huì)發(fā)送非常尷尬的兩條不同的消息,這不是我想要的.
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!
推薦答案
我認(rèn)為您本質(zhì)上是在問(wèn)如何將字符串連接在一起.這是通過(guò)加號(hào)運(yùn)算符完成的.如果任何操作數(shù)是字符串,它將所有變量都視為字符串:
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!"
}
或者,您可以像這樣使用模板參數(shù)(那些是反引號(hào),而不是引號(hào)):
Alternatively, you can use template params like so (those are backticks, not quotes):
message.channel.send(`You got... ${response}!`);
這篇關(guān)于基本的一條消息骰子滾輪?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!