MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / startDiscordBot

Function startDiscordBot

web/src/discord/client.ts:20–167  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

18 * @returns A promise that resolves with the client when ready, or rejects on error.
19 */
20export function startDiscordBot(): Promise<Client> {
21 return new Promise((resolve, reject) => {
22 const client = new Client({
23 intents: [
24 GatewayIntentBits.Guilds,
25 GatewayIntentBits.GuildMembers,
26 GatewayIntentBits.GuildMessages,
27 GatewayIntentBits.MessageContent,
28 ],
29 })
30
31 let isResolved = false
32
33 client.once(Events.ClientReady, (c) => {
34 logger.info(`Discord bot ready! Logged in as ${c.user.tag}`)
35 isResolved = true
36 resolve(client)
37 })
38
39 client.once('error', (error) => {
40 if (!isResolved) {
41 reject(error)
42 }
43 })
44
45 // Listen for messages in the welcome channel
46 client.on(Events.MessageCreate, async (message) => {
47 if (message.channelId !== WELCOME_CHANNEL_ID) return
48
49 // Check if this is a system message about a new member (7 is GuildMemberJoin)
50 if (message.system && message.type === 7) {
51 try {
52 await message.reply({
53 content: `Hey there! Enter \`/link\` to connect your Discord account with Codebuff (don't worry, only you can see it).`,
54 })
55 } catch (error) {
56 logger.error({ error }, 'Failed to send welcome message')
57 }
58 }
59 })
60
61 // Handle slash commands
62 client.on(Events.InteractionCreate, async (interaction: Interaction) => {
63 if (!interaction.isChatInputCommand()) return
64
65 const command = interaction as ChatInputCommandInteraction
66
67 // Check rate limit before processing command
68 if (isRateLimited(command.user.id)) {
69 await command.reply({
70 content:
71 'You are sending commands too quickly. Please wait a minute and try again.',
72 ephemeral: true,
73 })
74 return
75 }
76
77 if (command.commandName === 'link') {

Callers 1

mainFunction · 0.90

Calls 6

isRateLimitedFunction · 0.90
onceMethod · 0.80
fromMethod · 0.80
setMethod · 0.80
onMethod · 0.65
resolveFunction · 0.50

Tested by

no test coverage detected