Reputation: 193
I've been having some hard time writing this and I searched a lot for any similar questions but didn't find any.
So I have this Schema:
new mongoose.Schema({
Guild: String,
Username: String,
UserID: String,
Channel: String,
Type: String,
Reason: String,
Time: Date,
})
And I want to automatically delete the channel after x time of its creation, the Time
is saved as <channel>.createdTimestamp
And here is the condition of the event:
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"));
So as I said I just need some help on how to check if x ms has passed to use it in an if()
statement!
Upvotes: 0
Views: 150
Reputation: 135
You can use moment to subtract the diff in seconds pretty easy.
var now = moment(new Date());
var end = moment(discordChannelCreationTimestamp);
var duration = moment.duration(now.diff(end));
var seconds = duration.asSeconds();
Upvotes: 1
Reputation: 582
You can use Date.now()
to get the current time in milliseconds, then subtract whatever time you need (in milliseconds), and check that versus your created timestamp, if the number is greater it's been more than a minute!
const minuteInMs = 60000;
if (Date.now() - minuteInMs > channel.createdTimestamp) {
console.log("more than a minute");
} else {
console.log("less than a minute");
}
Upvotes: 1