File size: 5,313 Bytes
e903adb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { SlashCommandBuilder, ChannelType, EmbedBuilder } 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, res) {
  const perms = BigInt(interaction.member.permissions)
  const MANAGE_GUILD = 32n
  if (!(perms & MANAGE_GUILD)) {
    return res.json({
      type: 4,
      data: { content: 'You need Manage Server permission to use this command.', flags: 64 },
    })
  }

  const sub = interaction.data.options[0].name
  const guildId = interaction.guild_id

  const getOpt = (name) => {
    const opts = interaction.data.options[0].options ?? []
    return opts.find((o) => o.name === name)?.value ?? null
  }

  if (sub === 'channels') {
    const welcomeId = getOpt('welcome')
    const leaveId = getOpt('leave')

    const config = await loadGuildConfig(guildId)
    config.welcomeChannelId = welcomeId
    config.leaveChannelId = leaveId
    await setGuildConfig(guildId, config)

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

    return res.json({ type: 4, data: { embeds: [embed.toJSON()], flags: 64 } })
  }

  if (sub === 'autorole-add') {
    const roleId = getOpt('role')
    const config = await loadGuildConfig(guildId)

    if (config.autoRoles.includes(roleId)) {
      return res.json({
        type: 4,
        data: { content: 'That role is already in the auto-assign list.', flags: 64 },
      })
    }

    config.autoRoles.push(roleId)
    await setGuildConfig(guildId, config)

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

    return res.json({ type: 4, data: { embeds: [embed.toJSON()], flags: 64 } })
  }

  if (sub === 'autorole-remove') {
    const roleId = getOpt('role')
    const config = await loadGuildConfig(guildId)

    if (!config.autoRoles.includes(roleId)) {
      return res.json({
        type: 4,
        data: { content: 'That role is not in the list.', flags: 64 },
      })
    }

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

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

    return res.json({ type: 4, data: { embeds: [embed.toJSON()], flags: 64 } })
  }

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

    if (!config.autoRoles.length) {
      return res.json({
        type: 4,
        data: { content: 'No auto-roles configured.', flags: 64 },
      })
    }

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

    return res.json({ type: 4, data: { embeds: [embed.toJSON()], flags: 64 } })
  }

  if (sub === 'dm') {
    const enabled = getOpt('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'}**`)

    return res.json({ type: 4, data: { embeds: [embed.toJSON()], flags: 64 } })
  }
}