投币指令

时间:2018-07-23 05:25:06

标签: node.js discord discord.js

昨天我开始编码我的第一个Discord机器人,已经有了掷骰子的命令,现在想要掷硬币的命令。

我该如何编码呢?​​

使用Node.JS和Discord.JS库。

3 个答案:

答案 0 :(得分:1)

在您的命令中,您可以使用它生成一个随机数(01):Math.random()为您提供一个介于0和1之间的数字(不包括在内),而{{ 3}}将该数字四舍五入为0或1。

Math.round(Math.random()); //O or 1

然后,您可以使用该号码发送频道,无论频道是头还是尾。

答案 1 :(得分:1)

if(command == "ht")
{
      function doRandHT() {
var rand = ['HEADS!','TAILS!'];

return rand[Math.floor(Math.random()*rand.length)];
}

 const embed = {
"title": `Here is the winner!`,
"description": doRandHT(),
"color": 7584788,
};
message.channel.send({ embed });


};

您可以对Discord.js中的每个随机命令使用此简单命令,我还将其与嵌入设计集成在一起,因此它将放在设计良好的盒子中。

警告:您可能需要在代码中进行更改才能使其正常运行。有些使用不同的命令结构,仅粘贴它就不要期望它起作用。进行更改,查看其他命令并根据它们进行更改。

这是经过充分测试并且可以正常运行的命令。

答案 2 :(得分:0)

如果您不了解JavaScript,请转到Mozilla Developer Network (MDN)

如果您想了解有关Discord.js库的信息,请转到the Discord.js Documentation并使用右上角的搜索栏。

const Discord = require('discord.js');
const bot = new Discord.Client({
  disableEveryone: true
});

//The 'message' event, emitted whenever somebody says something in a text channel
bot.on('message', async koolMessage => {
  if (message.content.startsWith("coinflip")) {
    //If the message's content starts with "coinflip" run the following:
    let outcomes = ["Heads", "Tails"];
    //An array that has the possible outcomes from flipping a coin, heads and tails
    let outcomesIndex = Math.round(Math.random() * outcomes.length);
    //This will be what index of the outcomes array should be selected (arrays start at 0)
    //Math.round() rounds the parameter inside, in this case, Math.random()
    koolMessage.channel.send(outcomes[outcomesIndex]);
    //'koolMessage' is what we decided to call the message in the 'message' event, above. It can be called whatever you'd like
    //'channel' is the text channel in which 'koolMessage' was sent
    //The send() function sends a message with the included argument (e.g. send("hello there!")). It must be sent in a channel, in this case, the channel in which the 'koolMessage' from the user was sent
    //If we would have outcomes[0], the output would be "Heads", if we would have outcomes[1] the output would be "Tails", so we randomize it, giving us either outcomes[0] or outcomes[1]
  }
});

bot.login('YOUR TOKEN');