I want to make cooldown on a command, cooldown has to work for everyone.
//Start of code or something
var cooldowns = {}
var minute = 60000;
var hour = minute * 24;
//Set cooldown
cooldowns[message.author.id] = Date.now() + hour * 24; //Set a 24 hour cooldown
//At command check
if(cooldowns[message.author.id]){
if(cooldowns[message.author.id] > Date.now()) delete cooldowns[message.author.id];
else console.log("user still has " + Math.round((cooldowns[message.author.id] - Date.now)/minute) + " minutes left"
}
You can use Discord.collection for this
This guide, describes how yo set cooldown per user, but all whats you need its channge message.author.id to message.guild.id
Related
Writing a command in djs that should display my uptime. When i run it, it displays the total number of seconds, minutes, hours, etc, but it doesn't limit seconds and minutes to seconds, or hours to 24.
const seconds = Math.floor(message.client.uptime / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
if(command === "uptime") {
message.channel.send(`The bot has been up for` + ` ` + `${days} days,` + ` ` + `${hours} hours,` + ` ` + `${minutes} minutes` + ` ` + `${seconds} seconds.`)
return;
}
Why would division limit numbers? 5678000 / 1000 will always give 5678. What you want to do is modulo (%) instead of division (/).
const seconds = message.client.uptime % 1000;
const minutes = Math.floor((message.client.uptime / 1000)) % 60;
const hours = Math.floor((message.client.uptime / (60 * 1000))) % 60;
const days = Math.floor(message.client.uptime / (60 * 1000 * 60 * 24));
if(command === "uptime") {
message.channel.send(`The bot has been up for` + ` ` + `${days} days,` + ` ` + `${hours} hours,` + ` ` + `${minutes} minutes` + ` ` + `${seconds} seconds.`)
return;
}
There is a better solution, though. Convert number to Date format, and use it to directly get minutes, hours, days, etc. - the only downside of it is it gives day of the month, not the days passed since JS Epoch time, hence (unless there is an even cleaner solution I am not aware of) you need to initialize one more variable.
Also, why concatenate strings, if you can simply pass a single string with params?
Anyway, here is my solution to your problem:
var uptime = new Date(message.client.uptime);
const days = Math.floor(message.client.uptime / (60 * 1000 * 60 * 24));
if(command === "uptime") {
message.channel.send(`The bot has been up for ${days} days, ${uptime.getHours()} hours, ${uptime.getMinutes()} minutes ${uptime.getSeconds()} seconds.`)
return;
}
There is start date and end date. Using moment ,will get difference between 2 dates in hours.
var now = moment(sessionData.StartTime);
var end = moment(sessionData.EndTime);
var duration = moment.duration(end.diff(now));
var days = duration.asHours();
it returns : 3.08 .
I want to show that difference like this- 3 hrs 15 m.
Is this possible to achieve
Try get the difference between two days using diff followed by expressing it terms of duration. Then you would be able to get exact number of days, hours, minutes
let now = moment("2017-01-26T14:21:22+0000");
let expiration = moment("2017-01-29T17:24:22+0000");
let diff = expiration.diff(now);
let diffDuration = moment.duration(diff);
let dayDiff = diffDuration.days() + "d";
let hoursDiff = diffDuration.hours() + "hrs";
let minDiff = diffDuration.minutes() + "m";
console.log(`${dayDiff} ${hoursDiff} ${minDiff}`);
I want to count how many hours and minutes between two timestamp which are generated by Date.parse. After I get the difference, I need to convert it into hours and minutes like 2.10 (means, 2 hours and 10 minutes). Once I have read that to do it you need to divide it with 3600 so I tried this code but it just gives me 0.89 instead of 1.26.
var now = new Date();
var endTime = Date.parse(now)/1000;
var startTime = Date.parse("2018-03-16 10:29:17")/1000;
$scope.timestamp_difference = startTime - endTime;
$scope.hours = $scope.timestamp_difference/3600;
How to do it right?
In case you haven't heard, Momentjs makes working with dates and times pretty damn easy in javascript i updated code for you may hope it will helps you now
var date1 = moment('03/15/2018 11:00', 'MM/DD/YYYY hh:mm'),
date2 = moment('03/16/2018 10:00', 'MM/DD/YYYY hh:mm');
var duration = moment.duration(date2.diff(date1));
//you will get 23 hours 00 minute
alert(duration.asHours().toFixed(2))
http://jsfiddle.net/dp7rzmw5/9771/
Output: 23:00 hrs
function getTimeDifference(timestampDifference) {
var hours = ~~(timestampDifference / 3600);
var minuts = ~~((timestampDifference - hours * 3600) / 60);
var seconds = timestampDifference - hours * 3600 - minuts * 60;
return hours + '.' + minuts + '.' + seconds;
}
var minutes = "0" + Math.floor(timestampDifference / 60);
// get minutes
var seconds = "0" + Math.floor(timestampDifference - minutes * 60);
// get seconds
var hour = "0" + Math.floor(timestampDifference / 3600);
//get hour
if ( hour > 1 ) {
return hour.substr(-2) + ":" + minutes.substr(-2) + ":" + seconds.substr(-2);
} else {
return minutes.substr(-2) + ":" + seconds.substr(-2);
}
// check if the hour is greater then 1 than return time with hour else return minutes.
I am implementing a function to have a countdown in Angular form current time - existing time in future. If the time has elapsed then display a message. Timer ran out in ..... HH:MM:SS
The end time. Lets call it endTime eg:
9/15/2016 9:16:00 PM
Current time. Time current moment we live.
Lets call it currentTime.
The goal is to get a timer that is Current time - end time. Save it to a Variable TotalHours.
Then calculate the time remaining for NOW to total hours. For example TotalHours = 5. And NOW is 9/14/2016 1:16:00 PM then FinalCountDown = 6:16:00 PM. That is the timer I want running...
Here is how I am doing it...
if (info.endTime) {
var CurrentTime = new Date().toLocaleString('en-US');
moment.locale(); // en
var TotalHours = moment.utc(moment(info.diffTime, "DD/MM/YYYY HH:mm:ss").diff(moment(CurrentTime, "DD/MM/YYYY HH:mm:ss"))).format("HH:mm:ss");
info.finalCountDown= TotalHours;
};
The issue here is the following:
Case 1:
endTime = 9/15/2016 9:16:00 AM
currentTime = 9/15/2016 1:21:00 PM
TotalHours = 4:05:00
But... if its after next 2 days...
Case 2:
endTime = 9/17/2016 9:16:00 AM
currentTime = 9/15/2016 1:21:00 PM
TotalHours = 4:05:00
Total hours is still the same...
I need it to add 24hours + 24 hours + extra time = 48 + 4:05:00 = 52:05:00
also I want it to display as: 52h:05m:00s
Please let me know how to solve this...
A quick and dirty solution would be to simply convert the difference between the two date/time objects to milliseconds and then do some math on the milliseconds and format the output as follows:
var currentTime = new Date("9-15-2016 13:21:00");
var endTime = new Date("9-17-2016 09:16:00");
var ms = (endTime - currentTime); // ms of difference
var days = Math.round(ms/ 86400000);
var hrs = Math.round((ms% 86400000) / 3600000);
var mins = Math.round(((ms% 86400000) % 3600000) / 60000);
$scope.finalCountdown = (days + "d:" + hrs + " h:" + mins + "m left");
You could add in a calculation for the seconds if you needed and you can do some formatting of the numbers to have leading zeros.
However, doing this doesn't account for issues such as leap-years and other data and time anomalies. A better suggestion would be to use angular-moment which utilizes Moment.js as it can handle differences and formatting with ease.
I'm working on a Track & Share Module for Pilots, the app is build with AngularJS within Ionic and Cordova framework. I'm currently developting and testing for android only.
The case:
There are four buttons, first checks if gps is enabled, second starts the tracking, third stops the tracking, fourth send the trackdata to the web-API if internet connection is stable. I can't guarantee 100% connectivity up in the air, so I can't send the trackdata on every tracked waypoint direct to the api - I have to store it until the aircraft is on the ground again.
The problem:
The tracked waypoints are (1) temporally/timely highly variable (I can't get any pattern on my test-tracks) and (2) the tracked altitude/heading/speed data isn't recorded on some trackpoints.
My flown test-route is about 40 minutes. I'm tracking every 60 seconds. So I must get at least minimum 35-40 trackpoints. But: I just get between 9 and 15 trackpoints on that route..
Trackpoints:
1442050712218|51.4514495|6.8892898|null|null|null; 12.9.2015 11:38:32
1442051327924|51.5183441|6.8183962|null|null|null; 12.9.2015 11:48:48
1442051511529|51.8569473|6.8611548|null|null|null; 12.9.2015 11:51:52
1442051732401|51.9828794|6.9063169|null|null|null; 12.9.2015 11:55:32
1442051912503|52.0233909|6.9596959|1300|64|52.25; 12.9.2015 11:58:33
1442052014828|52.0400627|7.0322238|1332|75|51.25; 12.9.2015 12:00:15
1442052517583|52.1472176|7.3813409|1307|70|51.5; 12.9.2015 12:08:38
1442052746410|52.1859082|7.5392811|1217|68|53; 12.9.2015 12:12:26
1442053119338|52.224271|7.874347|null|null|null; 12.9.2015 12:18:39
1442053401324|52.2677044|7.9679879|null|null|null; 12.9.2015 12:23:21
.service('TrackingFunctions', ['$interval', '$rootScope', '$localstorage', function($interval, $rootScope, $localstorage) {
// Erstellt einmalige "global" Referenz, dass immer die selbe Instanz angesprochen wird
var tracker;
this.doTracking = function(execTracking){
if(!execTracking){ // if no tracker start new
$localstorage.TRACKDATA = [];
return $interval(function(){
$localstorage.isTrackerActive = true;
var geo_options = {
enableHighAccuracy: true,
maximumAge: 30000,
timeout: 20000
};
function geo_success(position) {
console.log(position);
var tsmp = position.timestamp;
var lat = position.coords.latitude;
var lng = position.coords.longitude;
var alt = position.coords.altitude;
var hdg = position.coords.heading;
var spd = position.coords.speed;
var ARRAYDATA = tsmp + "|" + lat + "|" + lng + "|" + alt + "|" +hdg + "|" + spd + ";";
$localstorage.TRACKDATA.push(ARRAYDATA);
}
function geo_error() {
//$scope.alt = "Fehler " + error.message + ' Error Code: ' + error.code;
}
var wpid = navigator.geolocation.getCurrentPosition(geo_success, geo_error, geo_options);
},60000); // getrackt wird alle 60 Sekunden
} else { // if tracker cancel
$interval.cancel(execTracking);
console.log("tracker deaktiviert!");
}
};
}])
How can I fix that the app tracks every 60 seconds and the missing data will be recorded, too? The function is crucial fot the app and webservice for flight-training for solo-flights of the student pilot.
Thank you.
Just an idea:
The timeout is not a accurate task, that means, timeout is working when all other tasks are done. It's more like: You should run every 60 seconds.
In your case, I would save the position continuous and would write a «clock/timer», called every second. In your script you calculate the exact starttime, e.g.: 09:27:16. After starttime + x * 60 seconds you trigger in your clock the final saving of the position.
If this is not working, I would test it by using a Web Worker.