From 2736487169e0696f9f4b9f5060e67f7c01f35520 Mon Sep 17 00:00:00 2001 From: nwjgit Date: Tue, 20 Feb 2024 03:32:41 -0600 Subject: [PATCH] dye swap --- src/lib/constants.ts | 1 + src/mahoji/commands/use.ts | 13 +- .../lib/abstracted_commands/useCommand.ts | 143 +++++++++++++++++- 3 files changed, 149 insertions(+), 8 deletions(-) diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 50b28b0fac..b2b19a7423 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -121,6 +121,7 @@ export const enum Emoji { Purple = '🟪', Green = '🟩', Blue = '🟦', + Red = '🟥', Thieving = '<:thieving:630910829352452123>', Hunter = '<:hunter:630911040166559784>', Ely = '<:ely:784453586033049630>', diff --git a/src/mahoji/commands/use.ts b/src/mahoji/commands/use.ts index 0d159a4aa3..c26cac55e7 100644 --- a/src/mahoji/commands/use.ts +++ b/src/mahoji/commands/use.ts @@ -1,5 +1,6 @@ import { CommandRunOptions } from 'mahoji'; +import { channelIsSendable } from '../../lib/util'; import { allUsableItems, useCommand } from '../lib/abstracted_commands/useCommand'; import { ownedItemOption } from '../lib/mahojiCommandOptions'; import { OSBMahojiCommand } from '../lib/util'; @@ -24,8 +25,16 @@ export const mahojiUseCommand: OSBMahojiCommand = { description: 'Optional second item to use the first one on.' } ], - run: async ({ options, userID }: CommandRunOptions<{ item: string; secondary_item?: string }>) => { + run: async ({ + options, + userID, + channelID, + interaction + }: CommandRunOptions<{ item: string; secondary_item?: string }>) => { const user = await mUserFetch(userID); - return useCommand(user, options.item, options.secondary_item); + const channel = globalClient.channels.cache.get(channelID.toString()); + if (!channelIsSendable(channel)) return { ephemeral: true, content: 'Invalid channel.' }; + + return useCommand(user, channel, interaction, options.item, options.secondary_item); } }; diff --git a/src/mahoji/lib/abstracted_commands/useCommand.ts b/src/mahoji/lib/abstracted_commands/useCommand.ts index 8cc1c9e35d..256af89ca9 100644 --- a/src/mahoji/lib/abstracted_commands/useCommand.ts +++ b/src/mahoji/lib/abstracted_commands/useCommand.ts @@ -1,15 +1,17 @@ -import { notEmpty } from 'e'; +import { ButtonBuilder, ButtonStyle, ChatInputCommandInteraction, TextChannel } from 'discord.js'; +import { notEmpty, Time } from 'e'; import { Bank } from 'oldschooljs'; import { Item } from 'oldschooljs/dist/meta/types'; -import { BitField } from '../../../lib/constants'; -import { assert } from '../../../lib/util'; +import { BitField, Emoji } from '../../../lib/constants'; +import { assert, awaitMessageComponentInteraction, makeComponents } from '../../../lib/util'; import getOSItem, { getItem } from '../../../lib/util/getOSItem'; +import { deferInteraction } from '../../../lib/util/interactionReply'; import { flowerTable } from './hotColdCommand'; interface Usable { items: Item[]; - run: (user: MUser) => Promise; + run: (user: MUser, channel: TextChannel, interaction: ChatInputCommandInteraction) => Promise; } export const usables: Usable[] = []; @@ -124,9 +126,138 @@ for (const genericU of genericUsables) { } }); } +const dyeUsables: { + name: string; + items: [Item, Item] | [Item]; + cost: Bank; + response: (loot: Bank) => string; +}[] = [ + { + name: 'Abyssal red dye', + items: [getOSItem('Abyssal red dye')], + cost: new Bank().add('Abyssal red dye').freeze(), + response: loot => `You swapped 1x Abyssal red dye for ${loot}!` + }, + { + name: 'Abyssal blue dye', + items: [getOSItem('Abyssal blue dye')], + cost: new Bank().add('Abyssal blue dye').freeze(), + response: loot => `You swapped 1x Abyssal blue dye for ${loot}!` + }, + { + name: 'Abyssal green dye', + items: [getOSItem('Abyssal green dye')], + cost: new Bank().add('Abyssal green dye').freeze(), + response: loot => `You swapped 1x Abyssal green dye for ${loot}!` + } +]; + +const redDye = new ButtonBuilder() + .setLabel('Abyssal red dye') + .setStyle(ButtonStyle.Secondary) + .setCustomId('ABBY_RED_DYE') + .setEmoji(Emoji.Red); + +const blueDye = new ButtonBuilder() + .setLabel('Abyssal blue dye') + .setStyle(ButtonStyle.Secondary) + .setCustomId('ABBY_BLUE_DYE') + .setEmoji(Emoji.Blue); + +const greenDye = new ButtonBuilder() + .setLabel('Abyssal green dye') + .setStyle(ButtonStyle.Secondary) + .setCustomId('ABBY_GREEN_DYE') + .setEmoji(Emoji.Green); + +for (const dye of dyeUsables) { + usables.push({ + items: dye.items, + run: async (user, channel, interaction) => { + const components = []; + + await deferInteraction(interaction); + + // Only show the two dyes that doesn't match the dye being swapped + switch (dye.name) { + case 'Abyssal red dye': { + components.push(blueDye, greenDye); + break; + } + case 'Abyssal blue dye': { + components.push(redDye, greenDye); + break; + } + case 'Abyssal green dye': { + components.push(redDye, blueDye); + break; + } + } + const str = `Select the abyssal dye you would like swap ${dye.name} with.`; + const sentMessage = await channel.send({ + content: str, + components: makeComponents(components) + }); + + try { + const selection = await awaitMessageComponentInteraction({ + message: sentMessage, + filter: i => { + if (i.user.id !== user.id) { + i.reply({ ephemeral: true, content: 'This is not your confirmation message.' }); + return false; + } + return true; + }, + time: Time.Second * 15 + }); + + const loot = new Bank(); + switch (selection.customId) { + case 'ABBY_RED_DYE': { + await user.transactItems({ + itemsToAdd: loot.add(getOSItem('Abyssal red dye')).freeze(), + itemsToRemove: dye.cost, + collectionLog: false + }); + break; + } + case 'ABBY_BLUE_DYE': { + await user.transactItems({ + itemsToAdd: loot.add(getOSItem('Abyssal blue dye')).freeze(), + itemsToRemove: dye.cost, + collectionLog: false + }); + break; + } + case 'ABBY_GREEN_DYE': { + await user.transactItems({ + itemsToAdd: loot.add(getOSItem('Abyssal green dye')).freeze(), + itemsToRemove: dye.cost, + collectionLog: false + }); + break; + } + } + await sentMessage.delete(); + return dye.response(loot); + } catch (err) { + await sentMessage.delete(); + return "You didn't select a dye in time."; + } + } + }); +} + export const allUsableItems = new Set(usables.map(i => i.items.map(i => i.id)).flat(2)); -export async function useCommand(user: MUser, _firstItem: string, _secondItem?: string) { +export async function useCommand( + user: MUser, + channel: TextChannel, + interaction: ChatInputCommandInteraction, + _firstItem: string, + _secondItem?: string +) { const firstItem = getItem(_firstItem); const secondItem = _secondItem === undefined ? null : getItem(_secondItem); if (!firstItem || (_secondItem !== undefined && !secondItem)) return "That's not a valid item."; @@ -140,5 +271,5 @@ export async function useCommand(user: MUser, _firstItem: string, _secondItem?: const usable = usables.find(i => i.items.length === items.length && i.items.every(t => items.includes(t))); if (!usable) return `That's not a usable ${items.length === 1 ? 'item' : 'combination'}.`; - return usable.run(user); + return usable.run(user, channel, interaction); }