简体   繁体   English

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

[英]discord.js question. how do i make the bot give a different response to one command?

So I have the command -joke I want the bot to give a random response to that command.所以我有命令-joke我希望机器人对该命令给出随机响应。 How do I add it to this code:如何将其添加到此代码中:

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');
 }
}

Use an array to store your jokes.使用数组来存储您的笑话。

Assuming your array is called jokes:假设你的数组被称为笑话:

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

This should then get you a random one from your array.然后,这应该会从您的阵列中随机获得一个。 Just send the output of that function to your user只需将 function 的 output 发送给您的用户

Create an array with jokes:创建一个带有笑话的数组:

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

and get a joke from the array at a random index:并在随机索引处从数组中获取一个笑话:

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

then send the random joke in the current text channel:然后在当前文本频道中发送随机笑话:

message.channel.send(randJoke);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM