【发布时间】:2021-08-13 09:05:40
【问题描述】:
我是编码新手,我正在制作一个不和谐的机器人,我想发出一个命令,让所有成员都需要等待一段时间才能再次使用它,但其中一些不需要同时等待。
例如:角色 X 的成员等待 20 秒,角色 Y 的成员等待 10 秒。
问题是我无法使用名为 humanize-duration 的 npm 包来使其工作以腾出时间。嵌入说明未显示正确的时间戳。可能是我自己搞错了。
我试图解决我的问题?
我尝试阅读 discord.js 文档、humanize-duration 文档和 this post,但无法修复。另外,我没有收到任何错误。顺便说一句,我使用了一个命令处理程序。
这是我用作示例的代码(ping 命令):
const Discord = require("discord.js");
const humanizeDuration = require("humanize-duration");
let cooldown = new Set();
module.exports = {
name: "ping",
description: "It shows the bot latency.",
execute(client, message, args) {
let guild = client.guilds.cache.get("828376495969402901");
let member = guild.members.cache.get(message.author.id);
if(cooldown.has(message.author.id) && !member.roles.cache.has("830503345251680298")) {
const remaining = humanizeDuration(cooldown.has(message.author.id) && !member.roles.cache.has("830503345251680298") - Date.now(), { units: ["s"], round: true, language: "en" });
const cooldownEmbed = new Discord.MessageEmbed()
.setTitle(`Slow down, bud`)
.setDescription(`You can run \`ping\` again in **${remaining}**.\nThe default cooldown is \`20s\`, but boosters can only wait \`10s\`!`)
.setColor(`RANDOM`);
return message.channel.send(cooldownEmbed)
}else if (cooldown.has(message.author.id) && member.roles.cache.has("830503345251680298")) {
const remaining = humanizeDuration(cooldown.has(message.author.id) && member.roles.cache.has("830503345251680298") - Date.now(), { units: ["s"], round: true, language: "en" });
const cooldownEmbed = new Discord.MessageEmbed()
.setTitle(`Too fast`)
.setDescription(`You can run this command again in **${remaining}**. (booster perk)`)
.setColor(`RANDOM`);
return message.channel.send(cooldownEmbed)
}
if(cooldown.add(message.author.id) && !member.roles.cache.has("830503345251680298")) {
setTimeout(() => {
cooldown.delete(message.author.id) && !member.roles.cache.has("830503345251680298")
}, 10000)
}else if(cooldown.add(message.author.id) && member.roles.cache.has("830503345251680298")) {
setTimeout(() => {
cooldown.delete(message.author.id) && member.roles.cache.has("830503345251680298")
}, 2000)
}
message.channel.send('Pinging...').then(message => {
const embed = new Discord.MessageEmbed()
.setDescription(`Pong! :ping_pong: \`${Date.now() - message.createdTimestamp} ms\``)
.setColor(`RANDOM`);
message.edit(" ", embed);
})
}
}
非常感谢您提前提供的帮助。
亲切的问候。
【问题讨论】:
标签: javascript discord.js bots