2024-06-15 16:52:42 +00:00
const { Events , EmbedBuilder } = require ( 'discord.js' ) ;
2024-06-16 08:28:17 +00:00
const fs = require ( 'fs' ) ;
2024-06-15 16:52:42 +00:00
function randomColor ( ) {
const r = Math . floor ( Math . random ( ) * 200 ) + 50 ;
const g = Math . floor ( Math . random ( ) * 200 ) + 50 ;
const b = Math . floor ( Math . random ( ) * 200 ) + 50 ;
return ` # ${ r . toString ( 16 ) } ${ g . toString ( 16 ) } ${ b . toString ( 16 ) } ` ;
}
2024-06-16 08:28:17 +00:00
2024-06-15 16:52:42 +00:00
module . exports = {
2024-06-16 08:28:17 +00:00
name : Events . GuildMemberAdd ,
2024-06-15 16:52:42 +00:00
once : false ,
2024-06-16 08:28:17 +00:00
execute ( member ) {
const { guild , user } = member ;
const filePath = ` ./guilds/guilds- ${ guild . id } .json ` ;
if ( fs . existsSync ( filePath ) ) {
const data = JSON . parse ( fs . readFileSync ( filePath ) ) ;
if ( ! data . hello || ! data . helloChannel ) return ;
let title = data . welcomeMessage && data . welcomeMessage . title ? data . welcomeMessage . title : "👋 Welcome!" ;
let description = data . welcomeMessage && data . welcomeMessage . content ? data . welcomeMessage . content . replace ( "{{USER}}" , user . username ) : ` Welcome to the server, ${ user . username } ! ` ;
if ( data . rulesChannel == null || data . rulesMessage == null )
{
}
else
{
//data.rulesMessage + #data.rulesChannel
description += "\n" + data . rulesMessage ;
//replace {{RULES}} with the rules channel
description = description . replace ( "{{RULES}}" , ` <# ${ data . rulesChannel } > ` ) ;
}
const embed = new EmbedBuilder ( )
. setTitle ( title )
. setDescription ( description )
. setColor ( randomColor ( ) )
. setThumbnail ( user . avatarURL ( ) )
. setTimestamp ( ) ;
2024-06-15 16:52:42 +00:00
2024-06-16 08:28:17 +00:00
guild . channels . fetch ( data . helloChannel )
. then ( channel => {
if ( channel ) {
channel . send ( { embeds : [ embed ] } )
. catch ( console . error ) ;
} else {
console . error ( "Welcome channel not found." ) ;
}
} )
2024-06-16 09:16:56 +00:00
. catch (
//if the channel is not found, set the welcome channel to null and hello to false
data . helloChannel = null ,
data . hello = false ,
fs . writeFileSync ( filePath , JSON . stringify ( data ) )
) ;
2024-06-16 08:28:17 +00:00
} else {
return ;
}
2024-06-15 16:52:42 +00:00
}
2024-06-16 08:28:17 +00:00
} ;