Spaces:
Sleeping
Sleeping
Upload 12 files
Browse files- src/bot.js +44 -0
- src/events/guildMemberAdd.js +108 -0
- src/events/ready.js +32 -0
- src/index.js +4 -10
src/bot.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Client, GatewayIntentBits, Partials } from 'discord.js'
|
| 2 |
+
import { readdirSync } from 'node:fs'
|
| 3 |
+
import { resolve, dirname } from 'node:path'
|
| 4 |
+
import { fileURLToPath, pathToFileURL } from 'node:url'
|
| 5 |
+
|
| 6 |
+
const __dirname = dirname(fileURLToPath(import.meta.url))
|
| 7 |
+
|
| 8 |
+
export const client = new Client({
|
| 9 |
+
intents: [
|
| 10 |
+
GatewayIntentBits.Guilds,
|
| 11 |
+
GatewayIntentBits.GuildMembers,
|
| 12 |
+
GatewayIntentBits.GuildMessages,
|
| 13 |
+
GatewayIntentBits.MessageContent,
|
| 14 |
+
],
|
| 15 |
+
partials: [Partials.GuildMember],
|
| 16 |
+
})
|
| 17 |
+
|
| 18 |
+
export async function startBot() {
|
| 19 |
+
const token = process.env.DISCORD_TOKEN
|
| 20 |
+
if (!token) throw new Error('Missing required environment variable: DISCORD_TOKEN')
|
| 21 |
+
|
| 22 |
+
console.log('[bot] Starting Discord client...')
|
| 23 |
+
|
| 24 |
+
// Dynamically load all event files from src/events/
|
| 25 |
+
const eventsDir = resolve(__dirname, 'events')
|
| 26 |
+
const eventFiles = readdirSync(eventsDir).filter((f) => f.endsWith('.js'))
|
| 27 |
+
|
| 28 |
+
for (const file of eventFiles) {
|
| 29 |
+
const filePath = pathToFileURL(resolve(eventsDir, file)).href
|
| 30 |
+
const event = await import(filePath)
|
| 31 |
+
|
| 32 |
+
if (event.once) {
|
| 33 |
+
client.once(event.name, (...args) => event.execute(...args))
|
| 34 |
+
} else {
|
| 35 |
+
client.on(event.name, (...args) => event.execute(...args))
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
await client.login(token)
|
| 40 |
+
|
| 41 |
+
return new Promise((resolve) => {
|
| 42 |
+
client.once('ready', () => resolve())
|
| 43 |
+
})
|
| 44 |
+
}
|
src/events/guildMemberAdd.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import {
|
| 2 |
+
loadGuildConfig,
|
| 3 |
+
getMemberData,
|
| 4 |
+
initMemberData,
|
| 5 |
+
setMemberData,
|
| 6 |
+
} from '../storage/cache.js'
|
| 7 |
+
|
| 8 |
+
export const name = 'guildMemberAdd'
|
| 9 |
+
export const once = false
|
| 10 |
+
|
| 11 |
+
export async function execute(member) {
|
| 12 |
+
const { guild, user } = member
|
| 13 |
+
const guildId = guild.id
|
| 14 |
+
const userId = user.id
|
| 15 |
+
const username = user.username
|
| 16 |
+
|
| 17 |
+
// STEP 1 — Load guild config
|
| 18 |
+
let config
|
| 19 |
+
try {
|
| 20 |
+
config = await loadGuildConfig(guildId)
|
| 21 |
+
if (!config) {
|
| 22 |
+
console.warn(`[guildMemberAdd] No config found for guild ${guildId}, skipping`)
|
| 23 |
+
return
|
| 24 |
+
}
|
| 25 |
+
} catch (err) {
|
| 26 |
+
console.error(`[guildMemberAdd] Step 1 failed (load config): ${err.message}`)
|
| 27 |
+
return
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
// STEP 2 — Create or update member profile
|
| 31 |
+
let memberData
|
| 32 |
+
try {
|
| 33 |
+
const existing = await getMemberData(guildId, userId)
|
| 34 |
+
if (existing) {
|
| 35 |
+
existing.joinCount += 1
|
| 36 |
+
existing.joinedAt = new Date().toISOString()
|
| 37 |
+
existing.leftAt = null
|
| 38 |
+
await setMemberData(guildId, userId, existing)
|
| 39 |
+
memberData = existing
|
| 40 |
+
} else {
|
| 41 |
+
memberData = await initMemberData(guildId, userId, username)
|
| 42 |
+
}
|
| 43 |
+
} catch (err) {
|
| 44 |
+
console.error(`[guildMemberAdd] Step 2 failed (member profile): ${err.message}`)
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
// STEP 3 — Assign auto roles
|
| 48 |
+
try {
|
| 49 |
+
if (config.autoRoles?.length > 0) {
|
| 50 |
+
for (const roleId of config.autoRoles) {
|
| 51 |
+
try {
|
| 52 |
+
await member.roles.add(roleId)
|
| 53 |
+
console.log(`[autorole] Assigned ${roleId} to ${username}`)
|
| 54 |
+
} catch (err) {
|
| 55 |
+
console.error(`[autorole] Failed to assign ${roleId} to ${username}: ${err.message}`)
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
} catch (err) {
|
| 60 |
+
console.error(`[guildMemberAdd] Step 3 failed (auto roles): ${err.message}`)
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
// STEP 4 — Send welcome channel message
|
| 64 |
+
try {
|
| 65 |
+
if (config.welcomeChannelId) {
|
| 66 |
+
const channel = guild.channels.cache.get(config.welcomeChannelId)
|
| 67 |
+
if (!channel) {
|
| 68 |
+
console.warn(
|
| 69 |
+
`[guildMemberAdd] Welcome channel ${config.welcomeChannelId} not found in guild ${guild.name}`
|
| 70 |
+
)
|
| 71 |
+
} else {
|
| 72 |
+
const parsed = config.welcomeMessage
|
| 73 |
+
.replaceAll('{user}', member.toString())
|
| 74 |
+
.replaceAll('{server}', guild.name)
|
| 75 |
+
.replaceAll('{count}', guild.memberCount)
|
| 76 |
+
|
| 77 |
+
// TODO: attach welcome card image
|
| 78 |
+
await channel.send(parsed)
|
| 79 |
+
}
|
| 80 |
+
}
|
| 81 |
+
} catch (err) {
|
| 82 |
+
console.error(`[guildMemberAdd] Step 4 failed (welcome message): ${err.message}`)
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
// STEP 5 — Send DM
|
| 86 |
+
try {
|
| 87 |
+
if (config.dmEnabled) {
|
| 88 |
+
const parsedDm = config.dmMessage
|
| 89 |
+
.replaceAll('{username}', username)
|
| 90 |
+
.replaceAll('{server}', guild.name)
|
| 91 |
+
|
| 92 |
+
let dmSent = false
|
| 93 |
+
try {
|
| 94 |
+
await user.send(parsedDm)
|
| 95 |
+
dmSent = true
|
| 96 |
+
} catch {
|
| 97 |
+
console.log(`[dm] Could not send DM to ${username}, DMs likely disabled`)
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
if (dmSent && memberData) {
|
| 101 |
+
memberData.dmSent = true
|
| 102 |
+
await setMemberData(guildId, userId, memberData)
|
| 103 |
+
}
|
| 104 |
+
}
|
| 105 |
+
} catch (err) {
|
| 106 |
+
console.error(`[guildMemberAdd] Step 5 failed (DM): ${err.message}`)
|
| 107 |
+
}
|
| 108 |
+
}
|
src/events/ready.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { ActivityType } from 'discord.js'
|
| 2 |
+
import { loadGuildConfig } from '../storage/cache.js'
|
| 3 |
+
|
| 4 |
+
export const name = 'ready'
|
| 5 |
+
export const once = true
|
| 6 |
+
|
| 7 |
+
export async function execute(client) {
|
| 8 |
+
console.log(`[ready] Logged in as ${client.user.tag}`)
|
| 9 |
+
|
| 10 |
+
client.user.setPresence({
|
| 11 |
+
status: 'online',
|
| 12 |
+
activities: [
|
| 13 |
+
{
|
| 14 |
+
type: ActivityType.Watching,
|
| 15 |
+
name: 'for new members',
|
| 16 |
+
},
|
| 17 |
+
],
|
| 18 |
+
})
|
| 19 |
+
|
| 20 |
+
let loaded = 0
|
| 21 |
+
for (const [, guild] of client.guilds.cache) {
|
| 22 |
+
try {
|
| 23 |
+
await loadGuildConfig(guild.id)
|
| 24 |
+
console.log(`[ready] Loaded config for guild: ${guild.name}`)
|
| 25 |
+
loaded++
|
| 26 |
+
} catch (err) {
|
| 27 |
+
console.error(`[ready] Failed to load config for guild ${guild.name}: ${err.message}`)
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
console.log(`[ready] Watching ${loaded} guilds`)
|
| 32 |
+
}
|
src/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
-
// Bot initialization will be added in the next step
|
| 2 |
-
|
| 3 |
import { startHealthServer } from './health.js'
|
|
|
|
| 4 |
|
| 5 |
;(async () => {
|
| 6 |
const requiredEnvVars = ['HF_TOKEN', 'HF_BUCKET_NAME', 'DISCORD_TOKEN', 'DISCORD_CLIENT_ID']
|
|
@@ -15,13 +14,8 @@ import { startHealthServer } from './health.js'
|
|
| 15 |
process.exit(1)
|
| 16 |
})
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
const { loadGuildConfig } = await import('./storage/cache.js')
|
| 21 |
-
const result = await loadGuildConfig('test123')
|
| 22 |
-
console.log('[test] guild config:', result)
|
| 23 |
-
} catch (err) {
|
| 24 |
-
console.error(err)
|
| 25 |
process.exit(1)
|
| 26 |
-
}
|
| 27 |
})()
|
|
|
|
|
|
|
|
|
|
| 1 |
import { startHealthServer } from './health.js'
|
| 2 |
+
import { startBot } from './bot.js'
|
| 3 |
|
| 4 |
;(async () => {
|
| 5 |
const requiredEnvVars = ['HF_TOKEN', 'HF_BUCKET_NAME', 'DISCORD_TOKEN', 'DISCORD_CLIENT_ID']
|
|
|
|
| 14 |
process.exit(1)
|
| 15 |
})
|
| 16 |
|
| 17 |
+
await startBot().catch((err) => {
|
| 18 |
+
console.error('Failed to start bot:', err)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
process.exit(1)
|
| 20 |
+
})
|
| 21 |
})()
|