2026-03-09 09:00:15 -04:00
const { Client , Events , Message } = require ( '@fluxerjs/core' ) ;
const { messageHelper } = require ( "./helpers/messageHelper.js" ) ;
const { enums } = require ( "./enums.js" ) ;
const { commands } = require ( "./commands.js" ) ;
const { webhookHelper } = require ( "./helpers/webhookHelper.js" ) ;
const env = require ( 'dotenv' ) ;
const { utils } = require ( "./helpers/utils.js" ) ;
const { AppDataSource } = require ( "../database/data-source" ) ;
2026-02-16 11:07:43 -05:00
2026-03-09 09:00:15 -04:00
env . config ( ) ;
2026-02-13 18:20:29 -05:00
2026-02-25 19:30:39 -05:00
const token = process . env . FLUXER _BOT _TOKEN ;
2026-03-13 23:56:35 +11:00
const debug = process . env . debug ;
2026-02-13 18:20:29 -05:00
if ( ! token ) {
console . error ( "Missing FLUXER_BOT_TOKEN environment variable." ) ;
process . exit ( 1 ) ;
}
2026-03-09 09:00:15 -04:00
client = new Client ( { intents : 0 } ) ;
module . exports . client = client ;
2026-02-13 18:20:29 -05:00
2026-02-14 11:37:04 -05:00
client . on ( Events . MessageCreate , async ( message ) => {
2026-03-09 09:00:15 -04:00
await module . exports . handleMessageCreate ( message ) ;
2026-02-24 12:42:23 -05:00
} ) ;
2026-02-13 18:20:29 -05:00
2026-02-24 12:42:23 -05:00
/ * *
* Calls functions based off the contents of a message object .
*
* @ async
* @ param { Message } message - The message object
*
* * /
2026-03-09 09:00:15 -04:00
module . exports . handleMessageCreate = async function ( message ) {
2026-02-24 12:42:23 -05:00
try {
2026-02-25 19:30:39 -05:00
// Ignore bots
if ( message . author . bot ) return ;
2026-02-14 11:37:04 -05:00
// Parse command and arguments
const content = message . content . trim ( ) ;
2026-02-14 14:30:12 -05:00
// If message doesn't start with the bot prefix, it could still be a message with a proxy tag. If it's not, return.
2026-02-14 11:37:04 -05:00
if ( ! content . startsWith ( messageHelper . prefix ) ) {
2026-02-25 19:30:39 -05:00
return await webhookHelper . sendMessageAsMember ( client , message ) ;
2026-02-14 11:37:04 -05:00
}
2026-02-13 18:20:29 -05:00
2026-02-14 11:37:04 -05:00
const commandName = content . slice ( messageHelper . prefix . length ) . split ( " " ) [ 0 ] ;
2026-02-24 12:42:23 -05:00
2026-02-14 11:37:04 -05:00
// If there's no command name (ie just the prefix)
2026-02-19 21:45:10 -05:00
if ( ! commandName ) return await message . reply ( enums . help . SHORT _DESC _PLURALFLUX ) ;
2026-02-14 10:07:27 -05:00
2026-02-14 11:37:04 -05:00
const args = messageHelper . parseCommandArgs ( content , commandName ) ;
2026-02-24 12:42:23 -05:00
let command = commands . commandsMap . get ( commandName )
if ( ! command ) {
const commandFromAlias = commands . aliasesMap . get ( commandName ) ;
command = commandFromAlias ? commands . commandsMap . get ( commandFromAlias . command ) : null ;
}
2026-02-14 11:44:36 -05:00
if ( command ) {
2026-02-25 19:30:39 -05:00
await command . execute ( message , args ) ;
2026-02-13 18:20:29 -05:00
}
2026-02-19 21:45:10 -05:00
else {
await message . reply ( enums . err . COMMAND _NOT _RECOGNIZED ) ;
}
2026-02-13 18:20:29 -05:00
}
2026-02-14 10:07:27 -05:00
catch ( error ) {
2026-03-15 13:39:56 +11:00
if ( debug ) { console . error ( "An error occurred at unix timestamp " + Date . now ( ) + "while processing the command: " + message + " with error:" + error ) ; }
2026-03-13 23:39:54 +11:00
else { console . error ( error ) ; }
process . exit ( 2 ) ; //need this for now just to make sure the bot continues to restart on errors, since it would seem that fluxer.js doesn't define custom error types. TODO: map out some exit codes
2026-02-13 18:20:29 -05:00
}
2026-02-24 12:42:23 -05:00
}
2026-02-13 18:20:29 -05:00
2026-02-14 11:37:04 -05:00
client . on ( Events . Ready , ( ) => {
console . log ( ` Logged in as ${ client . user ? . username } ` ) ;
2026-03-13 23:56:35 +11:00
if ( debug ) { console . log ( ` Currently running in debug mode! ` ) }
2026-02-13 18:20:29 -05:00
} ) ;
2026-02-19 21:45:10 -05:00
let guildCount = 0 ;
client . on ( Events . GuildCreate , ( ) => {
guildCount ++ ;
2026-02-24 12:42:23 -05:00
debouncePrintGuilds ( ) ;
2026-02-19 21:45:10 -05:00
} ) ;
function printGuilds ( ) {
console . log ( ` Serving ${ client . guilds . size } guild(s) ` ) ;
}
2026-02-24 12:42:23 -05:00
const debouncePrintGuilds = utils . debounce ( printGuilds , 2000 ) ;
2026-03-09 09:00:15 -04:00
// export const debounceLogin = utils.debounce(client.login, 60000);
2026-02-19 21:45:10 -05:00
2026-03-09 09:00:15 -04:00
module . exports . login = async function ( ) {
2026-02-24 12:42:23 -05:00
try {
2026-03-09 09:00:15 -04:00
if ( ! AppDataSource . isInitialized ) {
await AppDataSource . initialize ( ) ;
}
2026-02-24 12:42:23 -05:00
await client . login ( token ) ;
} catch ( err ) {
console . error ( 'Login failed:' , err ) ;
process . exit ( 1 ) ;
}
2026-02-25 19:30:39 -05:00
}
function main ( )
{
2026-03-09 09:00:15 -04:00
exports . login ( ) ;
2026-02-25 19:30:39 -05:00
}
main ( ) ;