Add snowflake timestamp converter functions
This commit is contained in:
parent
8026cf0cad
commit
8439512f1a
4 changed files with 39 additions and 27 deletions
|
@ -2,6 +2,8 @@
|
|||
|
||||
const DiscordTypes = require("discord-api-types/v10")
|
||||
|
||||
const EPOCH = 1420070400000
|
||||
|
||||
/**
|
||||
* @param {string[]} userRoles
|
||||
* @param {DiscordTypes.APIGuild["roles"]} guildRoles
|
||||
|
@ -56,5 +58,17 @@ function isWebhookMessage(message) {
|
|||
return message.webhook_id && !isInteractionResponse
|
||||
}
|
||||
|
||||
/** @param {string} snowflake */
|
||||
function snowflakeToTimestampExact(snowflake) {
|
||||
return Number(BigInt(snowflake) >> 22n) + EPOCH
|
||||
}
|
||||
|
||||
/** @param {number} timestamp */
|
||||
function timestampToSnowflakeInexact(timestamp) {
|
||||
return String((timestamp - EPOCH) * 2**22)
|
||||
}
|
||||
|
||||
module.exports.getPermissions = getPermissions
|
||||
module.exports.isWebhookMessage = isWebhookMessage
|
||||
module.exports.snowflakeToTimestampExact = snowflakeToTimestampExact
|
||||
module.exports.timestampToSnowflakeInexact = timestampToSnowflakeInexact
|
||||
|
|
23
discord/utils.test.js
Normal file
23
discord/utils.test.js
Normal file
|
@ -0,0 +1,23 @@
|
|||
const {test} = require("supertape")
|
||||
const data = require("../test/data")
|
||||
const utils = require("./utils")
|
||||
|
||||
test("is webhook message: identifies bot interaction response as not a message", t => {
|
||||
t.equal(utils.isWebhookMessage(data.interaction_message.thinking_interaction), false)
|
||||
})
|
||||
|
||||
test("is webhook message: identifies webhook interaction response as not a message", t => {
|
||||
t.equal(utils.isWebhookMessage(data.interaction_message.thinking_interaction_without_bot_user), false)
|
||||
})
|
||||
|
||||
test("is webhook message: identifies webhook message as a message", t => {
|
||||
t.equal(utils.isWebhookMessage(data.special_message.bridge_echo_webhook), true)
|
||||
})
|
||||
|
||||
test("discord utils: converts snowflake to timestamp", t => {
|
||||
t.equal(utils.snowflakeToTimestampExact("86913608335773696"), 1440792219004)
|
||||
})
|
||||
|
||||
test("discerd utils: converts timestamp to snowflake", t => {
|
||||
t.match(utils.timestampToSnowflakeInexact(1440792219004), /^869136083357.....$/)
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue