|
| 1 | +const { MessageFlags, ChatInputCommandInteraction, GuildMember } = require("discord.js"); |
| 2 | +const { commandMention } = require("../shared"); |
| 3 | +const { InteractionOrigin } = require("../classes"); |
| 4 | +const { Bounty, Hunter } = require("../../database/models"); |
| 5 | + |
| 6 | +/** @param {(interaction: ChatInputCommandInteraction, origin: InteractionOrigin, runMode: "development" | "test" | "production", logicLayer: typeof import("../../logic"), entities: { member: GuildMember; hunter: Hunter; }) => Promise<void>} next */ |
| 7 | +function ensureUserFromSlashOptionHasBountyHunter(optionName, next) { |
| 8 | + /** |
| 9 | + * @param {ChatInputCommandInteraction} interaction |
| 10 | + * @param {InteractionOrigin} origin |
| 11 | + * @param {"development" | "test" | "production"} runMode |
| 12 | + * @param {typeof import("../../logic")} logicLayer |
| 13 | + */ |
| 14 | + return async (interaction, origin, runMode, logicLayer) => { |
| 15 | + const member = interaction.options.getMember(optionName); |
| 16 | + const hunter = member.id === origin.hunter.userId ? origin.hunter : await logicLayer.hunters.findOneHunter(member.id, interaction.guild.id); |
| 17 | + if (!hunter) { |
| 18 | + interaction.reply({ content: `${member} has not interacted with BountyBot on this server.`, flags: MessageFlags.Ephemeral }); |
| 19 | + return; |
| 20 | + } |
| 21 | + next(interaction, origin, runMode, logicLayer, { member, hunter }); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/** @param {(interaction: ChatInputCommandInteraction, origin: InteractionOrigin, runMode: "development" | "test" | "production", logicLayer: typeof import("../../logic"), input: number) => Promise<void>} next */ |
| 26 | +function ensureNumberFromSlashOptionIsGreaterThanOne(optionName, next) { |
| 27 | + /** |
| 28 | + * @param {ChatInputCommandInteraction} interaction |
| 29 | + * @param {InteractionOrigin} origin |
| 30 | + * @param {"development" | "test" | "production"} runMode |
| 31 | + * @param {typeof import("../../logic")} logicLayer |
| 32 | + */ |
| 33 | + return async (interaction, origin, runMode, logicLayer) => { |
| 34 | + const input = interaction.options.getNumber(optionName); |
| 35 | + if (input <= 1) { |
| 36 | + interaction.reply({ content: `The value provided for ${optionName} must be greater than 1.`, flags: MessageFlags.Ephemeral }) |
| 37 | + return; |
| 38 | + } |
| 39 | + next(interaction, origin, runMode, logicLayer, input); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/** @param {(interaction: ChatInputCommandInteraction, origin: InteractionOrigin, runMode: "development" | "test" | "production", logicLayer: typeof import("../../logic"), bounties: Bounty[]) => Promise<void>} next */ |
| 44 | +function ensureCompanyHasEnoughOpenEvergreenBounties(countThreshold, next) { |
| 45 | + /** |
| 46 | + * @param {ChatInputCommandInteraction} interaction |
| 47 | + * @param {InteractionOrigin} origin |
| 48 | + * @param {"development" | "test" | "production"} runMode |
| 49 | + * @param {typeof import("../../logic")} logicLayer |
| 50 | + */ |
| 51 | + return async (interaction, origin, runMode, logicLayer) => { |
| 52 | + const evergreenBounties = await logicLayer.bounties.findEvergreenBounties(interaction.guild.id); |
| 53 | + if (evergreenBounties.length < countThreshold) { |
| 54 | + let content = countThreshold === 1 ? |
| 55 | + "This server doesn't appear to have any evergreen bounties." : |
| 56 | + "This server must have at least 2 evergreen bounties to be able to swap rewards."; |
| 57 | + interaction.reply({ content, flags: MessageFlags.Ephemeral }); |
| 58 | + return; |
| 59 | + } |
| 60 | + next(interaction, origin, runMode, logicLayer, evergreenBounties); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/** @param {(interaction: ChatInputCommandInteraction, origin: InteractionOrigin, runMode: "development" | "test" | "production", logicLayer: typeof import("../../logic"), bounties: Bounty[]) => Promise<void>} next */ |
| 65 | +function ensureHunterHasOpenBounty(next) { |
| 66 | + /** |
| 67 | + * @param {ChatInputCommandInteraction} interaction |
| 68 | + * @param {InteractionOrigin} origin |
| 69 | + * @param {"development" | "test" | "production"} runMode |
| 70 | + * @param {typeof import("../../logic")} logicLayer |
| 71 | + */ |
| 72 | + return async (interaction, origin, runMode, logicLayer) => { |
| 73 | + const bounties = await logicLayer.bounties.findOpenBounties(origin.user.id, origin.company.id); |
| 74 | + if (bounties.length < 1) { |
| 75 | + interaction.reply({ content: `You don't appear to have any open bounties on this server. Post one with ${commandMention("bounty post")}?`, flags: MessageFlags.Ephemeral }); |
| 76 | + return; |
| 77 | + } |
| 78 | + next(interaction, origin, runMode, logicLayer, bounties); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +module.exports = { |
| 83 | + ensureUserFromSlashOptionHasBountyHunter, |
| 84 | + ensureNumberFromSlashOptionIsGreaterThanOne, |
| 85 | + ensureCompanyHasEnoughOpenEvergreenBounties, |
| 86 | + ensureHunterHasOpenBounty |
| 87 | +}; |
0 commit comments