简体   繁体   English

如何在 Javascript 中为 discord 机器人制作随机发生器?

[英]How can I make a random generator in Javascript for a discord bot?

I'm a beginner in coding and I'm trying to learn to code a discord bot that messages a random generated things, like lets say I'm doing an advice command, what I want it to do is to pick 1 out of 10 advice tips randomly.我是编码的初学者,我正在尝试学习编写一个 discord 机器人,该机器人会发送随机生成的信息,比如我正在做一个建议命令,我想要它做的是从 10 中选择 1随机提示。 Hopefully anyone understands, thank you.希望有人能理解,谢谢。

You should first create an array for the randomly generated outcomes.您应该首先为随机生成的结果创建一个数组。 If we're talking about advice as you mentioned, you would want to type:如果我们谈论的是您提到的建议,您需要输入:

const advices = [
  'eat melon',
  'eat melon every day',
  'eat melon a lot',
  'melon is healthy',
  'melon is very epic'
];

Once you've created your array, you can generate a random value from your array by typing:创建数组后,您可以通过键入以下内容从数组中生成随机值:

// get a random number based on the array's length
var randomIndex = Math.floor(Math.random() * advices.length);
// creates the random output of your array
var randomElement = advices[randomIndex];

From there, you can simply use randomElement as the randomly generated value from your array.从那里,您可以简单地使用randomElement作为从数组中随机生成的值。

message.reply(`Here is a good advice: ${randomElement}`);

暂无
暂无

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

相关问题 如何让我的 Discord 机器人(Javascript)回答用户提出的特定问题的随机数? - How can I make my Discord bot (Javascript) answer a random number to a specific question an user asks? Discord Bot 中的随机数生成器 - Random Number Generator In Discord Bot 如何制作一个不和谐的机器人,当他运行命令时会向用户发送特定代码? 有点像帐户生成器 - How can I make a discord bot that will message user a specific code when he runs a command? Kind of like an account generator 当被问到某个问题时,如何让我的 Discord Bot (Javascript) 在标签中回复用户? - How can i make my Discord Bot (Javascript) reply to the user in tag, when a certain question is asked? 如何让我的随机数生成器停止生成相同的数字?(Javascript) - How can i make my random number generator stop producing the same numbers?(Javascript) 随机图像生成器不适用于Discord Bot - Random image generator not working for discord bot 我想为不和谐做一个随机问题机器人,但我无法弄清楚什么是错的 - I wish to make a random question bot for discord, but I can't figure out what's wrong 我怎样才能制作一个不和谐的机器人来做这样的事情? - How can I make a discord bot that does something like this? 如何让我的 Discord 机器人给用户一个角色? - How can I make my Discord bot give a user a role? 如何让 discord 机器人根据命令创建通道 - How can I make a discord bot creates a channel on a command
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM