discord.js 问题。如何让机器人对一个命令做出不同的响应?

时间:2020-12-28 00:46:47

标签: javascript visual-studio visual-studio-code discord.js

所以我有命令 -joke 我希望机器人对该命令给出随机响应。 如何将其添加到此代码中:

module.exports = {
name: 'joke',
description: "tells a joke",
execute(message, args) {
   message.channel.send('Why do we tell actors to break a leg? Because every 
   play has a cast');
 }
}

2 个答案:

答案 0 :(得分:0)

使用数组来存储你的笑话。

假设你的数组被称为笑话:

const getJoke = () => {
 return jokes[Math.floor(Math.random() * jokes.length))];
}

这应该会从您的阵列中随机获得一个。只需将该函数的输出发送给您的用户

答案 1 :(得分:0)

用笑话创建一个数组:

const jokes = ["Joke1", "Joke2", "Joke3"];

并从随机索引的数组中获取一个笑话:

const randJoke = jokes[Math.floor(Math.random() * jokes.length)];

然后在当前文本频道发送随机笑话:

message.channel.send(randJoke);