File size: 5,657 Bytes
3fc73f4
 
 
 
 
9b7ecf4
3fc73f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b7ecf4
3fc73f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b7ecf4
3fc73f4
 
 
 
 
 
 
 
 
 
9b7ecf4
3fc73f4
 
 
 
 
 
 
 
 
 
 
 
9b7ecf4
3fc73f4
 
 
 
 
 
 
 
 
 
9b7ecf4
3fc73f4
 
 
 
 
 
 
 
 
 
 
 
9b7ecf4
3fc73f4
 
 
 
 
 
 
 
 
9b7ecf4
3fc73f4
 
 
 
 
 
 
 
 
 
9b7ecf4
3fc73f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b7ecf4
3fc73f4
 
 
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import {
  SlashCommandBuilder,
  ChannelType,
  PermissionFlagsBits,
  EmbedBuilder,
  MessageFlags,
} from 'discord.js'
import { loadGuildConfig, setGuildConfig } from '../storage/cache.js'

export const data = new SlashCommandBuilder()
  .setName('setup')
  .setDescription('Configure the bot for this server')
  .addSubcommand((sub) =>
    sub
      .setName('channels')
      .setDescription('Set the welcome and leave channels')
      .addChannelOption((opt) =>
        opt
          .setName('welcome')
          .setDescription('Channel to post welcome messages in')
          .addChannelTypes(ChannelType.GuildText)
          .setRequired(true)
      )
      .addChannelOption((opt) =>
        opt
          .setName('leave')
          .setDescription('Channel to post leave messages in')
          .addChannelTypes(ChannelType.GuildText)
          .setRequired(true)
      )
  )
  .addSubcommand((sub) =>
    sub
      .setName('autorole-add')
      .setDescription('Add a role to be assigned when a member joins')
      .addRoleOption((opt) =>
        opt
          .setName('role')
          .setDescription('Role to auto-assign')
          .setRequired(true)
      )
  )
  .addSubcommand((sub) =>
    sub
      .setName('autorole-remove')
      .setDescription('Remove a role from auto-assign list')
      .addRoleOption((opt) =>
        opt
          .setName('role')
          .setDescription('Role to remove')
          .setRequired(true)
      )
  )
  .addSubcommand((sub) =>
    sub
      .setName('autorole-list')
      .setDescription('Show all currently configured auto-assign roles')
  )
  .addSubcommand((sub) =>
    sub
      .setName('dm')
      .setDescription('Toggle welcome DM on or off')
      .addBooleanOption((opt) =>
        opt
          .setName('enabled')
          .setDescription('Send a DM to new members when they join')
          .setRequired(true)
      )
  )

export async function execute(interaction) {
  // All subcommands require Manage Server
  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 === 'channels') {
    const welcome = interaction.options.getChannel('welcome')
    const leave = interaction.options.getChannel('leave')

    const config = await loadGuildConfig(guildId)
    config.welcomeChannelId = welcome.id
    config.leaveChannelId = leave.id
    await setGuildConfig(guildId, config)

    const embed = new EmbedBuilder()
      .setColor(0x5865f2)
      .setTitle('Channels Updated')
      .setDescription(
        `✅ Welcome channel set to ${welcome}\n✅ Leave channel set to ${leave}`
      )

    await interaction.reply({ embeds: [embed], flags: MessageFlags.Ephemeral })
    return
  }

  if (sub === 'autorole-add') {
    const role = interaction.options.getRole('role')
    const config = await loadGuildConfig(guildId)

    if (config.autoRoles.includes(role.id)) {
      await interaction.reply({
        content: 'That role is already in the auto-assign list.',
        flags: MessageFlags.Ephemeral,
      })
      return
    }

    config.autoRoles.push(role.id)
    await setGuildConfig(guildId, config)

    const embed = new EmbedBuilder()
      .setColor(0x57f287)
      .setTitle('Auto-Role Added')
      .setDescription(`New members will now receive <@&${role.id}>`)

    await interaction.reply({ embeds: [embed], flags: MessageFlags.Ephemeral })
    return
  }

  if (sub === 'autorole-remove') {
    const role = interaction.options.getRole('role')
    const config = await loadGuildConfig(guildId)

    if (!config.autoRoles.includes(role.id)) {
      await interaction.reply({
        content: 'That role is not in the list.',
        flags: MessageFlags.Ephemeral,
      })
      return
    }

    config.autoRoles = config.autoRoles.filter((id) => id !== role.id)
    await setGuildConfig(guildId, config)

    const embed = new EmbedBuilder()
      .setColor(0xed4245)
      .setTitle('Auto-Role Removed')
      .setDescription(`<@&${role.id}> will no longer be assigned to new members`)

    await interaction.reply({ embeds: [embed], flags: MessageFlags.Ephemeral })
    return
  }

  if (sub === 'autorole-list') {
    const config = await loadGuildConfig(guildId)

    if (!config.autoRoles.length) {
      await interaction.reply({
        content: 'No auto-roles configured.',
        flags: MessageFlags.Ephemeral,
      })
      return
    }

    const roleList = config.autoRoles.map((id) => `<@&${id}>`).join('\n')
    const embed = new EmbedBuilder()
      .setColor(0x5865f2)
      .setTitle('Auto-Assign Roles')
      .setDescription(roleList)

    await interaction.reply({ embeds: [embed], flags: MessageFlags.Ephemeral })
    return
  }

  if (sub === 'dm') {
    const enabled = interaction.options.getBoolean('enabled')
    const config = await loadGuildConfig(guildId)

    config.dmEnabled = enabled
    await setGuildConfig(guildId, config)

    const embed = new EmbedBuilder()
      .setColor(enabled ? 0x57f287 : 0xed4245)
      .setTitle('Welcome DM Updated')
      .setDescription(
        `Welcome DMs are now **${enabled ? 'enabled' : 'disabled'}**`
      )

    await interaction.reply({ embeds: [embed], flags: MessageFlags.Ephemeral })
    return
  }
}