抛硬币指令

Coin flip command

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

我该如何编码?

使用 Node.JS 和 Discord.JS 库。

在您的命令中,您可以使用它来生成随机数(01):Math.random() gives you a number between 0 and 1 (excluded), while Math.round() 将该数字四舍五入为 0 或 1。

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

然后您可以使用该数字在频道中发送正面或反面。

如果您对 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 (koolMessage.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');

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 中的每个随机命令使用这个普通命令,我还将它与嵌入设计集成在一起,因此它会出现在一个精心设计的盒子中。

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

这是经过全面测试且有效的随机命令。