pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import { LoadingEmbed } from "./../utils/defaultEmbeds.js"; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 2 | import Discord, { |
| 3 | CommandInteraction, |
| 4 | GuildMember, |
| 5 | MessageActionRow, |
| 6 | MessageButton, |
| 7 | MessageSelectMenu |
| 8 | } from "discord.js"; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 9 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
| 10 | import { WrappedCheck } from "jshaiku"; |
| 11 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
| 12 | import getEmojiByName from "../utils/getEmojiByName.js"; |
| 13 | import addPlural from "../utils/plurals.js"; |
| 14 | import client from "../utils/client.js"; |
| 15 | |
| 16 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 17 | builder // TODO: DON'T RELEASE THIS |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 18 | .setName("all") |
| 19 | .setDescription("Gives or removes a role from everyone"); |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 20 | |
| 21 | class Filter { |
| 22 | name: string; |
| 23 | data: object; |
| 24 | checkFunction: (member) => boolean; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 25 | inverted = false; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 26 | constructor(name: (data) => string | string, data: object, check: (member) => boolean) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 27 | if (typeof name === "function") { |
| 28 | this.name = name(data); |
| 29 | } else { |
| 30 | this.name = name; |
| 31 | } |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 32 | this.data = data; |
| 33 | this.checkFunction = check; |
| 34 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 35 | flip() { |
| 36 | this.inverted = true; |
| 37 | return this; |
| 38 | } |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 39 | check(member) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 40 | if (this.inverted) return !this.checkFunction(member); |
| 41 | else return this.checkFunction(member); |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
| 45 | const filterList = { |
| 46 | member: { |
| 47 | render: "Member", |
| 48 | has: { |
| 49 | render: "has", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 50 | role: (role) => |
| 51 | new Filter( |
| 52 | (data) => `Member has role <@&${data.role}>`, |
| 53 | { role: role, type: Discord.Role, render: "role" }, |
| 54 | (member) => { |
| 55 | return member.roles.cache.has(role); |
| 56 | } |
| 57 | ) |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 58 | }, |
| 59 | joined: { |
| 60 | render: "joined", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 61 | before: (date) => |
| 62 | new Filter( |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 63 | (_data) => `Joined server before <t:${Math.round(date.getTime() / 1000)}:D>`, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 64 | { date: date, type: Date, render: "before" }, |
| 65 | (member) => { |
| 66 | return member.joinedTimestamp < date.getTime(); |
| 67 | } |
| 68 | ) |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 69 | }, |
| 70 | nickname: { |
| 71 | render: "Nickname", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 72 | set: () => |
| 73 | new Filter( |
| 74 | (_data) => 'Member has a nickname set"', |
| 75 | { render: "set" }, |
| 76 | (member) => { |
| 77 | return member.nickname !== null; |
| 78 | } |
| 79 | ), |
| 80 | includes: (name) => |
| 81 | new Filter( |
| 82 | (_data) => `Nickname includes "${name}"`, |
| 83 | { nickname: name, type: String, render: "includes" }, |
| 84 | (member) => { |
| 85 | return member.displayName.includes(name); |
| 86 | } |
| 87 | ), |
| 88 | startsWith: (name) => |
| 89 | new Filter( |
| 90 | (_data) => `Nickname starts with "${name}"`, |
| 91 | { nickname: name, type: String, render: "starts with" }, |
| 92 | (member) => { |
| 93 | return member.displayName.startsWith(name); |
| 94 | } |
| 95 | ), |
| 96 | endsWith: (name) => |
| 97 | new Filter( |
| 98 | (_data) => `Nickname ends with "${name}"`, |
| 99 | { nickname: name, type: String, render: "ends with" }, |
| 100 | (member) => { |
| 101 | return member.displayName.endsWith(name); |
| 102 | } |
| 103 | ) |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 104 | } |
| 105 | }, |
| 106 | account: { |
| 107 | render: "Account", |
| 108 | created: { |
| 109 | render: "created", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 110 | before: (date) => |
| 111 | new Filter( |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 112 | (_data) => `Account created before <t:${Math.round(date.getTime() / 1000)}:D>`, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 113 | { date: date, type: Date, render: "before" }, |
| 114 | (member) => { |
| 115 | return member.user.createdTimestamp < date.getTime(); |
| 116 | } |
| 117 | ) |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 118 | }, |
| 119 | is: { |
| 120 | render: "is", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 121 | human: () => |
| 122 | new Filter( |
| 123 | (_data) => "Member is a human", |
| 124 | { human: true, render: "human" }, |
| 125 | (member) => { |
| 126 | return !member.bot; |
| 127 | } |
| 128 | ) |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 129 | }, |
| 130 | username: { |
| 131 | render: "Username", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 132 | includes: (name) => |
| 133 | new Filter( |
| 134 | (_data) => `Nickname includes "${name}"`, |
| 135 | { nickname: name, type: String, render: "includes" }, |
| 136 | (member) => { |
| 137 | return member.user.name.includes(name); |
| 138 | } |
| 139 | ), |
| 140 | startsWith: (name) => |
| 141 | new Filter( |
| 142 | (_data) => `Nickname starts with "${name}"`, |
| 143 | { nickname: name, type: String, render: "starts with" }, |
| 144 | (member) => { |
| 145 | return member.user.name.startsWith(name); |
| 146 | } |
| 147 | ), |
| 148 | endsWith: (name) => |
| 149 | new Filter( |
| 150 | (_data) => `Nickname ends with "${name}"`, |
| 151 | { nickname: name, type: String, render: "ends with" }, |
| 152 | (member) => { |
| 153 | return member.user.name.endsWith(name); |
| 154 | } |
| 155 | ) |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 156 | } |
| 157 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 158 | }; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 159 | |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 160 | const callback = async (interaction: CommandInteraction): Promise<unknown> => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 161 | await interaction.reply({ |
| 162 | embeds: LoadingEmbed, |
| 163 | ephemeral: true, |
| 164 | fetchReply: true |
| 165 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 166 | const filters: Filter[] = [ |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 167 | filterList.member.has.role("959901346000154674"), |
| 168 | filterList.member.nickname.startsWith("Pinea"), |
| 169 | filterList.member.joined.before(new Date(2022, 1)).flip() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 170 | ]; |
| 171 | const all = true; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 172 | while (true) { |
| 173 | let count = 0; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 174 | const affected = []; |
| 175 | const members = interaction.guild.members.cache; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 176 | if (all) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 177 | members.forEach((member) => { |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 178 | let applies = true; |
Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame] | 179 | for (const filter of filters) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 180 | if (!filter.check(member)) { |
| 181 | applies = false; |
Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame] | 182 | break; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 183 | } |
Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame] | 184 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 185 | if (applies) { |
| 186 | affected.push(member); |
| 187 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 188 | }); |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 189 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 190 | members.forEach((member) => { |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 191 | let applies = false; |
Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame] | 192 | for (const filter of filters) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 193 | if (filter.check(member)) { |
| 194 | applies = true; |
Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame] | 195 | break; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 196 | } |
Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame] | 197 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 198 | if (applies) { |
| 199 | affected.push(member); |
| 200 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 201 | }); |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 202 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 203 | await interaction.editReply({ |
| 204 | embeds: [ |
| 205 | new EmojiEmbed() |
| 206 | .setTitle("Role all") |
| 207 | .setDescription( |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 208 | (all ? "All of the following must be true:" : "Any of the following must be true") + |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 209 | "\n" + |
| 210 | filters |
| 211 | .map((f) => { |
| 212 | count++; |
| 213 | return ( |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 214 | (count === 1 ? getEmojiByName("ICONS.FILTER") : all ? "**and** " : "**or** ") + |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 215 | (f.inverted ? "**not** " : "") + |
| 216 | `${f.name}` |
| 217 | ); |
| 218 | }) |
| 219 | .join("\n") + |
| 220 | "\n\n" + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 221 | `This will affect ${addPlural(affected.length, "member")}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 222 | ) |
| 223 | .setEmoji("GUILD.ROLES.CREATE") |
| 224 | .setStatus("Success") |
| 225 | ], |
| 226 | components: [ |
| 227 | new MessageActionRow().addComponents([ |
| 228 | new MessageSelectMenu() |
| 229 | .setOptions( |
| 230 | filters.map((f, index) => ({ |
| 231 | label: (f.inverted ? "(Not) " : "") + f.name, |
| 232 | value: index.toString() |
| 233 | })) |
| 234 | ) |
| 235 | .setMinValues(1) |
| 236 | .setMaxValues(filters.length) |
| 237 | .setCustomId("select") |
| 238 | .setPlaceholder("Remove a filter") |
| 239 | ]), |
| 240 | new MessageActionRow().addComponents([ |
| 241 | new MessageButton() |
| 242 | .setLabel("Apply") |
| 243 | .setStyle("PRIMARY") |
| 244 | .setCustomId("apply") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 245 | .setEmoji(client.emojis.cache.get(getEmojiByName("CONTROL.TICK", "id"))) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 246 | .setDisabled(affected.length === 0), |
| 247 | new MessageButton() |
| 248 | .setLabel("Add filter") |
| 249 | .setStyle("PRIMARY") |
| 250 | .setCustomId("add") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 251 | .setEmoji(client.emojis.cache.get(getEmojiByName("ICONS.FILTER", "id"))) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 252 | .setDisabled(filters.length >= 25) |
| 253 | ]) |
| 254 | ] |
| 255 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 256 | break; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 257 | } |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 258 | return; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 259 | }; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 260 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 261 | const check = async (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 262 | const member = interaction.member as GuildMember; |
| 263 | const me = interaction.guild.me!; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 264 | if (!me.permissions.has("MANAGE_ROLES")) throw new Error("I do not have the *Manage Roles* permission"); |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 265 | // Allow the owner to role anyone |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 266 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 267 | // Check if the user has manage_roles permission |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 268 | if (!member.permissions.has("MANAGE_ROLES")) throw new Error("You do not have the *Manage Roles* permission"); |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 269 | // Allow role |
| 270 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 271 | }; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 272 | |
| 273 | export { command }; |
| 274 | export { callback }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 275 | export { check }; |