11import { EmbedBuilder , ChatInputCommandInteraction , GuildMemberRoleManager } from 'discord.js' ;
22import { KOGBot } from '../../index.js' ;
3- import knex , { Knex } from "knex" ;
3+ import knex from "knex" ;
44
55class LogEventCommand implements SlashCommand {
66 name = 'log' ;
@@ -18,133 +18,117 @@ class LogEventCommand implements SlashCommand {
1818 const allowedroleID = this . kogBot . environment . discord . mr_role ; // Set this later when I have perms
1919 const logChannel = this . kogBot . environment . discord . logChannel ; // log channel
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- }
3421
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.' )
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 ( '#E73A3A' )
26+ . setTitle ( 'Error' )
27+ . setDescription ( "You don't have the required role to use this command." )
3928 . setTimestamp ( ) ;
4029
41- await interaction . reply ( { embeds : [ logStart ] , ephemeral : true } ) ;
30+ await interaction . reply ( { embeds : [ noperms ] , ephemeral : true } ) ;
31+ return ;
32+ }
4233
43- if ( ! interaction . channel ) {
44- await interaction . reply ( 'Channel not found.' ) ;
45- return ;
46- }
34+ const logStart = new EmbedBuilder ( )
35+ . setColor ( '#9033FF' )
36+ . setTitle ( 'Logging' )
37+ . 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.' )
38+ . setTimestamp ( ) ;
4739
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 ;
40+ await interaction . reply ( { embeds : [ logStart ] , ephemeral : true } ) ;
5141
52- const mentionRegexthing = / ^ < @ \d + > (?: , \s ? < @ \d + > ) * $ / ;
42+ if ( ! interaction . channel ) {
43+ await interaction . reply ( 'Channel not found.' ) ;
44+ return ;
45+ }
5346
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- }
47+ const filter = ( response : any ) => response . user . id === interaction . user . id ;
48+ const collected = await interaction . channel ?. awaitMessageComponent ( { filter, time : 60000 } ) . catch ( ( ) => null ) ;
49+ const response = collected ?. isMessageComponent ( ) ? collected . message . content : null ;
5850
59- if ( response ?. toLowerCase ( ) === 'cancel' ) {
60- await interaction . reply ( `<@${ interaction . user . id } > canceled the event log.` ) ;
61- return ;
62- }
51+ const mentionRegexthing = / ^ < @ \d + > (?: , \s ? < @ \d + > ) * $ / ;
6352
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- }
53+ if ( ! mentionRegexthing . test ( response ! ) ) {
54+ 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.' ) ;
55+ return ;
56+ }
6857
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 [ ] = [ ] ;
58+ if ( response ?. toLowerCase ( ) === 'cancel' ) {
59+ await interaction . reply ( `<@${ interaction . user . id } > canceled the event log.` ) ;
60+ return ;
61+ }
7562
76- for ( const mention of mentions ) {
77- const userId = mention . replace ( '<@' , '' ) . replace ( '>' , '' ) ;
78- if ( userId ) userIds . push ( userId ) ;
79- }
63+ if ( response && typeof response === 'object' && 'message' in response && ( response as any ) . message === 'time' ) {
64+ await interaction . reply ( `<@ ${ interaction . user . id } > you took too long to follow up, please try again.` ) ;
65+ return ;
66+ }
8067
81- if ( userIds . length === 0 ) {
82- await interaction . reply ( 'No users mentioned. Please try again.' ) ;
83- return ;
84- }
68+ if ( ! response ) {
69+ await interaction . followUp ( 'No response received. Please try again.' ) ;
70+ return ;
71+ }
72+ const mentions = response . split ( ',' ) . map ( ( id : string ) => id . trim ( ) . replace ( '<@' , '' ) . replace ( '>' , '' ) ) ;
73+ const userIds : string [ ] = [ ] ;
8574
86- const host = interaction . user . id ;
87- userIds . push ( host ) ;
75+ for ( const mention of mentions ) {
76+ const userId = mention . replace ( '<@' , '' ) . replace ( '>' , '' ) ;
77+ if ( userId ) userIds . push ( userId ) ;
78+ }
8879
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 ( ) ;
80+ if ( userIds . length === 0 ) {
81+ await interaction . reply ( 'No users mentioned. Please try again.' ) ;
82+ return ;
83+ }
9584
96- if ( logChannel ) {
97- await logChannel . send ( { embeds : [ logEmbed ] } ) ;
98- }
85+ const host = interaction . user . id ;
86+ userIds . push ( host ) ;
9987
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- }
117- } 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- }
123- }
124- } catch ( err ) {
125- console . error ( err ) ;
126- }
127- }
88+ const timestamp = Math . floor ( Date . now ( ) / 1000 ) ;
89+ const logEmbed = new EmbedBuilder ( )
90+ . setColor ( '#9033FF' )
91+ . setTitle ( 'Event log' )
92+ . 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` )
93+ . setTimestamp ( ) ;
12894
129- const dbEmbed = new EmbedBuilder ( )
130- . setColor ( '#9033FF' )
131- . setTitle ( 'Log Event' )
132- . setDescription ( 'Database updated, event has been logged successfully.' )
133- . setTimestamp ( ) ;
95+ if ( logChannel ) {
96+ await logChannel . send ( { embeds : [ logEmbed ] } ) ;
97+ }
13498
135- if ( logChannel ) {
136- await logChannel . send ( { embeds : [ dbEmbed ] } ) ;
99+ const db = knex ( { client : 'mysql' , connection : this . kogBot . environment . database } ) ;
100+
101+ for ( const userId of userIds ) {
102+ const results = await db ( 'KOGDB' ) . where ( { userId } ) ;
103+
104+ if ( results . length > 0 ) {
105+ if ( userId === host ) {
106+ await db ( 'KOGDB' ) . where ( { userId } ) . update ( {
107+ eventsAttended : db . raw ( 'eventsAttended + 1' ) ,
108+ eventsHosted : db . raw ( 'eventsHosted + 1' )
109+ } ) ;
110+ } else {
111+ await db ( 'KOGDB' ) . where ( { userId } ) . update ( {
112+ eventsAttended : db . raw ( 'eventsAttended + 1' )
113+ } ) ;
114+ }
115+ } else {
116+ if ( userId === host ) {
117+ await db ( 'KOGDB' ) . insert ( { userId, eventsAttended : 1 , eventsHosted : 1 } ) ;
118+ } else {
119+ await db ( 'KOGDB' ) . insert ( { userId, eventsAttended : 1 , eventsHosted : 0 } ) ;
120+ }
137121 }
122+ }
138123
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 ( ) ;
124+ const dbEmbed = new EmbedBuilder ( )
125+ . setColor ( '#9033FF' )
126+ . setTitle ( 'Log Event' )
127+ . setDescription ( 'Database updated, event has been logged successfully.' )
128+ . setTimestamp ( ) ;
146129
147- await interaction . reply ( { embeds : [ errorEmbed ] , ephemeral : true } ) ;
130+ if ( logChannel ) {
131+ await logChannel . send ( { embeds : [ dbEmbed ] } ) ;
148132 }
149133 }
150134}
0 commit comments