From 4d72f8562ea4f1b322fa857cda9620614e4aceee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Gro=C3=9F?= Date: Mon, 7 Aug 2023 16:23:18 +0200 Subject: [PATCH] Profile: Implement custom profile backgrounds --- .gitignore | 1 + assets/userdata/profiles/.gitkeep | 0 commands/editprofile.js | 36 +++++++++++++++++++++++++++---- commands/profile.js | 14 +++++++++++- config/constants.js | 9 +++++--- 5 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 assets/userdata/profiles/.gitkeep diff --git a/.gitignore b/.gitignore index c3bec1c..1524d30 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ assets/image_cache assets/cards assets/import +assets/userdata ### Visual Studio Code ### .vscode diff --git a/assets/userdata/profiles/.gitkeep b/assets/userdata/profiles/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/commands/editprofile.js b/commands/editprofile.js index 8b8c624..771a9d6 100644 --- a/commands/editprofile.js +++ b/commands/editprofile.js @@ -1,6 +1,6 @@ -const { SlashCommandBuilder, ComponentType, TextInputBuilder, TextInputStyle, ActionRowBuilder, ButtonBuilder, ButtonStyle, ModalBuilder } = require("discord.js"); +const { SlashCommandBuilder, ComponentType, TextInputBuilder, TextInputStyle, ActionRowBuilder, ButtonBuilder, ButtonStyle, ModalBuilder, Attachment } = require("discord.js"); const { Card, User, Character } = require("../models"); -const { UserUtils, ReplyUtils } = require("../util"); +const { UserUtils, ReplyUtils, GeneralUtils } = require("../util"); const pageSize = 8; @@ -8,12 +8,18 @@ const pageSize = 8; module.exports = { data: new SlashCommandBuilder() .setName("editprofile") - .setDescription("Edit your profile"), + .setDescription("Edit your profile") + .addAttachmentOption((option) => + option + .setName("attachement") + .setDescription("Attachement to be used") + .setRequired(false) + ), permissionLevel: 0, async execute(interaction) { await interaction.deferReply(); let user = await UserUtils.getUserByDiscordId(interaction.member.id); - + let patreon = await UserUtils.getPatreonPerks(interaction.client, user); let profile = await user.getProfile(); @@ -31,6 +37,15 @@ module.exports = { .setStyle(ButtonStyle.Primary) ); + if (patreon.perks?.["custom_bg"] === true && interaction.options.getAttachment("attachement")) { + mainRow.addComponents( + new ButtonBuilder() + .setLabel('Set attachment as custom background') + .setCustomId('setCustomBg') + .setStyle(ButtonStyle.Primary) + ); + } + const pingRow = new ActionRowBuilder(); pingRow.addComponents( new ButtonBuilder() @@ -62,6 +77,19 @@ module.exports = { case 'editShowcase': await this.openShowcaseModal(i, user, profile); break; + case 'setCustomBg': + await i.deferReply(); + let allowedContentTypes = [ "image/png", "image/jpeg" ]; + let image = interaction.options.getAttachment("attachement"); + if (!allowedContentTypes.includes(image.contentType)) { + await i.editReply({ content: "An invalid image has been attached. Allowed are .png and .jpeg", ephemeral: true }); + return; + } + await GeneralUtils.downloadFile(image.url, `/app/assets/userdata/profiles/${image.id}_${image.name}`); + profile.customBackground = `${process.env.ASSET_URL}/userdata/profiles/${image.id}_${image.name}`; + await profile.save(); + await i.editReply('Custom profile background has been set!'); + break; case 'toggle-wishlist-ping': await i.deferUpdate(); user.wishlistPing = !user.wishlistPing; diff --git a/commands/profile.js b/commands/profile.js index ceb12d7..feee576 100644 --- a/commands/profile.js +++ b/commands/profile.js @@ -24,7 +24,7 @@ module.exports = { let discordUser = interaction.options.getUser("user") ? interaction.options.getUser("user") : interaction.member.user; let user = await UserUtils.getUserByDiscordId(discordUser.id); - + let patreon = await UserUtils.getPatreonPerks(interaction.client, user); let profile = await user.getProfile(); let customStatus = profile.customStatus; @@ -154,6 +154,18 @@ module.exports = { ] } + if (patreon.perks?.['custom_bg'] && profile.customBackground) { + job.elements.unshift( + { + "type": "image", + "asset": profile.customBackground, + "x": 0, + "y": 0, + "width": 1200, + "height": 600 + } + ); + } console.log("Fetching ", ); if(process.env.NODE_ENV === "development") { await interaction.channel.send(`\`\`\`${JSON.stringify(job)}\`\`\``); diff --git a/config/constants.js b/config/constants.js index b05c89b..645e279 100644 --- a/config/constants.js +++ b/config/constants.js @@ -101,7 +101,8 @@ const PATREON = { wishlist: 15, currency: 1.25, daily: 1.5 - } + }, + custom_bg: true }, 4 : { modifiers: { @@ -110,7 +111,8 @@ const PATREON = { wishlist: 25, currency: 1.75, daily: 2 - } + }, + custom_bg: true }, 5 : { modifiers: { @@ -119,7 +121,8 @@ const PATREON = { wishlist: 45, currency: 2, daily: 4 - } + }, + custom_bg: true } } }