-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.js
34 lines (32 loc) · 1.26 KB
/
utility.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
exports.parseEmbedUser = (message) => {
// check that the message has an embed
if (message.embeds.length === 0) return null;
const embed = message.embeds[0].toJSON();
if (!embed.description) return null;
const description = embed.description.split(/ +/);
if (description.length < 6) return null;
return description[5].slice(2, -1)
}
exports.searchChannel = async (bot, channelID, userID) => {
try {
const channel = bot.channels.resolve(channelID);
const message_bulk = await channel.messages.fetch({ limit: 100 });
return message_bulk.find(message => exports.parseEmbedUser(message) === userID);
} catch (error) {
throw 'utility.js: searchChannel() There was an error searching channel: ' + channelID + ('\n' + error).replace(/\n/g, '\n\t');
}
}
exports.deleteAllChannels = async (bot, channelIDs, userID) => {
try {
let promises = await channelIDs.map(async id => {
let message = await exports.searchChannel(bot, id, userID);
if (!message) return null;
return message.delete();
});
// remove nulls
promises = promises.filter(p => p);
return await Promise.allSettled(promises);
} catch (error) {
throw 'utility.js: deleteAllChannels() There was an error deleting messages: ' + ('\n' + error).replace(/\n/g, '\n\t');
}
}