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