【问题标题】:Basic one-message dice roller?基本的一条消息骰子滚轮?
【发布时间】:2019-09-24 12:30:55
【问题描述】:

这是我在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);
}

到目前为止,它只是吐出像这样的数字

96

这...对于这个机器人来说非常不合时宜,我已经赋予了如此多的个性。我想要的是在它吐出的数字之前和之后有文字,就像这样。

You got... 96!

如果我将这样的内容放入代码中,它具有部分相同的效果,它只是发送非常尴尬的两条不同的消息,这不是我想要的。

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

感谢您提供故障排除帮助!谢谢!

【问题讨论】:

  • 您可以从消息中提取d100 部分并将其转换为自动响应的参数。

标签: javascript node.js discord discord.js


【解决方案1】:

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

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!"
}

或者,您可以像这样使用模板参数(那些是反引号,而不是引号):

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

【讨论】:

    猜你喜欢
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2016-08-10
    • 2018-09-24
    • 1970-01-01
    • 2021-06-25
    • 2016-12-26
    相关资源
    最近更新 更多