File size: 3,412 Bytes
9b7ecf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
  }
}