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