-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.ts
164 lines (147 loc) · 4.2 KB
/
functions.ts
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { QuackJSTime } from '../global'
import * as fs from 'fs'
import path from 'path'
import ms from 'ms'
import { Log } from './log.js'
import { Locale } from '../handlers/locale.js'
/**
* A function which returns an object containing useful time numbers and information.
*
* @param date - The date that will be processed.
* @returns An object for time purposes.
*/
export const Time = (date = new Date()): QuackJSTime => {
const d = new Date(date)
return {
ms: d.getTime(),
UTC: d.toUTCString(),
ISO: d.toISOString(),
TZ: d.toString(),
date: d.getDate(),
month: d.getMonth() + 1,
year: d.getFullYear(),
hours: d.getHours(),
minutes: d.getMinutes(),
seconds: d.getSeconds(),
}
}
/**
* A function for handling errors.
*
* @param e - The error to be processed.
*/
export const Exception = (e: Error): void => {
fs.appendFileSync('errors.txt', `${Time().TZ}\n${e.stack}\n───────────────\n`)
Log(Locale().utils.errors.error, 'e')
}
/**
* A function for grabbing all of the files in a directory.
*
* @param directory - The directory to be scanned.
* @returns All of the files' paths in a directory.
*/
export const GetFiles = (directory: string): string[] => {
const recursivelyGetFiles = (dir: string, allFiles: string[] = []) => {
const files: string[] = fs.readdirSync(dir)
files.forEach((file) => {
if (fs.lstatSync(dir + '/' + file).isDirectory()) {
allFiles = recursivelyGetFiles(dir + '/' + file, allFiles)
} else {
allFiles?.push(path.join('./', dir, '/', file))
}
})
return allFiles
}
return recursivelyGetFiles(directory)
}
/**
* A function for creating directories.
*
* @param name - The name of the directory to be made.
* @returns `true` if the directory was made, `false` if the directory was not made.
*/
export const MkDir = (name: string): boolean => {
if (!fs.existsSync(`./${name}`)) {
fs.mkdirSync(`./${name}`)
return true
} else {
return false
}
}
/**
* A function for padding a number with zeros; usefully for ticketing modules.
*
* @param number - The number to be parsed.
* @param length - The length of which the string should be.
* @returns A string with the `number` and the number of padded zeros in front of it.
*/
export const PadWithZeros = (number: number, length: number): string => {
let n = '' + number
while (n.length < length) {
n = '0' + n
}
return n
}
/**
* A function for generating a random number between min and max inclusively.
*
* @param min - The minimum amount the function will return.
* @param max - The maximum amount the function will return.
* @returns A random number between `min` and `max`.
*/
export const Random = (min: number, max: number): number => {
return Math.floor(Math.random() * (max + 1 - min)) + min
}
/**
* A function for randomly capitalizing a string.
*
* @param string - The string to be randomly capitalized.
* @returns A string with random capitalization.
*/
export const RandomizeCapitalization = (string: string): string => {
return string
.split('')
.map((chr) => (Random(0, 1) ? chr.toLowerCase() : chr.toUpperCase()))
.join('')
}
/**
* A function for generating a random id.
*
* @param length - The length for which the id will be.
* @param base - The base for which the id will be.
* @returns Returns a random id with a base of `base` and length of `length`.
*/
export const GenerateID = (length: number = 8, base: number = 16): string => {
const id = 'x'
.repeat(length)
.split('')
.map(() =>
Math.floor(Math.random() * base)
.toString(base)
.slice(-1),
)
.join('')
return RandomizeCapitalization(id)
}
/**
* A function for grabbing the id of a custom emoji.
*
* @param e - The emoji to be parsed.
* @returns The string representation of the emoji.
*/
export const Emoji = (e: string): string => {
return e.replace(/<:.+:|>/g, '')
}
/**
* A function for generating a backup of a file.
*
* @param file - The file that will be backed up.
*/
export const Backup = (file: string): void => {
const time = Time()
fs.copyFile(file, `./backups/${time.year}-${time.month}-${time.date}-${file}`, (err) => {
if (err) return Exception(err)
Log(Locale().utils.success.backup.replace(/{file}/g, file), 's')
})
}
export const MS = ms