gpt4 book ai didi

javascript - 从 71 种可能性中生成一个随机短语

转载 作者:行者123 更新时间:2023-12-02 20:48:49 25 4
gpt4 key购买 nike

我对使用 javascript 非常陌生。我正在为我的不和谐服务器开发一个机器人,它将从数组中随机生成一个短语。我已经尝试了几种不同格式的代码,这些代码与我在此处和网络上发布的代码不同,但是当我运行它时,这些命令将不起作用,有时会阻止机器人一起运行。这是我现在实现的代码示例:

bot.on('message', msg=>{
if (msg.content === "!command"){
if (num==1){
msg.reply("phrase");
} else if (num==2){
msg.reply("phrase");
} else if (num==3){
msg.reply("phrase");
} else if (num==4){
msg.reply("phrase");
} else if (num==5){
msg.reply("phrase");
} else {
msg.reply("I should never say this");
}

我提前感谢大家提供的任何帮助,因为我在这方面遇到了很多困难!

最佳答案

您可以定义一个短语数组并使用 Math.random 来选择它:

const phrases = ['a', 'b', 'c'];

const index = Math.floor(Math.random() * phrases.length);

const phrase = phrases[index];

console.log(phrase);

关于javascript - 从 71 种可能性中生成一个随机短语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61666740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com