1- import { EmbedBuilder , ChatInputCommandInteraction , GuildMemberRoleManager } from 'discord.js' ;
1+ import { EmbedBuilder , ChatInputCommandInteraction , GuildMemberRoleManager , Colors } from 'discord.js' ;
22import { KOGBot } from '../../index.js' ;
33import knex , { Knex } from "knex" ;
44
@@ -15,136 +15,128 @@ class LogEventCommand implements SlashCommand {
1515 }
1616
1717 async execute ( interaction : ChatInputCommandInteraction ) : Promise < void > {
18- const allowedroleID = this . kogBot . environment . discord . mr_role ; // Set this later when I have perms
19- const logChannel = this . kogBot . environment . discord . logChannel ; // log channel
18+ const allowedroleID = this . kogBot . environment . roles . mr_role ;
19+ const logChannel = this . kogBot . environment . discord . logChannel ;
2020 const userId = interaction . user . id ; // Needed for DB
21- //const DB = await this.db('KOGDB').where({ userid: userId });
22- try {
23- // Check for required role
24- if ( ! ( interaction . member ?. roles instanceof GuildMemberRoleManager ) || ! interaction . member . roles . cache . has ( allowedroleID ) ) {
25- const noperms = new EmbedBuilder ( )
26- . setColor ( '#E73A3A' )
27- . setTitle ( 'Error' )
28- . setDescription ( "You don't have the required role to use this command." )
29- . setTimestamp ( ) ;
30-
31- await interaction . reply ( { embeds : [ noperms ] , ephemeral : true } ) ;
32- return ;
33- }
21+
22+ // Check for required role
23+ if ( ! ( interaction . member ?. roles instanceof GuildMemberRoleManager ) || ! interaction . member . roles . cache . has ( allowedroleID ) ) {
24+ const noperms = new EmbedBuilder ( )
25+ . setColor ( Colors . Red )
26+ . setTitle ( 'Error' )
27+ . setDescription ( "You don't have the required role to use this command." )
28+ . setTimestamp ( )
29+ . setFooter ( {
30+ text : `Kleiner Oil Group` ,
31+ iconURL : interaction . guild ?. iconURL ( ) as string
32+ } )
33+
34+ await interaction . reply ( { embeds : [ noperms ] , ephemeral : true } ) ;
35+ return ;
36+ }
3437
35- const logStart = new EmbedBuilder ( )
36- . setColor ( '#9033FF' )
37- . setTitle ( 'Logging' )
38- . setDescription ( 'To log an event, please follow the format:\n\n<@1138235120424325160>,<@573540579682811906>,<@1344176447551574078>,<@110877167897853952>,<@1125601338768756756>...\n\nNames must be separated by commas and must be mentions.' )
39- . setTimestamp ( ) ;
38+ const logStart = new EmbedBuilder ( )
39+ . setColor ( '#9033FF' )
40+ . setTitle ( 'Logging' )
41+ . setDescription ( 'To log an event, please follow the format:\n\n<@1138235120424325160>,<@573540579682811906>,<@1344176447551574078>,<@110877167897853952>,<@1125601338768756756>...\n\nNames must be separated by commas and must be mentions.' )
42+ . setTimestamp ( ) ;
4043
41- await interaction . reply ( { embeds : [ logStart ] , ephemeral : true } ) ;
44+ await interaction . reply ( { embeds : [ logStart ] , ephemeral : true } ) ;
4245
43- if ( ! interaction . channel ) {
44- await interaction . reply ( 'Channel not found.' ) ;
45- return ;
46- }
46+ if ( ! interaction . channel ) {
47+ await interaction . reply ( 'Channel not found.' ) ;
48+ return ;
49+ }
4750
48- const filter = ( response : any ) => response . user . id === interaction . user . id ;
49- const collected = await interaction . channel ?. awaitMessageComponent ( { filter, time : 60000 } ) . catch ( ( ) => null ) ;
50- const response = collected ?. isMessageComponent ( ) ? collected . message . content : null ;
51+ const filter = ( response : any ) => response . user . id === interaction . user . id ;
52+ const collected = await interaction . channel ?. awaitMessageComponent ( { filter, time : 60000 } ) . catch ( ( ) => null ) ;
53+ const response = collected ?. isMessageComponent ( ) ? collected . message . content : null ;
5154
52- const mentionRegexthing = / ^ < @ \d + > (?: , \s ? < @ \d + > ) * $ / ;
55+ const mentionRegexthing = / ^ < @ \d + > (?: , \s ? < @ \d + > ) * $ / ;
5356
54- if ( ! mentionRegexthing . test ( response ! ) ) {
55- await interaction . followUp ( 'Invalid format. Please make sure the names are separated by commas and each name is a mention. Run the command again with the correct format.' ) ;
56- return ;
57- }
57+ if ( ! mentionRegexthing . test ( response ! ) ) {
58+ await interaction . followUp ( 'Invalid format. Please make sure the names are separated by commas and each name is a mention. Run the command again with the correct format.' ) ;
59+ return ;
60+ }
5861
59- if ( response ?. toLowerCase ( ) === 'cancel' ) {
60- await interaction . reply ( `<@${ interaction . user . id } > canceled the event log.` ) ;
61- return ;
62- }
62+ if ( response ?. toLowerCase ( ) === 'cancel' ) {
63+ await interaction . reply ( `<@${ interaction . user . id } > canceled the event log.` ) ;
64+ return ;
65+ }
6366
64- if ( response && typeof response === 'object' && 'message' in response && ( response as any ) . message === 'time' ) {
65- await interaction . reply ( `<@${ interaction . user . id } > you took too long to follow up, please try again.` ) ;
66- return ;
67- }
67+ if ( response && typeof response === 'object' && 'message' in response && ( response as any ) . message === 'time' ) {
68+ await interaction . reply ( `<@${ interaction . user . id } > you took too long to follow up, please try again.` ) ;
69+ return ;
70+ }
6871
69- if ( ! response ) {
70- await interaction . followUp ( 'No response received. Please try again.' ) ;
71- return ;
72- }
73- const mentions = response . split ( ',' ) . map ( ( id : string ) => id . trim ( ) . replace ( '<@' , '' ) . replace ( '>' , '' ) ) ;
74- const userIds : string [ ] = [ ] ;
72+ if ( ! response ) {
73+ await interaction . followUp ( 'No response received. Please try again.' ) ;
74+ return ;
75+ }
76+ const mentions = response . split ( ',' ) . map ( ( id : string ) => id . trim ( ) . replace ( '<@' , '' ) . replace ( '>' , '' ) ) ;
77+ const userIds : string [ ] = [ ] ;
7578
76- for ( const mention of mentions ) {
77- const userId = mention . replace ( '<@' , '' ) . replace ( '>' , '' ) ;
78- if ( userId ) userIds . push ( userId ) ;
79- }
79+ for ( const mention of mentions ) {
80+ const userId = mention . replace ( '<@' , '' ) . replace ( '>' , '' ) ;
81+ if ( userId ) userIds . push ( userId ) ;
82+ }
8083
81- if ( userIds . length === 0 ) {
82- await interaction . reply ( 'No users mentioned. Please try again.' ) ;
83- return ;
84- }
84+ if ( userIds . length === 0 ) {
85+ await interaction . reply ( 'No users mentioned. Please try again.' ) ;
86+ return ;
87+ }
8588
86- const host = interaction . user . id ;
87- userIds . push ( host ) ;
89+ const host = interaction . user . id ;
90+ userIds . push ( host ) ;
8891
89- const timestamp = Math . floor ( Date . now ( ) / 1000 ) ;
90- const logEmbed = new EmbedBuilder ( )
91- . setColor ( '#9033FF' )
92- . setTitle ( 'Event log' )
93- . setDescription ( `A new event was logged.\n\nHost: <@${ host } >\n\nTime: <t:${ timestamp } :F>\n\nAttendees: ${ mentions . map ( ( id : string ) => `<@${ id } >` ) . join ( ', ' ) } \n\nSquadron Rally: False` )
94- . setTimestamp ( ) ;
92+ const timestamp = Math . floor ( Date . now ( ) / 1000 ) ;
93+ const logEmbed = new EmbedBuilder ( )
94+ . setColor ( '#9033FF' )
95+ . setTitle ( 'Event log' )
96+ . setDescription ( `A new event was logged.\n\nHost: <@${ host } >\n\nTime: <t:${ timestamp } :F>\n\nAttendees: ${ mentions . map ( ( id : string ) => `<@${ id } >` ) . join ( ', ' ) } \n\nSquadron Rally: False` )
97+ . setTimestamp ( ) ;
9598
96- if ( logChannel ) {
97- await logChannel . send ( { embeds : [ logEmbed ] } ) ;
98- }
99+ if ( logChannel ) {
100+ await logChannel . send ( { embeds : [ logEmbed ] } ) ;
101+ }
102+
103+ const db = knex ( { client : 'mysql' , connection : this . kogBot . environment . database } ) ;
104+
105+ for ( const userId of userIds ) {
106+ try {
107+ const results = await db ( 'KOGDB' ) . where ( { userId } ) ;
99108
100- const db = knex ( { client : 'mysql' , connection : this . kogBot . environment . database } ) ;
101-
102- for ( const userId of userIds ) {
103- try {
104- const results = await db ( 'KOGDB' ) . where ( { userId } ) ;
105-
106- if ( results . length > 0 ) {
107- if ( userId === host ) {
108- await db ( 'KOGDB' ) . where ( { userId } ) . update ( {
109- eventsAttended : db . raw ( 'eventsAttended + 1' ) ,
110- eventsHosted : db . raw ( 'eventsHosted + 1' )
111- } ) ;
112- } else {
113- await db ( 'KOGDB' ) . where ( { userId } ) . update ( {
114- eventsAttended : db . raw ( 'eventsAttended + 1' )
115- } ) ;
116- }
109+ if ( results . length > 0 ) {
110+ if ( userId === host ) {
111+ await db ( 'KOGDB' ) . where ( { userId } ) . update ( {
112+ eventsAttended : db . raw ( 'eventsAttended + 1' ) ,
113+ eventsHosted : db . raw ( 'eventsHosted + 1' )
114+ } ) ;
117115 } else {
118- if ( userId === host ) {
119- await db ( 'KOGDB' ) . insert ( { userId, eventsAttended : 1 , eventsHosted : 1 } ) ;
120- } else {
121- await db ( 'KOGDB' ) . insert ( { userId, eventsAttended : 1 , eventsHosted : 0 } ) ;
122- }
116+ await db ( 'KOGDB' ) . where ( { userId } ) . update ( {
117+ eventsAttended : db . raw ( 'eventsAttended + 1' )
118+ } ) ;
119+ }
120+ } else {
121+ if ( userId === host ) {
122+ await db ( 'KOGDB' ) . insert ( { userId, eventsAttended : 1 , eventsHosted : 1 } ) ;
123+ } else {
124+ await db ( 'KOGDB' ) . insert ( { userId, eventsAttended : 1 , eventsHosted : 0 } ) ;
123125 }
124- } catch ( err ) {
125- console . error ( err ) ;
126126 }
127+ } catch ( err ) {
128+ console . error ( err ) ;
127129 }
130+ }
128131
129- const dbEmbed = new EmbedBuilder ( )
130- . setColor ( '#9033FF' )
131- . setTitle ( 'Log Event' )
132- . setDescription ( 'Database updated, event has been logged successfully.' )
133- . setTimestamp ( ) ;
134-
135- if ( logChannel ) {
136- await logChannel . send ( { embeds : [ dbEmbed ] } ) ;
137- }
138-
139- } catch ( error ) {
140- console . log ( error ) ;
141- const errorEmbed = new EmbedBuilder ( )
142- . setColor ( '#E73A3A' )
143- . setTitle ( 'Error' )
144- . setDescription ( 'An error occurred while executing this command.' )
145- . setTimestamp ( ) ;
132+ const dbEmbed = new EmbedBuilder ( )
133+ . setColor ( '#9033FF' )
134+ . setTitle ( 'Log Event' )
135+ . setDescription ( 'Database updated, event has been logged successfully.' )
136+ . setTimestamp ( ) ;
146137
147- await interaction . reply ( { embeds : [ errorEmbed ] , ephemeral : true } ) ;
138+ if ( logChannel ) {
139+ await logChannel . send ( { embeds : [ dbEmbed ] } ) ;
148140 }
149141 }
150142}
0 commit comments