Skip to content

Commit

Permalink
dye swap
Browse files Browse the repository at this point in the history
  • Loading branch information
nwjgit committed Feb 20, 2024
1 parent 01acb11 commit 2736487
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export const enum Emoji {
Purple = '🟪',
Green = '🟩',
Blue = '🟦',
Red = '🟥',
Thieving = '<:thieving:630910829352452123>',
Hunter = '<:hunter:630911040166559784>',
Ely = '<:ely:784453586033049630>',
Expand Down
13 changes: 11 additions & 2 deletions src/mahoji/commands/use.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
}
};
143 changes: 137 additions & 6 deletions src/mahoji/lib/abstracted_commands/useCommand.ts
Original file line number Diff line number Diff line change
@@ -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<string>;
run: (user: MUser, channel: TextChannel, interaction: ChatInputCommandInteraction) => Promise<string>;
}
export const usables: Usable[] = [];

Expand Down Expand Up @@ -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.";
Expand All @@ -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);
}

0 comments on commit 2736487

Please sign in to comment.