【发布时间】:2021-10-10 20:57:27
【问题描述】:
我正在为我的不和谐机器人的命令构建一个冷却“处理程序”。它有效,但有一些问题。例如,当有人输入错误的参数或没有为我的命令写任何内容时,它仍然会触发冷却。我需要一种方法来以某种方式在我的脚本或其他方式中删除该冷却时间。此外,我不能只检查参数,例如是否有单词或数字,因为有时我需要检查数据库中的一些值。我想在命令的脚本中移除冷却时间。
const { prefix: defaultPrefix } = require("../../config");
const prefixSchema = require("../../models/prefixSchema");
const languageSchema = require('../../models/languageSchema');
const { translations } = require("../../lang.json");
const discord = require("discord.js")
module.exports = async (Discord, client, message) => {
if (message.channel.type == 'dm') return;
let prefix;
let dbPrefix = await prefixSchema.findOne({ guildID: message.guild.id });
if (dbPrefix) {
prefix = dbPrefix.prefix;
} else {
prefix = defaultPrefix;
}
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const cmd = args.shift().toLowerCase();
const command =
client.commands.get(cmd) ||
client.commands.find((a) => a.aliases && a.aliases.includes(cmd));
const jezyk = await languageSchema.findOne({
guildID: message.guild.id,
})
const lang = jezyk.language
if (!cooldowns.has(command.name)) {
cooldowns.set(command.name, new discord.Collection());
}
const current_time = Date.now();
const time_stamps = cooldowns.get(command.name);
const cooldown_amount = (command.cooldown) * 1000;
if (time_stamps.has(message.author.id)) {
const expiration_time = time_stamps.get(message.author.id) + cooldown_amount;
if (current_time < expiration_time) {
//const time_left = (expiration_time - current_time) / 1000;
const time_left = new Date(expiration_time - current_time).toISOString().substr(11, 8)
const bicon = client.user.displayAvatarURL();
const cooldown_command = new discord.MessageEmbed()
.setAuthor(`${translations.ERROR_OCCUR[lang]}`, client.user.avatarURL())
.setDescription(`${translations.TIMEOUT_DESCRIPTION_1[lang]} **${time_left}** ${translations.TIMEOUT_DESCRIPTION_2[lang]} **${command.name}**!`)
.setColor(`#FF6347`)
.setFooter(`${translations.DATE[lang]}`)
.setTimestamp();
return message.channel.send(cooldown_command);
}
}
time_stamps.set(message.author.id, current_time);
setTimeout(() => time_stamps.delete(message.author.id), cooldown_amount);
try {
if (command) command.execute(message, args, client);
} catch (err) {
message.reply("wystąpił błąd krytyczny!");
console.log(err);
}
};
【问题讨论】:
标签: javascript node.js discord discord.js