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 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 160 | const callback = async (interaction: CommandInteraction): Promise<void | 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 | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 179 | filters.forEach((filter) => { |
| 180 | if (!filter.check(member)) { |
| 181 | applies = false; |
| 182 | } |
| 183 | }); |
| 184 | if (applies) { |
| 185 | affected.push(member); |
| 186 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 187 | }); |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 188 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 189 | members.forEach((member) => { |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 190 | let applies = false; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 191 | filters.forEach((filter) => { |
| 192 | if (filter.check(member)) { |
| 193 | applies = true; |
| 194 | } |
| 195 | }); |
| 196 | if (applies) { |
| 197 | affected.push(member); |
| 198 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 199 | }); |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 200 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 201 | await interaction.editReply({ |
| 202 | embeds: [ |
| 203 | new EmojiEmbed() |
| 204 | .setTitle("Role all") |
| 205 | .setDescription( |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 206 | (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] | 207 | "\n" + |
| 208 | filters |
| 209 | .map((f) => { |
| 210 | count++; |
| 211 | return ( |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 212 | (count === 1 ? getEmojiByName("ICONS.FILTER") : all ? "**and** " : "**or** ") + |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 213 | (f.inverted ? "**not** " : "") + |
| 214 | `${f.name}` |
| 215 | ); |
| 216 | }) |
| 217 | .join("\n") + |
| 218 | "\n\n" + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 219 | `This will affect ${addPlural(affected.length, "member")}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 220 | ) |
| 221 | .setEmoji("GUILD.ROLES.CREATE") |
| 222 | .setStatus("Success") |
| 223 | ], |
| 224 | components: [ |
| 225 | new MessageActionRow().addComponents([ |
| 226 | new MessageSelectMenu() |
| 227 | .setOptions( |
| 228 | filters.map((f, index) => ({ |
| 229 | label: (f.inverted ? "(Not) " : "") + f.name, |
| 230 | value: index.toString() |
| 231 | })) |
| 232 | ) |
| 233 | .setMinValues(1) |
| 234 | .setMaxValues(filters.length) |
| 235 | .setCustomId("select") |
| 236 | .setPlaceholder("Remove a filter") |
| 237 | ]), |
| 238 | new MessageActionRow().addComponents([ |
| 239 | new MessageButton() |
| 240 | .setLabel("Apply") |
| 241 | .setStyle("PRIMARY") |
| 242 | .setCustomId("apply") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 243 | .setEmoji(client.emojis.cache.get(getEmojiByName("CONTROL.TICK", "id"))) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 244 | .setDisabled(affected.length === 0), |
| 245 | new MessageButton() |
| 246 | .setLabel("Add filter") |
| 247 | .setStyle("PRIMARY") |
| 248 | .setCustomId("add") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 249 | .setEmoji(client.emojis.cache.get(getEmojiByName("ICONS.FILTER", "id"))) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 250 | .setDisabled(filters.length >= 25) |
| 251 | ]) |
| 252 | ] |
| 253 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 254 | break; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 255 | } |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 256 | return; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 257 | }; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 258 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 259 | const check = async (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 260 | const member = interaction.member as GuildMember; |
| 261 | const me = interaction.guild.me!; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 262 | if (!me.permissions.has("MANAGE_ROLES")) throw "I do not have the *Manage Roles* permission"; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 263 | // Allow the owner to role anyone |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 264 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 265 | // Check if the user has manage_roles permission |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 266 | if (!member.permissions.has("MANAGE_ROLES")) throw "You do not have the *Manage Roles* permission"; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 267 | // Allow role |
| 268 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 269 | }; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 270 | |
| 271 | export { command }; |
| 272 | export { callback }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 273 | export { check }; |