From ddd7f38ce660d4be4b8f165057c6842d6402d751 Mon Sep 17 00:00:00 2001 From: Nayv <43297329+nayvcake@users.noreply.github.com> Date: Thu, 20 Jul 2023 15:05:50 -0300 Subject: [PATCH] new: releasing `InventoryBackgroundCommand` for prod --- .../social/InventoryBackgroundCommand.js | 129 ++++++++++++ .../slash/social/InventoryBaseCommand.js | 8 +- .../InventoryBackgroundInteraction.js | 193 ++++++++++++++++++ 3 files changed, 326 insertions(+), 4 deletions(-) create mode 100644 apps/discord/src/commands/slash/social/InventoryBackgroundCommand.js create mode 100644 apps/discord/src/interactionFunctions/social/inventoryBackgroundStruct/InventoryBackgroundInteraction.js diff --git a/apps/discord/src/commands/slash/social/InventoryBackgroundCommand.js b/apps/discord/src/commands/slash/social/InventoryBackgroundCommand.js new file mode 100644 index 000000000..f85b0fe66 --- /dev/null +++ b/apps/discord/src/commands/slash/social/InventoryBackgroundCommand.js @@ -0,0 +1,129 @@ +import { defineState } from '../../../defineTypes/defineState' +import { ConstantBackground, getBackground } from '../../../lib' +import { Command, SlashCommandContext } from '../../../structures/util' + +export default class InventoryBackgroundCommand extends Command { + constructor() { + super({ + name: 'inventory background', + permissions: [{ + entity: 'bot', + permissions: ['embedLinks'] + }] + }) + } + + /** + * @method run + * @param {SlashCommandContext} ctx + */ + async run(ctx) { + const useState = defineState({ + memberState: { avatarURL: ctx.message.member.avatarURL }, + user: ctx.db.user, + pageState: { + + }, + stateMessage: { + backgroundDefault: {}, + profileSelected: 'default', + profileUrl: '', + disabled: false, + }, + metadataMessage: { + attachments: [] + } + }) + const backgroundImage = await getBackground(useState.user.background, { + cache: true + }) + const valueBackground = Object.values(ConstantBackground) + const backgroundSelected = valueBackground.find((value) => useState.user.background == value.name) + useState.stateMessage.backgroundDefault = backgroundSelected + useState.stateMessage.disabled = true + const backgroundIsDefault = useState.stateMessage.backgroundDefault.name === 'gochiusa_3' ? ` (${ctx._locale('commands:inventory.background.default')})` : '' + const backgroundAvailable = valueBackground + .map((value) => ({ + label: value.title + (value.name == useState.user.background ? ' (' + ctx._locale('commands:inventory.background.used') + ')' : ''), + value: value.name, + description: ctx.db.user.backgroundList.find((v) => value.name == v) ? + '🔓 ' + ctx._locale('commands:inventory.background.unlocked') : ctx._locale('commands:inventory.background.locked'), + emoji: ctx.db.user.backgroundList.find((v) => value.name == v) ? value.emoji : { id: null, name: '🔒' }, + disabled: true, + default: value.name == useState.user.background + })) + + useState.metadataMessage = { + embeds: [ + { + color: useState.user.profileColor.convertToColor(), + title: `✨🖼️ **|** **${ctx._locale('commands:inventory.background.title', { 0: useState.stateMessage.backgroundDefault.title })}**`, + description: `${ctx._locale('commands:inventory.background.description', { 0: `<@${ctx.db.user.id}>` })}\n### - 🖼️ **${ctx._locale('commands:inventory.background.backgroundName', { 0: useState.stateMessage.backgroundDefault.title + backgroundIsDefault })}**\n### - <:chino_woah:568083767684628481> **${ctx._locale('commands:inventory.background.profile', { 0: useState.user.profileType.toTitle() })}**\n### - 🎒 **${ctx._locale('commands:inventory.background.backgroundQuantity', { 0: useState.user.backgroundList.length - 1 })}**`, + image: { + url: `attachment://${useState.user.background}.png` + } + } + ], + components: [ + { + type: 1, + components: [{ + type: 3, + custom_id: 'inventoryBackground', + max_values: 1, + min_values: 1, + options: backgroundAvailable + }] + }, + { + type: 1, + components: [ + { + type: 2, + style: 2, + label: ctx._locale('commands:inventory.background.reload'), + custom_id: 'reload', + emoji: { + id: null, + name: '🔄' + } + }, + { + type: 2, + style: 1, + label: ctx._locale(useState.stateMessage.disabled ? 'commands:inventory.background.backgroundDefault' : 'commands:inventory.background.selectBackground'), + custom_id: `select:${useState.backgroundDefault}`, + disabled: useState.stateMessage.disabled, + emoji: { + id: null, + name: useState.stateMessage.disabled ? '📌' : '💙' + } + }, + { + type: 2, + style: 3, + label: ctx._locale('commands:inventory.background.previewProfile'), + custom_id: 'profilePreview', + emoji: { + id: null, + name: '✨' + } + } + ] + } + ] + } + ctx.send(useState.metadataMessage, { + file: { + name: useState.user.background + '.png', + file: backgroundImage + } + }).then((message) => { + ctx.createInteractionFunction(['inventoryBackgroundCommand'], message, { + state: useState, + users: [ctx.message.author.id] + }) + }) + + } +} diff --git a/apps/discord/src/commands/slash/social/InventoryBaseCommand.js b/apps/discord/src/commands/slash/social/InventoryBaseCommand.js index afc25411f..fb99562b4 100644 --- a/apps/discord/src/commands/slash/social/InventoryBaseCommand.js +++ b/apps/discord/src/commands/slash/social/InventoryBaseCommand.js @@ -18,10 +18,10 @@ export default class InventoryBaseCommand extends Command { .setType(1) .setName('profile') .setDescription('Change profiles that you\'ve purchased or that you already have.'), - // new CommandOptions() - // .setType(1) - // .setName('background') - // .setDescription('Change background that you\'ve purchased or that you already have.'), + new CommandOptions() + .setType(1) + .setName('background') + .setDescription('Change background that you\'ve purchased or that you already have.'), ) }) } diff --git a/apps/discord/src/interactionFunctions/social/inventoryBackgroundStruct/InventoryBackgroundInteraction.js b/apps/discord/src/interactionFunctions/social/inventoryBackgroundStruct/InventoryBackgroundInteraction.js new file mode 100644 index 000000000..a15dc5102 --- /dev/null +++ b/apps/discord/src/interactionFunctions/social/inventoryBackgroundStruct/InventoryBackgroundInteraction.js @@ -0,0 +1,193 @@ +/* eslint-disable no-unused-vars */ +/* eslint-disable no-const-assign */ +/* eslint-disable import/named */ +import { createStateGeneric } from '../../../defineTypes/defineState'; +import { ConstantBackground, getBackground, requestTokamak } from '../../../lib'; +// eslint-disable-next-line no-unused-vars +import { defineInteraction, defineInteractionDefault, defineInteractionFunction } from '../../../structures/InteractionFunction'; + +/** + * @type {{ + * memberState: { avatarURL: string; }; + * user: { id: string }; + * pageState: {}; + * stateMessage: { + * backgroundDefault: ConstantBackground['gochiusa_3']; + * profileSelected: keyof ConstantBackground; + * profileUrl: string; + * }; + * metadataMessage: { embeds: [], components: [], content: '' | null }; + *}} + */ +const StateUser = createStateGeneric(); + +export default defineInteractionDefault( + defineInteraction({ + name: 'inventoryBackgroundCommand' + }), + defineInteractionFunction(async ({ ctx, getData, useState, defineState }) => { + const { data, member } = getData() + const updateMessage = async (isUpdate = false) => { + const userDB = await ctx.client.database.users.getOrCreate(member.user.id) + const { metadataMessage, stateMessage, user, actionState } = useState() + const valueBackground = Object.values(ConstantBackground) + + const backgroundSelected = valueBackground.find((value) => isUpdate ? stateMessage.backgroundDefault.name == value.name : data?.values.at(0) == value.name) + const backgroundImage = await getBackground(backgroundSelected.name, { + cache: true + }) + actionState.modifyObject({ + stateMessage: { + backgroundDefault: backgroundSelected, + disabled: userDB.background === backgroundSelected.name + } + }) + const backgroundIsDefault = stateMessage.backgroundDefault.name === 'gochiusa_3' ? ` (${ctx._locale('commands:inventory.background.default')})` : '' + const backgroundAvailable = valueBackground + .map((value) => ({ + label: value.title + (value.name == user.background ? ' (' + ctx._locale('commands:inventory.background.used') + ')' : ''), + value: value.name, + description: userDB.backgroundList.find((v) => value.name == v) ? + '🔓 ' + ctx._locale('commands:inventory.background.unlocked') : ctx._locale('commands:inventory.background.locked'), + emoji: userDB.backgroundList.find((v) => value.name == v) ? value.emoji : { id: null, name: '🔒' }, + default: stateMessage.backgroundDefault.name == value.name + })) + const metadataUpdated = { + metadataMessage: { + attachments: [], + embeds: [ + { + color: user.profileColor.convertToColor(), + title: `✨🖼️ **|** **${ctx._locale('commands:inventory.background.title', { 0: backgroundSelected.title })}**`, + description: `${ctx._locale('commands:inventory.background.description', { 0: `<@${userDB.id}>` })}\n### - 🖼️ **${ctx._locale('commands:inventory.background.backgroundName', { 0: stateMessage.backgroundDefault.title + backgroundIsDefault })}**\n### - <:chino_woah:568083767684628481> **${ctx._locale('commands:inventory.background.profile', { 0: user.profileType.toTitle() })}**\n### - 🎒 **${ctx._locale('commands:inventory.background.backgroundQuantity', { 0: user.backgroundList.length - 1 })}**`, + image: { + url: `attachment://${backgroundSelected.name}.png` + } + } + ], + components: [ + { + type: 1, + components: [{ + type: 3, + custom_id: 'inventoryBackground', + max_values: 1, + min_values: 1, + options: backgroundAvailable + }] + }, + { + type: 1, + components: [ + { + type: 2, + style: 2, + label: ctx._locale('commands:inventory.background.reload'), + custom_id: 'reload', + emoji: { + id: null, + name: '🔄' + } + }, + { + type: 2, + style: 1, + label: ctx._locale(userDB.background === backgroundSelected.name ? 'commands:inventory.background.backgroundDefault' : 'commands:inventory.background.selectBackground'), + custom_id: `select:${stateMessage.backgroundDefault.name}`, + disabled: userDB.background === backgroundSelected.name, + emoji: { + id: null, + name: userDB.background === backgroundSelected.name ? '📌' : '💙' + } + }, + { + type: 2, + style: 3, + label: ctx._locale('commands:inventory.background.previewProfile'), + custom_id: 'profilePreview', + emoji: { + id: null, + name: '✨' + } + } + ] + } + ] + } + } + actionState.modifyObject(metadataUpdated) + ctx.editMessage(metadataUpdated.metadataMessage, { + image: { + name: backgroundSelected.name + '.png', + file: backgroundImage + } + }) + } + + // Profile Preview + if (data.custom_id === 'profilePreview' && data.component_type == 2) { + const { memberState, metadataMessage, stateMessage } = useState() + const userDB = await ctx.client.database.users.getOrCreate(member.user.id) + const profileUser = { + type: userDB.profileType, + name: member.user.username, + money: Number(userDB.yens).toLocaleString(), + aboutMe: userDB.aboutme !== '' ? userDB.aboutme : ctx._locale('commands:profile.defaultAboutMe', { 0: '/' }), + married: false, + partnerName: '', + bgId: stateMessage.backgroundDefault.name, + stickerId: userDB.sticker, + favColor: userDB.profileColor, + avatarUrl: memberState.avatarURL, + badges: [] + } + const profile = await requestTokamak({ + action: 'renderProfile', + profileStruct: profileUser + }) + metadataMessage.embeds[0].image.url = 'attachment://profile.png' + ctx.editMessage(metadataMessage, { + image: { + name: 'profile.png', + file: profile.buffer + } + }) + return + } + + // Update background + if (data.custom_id === 'inventoryBackground' && data.component_type == 3) { + const { metadataMessage, stateMessage, user, actionState } = useState() + const userDB = await ctx.client.database.users.getOrCreate(member.user.id) + if (userDB.backgroundList.find((background) => data.values.find((i) => background == i)) == null) { + return ctx.replyT('error', 'commands:inventory.background.needsToBuy', { + enableEphemeral: true, + options: { mentionUser: [member.user.id] } + }) + } + const background = Object.values(ConstantBackground).find((bg) => bg.name === data.values.at(0)) + actionState.modifyObject({ + stateMessage: { + backgroundDefault: background + } + }) + updateMessage() + } + + // Select Background + if (data.custom_id.startsWith('select:') && data.component_type == 2) { + const { metadataMessage, stateMessage, user, actionState } = useState() + // eslint-disable-next-line no-unused-vars + const [_, background] = data.custom_id.split(':') + user.background = background + user.save().then(() => updateMessage(true)) + return + } + + // Reload + if (data.custom_id === 'reload' && data.component_type == 2) { + updateMessage(true) + return + } + }, StateUser) +)