我一直很難寫這篇文章,我搜索了很多類似的問題,但沒有找到。
所以我有這個架構:
new mongoose.Schema({
Guild: String,
Username: String,
UserID: String,
Channel: String,
Type: String,
Reason: String,
Time: Date,
})
我想在創建 x 次后自動洗掉頻道,Time保存為<channel>.createdTimestamp
這是事件的條件:
setInterval(async () => {
hSchema.find().then((data) => {
if (!data && !data.length) return
data.forEach(async (value) => {
const user = value.UserID;
var time = value.Time; // time of the creation of channel
var res = time.getTime(); // time in ms
// check if user is premium
uSchema.findOne({ User: user}, async (err, userData) => {
// if user is not premium
if (!userData) {
const destruct = ms('1m'); // time to pass before deleting
var date = res destruct;
if () { // I can't figure out how to check if x time has passed since the creation of channel
// Do stuff here
const ch = data.Channel;
const channel = client.channels.cache.get(ch)
channel.delete()
value.delete()
}
}
})
})
});
}, ms("10 seconds"));
所以正如我所說,我只需要一些幫助來檢查如何檢查 x ms 是否已通過以在if()陳述句中使用它!
uj5u.com熱心網友回復:
您可以使用Date.now()以毫秒為單位獲取當前時間,然后減去您需要的任何時間(以毫秒為單位),并檢查與您創建的時間戳相比,如果數字更大,則超過一分鐘!
const minuteInMs = 60000;
if (Date.now() - minuteInMs > channel.createdTimestamp) {
console.log("more than a minute");
} else {
console.log("less than a minute");
}
uj5u.com熱心網友回復:
您可以使用 moment 在幾秒鐘內很容易地減去差異。
var now = moment(new Date());
var end = moment(discordChannelCreationTimestamp);
var duration = moment.duration(now.diff(end));
var seconds = duration.asSeconds();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/496963.html
標籤:javascript 不和谐.js
上一篇:如何在Svelte中添加表單驗證
