File size: 2,181 Bytes
ede5236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**

 * src/events/guildCreate.js

 *

 * Fires when the bot is added to a new server.

 * Initialises guild config and sends a setup-guide embed.

 */

import { ChannelType, PermissionFlagsBits } from 'discord.js'
import { initGuildConfig } from '../storage/cache.js'

export const name = 'guildCreate'
export const once = false

export async function execute(guild) {
  // Step 1 — Announce and initialise
  console.log(`[guild] Bot added to new guild: ${guild.name} (${guild.id})`)

  try {
    await initGuildConfig(guild.id)
    console.log(`[guild] Initialized config for: ${guild.name}`)
  } catch (err) {
    console.error(`[guild] Failed to initialize config for ${guild.name}: ${err.message}`)
  }

  // Step 2 — Find a channel we can talk in
  let targetChannel = null

  try {
    if (guild.systemChannel) {
      targetChannel = guild.systemChannel
    } else {
      const me = guild.members.me
      for (const [, channel] of guild.channels.cache) {
        if (channel.type !== ChannelType.GuildText) continue
        if (me && !channel.permissionsFor(me)?.has(PermissionFlagsBits.SendMessages)) continue
        targetChannel = channel
        break
      }
    }
  } catch (err) {
    console.error(`[guild] Failed to locate a text channel in ${guild.name}: ${err.message}`)
  }

  // Step 3 — Send the setup-guide embed
  if (!targetChannel) {
    console.log(`[guild] Could not send setup guide to ${guild.name}`)
    return
  }

  try {
    await targetChannel.send({
      embeds: [
        {
          color: 0x5865F2,
          title: 'Thanks for adding me!',
          description:
            "I'm your new welcome bot. Here's how to get started:\n\n" +
            'Use **/setup channels** to set your welcome and leave channels.\n' +
            'Use **/welcome background** to pick a card style.\n' +
            'Use **/preview** to see how your welcome card looks.\n\n' +
            'If you need help, use **/help**.',
          footer: { text: 'Archy Welcomer' },
        },
      ],
    })
  } catch {
    console.log(`[guild] Could not send setup guide to ${guild.name}`)
  }
}