gpt4 book ai didi

javascript - 试图让我的 Discord Bot 在不循环消息的情况下重复生成答案,chooseanswer 未定义

转载 作者:行者123 更新时间:2023-12-04 09:26:26 25 4
gpt4 key购买 nike

对于我的 8ball 命令,我试图让它不断地从数组中生成新的答案。
但是,我无法正确定义它,因为它位于函数内部。
如果我将发送放入 random()函数,它会循环,这是我不想要的。
这是我的代码:

const Discord = require("discord.js");

module.exports = {
name: "8ball",
description: "Ask something to the magic 8ball.",
execute(msg, args){
let question = args.slice(1).join(" ");

if (!question){
msg.channel.send("You need to ask the magic 8ball something, dude.");
return;
}

function random(){
const answers = require("../data/8ball.json").answers;
const chooseanswer = answers[Math.floor(Math.random() * answers.length)];
return true;
}
setInterval(random, 3000);

msg.channel.send("**You shake your 8ball and think hard, yet simple.**");
msg.channel.send(`:8ball: You look into your 8ball and see "${chooseanswer}".`);

}
}

最佳答案

如我所见,您可以使用返回状态来捕获所选答案。它不会修改列表并始终返回不同的答案。
我已将该函数放在 module.exports 函数之外,并将答案 require 放在文件的顶部。

const Discord = require("discord.js");
const answersList = require("../data/8ball.json").answers;

module.exports = {
name: "8ball",
description: "Ask something to the magic 8ball.",
execute(msg, args){
let question = args.slice(1).join(" ");

if (!question){
msg.channel.send("You need to ask the magic 8ball something, dude.");
return;
}

msg.channel.send("**You shake your 8ball and think hard, yet simple.**");
msg.channel.send(":8ball: You look into your 8ball and see " + random(answersList) + ".");

}
}

function random(answers){
const chooseanswer = answers[Math.floor(Math.random() * answers.length)];
return chooseanswer;
}

关于javascript - 试图让我的 Discord Bot 在不循环消息的情况下重复生成答案,chooseanswer 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62995771/

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