forked from hrishike-sh/Carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
81 lines (70 loc) · 1.95 KB
/
scripts.js
File metadata and controls
81 lines (70 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const { randomBytes } = require('crypto')
/**
* @param {String} string
*
* @returns Calculated value or null
*/
const parseAmount = (string) => {
// Checks if first digit is valid number
if (isNaN(string[0])) return null
// Return if number is like "5e4" etc.
if (!isNaN(Number(string))) return Number(string)
// Check for "m", "k" etc. and return value
if (!string.endsWith('m') && !string.endsWith('k') && !string.endsWith('b'))
return null
// Add values of m, k and b
const val = string[string.length - 1]
const rawString = string.replace(string[string.length - 1], '')
const calculated = parseInt(rawString) * StringValues[val]
// Invalid number
if (isNaN(calculated)) return null
else return calculated
}
const StringValues = {
m: 1e6,
k: 1e3,
b: 1e9,
}
const { Client } = require('discord.js')
/**
* @param {Client} client
* @param {String} id
*/
const DMUser = async (client, id, { embeds, content }) => {
try {
const user = await client.users.fetch(id)
if (user) {
;(await user.createDM()).send({
content: content ? content.toString() : '',
embeds: embeds ? [embeds] : [],
})
}
} catch (e) {
return null
}
}
/**
*
* @param {Number} min
* @param {Number} max
*
* @returns Random number between `min` and `max`
*/
const getRandom = (min, max) => {
return Math.floor(Math.random() * (max - min)) + min
}
const sleep = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms))
}
const formatTime = (time, format) => {
return `<t:${(time / 1000).toFixed(0)}:${format || 'R'}>`
}
const getRandomHash = () => {
return randomBytes(18).toString('hex')
}
module.exports.parseAmount = parseAmount
module.exports.dmUser = DMUser
module.exports.getRandom = getRandom
module.exports.sleep = sleep
module.exports.formatTime = formatTime
module.exports.getRandomHash = getRandomHash