|
1 | 1 | module.exports = async (d) => { |
2 | | - const data = d.util.aoiFunc(d); |
3 | | - const { code } = d.command; |
4 | | - if (data.err) return d.error(data.err); |
| 2 | + const data = d.util.aoiFunc(d); |
| 3 | + if (data.err) return d.error(data.err); |
5 | 4 |
|
6 | | - let [channelID = d.channel.id, amount, filters = "everyone", returnCount = "false" ] = data.inside.splits; |
| 5 | + let [channelID = d.channel?.id, amount, filters, returnCount = "false"] = data.inside.splits; |
7 | 6 |
|
8 | | - if (isNaN(amount) || amount < 1) return d.aoiError.fnError(d, "custom", { inside: data.inside }, "Amount" ); |
| 7 | + if (isNaN(amount) || amount < 1) return d.aoiError.fnError(d, "custom", { inside: data.inside }, "Invalid Amount, must be a number and greater than 0"); |
9 | 8 |
|
10 | | - amount = Number(amount) |
| 9 | + amount = Number(amount); |
11 | 10 |
|
12 | | - if (amount > 100) amount = 100 |
| 11 | + if (amount > 100) amount = 100; |
13 | 12 |
|
14 | | - const channel = await d.util.getChannel(d, channelID); |
15 | | - if (!channel) return d.aoiError.fnError(d, "channel", { inside: data.inside }); |
| 13 | + const channel = await d.util.getChannel(d, channelID); |
| 14 | + if (!channel) return d.aoiError.fnError(d, "channel", { inside: data.inside }); |
16 | 15 |
|
17 | | - let messages = await channel.messages |
18 | | - .fetch({ limit: amount, cache: true }) |
19 | | - .catch((err) => { |
20 | | - d.aoiError.fnError(d, "custom", {}, "Failed To Fetch Messages With Reason: " + err); |
| 16 | + let messages = await channel.messages.fetch({ limit: amount, cache: true }).catch((err) => { |
| 17 | + d.aoiError.fnError(d, "custom", {}, "Failed To Fetch Messages With Reason: " + err); |
21 | 18 | }); |
22 | 19 |
|
23 | | - filters = filters.toLowerCase().split(","); |
24 | | - |
25 | | - messages = [...messages.values()] |
26 | | - .filter((x) => { |
27 | | - if (filters.includes("")) return true; |
28 | | - if (filters.includes("everyone")) return true; |
29 | | - if (filters.includes("unpinned") && !x.pinned) return true; |
30 | | - if (filters.includes("bots") && x.author?.bot) return true; |
31 | | - if ( |
32 | | - filters.some( |
33 | | - (filter) => |
34 | | - filter.startsWith("user:") && x.author?.id === filter.split(":")[1] |
35 | | - ) |
36 | | - ) |
37 | | - return true; |
38 | | - return false; |
39 | | - }) |
40 | | - .slice(0, amount); |
41 | | - |
42 | | - if (!messages.length) { |
43 | | - messages = [...messages.values()].slice(0, amount); |
44 | | - } |
45 | | - |
46 | | - let result = await channel.bulkDelete(messages, true).catch((err) => { |
47 | | - d.aoiError.fnError(d, "custom" ,{}, "Failed To Delete Message With Reason: " + err); |
48 | | - }); |
49 | | - |
50 | | - result = returnCount === "true" ? messages.length : null; |
51 | | - |
52 | | - return { |
53 | | - code: d.util.setCode({ function: d.func, code, inside: data.inside, result }), |
54 | | - }; |
| 20 | + filters = filters |
| 21 | + .split("}") |
| 22 | + .map(function (x) { |
| 23 | + x = x.replace("{", "").trim(); |
| 24 | + if (x.length === 0) return null; |
| 25 | + let [key, value] = x.split(":"); |
| 26 | + return { [key]: value.split(",") }; |
| 27 | + }) |
| 28 | + .filter(Boolean); |
| 29 | + |
| 30 | + messages = [...messages.values()] |
| 31 | + .filter((x) => { |
| 32 | + let passed = false; |
| 33 | + for (let filter of filters) { |
| 34 | + let key = Object.keys(filter)[0]; |
| 35 | + let value = filter[key]; |
| 36 | + if (key === "users" && value.includes(x.author?.id)) passed = true; |
| 37 | + if (key === "words" && value.some((word) => x.content.includes(word))) passed = true; |
| 38 | + if (key === "bots" && x.author?.bot === (value === "true")) passed = true; |
| 39 | + if (key === "unpinned" && x.pinned !== (value === "true")) passed = true; |
| 40 | + } |
| 41 | + return passed; |
| 42 | + }) |
| 43 | + .slice(0, amount); |
| 44 | + |
| 45 | + const result = await channel.bulkDelete(messages, true).catch((err) => { |
| 46 | + d.aoiError.fnError(d, "custom", {}, "Failed To Delete Message With Reason: " + err); |
| 47 | + }); |
| 48 | + |
| 49 | + data.result = returnCount === "true" ? result.size : null; |
| 50 | + |
| 51 | + return { |
| 52 | + code: d.util.setCode(data) |
| 53 | + }; |
55 | 54 | }; |
0 commit comments