1- import { Client , GatewayIntentBits , Events , ChatInputCommandInteraction } from 'discord.js'
1+ import { Client , GatewayIntentBits , Events , ChatInputCommandInteraction , MessageFlags } from 'discord.js'
22import { useNitroApp , useRuntimeConfig } from '#imports'
33import { KarmaService } from './karma'
44
55export function createDiscordBot ( ) {
66 const logger = useNitroApp ( ) . logger
7+ const runtimeConfig = useRuntimeConfig ( )
8+ const allowedGuildId = runtimeConfig . public . discordGuildId
79
810 const client = new Client ( {
911 intents : [
@@ -13,11 +15,21 @@ export function createDiscordBot() {
1315
1416 client . once ( Events . ClientReady , ( readyClient ) => {
1517 logger . info ( `Discord bot ready! Logged in as ${ readyClient . user . tag } ` )
18+ logger . info ( `Bot will only respond to commands from guild: ${ allowedGuildId } ` )
1619 } )
1720
1821 client . on ( Events . InteractionCreate , async ( interaction ) => {
1922 if ( ! interaction . isChatInputCommand ( ) ) return
2023
24+ if ( interaction . guildId !== allowedGuildId ) {
25+ logger . warn ( `Ignored command from unauthorized guild: ${ interaction . guildId } ` )
26+ await interaction . reply ( {
27+ content : '❌ This bot is not configured for this server.' ,
28+ flags : MessageFlags . Ephemeral
29+ } ) . catch ( ( ) => { } )
30+ return
31+ }
32+
2133 if ( interaction . commandName === 'karma' ) {
2234 await handleKarmaCommand ( interaction )
2335 }
@@ -30,47 +42,40 @@ async function handleKarmaCommand(interaction: ChatInputCommandInteraction) {
3042 const logger = useNitroApp ( ) . logger
3143
3244 try {
33- // Get the user who invoked the command
45+ await interaction . deferReply ( )
46+
3447 const giver = interaction . user
3548
36- // Get all mentioned users from the command
37- const users = interaction . options . resolved ?. users
49+ const receiverUser = interaction . options . getUser ( 'user' , true )
3850
39- if ( ! users || users . size === 0 ) {
40- await interaction . reply ( {
41- content : '❌ Please mention at least one user to give karma!' ,
42- ephemeral : true
51+ const reason = interaction . options . getString ( 'reason' , false ) || undefined
52+
53+ logger . info ( 'Karma command received:' , { giver : giver . id , receiver : receiverUser . id , reason : reason || 'No reason provided' } )
54+
55+ if ( ! receiverUser ) {
56+ await interaction . editReply ( {
57+ content : '❌ Please mention a user to give karma!'
4358 } )
4459 return
4560 }
4661
47- // Extract user IDs
48- const receiverIds = Array . from ( users . values ( ) ) . map ( user => user . id )
49-
50- // Defer reply for longer processing
51- await interaction . deferReply ( )
52-
53- // Call karma service directly
5462 const karmaService = new KarmaService ( )
55- const result = await karmaService . giveKarma ( giver . id , receiverIds )
63+ const result = await karmaService . giveKarma ( giver . id , receiverUser . id , reason )
5664
57- // Format success message
58- const userMentions = Array . from ( users . values ( ) )
59- . map ( user => `<@${ user . id } >` )
60- . join ( ', ' )
65+ let message = `✅ Karma given to <@${ receiverUser . id } >!\n\n<@${ receiverUser . id } >: **${ result . total_karma } ** karma`
6166
62- const karmaDetails = result . karma_counts
63- . map ( kc => `<@ ${ kc . receiver_discord_id } >: ** ${ kc . total_karma } ** karma` )
64- . join ( '\n' )
67+ if ( reason ) {
68+ message += `\n\n💬 Reason: ${ reason } `
69+ }
6570
6671 await interaction . editReply ( {
67- content : `✅ Karma given to ${ userMentions } !\n\n ${ karmaDetails } `
72+ content : message
6873 } )
6974
70- logger . info ( `Karma given by ${ giver . id } to ${ receiverIds . join ( ', ' ) } ` )
75+ logger . info ( `Karma given by ${ giver . id } to ${ receiverUser . id } ${ reason ? ` with reason: ${ reason } ` : '' } ` )
7176
7277 } catch ( error ) {
73- logger . error ( 'Error handling karma command :' , error )
78+ logger . info ( 'Error handling karma:' , error )
7479
7580 const errorMessage = error instanceof Error ? error . message : 'An error occurred while processing your request.'
7681 const userFriendlyMessage = `❌ ${ errorMessage } `
@@ -82,7 +87,7 @@ async function handleKarmaCommand(interaction: ChatInputCommandInteraction) {
8287 } else {
8388 await interaction . reply ( {
8489 content : userFriendlyMessage ,
85- ephemeral : true
90+ flags : MessageFlags . Ephemeral
8691 } )
8792 }
8893 }
0 commit comments