11import { SlashCommandBuilder , PermissionsBitField } from 'discord.js' ;
22import { buildEmbed } from '../utils/embed' ;
3- import { getGuildConfig } from '../config/store' ;
3+ import { getGuildConfig , upsertGuildConfig } from '../config/store' ;
44
55const data = new SlashCommandBuilder ( )
66 . setName ( 'ticketmessage' )
@@ -11,22 +11,37 @@ const data = new SlashCommandBuilder()
1111
1212async function execute ( interaction : any ) {
1313 if ( ! interaction . memberPermissions ?. has ( PermissionsBitField . Flags . ManageChannels ) ) {
14- return interaction . reply ( { content : 'Solo junta/admin.' , ephemeral : true } ) ;
14+ return interaction . reply ( { content : 'Solo junta/admin.' , flags : 1 << 6 } ) ;
1515 }
1616 const cfg = getGuildConfig ( interaction . guildId ) ;
1717 if ( ! cfg ?. channels . ticketTrigger ) {
18- return interaction . reply ( { content : 'Configura primero /setup' , ephemeral : true } ) ;
18+ return interaction . reply ( { content : 'Configura primero /setup' , flags : 1 << 6 } ) ;
1919 }
2020 const channel = interaction . guild . channels . cache . get ( cfg . channels . ticketTrigger ) ;
2121 if ( ! channel || ! channel . isTextBased ?.( ) ) {
22- return interaction . reply ( { content : 'Canal de tickets inválido' , ephemeral : true } ) ;
22+ return interaction . reply ( { content : 'Canal de tickets inválido' , flags : 1 << 6 } ) ;
2323 }
2424 const description =
2525 interaction . options . getString ( 'description' ) ?? 'Reacciona con 🎫 para crear un ticket.' ;
2626 const embed = buildEmbed ( { title : 'Tickets' , description } ) ;
27+
28+ // delete previous ticket message if stored
29+ if ( cfg . ticketMessageId ) {
30+ try {
31+ const prev = await channel . messages . fetch ( cfg . ticketMessageId ) . catch ( ( ) => null ) ;
32+ if ( prev ?. deletable ) await prev . delete ( ) ;
33+ } catch ( err ) {
34+ // ignore fetch/delete errors
35+ }
36+ }
37+
2738 const msg = await channel . send ( { embeds : [ embed ] } ) ;
2839 await msg . react ( '🎫' ) ;
29- await interaction . reply ( { content : 'Mensaje de tickets publicado.' , ephemeral : true } ) ;
40+
41+ // persist new message id
42+ upsertGuildConfig ( { ...cfg , guildId : interaction . guildId , ticketMessageId : msg . id } ) ;
43+
44+ await interaction . reply ( { content : 'Mensaje de tickets publicado.' , flags : 1 << 6 } ) ;
3045}
3146
3247export default { data, execute } ;
0 commit comments