Spaces:
Sleeping
Sleeping
Download src/commands/leave-background.js from Discord-Core-Bot/gateway-bot: direct link, hf CLI and curl.
- Browser
- Download file 3.41 kB
-
https://huggingface.co/spaces/Discord-Core-Bot/gateway-bot/resolve/main/src/commands/leave-background.js
- Command line
-
hf download hf://spaces/Discord-Core-Bot/gateway-bot/src/commands/leave-background.js
-
curl -L -o leave-background.js https://huggingface.co/spaces/Discord-Core-Bot/gateway-bot/resolve/main/src/commands/leave-background.js
3.41 kB
| import { SlashCommandBuilder, PermissionFlagsBits, EmbedBuilder, MessageFlags } from 'discord.js' | |
| import { loadGuildConfig, setGuildConfig } from '../storage/cache.js' | |
| import { uploadBinary } from '../storage/hfClient.js' | |
| export const data = new SlashCommandBuilder() | |
| .setName('leave-background') | |
| .setDescription('Set the background image for the leave card') | |
| .addSubcommand((sub) => | |
| sub | |
| .setName('default') | |
| .setDescription('Choose one of the built-in background styles') | |
| .addStringOption((opt) => | |
| opt | |
| .setName('style') | |
| .setDescription('The background style to use') | |
| .setRequired(true) | |
| .addChoices( | |
| { name: 'Style 1', value: 'default1' }, | |
| { name: 'Style 2', value: 'default2' }, | |
| { name: 'Style 3', value: 'default3' } | |
| ) | |
| ) | |
| ) | |
| .addSubcommand((sub) => | |
| sub | |
| .setName('upload') | |
| .setDescription('Upload a custom background image (PNG or JPG, 800x200)') | |
| .addAttachmentOption((opt) => | |
| opt | |
| .setName('image') | |
| .setDescription('Upload a custom background image (PNG or JPG, 800x200)') | |
| .setRequired(true) | |
| ) | |
| ) | |
| export async function execute(interaction) { | |
| if (!interaction.memberPermissions.has(PermissionFlagsBits.ManageGuild)) { | |
| await interaction.reply({ | |
| content: 'You need Manage Server permission to use this command.', | |
| flags: MessageFlags.Ephemeral, | |
| }) | |
| return | |
| } | |
| const sub = interaction.options.getSubcommand() | |
| const guildId = interaction.guildId | |
| if (sub === 'default') { | |
| const style = interaction.options.getString('style') | |
| const styleNames = { default1: 'Style 1', default2: 'Style 2', default3: 'Style 3' } | |
| const config = await loadGuildConfig(guildId) | |
| config.leaveBackground = style | |
| await setGuildConfig(guildId, config) | |
| const embed = new EmbedBuilder() | |
| .setColor(0xed4245) | |
| .setTitle('Leave Background Updated') | |
| .setDescription('Background set to ' + styleNames[style]) | |
| await interaction.reply({ embeds: [embed], flags: MessageFlags.Ephemeral }) | |
| return | |
| } | |
| if (sub === 'upload') { | |
| await interaction.deferReply({ flags: MessageFlags.Ephemeral }) | |
| const attachment = interaction.options.getAttachment('image') | |
| if (!attachment.contentType?.startsWith('image/')) { | |
| await interaction.editReply({ | |
| content: 'Invalid file type. Please upload a PNG or JPG image.', | |
| }) | |
| return | |
| } | |
| if (attachment.size > 2097152) { | |
| await interaction.editReply({ | |
| content: 'File is too large. Maximum allowed size is 2MB.', | |
| }) | |
| return | |
| } | |
| const response = await fetch(attachment.url) | |
| const arrayBuffer = await response.arrayBuffer() | |
| const buffer = Buffer.from(arrayBuffer) | |
| await uploadBinary( | |
| 'guilds/' + guildId + '/assets/leave_bg.png', | |
| buffer, | |
| 'image/png' | |
| ) | |
| const config = await loadGuildConfig(guildId) | |
| config.leaveBackground = 'custom' | |
| await setGuildConfig(guildId, config) | |
| const embed = new EmbedBuilder() | |
| .setColor(0xed4245) | |
| .setTitle('Leave Background Uploaded') | |
| .setDescription('Your custom leave background is now active.') | |
| await interaction.editReply({ embeds: [embed] }) | |
| return | |
| } | |
| } |