javascript - 如何在 Discord.js 中做出随机响应
问题描述
我猜标题解释了它。我没有写下代码,如果有人解释代码的步骤会很好。每次使用“壁纸”命令时,我希望它从网站发送壁纸的随机链接。有帮手吗?
解决方案
您必须创建一个Array
包含您要发送的响应的内容。然后,您可以使用Math.random
获取 0 到 1(包括 1)之间的随机数,并Math.floor
获取范围从 0 到 arrayLength - 1 的索引。
const Responses = [
"image 1",
"image 2",
"image 3",
"image 4",
"image 5"
];
const Response = Math.floor(Math.random() * Responses.length);
console.log(Responses[Response])
client.on("message", message => {
if (message.author.bot) return false;
if (!message.guild) return false;
if (message.content.indexOf(Prefix) !== 0) return false;
const arguments = message.content.slice(Prefix.length).split(/ +/g); // Splitting the message.content into an Array with the arguments.
// Input --> !test hello
// Output --> ["test", "hello"]
const command = arguments.shift().toLowerCase();
// Removing the first element from arguments since it's the command, and storing it in a variable.
if (command == "random") {
const Response = Math.floor(Math.random() * Responses.length);
message.channel.send(`Here is your random response: ${Responses[Response]}`);
};
});
推荐阅读
- file - Unix系统数据保护
- php - 如果我手动运行 Powershell 脚本,它可以在 Server 2016 中运行,但是当我尝试使用 php 命令运行它时它不起作用
- r - 删除第一行并在 R 中的数据框中添加下一行的迭代
- unqork - 无法登录堆栈溢出 Unqork 社区
- next.js - 请求 worker-javascript.js 时 Next.js getServerSIdeProps 出错
- python - 声明变量占位符时出错
- javascript - react-select - 格式化可创建选项
- sql - SQL Server 触发器无法回滚更改
- c++ - 按名称将默认/“可选”参数传递给 C++ 函数
- python - 根据 Pandas 中的两个日期按月计算总计