pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import { LoadingEmbed } from "./../utils/defaultEmbeds.js"; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 2 | import Discord, { CommandInteraction, GuildMember, MessageActionRow, MessageButton, MessageSelectMenu } from "discord.js"; |
| 3 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
| 4 | import { WrappedCheck } from "jshaiku"; |
| 5 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
| 6 | import getEmojiByName from "../utils/getEmojiByName.js"; |
| 7 | import addPlural from "../utils/plurals.js"; |
| 8 | import client from "../utils/client.js"; |
| 9 | |
| 10 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 11 | builder // TODO: DON'T RELEASE THIS |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 12 | .setName("all") |
| 13 | .setDescription("Gives or removes a role from everyone"); |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 14 | |
| 15 | class Filter { |
| 16 | name: string; |
| 17 | data: object; |
| 18 | checkFunction: (member) => boolean; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 19 | inverted = false; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 20 | constructor(name: (data) => string | string, data: object, check: (member) => boolean) { |
| 21 | if (typeof name === "function") { this.name = name(data); |
| 22 | } else { this.name = name; } |
| 23 | this.data = data; |
| 24 | this.checkFunction = check; |
| 25 | } |
| 26 | flip() { this.inverted = true; return this; } |
| 27 | check(member) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 28 | if (this.inverted) return !this.checkFunction(member); |
| 29 | else return this.checkFunction(member); |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | |
| 33 | const filterList = { |
| 34 | member: { |
| 35 | render: "Member", |
| 36 | has: { |
| 37 | render: "has", |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 38 | role: (role) => ( new Filter((data) => `Member has role <@&${data.role}>`, {role: role, type: Discord.Role, render: "role"}, (member) => { return member.roles.cache.has(role);})) |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 39 | }, |
| 40 | joined: { |
| 41 | render: "joined", |
| 42 | before: (date) => ( new Filter((data) => `Joined server before <t:${Math.round(date.getTime() / 1000)}:D>`, {date: date, type: Date, render: "before"}, (member) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 43 | return member.joinedTimestamp < date.getTime(); |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 44 | })) |
| 45 | }, |
| 46 | nickname: { |
| 47 | render: "Nickname", |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 48 | set: () => ( new Filter((data) => "Member has a nickname set\"", {render: "set"}, (member) => { return member.nickname !== null;})), |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 49 | includes: (name) => ( new Filter((data) => `Nickname includes "${name}"`, {nickname: name, type: String, render: "includes"}, (member) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 50 | return member.displayName.includes(name);})), |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 51 | startsWith: (name) => ( new Filter((data) => `Nickname starts with "${name}"`, {nickname: name, type: String, render: "starts with"}, (member) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 52 | return member.displayName.startsWith(name);})), |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 53 | endsWith: (name) => ( new Filter((data) => `Nickname ends with "${name}"`, {nickname: name, type: String, render: "ends with"}, (member) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 54 | return member.displayName.endsWith(name);})) |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 55 | } |
| 56 | }, |
| 57 | account: { |
| 58 | render: "Account", |
| 59 | created: { |
| 60 | render: "created", |
| 61 | before: (date) => ( new Filter((data) => `Account created before <t:${Math.round(date.getTime() / 1000)}:D>`, {date: date, type: Date, render: "before"}, (member) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 62 | return member.user.createdTimestamp < date.getTime(); |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 63 | })) |
| 64 | }, |
| 65 | is: { |
| 66 | render: "is", |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 67 | human: () => ( new Filter((data) => "Member is a human", {human: true, render: "human"}, (member) => { return !member.bot; })) |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 68 | }, |
| 69 | username: { |
| 70 | render: "Username", |
| 71 | includes: (name) => ( new Filter((data) => `Nickname includes "${name}"`, {nickname: name, type: String, render: "includes"}, (member) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 72 | return member.user.name.includes(name);})), |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 73 | startsWith: (name) => ( new Filter((data) => `Nickname starts with "${name}"`, {nickname: name, type: String, render: "starts with"}, (member) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 74 | return member.user.name.startsWith(name);})), |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 75 | endsWith: (name) => ( new Filter((data) => `Nickname ends with "${name}"`, {nickname: name, type: String, render: "ends with"}, (member) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 76 | return member.user.name.endsWith(name);})) |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 77 | } |
| 78 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 79 | }; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 80 | |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 81 | const callback = async (interaction: CommandInteraction): Promise<any> => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 82 | await interaction.reply({embeds: LoadingEmbed, ephemeral: true, fetchReply: true}); |
| 83 | const filters: Filter[] = [ |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 84 | filterList.member.has.role("959901346000154674"), |
| 85 | filterList.member.nickname.startsWith("Pinea"), |
| 86 | filterList.member.joined.before(new Date(2022, 1)).flip() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 87 | ]; |
| 88 | const all = true; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 89 | while (true) { |
| 90 | let count = 0; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 91 | const affected = []; |
| 92 | const members = interaction.guild.members.cache; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 93 | if (all) { |
| 94 | members.forEach(member => { |
| 95 | let applies = true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 96 | filters.forEach(filter => { if (!filter.check(member)) { applies = false; } }); |
| 97 | if (applies) { affected.push(member); } |
| 98 | }); |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 99 | } else { |
| 100 | members.forEach(member => { |
| 101 | let applies = false; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 102 | filters.forEach(filter => { if (filter.check(member)) { applies = true; } }); |
| 103 | if (applies) { affected.push(member); } |
| 104 | }); |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 105 | } |
| 106 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 107 | .setTitle("Role all") |
| 108 | .setDescription((all ? "All of the following must be true:" : "Any of the following must be true") + "\n" + |
| 109 | filters.map((f) => { |
| 110 | count ++; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 111 | return (count === 1 ? getEmojiByName("ICONS.FILTER") : (all ? "**and** " : "**or** ")) + |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 112 | (f.inverted ? "**not** " : "") + `${f.name}`; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 113 | }).join("\n") + "\n\n" + `This will affect ${addPlural(affected.length, "member")}`) |
| 114 | .setEmoji("GUILD.ROLES.CREATE") |
| 115 | .setStatus("Success") |
| 116 | ], components: [ |
| 117 | new MessageActionRow().addComponents([new MessageSelectMenu().setOptions(filters.map((f, index) => ({ |
| 118 | label: (f.inverted ? "(Not) " : "") + f.name, |
| 119 | value: index.toString() |
| 120 | }))).setMinValues(1).setMaxValues(filters.length).setCustomId("select").setPlaceholder("Remove a filter")]), |
| 121 | new MessageActionRow().addComponents([ |
| 122 | new MessageButton() |
| 123 | .setLabel("Apply") |
| 124 | .setStyle("PRIMARY") |
| 125 | .setCustomId("apply") |
| 126 | .setEmoji(client.emojis.cache.get(getEmojiByName("CONTROL.TICK", "id"))) |
| 127 | .setDisabled(affected.length === 0), |
| 128 | new MessageButton() |
| 129 | .setLabel("Add filter") |
| 130 | .setStyle("PRIMARY") |
| 131 | .setCustomId("add") |
| 132 | .setEmoji(client.emojis.cache.get(getEmojiByName("ICONS.FILTER", "id"))) |
| 133 | .setDisabled(filters.length >= 25) |
| 134 | ]) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 135 | ]}); |
| 136 | break; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 137 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 138 | }; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 139 | |
| 140 | const check = async (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 141 | const member = (interaction.member as GuildMember); |
| 142 | const me = (interaction.guild.me as GuildMember); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 143 | 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] | 144 | // Allow the owner to role anyone |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 145 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 146 | // Check if the user has manage_roles permission |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 147 | 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] | 148 | // Allow role |
| 149 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 150 | }; |
pineafan | d5a8e6a | 2022-07-25 17:38:14 +0100 | [diff] [blame] | 151 | |
| 152 | export { command }; |
| 153 | export { callback }; |
| 154 | export { check }; |