1+ const { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits, ChannelType } = require ( "discord.js" ) ;
2+ const Database = require ( "../../utils/database" ) ;
3+
4+ module . exports = {
5+ data : new SlashCommandBuilder ( )
6+ . setName ( "message-logs" )
7+ . setDescription ( "📝 Configure message logging settings for the server." )
8+ . setDefaultMemberPermissions ( PermissionFlagsBits . ManageGuild )
9+ . addSubcommand ( subcommand =>
10+ subcommand
11+ . setName ( "set" )
12+ . setDescription ( "Sets the channel for logging messages. Leave empty to disable." )
13+ . addChannelOption ( option =>
14+ option . setName ( "channel" )
15+ . setDescription ( "The text channel to send logs to." )
16+ . addChannelTypes ( ChannelType . GuildText )
17+ . setRequired ( false ) // Not required, to allow disabling
18+ )
19+ )
20+ . addSubcommandGroup ( group =>
21+ group
22+ . setName ( "exempt" )
23+ . setDescription ( "Exempt users or roles from message logging." )
24+ . addSubcommand ( subcommand =>
25+ subcommand
26+ . setName ( "add" )
27+ . setDescription ( "Add a user or role to the exemption list." )
28+ . addUserOption ( option => option . setName ( "user" ) . setDescription ( "The user to exempt." ) )
29+ . addRoleOption ( option => option . setName ( "role" ) . setDescription ( "The role to exempt." ) )
30+ )
31+ . addSubcommand ( subcommand =>
32+ subcommand
33+ . setName ( "remove" )
34+ . setDescription ( "Remove a user or role from the exemption list." )
35+ . addUserOption ( option => option . setName ( "user" ) . setDescription ( "The user to un-exempt." ) )
36+ . addRoleOption ( option => option . setName ( "role" ) . setDescription ( "The role to un-exempt." ) )
37+ )
38+ . addSubcommand ( subcommand =>
39+ subcommand
40+ . setName ( "list" )
41+ . setDescription ( "List all exempted users and roles." )
42+ )
43+ ) ,
44+ cooldown : 5 ,
45+ async execute ( interaction , client ) {
46+ const db = await Database . getInstance ( ) ;
47+ const subcommandGroup = interaction . options . getSubcommandGroup ( ) ;
48+ const subcommand = interaction . options . getSubcommand ( ) ;
49+
50+ await interaction . deferReply ( { ephemeral : true } ) ;
51+
52+ if ( ! subcommandGroup ) { // Handling the 'set' subcommand
53+ const channel = interaction . options . getChannel ( "channel" ) ;
54+ try {
55+ await db . updateServerConfig ( interaction . guild . id , {
56+ messageLogChannelId : channel ? channel . id : null ,
57+ } ) ;
58+ const embed = new EmbedBuilder ( )
59+ . setColor ( client . colors . success )
60+ . setTitle ( "✅ Log Channel Updated" )
61+ . setDescription ( channel ? `Message logs will now be sent to ${ channel } .` : "Message logging has been disabled." )
62+ await interaction . editReply ( { embeds : [ embed ] } ) ;
63+ } catch ( error ) {
64+ console . error ( "Error in /message-logs set:" , error ) ;
65+ await interaction . editReply ( { content : "An error occurred while updating the settings." } ) ;
66+ }
67+ } else if ( subcommandGroup === "exempt" ) {
68+ const user = interaction . options . getUser ( "user" ) ;
69+ const role = interaction . options . getRole ( "role" ) ;
70+
71+ if ( subcommand === "add" ) {
72+ if ( ! user && ! role ) return interaction . editReply ( { content : "You must specify a user or a role to add." } ) ;
73+ let update = { } ;
74+ if ( user ) update . $addToSet = { messageLogExemptUsers : user . id } ;
75+ if ( role ) update . $addToSet = { ...update . $addToSet , messageLogExemptRoles : role . id } ;
76+
77+ await db . updateServerConfig ( interaction . guild . id , update ) ;
78+ await interaction . editReply ( { content : `Successfully added ${ user || "" } ${ role || "" } to the exemption list.` } ) ;
79+
80+ } else if ( subcommand === "remove" ) {
81+ if ( ! user && ! role ) return interaction . editReply ( { content : "You must specify a user or a role to remove." } ) ;
82+ let update = { } ;
83+ if ( user ) update . $pull = { messageLogExemptUsers : user . id } ;
84+ if ( role ) update . $pull = { ...update . $pull , messageLogExemptRoles : role . id } ;
85+
86+ await db . updateServerConfig ( interaction . guild . id , update ) ;
87+ await interaction . editReply ( { content : `Successfully removed ${ user || "" } ${ role || "" } from the exemption list.` } ) ;
88+
89+ } else if ( subcommand === "list" ) {
90+ const config = await db . serverConfig . findOne ( { guildId : interaction . guild . id } ) ;
91+ const users = config . messageLogExemptUsers ?. map ( id => `<@${ id } >` ) . join ( "\n" ) || "None" ;
92+ const roles = config . messageLogExemptRoles ?. map ( id => `<@&${ id } >` ) . join ( "\n" ) || "None" ;
93+
94+ const embed = new EmbedBuilder ( )
95+ . setColor ( client . colors . primary )
96+ . setTitle ( "📜 Message Log Exemptions" )
97+ . addFields (
98+ { name : "Exempted Users" , value : users , inline : true } ,
99+ { name : "Exempted Roles" , value : roles , inline : true }
100+ ) ;
101+ await interaction . editReply ( { embeds : [ embed ] } ) ;
102+ }
103+ }
104+ } ,
105+ } ;
0 commit comments