2025-04-10 15:50:25 -04:00
const { sendCommandResponse } = require ( '../utils/messageUtils' ) ;
2024-09-08 10:25:14 -04:00
module . exports = {
name : 'react' ,
2025-04-11 07:22:24 -04:00
description : ` Automatically react with specified emojis to multiple users' messages, or stop reacting. Usage: .react [user1,user2,...] [emoji1] [emoji2] ... ` ,
2024-09-08 10:25:14 -04:00
async execute ( message , args , deleteTimeout ) {
2025-04-10 14:52:22 -04:00
const { processUserInput } = require ( '../utils/userUtils' ) ;
2024-09-08 10:25:14 -04:00
if ( args . length === 0 ) {
2024-09-08 12:56:52 -04:00
if ( message . client . targetReactUserIds && message . client . reactEmojis ) {
2025-04-10 15:50:25 -04:00
await sendCommandResponse (
message ,
2024-09-08 12:56:52 -04:00
` Currently reacting to messages from the following users: ${ message . client . targetReactUserIds
2025-04-10 15:07:02 -04:00
. map ( id => ` User ID: ${ id } ` )
2025-04-10 15:50:25 -04:00
. join ( ', ' ) } with the following emojis : $ { message . client . reactEmojis . join ( ' ' ) } . ` ,
deleteTimeout ,
false
2024-09-08 10:25:14 -04:00
) ;
} else {
2025-04-10 15:50:25 -04:00
await sendCommandResponse ( message , 'No active reaction target.' , deleteTimeout , false ) ;
2024-09-08 10:25:14 -04:00
}
return ;
}
if ( args [ 0 ] . toLowerCase ( ) === 'stop' ) {
if ( message . client . reactListener ) {
message . client . off ( 'messageCreate' , message . client . reactListener ) ;
message . client . reactListener = null ;
2024-09-08 12:56:52 -04:00
message . client . targetReactUserIds = null ;
2024-09-08 10:25:14 -04:00
message . client . reactEmojis = null ;
2025-04-10 15:50:25 -04:00
await sendCommandResponse ( message , 'Stopped reacting to messages.' , deleteTimeout , false ) ;
2024-09-08 10:25:14 -04:00
} else {
2025-04-10 15:50:25 -04:00
await sendCommandResponse ( message , 'No active reactions to stop.' , deleteTimeout , false ) ;
2024-09-08 10:25:14 -04:00
}
return ;
}
2025-04-11 07:22:24 -04:00
// Find where the emojis start
let emojiStartIndex = - 1 ;
for ( let i = 0 ; i < args . length ; i ++ ) {
// Check if this argument looks like an emoji (contains : or is a single character)
if ( args [ i ] . includes ( ':' ) || args [ i ] . length <= 2 ) {
emojiStartIndex = i ;
break ;
}
}
2024-09-08 10:25:14 -04:00
2025-04-11 07:22:24 -04:00
if ( emojiStartIndex === - 1 ) {
await sendCommandResponse ( message , 'Please provide at least one emoji to react with.' , deleteTimeout , false ) ;
return ;
}
// All arguments before emojiStartIndex are user IDs
const userInput = args . slice ( 0 , emojiStartIndex ) . join ( ' ' ) ;
const emojis = args . slice ( emojiStartIndex ) ;
console . log ( ` [REACT] Processing user input: " ${ userInput } " ` ) ;
const targetIds = processUserInput ( userInput ) ;
console . log ( ` [REACT] Extracted user IDs: ${ targetIds . join ( ', ' ) } ` ) ;
if ( targetIds . length === 0 ) {
await sendCommandResponse ( message , 'Please provide valid user IDs or @mentions. You can use multiple users separated by spaces or commas.' , deleteTimeout , false ) ;
2024-09-08 10:25:14 -04:00
return ;
}
2025-04-11 07:22:24 -04:00
// Process emojis to handle custom emojis
const processedEmojis = emojis . map ( emoji => {
// Check if it's a custom emoji (format: :name:)
const customEmojiMatch = emoji . match ( /^:([a-zA-Z0-9_]+):$/ ) ;
if ( customEmojiMatch ) {
// For custom emojis, we need to find the emoji ID from the guild
const emojiName = customEmojiMatch [ 1 ] ;
const customEmoji = message . guild ? . emojis . cache . find ( e => e . name === emojiName ) ;
if ( customEmoji ) {
return customEmoji . id ;
}
}
// For standard emojis, just return as is
return emoji ;
} ) ;
2024-09-08 12:56:52 -04:00
message . client . targetReactUserIds = targetIds ;
2025-04-11 07:22:24 -04:00
message . client . reactEmojis = processedEmojis ;
2024-09-08 10:25:14 -04:00
2025-04-11 07:22:24 -04:00
// Create a more detailed confirmation message with a different format
let userListText = '' ;
if ( targetIds . length === 1 ) {
userListText = ` User ID: ${ targetIds [ 0 ] } ` ;
} else {
userListText = targetIds . map ( ( id , index ) => ` User ID ${ index + 1 } : ${ id } ` ) . join ( '\n' ) ;
}
const confirmationMessage = ` I will now react to messages from: \n ${ userListText } \n \n With the following emojis: ${ emojis . join ( ' ' ) } ` ;
console . log ( ` [REACT] Confirmation message: ${ confirmationMessage } ` ) ;
2025-04-10 15:50:25 -04:00
await sendCommandResponse (
message ,
2025-04-11 07:22:24 -04:00
confirmationMessage ,
2025-04-10 15:50:25 -04:00
deleteTimeout ,
false
2024-09-08 10:25:14 -04:00
) ;
if ( message . client . reactListener ) {
message . client . off ( 'messageCreate' , message . client . reactListener ) ;
}
2025-04-10 15:50:25 -04:00
const getHumanizedDelay = ( ) => {
const baseDelay = Math . floor ( Math . random ( ) * ( 3000 - 1000 + 1 ) ) + 1000 ;
const jitter = Math . floor ( Math . random ( ) * 1000 ) - 500 ;
return Math . max ( 800 , baseDelay + jitter ) ;
} ;
2024-09-08 10:25:14 -04:00
message . client . reactListener = async ( msg ) => {
2024-09-18 08:23:57 -04:00
if ( message . client . targetReactUserIds && message . client . targetReactUserIds . includes ( msg . author . id ) ) {
2025-04-10 15:50:25 -04:00
try {
const shouldReact = Math . random ( ) < 0.95 ;
if ( ! shouldReact ) {
console . log ( ` [REACT] Randomly skipping reaction to message ${ msg . id } ` ) ;
return ;
}
const initialDelay = getHumanizedDelay ( ) ;
await new Promise ( resolve => setTimeout ( resolve , initialDelay ) ) ;
2025-04-11 07:22:24 -04:00
for ( const emoji of processedEmojis ) {
2025-04-10 15:50:25 -04:00
if ( Math . random ( ) < 0.05 ) {
console . log ( ` [REACT] Skipping emoji ${ emoji } for more human-like behavior ` ) ;
continue ;
}
try {
const reactDelay = getHumanizedDelay ( ) ;
if ( Math . random ( ) < 0.08 ) {
const extraDelay = Math . floor ( Math . random ( ) * 4000 ) + 1000 ;
console . log ( ` [REACT] Adding ${ extraDelay } ms extra delay before reacting with ${ emoji } ` ) ;
await new Promise ( resolve => setTimeout ( resolve , extraDelay ) ) ;
}
await new Promise ( resolve => setTimeout ( resolve , reactDelay ) ) ;
await msg . react ( emoji ) ;
} catch ( error ) {
console . error ( ` [REACT] Failed to react with ${ emoji } : ` , error ) ;
}
2024-09-08 10:25:14 -04:00
}
2025-04-10 15:50:25 -04:00
} catch ( error ) {
console . error ( '[REACT] Error in reaction handler:' , error ) ;
2024-09-08 10:25:14 -04:00
}
}
} ;
message . client . on ( 'messageCreate' , message . client . reactListener ) ;
} ,
2024-09-08 12:56:52 -04:00
} ;