-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update bot to use dynamic timestamps
Discord recently introduced dynamic in-line timestamps https://discord.com/developers/docs/reference#message-formatting-timestamp-styles which appear different to users based on their local timezone Updated the study session messages to use dynamic timezones to make scheduled event times clearer This also made the previously-introduced `!timezone` command obsolete - Replaced `!timezone` command with `!time` command which can be used by members to generate dynamic timestamps - Replaced `!help timezone` command with `!help time` command for additional information - Removed 'luxon' package, which was added only for the `!timezone` command
- Loading branch information
Showing
7 changed files
with
78 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,27 @@ | ||
function getUTCFullDate(date, separateIntoGroups = "") { | ||
if (!date) return ""; | ||
const dateUTC = date.toUTCString(); | ||
const dynamicDateFormats = { | ||
"short date time": 'f', | ||
"long date time": 'F', | ||
"short date": 'd', | ||
"long date": 'D', | ||
"short time": 't', | ||
"long time": 'T', | ||
"relative": 'R' | ||
}; | ||
|
||
// Optionally separate date, year, time, and timezone into groups | ||
const separateRegEx = /(?<onlyDate>\w{3},\s\d+\s\w{3})\s(?<year>\d{4})\s(?<time>\d+:\d+).+(?<zone>\w{3})/; | ||
const { onlyDate, year, time, zone } = dateUTC.match(separateRegEx).groups; | ||
switch (separateIntoGroups) { | ||
case "date": | ||
return onlyDate; | ||
case "year": | ||
return year; | ||
case "time": | ||
return time; | ||
case "zone": | ||
return zone; | ||
function getDynamicDateTime(date, outputFormat) { | ||
let format = 'f'; | ||
if (dynamicDateFormats[outputFormat] !== undefined) { | ||
format = dynamicDateFormats[outputFormat]; | ||
} | ||
|
||
return dateUTC.substr(0, dateUTC.length - 7); // Remove seconds and GMT string | ||
return `<t:${removeMilliseconds(date)}:${format}>` | ||
} | ||
|
||
function getUTCFullTime(date) { | ||
if (!date) return ""; | ||
const hour = date.getUTCHours(); | ||
const min = date.getUTCMinutes(); | ||
const fullHour = hour >= 10 ? hour : `0${hour}`; | ||
const fullMin = min > 10 ? min : `0${min}`; | ||
return `${fullHour}:${fullMin}`; | ||
/** | ||
* @description JavaScript includes milliseconds but Discord's dynamic datetime feature only supports up to second precision | ||
*/ | ||
function removeMilliseconds(date) { | ||
return Math.round(date.getTime() / 1000); | ||
} | ||
|
||
module.exports = { getUTCFullDate, getUTCFullTime }; | ||
module.exports = { getDynamicDateTime, dynamicDateFormats }; |