久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

反復從javascript數組中獲取隨機項?

get random item from javascript array repeatedly?(反復從javascript數組中獲取隨機項?)
本文介紹了反復從javascript數組中獲取隨機項?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在制作一個 Discord.js 機器人,該機器人的功能之一是在用戶鍵入!fact"時從 Javascript 事實數組中返回一個隨機項.這個問題已經被其他用戶問了很多,我使用了給他們的答案中的代碼,但我遇到了一個問題:機器人卡在"一個事實上,并且不會每次都隨機遍歷列表輸入!fact".這是我到目前為止的代碼示例:

I'm making a Discord.js bot and one of the functions of the bot is to return a random item from a Javascript array of facts when a user types "!fact". This question has been asked a lot by other users and I've used code from answers given to them but I've run into one problem: the bot gets "stuck" on one fact and doesn't go through the list randomly every time "!fact" is typed. This is an example of the code I have so far:

var facts = [ "Fact 1", "Fact 2", "Fact 3", "Fact 4" ]
var fact = Math.floor(Math.random() * facts.length);

然后,讓機器人發送消息:

And then, for the bot to send the message:

client.on('message', message => {
    if (message.content === "!fact") {
        message.channel.send(facts[fact]);
        console.log('Message sent');
    }
});

但這只會一遍又一遍地返回類似 Fact 1 的內容,無論!fact"被輸入多少次.我怎樣才能讓它每次都改變?

But this would only return something like Fact 1 over and over, no matter how many times "!fact" is typed. How can I make it change every time?

推薦答案

您在啟動時使用此行僅確定一次隨機事實:

You're determining your random fact just once at startup using this line:

var fact = Math.floor(Math.random() * facts.length);

要在每次 if 條件評估為真時獲得一個隨機事實,您需要為其中的事實重新分配一個新的隨機整數:

To get a random fact each time the if-condition evaluates to true you need to re-assign a new random integer to facts in there:

    client.on('message', message => {
        if (message.content === "!fact") {
            fact = Math.floor(Math.random() * facts.length);
            message.channel.send(facts[fact]);
            console.log('Message sent');
        }

});

這篇關于反復從javascript數組中獲取隨機項?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
主站蜘蛛池模板: 国产精品日韩欧美一区二区三区 | 日韩一级免费电影 | 日韩欧美精品在线 | 精品欧美乱码久久久久久1区2区 | 99福利在线观看 | 91大神在线资源观看无广告 | av黄色片在线观看 | 在线视频日韩 | 免费观看www| 亚洲精品一区二三区不卡 | 美女视频一区二区三区 | 精品久久久久久久久久久久 | 日一区二区三区 | 国产精品一区二区不卡 | 成人精品在线视频 | 久久久精品一区二区 | 久久99精品久久 | 日韩久久精品电影 | 亚洲精品欧美一区二区三区 | 国产精品污www在线观看 | 国产精品国产三级国产aⅴ中文 | 亚洲第一女人av | 91精品国产综合久久久久久首页 | 国产精品揄拍一区二区 | 人人干人人爽 | 99pao成人国产永久免费视频 | 日日干日日操 | 少妇一区二区三区 | 国产精品视频播放 | 午夜视频在线观看一区二区 | 少妇av片| 欧美激情一区二区三区 | 人人射人人插 | 9191成人精品久久 | 亚洲日韩视频 | 欧美色专区 | 久久午夜精品福利一区二区 | 日本黄色大片免费 | 99精品欧美一区二区三区综合在线 | 国产高清久久久 | 国产成人免费视频 |