-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.js
186 lines (170 loc) · 4.5 KB
/
util.js
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import Enmap from 'enmap';
import config from './config';
import UserDto from './dto/UserDto';
import ResponseDto from './dto/ResponseDto';
import Emotes from './constants/Emotes';
/**
* @type {import('discord.js').Client}
*/
let client;
/**
* Passes a client for the util functions to use.
* @param {import('discord.js').Client} newClient - The client.
*/
const setup = (newClient) => {
client = newClient;
};
const getClient = () => client;
/**
* Get an emoji from name from the default guild.
* @param {string} search - The string to search for.
* @returns {import('discord.js').EmojiIdentifierResolvable} The emoji for that name if found.
*/
const getEmoji = (search) =>
client.guilds.cache
.get(config.defaultGuild)
.emojis.cache.find((emote) => emote.name === search) || search;
/**
* Gets a member by their id
* @param query
* @returns {import('discord.js').GuildMember}
*/
const getMember = async (query) => {
const mention = /<@!?(\d+)>/;
const match = query.match(mention);
const user = match ? match[1] : query;
let result;
try {
result = await (await client.guilds.fetch(config.defaultGuild)).members.fetch(user);
} catch (e) {
// ignore errors
}
return result;
};
/**
* @param {string} name
* @returns {import('discord.js').Channel}
*/
function getChannel(name) {
return client.channels.cache.get(config.channels.get(name));
}
/**
* Fetch a user by id or mention string.
* @param {string} query - The user id or ping of the user.
* @returns {Promise<import('discord.js').User | null>} The user.
*/
const fetchUser = async (query) => {
const mention = /<@!?(\d+)>/;
const match = query.match(mention);
const user = match ? match[1] : query;
let result;
try {
result = await client.users.fetch(user);
} catch (e) {
// ignore errors
}
return result;
};
/**
* Fetch a channel by id or mention string.
* @param {string} query - The channel id or ping of the channel.
* @returns {import('discord.js').Channel | null} The channel.
*/
const fetchChannel = (query) => {
const mention = /<#(\d+)>/;
const match = query.match(mention);
const channel = match ? match[1] : query;
let result;
try {
result = client.channels.cache.get(channel);
} catch (e) {
// ignore errors
}
return result;
};
/**
* @param {string} name
* @return {import('discord.js').RoleResolvable}
*/
function getRole(name) {
return config.roles.get(name);
}
/**
* Converts a js timestamp to a discord markdown timestamp.
* @param {number} time - The js timestamp to convert.
* @param {'t'|'T'|'d'|'D'|'f'|'F'|'R'} format - The format to display the timestamp in.
* @see {@link https://discord.com/developers/docs/reference#message-formatting-timestamp-styles The Discord Docs on Timestamp Markdown}
* @returns {string} The timestamp stringified.
*/
function stringifyTimestamp(time, format = 'f') {
return `<t:${Math.round(time / 1000)}:${format}>`;
}
/**
* Transforms a string to an emojified representation of itself.
* NOTE: This will remove any non-alphanumeric characters.
* @param {String} String - The string to transform.
* @returns {String} The finished string.
*/
function makeTitle(string) {
let res = '';
for (const char of string) {
if (/[a-zA-Z0-9 ]/.test(char))
res += char === ' ' ? ' ' : getEmoji(Emotes.font[char.toLowerCase()]).toString();
}
return res;
}
// Databases
/**
* Serialize database objects.
*/
function serializer(object) {
return object.toJSON();
}
/**
* Deserialize user database objects.
*/
function deserializerUserDto(object) {
return UserDto.fromJSON(object);
}
/**
* Deserialize auto response database objects.
*/
function deserializerResponseDto(object) {
return ResponseDto.fromJSON(object);
}
/**
* Database to store user data in it.
* @type {Enmap<string, UserDto>}
* @see {@link UserDto}
*/
const userDb = new Enmap({
name: 'users',
serializer,
deserializer: deserializerUserDto,
autoEnsure: new UserDto(),
});
/**
* Database to store auto response data in it.
* @type {Enmap<string, ResponseDto>}
* @see {@link ResponseDto}
*/
const responseDb = new Enmap({
name: 'autoresponses',
serializer,
deserializer: deserializerResponseDto,
autoEnsure: new ResponseDto(),
});
export {
setup,
getEmoji,
userDb,
responseDb,
getChannel,
getRole,
getMember,
getClient,
fetchUser,
fetchChannel,
stringifyTimestamp,
makeTitle,
};