2022-01-08 14:38:24 -07:00
require ( 'dotenv' ) . config ( ) ; //initialize dotenv
const {
Client ,
Intents
} = require ( 'discord.js' ) ;
2022-11-04 22:04:53 -06:00
const Tesseract = require ( 'tesseract.js' ) ;
2023-09-04 01:59:56 -06:00
const fs = require ( 'fs' ) ;
2023-09-04 16:06:40 -06:00
// Change these in .env to match the user the bot pings "owner" when it deletes a message and which channel it logs messages to "modchat"
const owner = process . env . OWNER ;
const modchat = process . env . MODCHAT ;
const botStatus = process . env . BOT _STATUS ; // This shows as the bot's status in Discord
const adminRole = process . env . ADMIN _ROLE ; // So admins can use the !bot command
2023-09-04 18:24:02 -06:00
const welcomeChannelId = process . env . WELCOME _CHANNEL ; // Channel where welcome messages are sent
2023-09-04 12:47:37 -06:00
// The list file locations are initalized here
2023-09-04 02:03:05 -06:00
const badWordsList = './lists/badwords.txt' ;
const potentialBadWordsList = './lists/potentialbadwords.txt' ;
const whitelistList = './lists/whitelist.txt' ;
2023-09-08 21:16:27 -06:00
const dmList = './lists/dm.txt' ;
2023-09-04 01:59:56 -06:00
2023-09-04 12:47:37 -06:00
// The arrays are filled at runtime in bot.on('ready')
2023-09-04 01:59:56 -06:00
let badWords = [ ] ;
let potentialBadWords = [ ] ;
let whitelist = [ ] ;
2023-09-08 21:16:27 -06:00
let dm = [ ] ;
2022-11-04 22:04:53 -06:00
2022-01-08 14:38:24 -07:00
const bot = new Client ( {
intents : [ Intents . FLAGS . GUILDS , Intents . FLAGS . GUILD _MEMBERS , Intents . FLAGS . GUILD _MESSAGES ]
} ) ;
2022-11-04 22:04:53 -06:00
let usermessage ;
2022-01-08 14:38:24 -07:00
bot . on ( 'ready' , ( ) => {
console . log ( ` Logged in as ${ bot . user . tag } ! ` ) ;
2023-09-04 12:47:37 -06:00
bot . user . setActivity ( botStatus , { type : 'PLAYING' } ) ;
console . log ( ` Bot status set to " ` + botStatus + ` " ` ) ;
2023-09-04 01:59:56 -06:00
fillArrayFromFile ( badWordsList , badWords ) ;
fillArrayFromFile ( potentialBadWordsList , potentialBadWords ) ;
fillArrayFromFile ( whitelistList , whitelist ) ;
2023-09-08 21:16:27 -06:00
fillArrayFromFile ( dmList , dm ) ;
2023-09-04 01:59:56 -06:00
setTimeout ( ( ) => { console . log ( 'Bad Words From File:' , badWords ) ; } , 100 ) ;
setTimeout ( ( ) => { console . log ( 'Potential Bad Words From File:' , potentialBadWords ) ; } , 100 ) ;
setTimeout ( ( ) => { console . log ( 'Whitelist From File:' , whitelist ) ; } , 100 ) ;
2023-09-08 21:16:27 -06:00
setTimeout ( ( ) => { console . log ( 'DM list From File:' , dm ) ; } , 100 ) ;
2022-01-08 14:38:24 -07:00
} ) ;
bot . on ( 'guildMemberAdd' , ( member ) => {
console . log ( member )
2023-09-04 19:07:01 -06:00
member . guild . channels . fetch ( welcomeChannelId ) . then ( channel => {
2023-09-04 19:53:34 -06:00
const welcomeMessage = process . env . WELCOME _MESSAGE . replace ( '<@${member.id}>' , ` <@ ${ member . id } > ` ) ;
channel . send ( welcomeMessage ) ;
2022-01-08 14:38:24 -07:00
} ) ;
} ) ;
2022-01-20 13:05:23 -07:00
2023-09-04 01:59:56 -06:00
function fillArrayFromFile ( filePath , dataArray ) {
fs . readFile ( filePath , 'utf-8' , ( err , data ) => {
if ( err ) {
console . error ( 'Error reading file:' , err ) ;
return ;
}
// Split the file contents into an array (removing empty strings)
const lines = data . split ( '\n' ) . filter ( ( line ) => line . trim ( ) !== '' ) ;
// Split the file contents into an array (assuming each line is an item)
dataArray . length = 0 ;
dataArray . push ( ... data . split ( '\n' ) ) ;
// If there are empty values, remove them
// Implementation taken from https://stackoverflow.com/a/2843625
let i ;
len = dataArray . length , i ;
for ( i = 0 ; i < len ; i ++ )
dataArray [ i ] && dataArray . push ( dataArray [ i ] ) ;
dataArray . splice ( 0 , len ) ;
} ) ;
}
2023-09-04 15:38:04 -06:00
function deleteWordFromFile ( filePath , dataArray , word ) {
fs . readFile ( filePath , 'utf-8' , ( err , data ) => {
if ( err ) {
console . error ( 'Error reading file:' , err ) ;
return ;
}
// Modify the content to remove the word
const lines = data . split ( '\n' ) ;
const modifiedLines = lines . filter ( ( line ) => ! line . includes ( word ) ) ;
const modifiedContent = modifiedLines . join ( '\n' ) ;
// Write the new content to file
fs . writeFile ( filePath , modifiedContent , 'utf8' , ( err ) => {
if ( err ) {
console . error ( 'Error writing to the file:' , err ) ;
} else {
console . log ( word + ' removed successfully from list ' + filePath ) ;
}
} ) ;
} ) ;
// Fill array again so there's no need to restart the bot
setTimeout ( ( ) => { fillArrayFromFile ( filePath , dataArray ) ; } , 1000 ) ;
}
function addWordToFile ( filePath , dataArray , word ) {
fs . readFile ( filePath , 'utf-8' , ( err , data ) => {
if ( err ) {
console . error ( 'Error reading file:' , err ) ;
return ;
}
2023-09-08 21:16:27 -06:00
// Split the file content into an array of words
const words = data . trimRight ( ) . split ( '\n' ) ;
// Check if the word already exists in the list
if ( words . includes ( word ) ) {
console . log ( word + ' already exists in list ' + filePath ) ;
return ;
}
2023-09-04 15:38:04 -06:00
2023-09-15 17:21:29 -06:00
let updatedContent ;
2023-09-08 21:16:27 -06:00
if ( ! data . trim ( ) ) {
console . log ( filePath + ' is blank, not adding new line.' ) ;
updatedContent = word ;
}
else {
// Append new line and word
2023-09-15 17:21:29 -06:00
updatedContent = ` ${ data . trimRight ( ) } \n ${ word } ` ;
2023-09-08 21:16:27 -06:00
}
2023-09-04 15:38:04 -06:00
// Write the new content to file
fs . writeFile ( filePath , updatedContent , 'utf8' , ( err ) => {
if ( err ) {
console . error ( 'Error writing to the file:' , err ) ;
} else {
console . log ( word + ' added to list ' + filePath ) ;
}
} ) ;
} ) ;
// Fill array again so there's no need to restart the bot
setTimeout ( ( ) => { fillArrayFromFile ( filePath , dataArray ) ; } , 1000 ) ;
}
2023-09-08 21:16:27 -06:00
async function sendDMsToUsers ( message ) {
for ( const userId of dm ) {
try {
// Find the user by their ID
const user = await bot . users . fetch ( userId ) ;
if ( user ) {
// Send a DM to the user
await user . send ( message ) ;
console . log ( ` Sent a DM to ${ user . tag } ` ) ;
} else {
console . log ( ` User with ID ${ userId } not found ` ) ;
}
} catch ( error ) {
console . error ( ` Error sending DM to user with ID ${ userId } : ${ error . message } ` ) ;
}
}
}
2023-09-08 19:31:10 -06:00
function checkMessage ( message ) {
2023-09-04 15:38:04 -06:00
if ( message . author . bot ) {
return ;
}
else if ( message . content . includes ( "!bot" ) ) {
if ( message . member . roles . cache . has ( adminRole ) ) {
if ( message . channelId == modchat ) {
if ( message . content . includes ( "delete" ) ) {
if ( message . content . includes ( "pbadwords" ) ) {
const wordToDelete = message . content . replace ( /!bot delete pbadwords / , '' ) ;
deleteWordFromFile ( potentialBadWordsList , potentialBadWords , wordToDelete ) ;
message . channel . send ( "Deleted " + wordToDelete + " from potentialbadwords list!" ) ;
}
else if ( message . content . includes ( "badwords" ) ) {
const wordToDelete = message . content . replace ( /!bot delete badwords / , '' ) ;
deleteWordFromFile ( badWordsList , badWords , wordToDelete ) ;
message . channel . send ( "Deleted " + wordToDelete + " from badwords list!" ) ;
}
else if ( message . content . includes ( "whitelist" ) ) {
const wordToDelete = message . content . replace ( /!bot delete whitelist / , '' ) ;
deleteWordFromFile ( whitelistList , whitelist , wordToDelete ) ;
message . channel . send ( "Deleted " + wordToDelete + " from whitelist!" ) ;
}
else {
message . channel . send ( "Usage: \n```\n!bot delete badwords/pbadwords/whitelist word\n```" )
}
}
else if ( message . content . includes ( "add" ) ) {
if ( message . content . includes ( "pbadwords" ) ) {
const wordToAdd = message . content . replace ( /!bot add pbadwords / , '' ) ;
addWordToFile ( potentialBadWordsList , potentialBadWords , wordToAdd ) ;
message . channel . send ( "Added " + wordToAdd + " to potentialbadwords list!" ) ;
}
else if ( message . content . includes ( "badwords" ) ) {
const wordToAdd = message . content . replace ( /!bot add badwords / , '' ) ;
addWordToFile ( badWordsList , badWords , wordToAdd ) ;
message . channel . send ( "Added " + wordToAdd + " to badwords list!" ) ;
}
else if ( message . content . includes ( "whitelist" ) ) {
const wordToAdd = message . content . replace ( /!bot add whitelist / , '' ) ;
addWordToFile ( whitelistList , whitelist , wordToAdd ) ;
2023-09-05 20:37:32 -06:00
message . channel . send ( "Added " + wordToAdd + " to whitelist!" ) ;
2023-09-04 15:38:04 -06:00
}
else {
message . channel . send ( "Usage: \n```\n!bot add badwords/pbadwords/whitelist word\n```" )
}
}
else if ( message . content . includes ( "list" ) ) {
const pBadWordsString = potentialBadWords . join ( ', ' ) ;
const badWordsString = badWords . join ( ', ' ) ;
const whitelistString = whitelist . join ( ', ' ) ;
if ( message . content . includes ( "pbadwords" ) ) {
message . channel . send ( ` Potential bad words: ${ pBadWordsString } ` ) ;
}
else if ( message . content . includes ( "badwords" ) ) {
message . channel . send ( ` Bad words: ${ badWordsString } ` ) ;
}
else if ( message . content . includes ( "whitelist" ) ) {
message . channel . send ( ` Whitelist: ${ whitelistString } ` ) ;
}
else {
message . channel . send ( "Usage: \n```\n!bot list badwords/pbadwords/whitelist\n```" )
}
}
2023-09-08 21:16:27 -06:00
else if ( message . content . includes ( "dm" ) ) {
if ( message . content . includes ( "on" ) ) {
addWordToFile ( dmList , dm , message . author . id ) ;
message . channel . send ( "Added you to the DM list!" ) ;
}
else if ( message . content . includes ( "off" ) ) {
deleteWordFromFile ( dmList , dm , message . author . id ) ;
message . channel . send ( "Removed you from the DM list!" ) ;
}
else {
message . channel . send ( "Usage: \n```\n!bot dm on/off\n```" )
}
}
2023-09-04 15:38:04 -06:00
else {
2023-09-08 21:16:27 -06:00
message . channel . send ( "Usage: \n```\n!bot list badwords/pbadwords/whitelist\n!bot delete badwords/pbadwords/whitelist word\n!bot add badwords/pbadwords/whitelist word\n!bot dm on/off\n```" ) ;
2023-09-04 15:38:04 -06:00
}
}
else {
message . channel . send ( 'Commands can only be used in the <#' + modchat + '> channel!' ) ;
}
}
else {
message . channel . send ( 'Sorry, only an admin can use the !bot command!' ) ;
}
}
else if ( message . attachments . size > 0 ) {
2022-11-04 22:04:53 -06:00
let image = message . attachments . first ( ) . url ;
2022-11-04 22:50:44 -06:00
Tesseract . recognize (
image ,
'eng' ,
{ logger : m => console . log ( m ) }
) . then ( ( { data : { text } } ) => {
2023-09-04 15:38:04 -06:00
imagemessage = text . toLowerCase ( ) ;
console . log ( imagemessage ) ;
if ( badWords . some ( word => imagemessage . includes ( word ) ) ) {
2023-09-08 21:16:27 -06:00
const sender = ` ${ message . author } sent the following message in <# ${ message . channelId } >... `
2023-09-04 15:38:04 -06:00
message . delete ( ) ;
message . channel . send ( 'Hey! Please keep your language school appropriate... If I deleted your message by mistake, please contact' + owner + 'or wait for him to see it then he will fix it.' ) ;
2023-09-08 21:16:27 -06:00
bot . channels . cache . get ( modchat ) . send ( sender + '\n' + message . attachments . first ( ) . url ) ;
sendDMsToUsers ( sender + '\n' + message . attachments . first ( ) . url ) ;
2023-09-04 15:38:04 -06:00
}
2022-11-04 22:50:44 -06:00
} )
2022-11-04 22:04:53 -06:00
}
2023-09-04 15:38:04 -06:00
else if ( badWords . some ( word => message . content . toLowerCase ( ) . includes ( word ) ) ) {
2023-09-08 21:16:27 -06:00
const sender = ` ${ message . author } sent the following message in <# ${ message . channelId } >... `
2023-09-04 12:47:37 -06:00
message . channel . send ( 'Hey! Please keep your language school appropriate... If I deleted your message by mistake, please contact' + owner + 'or wait for him to see it then he will fix it.' ) ;
2022-01-20 13:05:23 -07:00
message . delete ( ) ;
2023-09-08 21:16:27 -06:00
bot . channels . cache . get ( modchat ) . send ( sender + '\n' + message . content ) ;
sendDMsToUsers ( sender + '\n' + message . content ) ;
2022-02-20 15:30:46 -07:00
}
2023-09-04 15:38:04 -06:00
else if ( message . content . includes ( 'bot say sorry' ) ) {
2022-08-23 23:09:48 -06:00
message . channel . send ( 'I am sorry for whatever I did. It will not happen again.' ) ;
2022-08-23 23:12:41 -06:00
}
2023-09-04 15:38:04 -06:00
else if ( message . content . includes ( 'green mario' ) ) {
2022-08-29 20:24:37 -06:00
message . channel . send ( 'Red Luigi' ) ;
}
2023-09-04 15:38:04 -06:00
else if ( message . content . includes ( 'how neat is that' ) ) {
2022-09-02 19:55:30 -06:00
message . channel . send ( "that's pretty neat!" ) ;
}
2023-09-04 15:38:04 -06:00
else if ( message . content . includes ( "that's pretty neat" ) ) {
2022-09-02 19:55:30 -06:00
message . channel . send ( 'how neat is that?' ) ;
2022-09-02 16:15:51 -06:00
}
2023-09-04 15:38:04 -06:00
else if ( potentialBadWords . some ( word => message . content . toLowerCase ( ) . includes ( word ) ) ) {
if ( whitelist . some ( word => message . content . toLowerCase ( ) . includes ( word ) ) ) { //These are not bad words and will be bypassed
2022-08-23 23:09:48 -06:00
return ;
2022-08-23 23:06:21 -06:00
}
2023-09-08 21:16:27 -06:00
const sender = owner + ` ${ message . author } sent the following message ***BUT WAS NOT DELETED...*** `
2023-09-08 19:18:05 -06:00
const link = "https://discord.com/channels/" + message . guildId + "/" + message . channelId + "/" + message . id ;
2023-09-08 21:16:27 -06:00
bot . channels . cache . get ( modchat ) . send ( sender + '\n' + message . content + '\n' + link ) ;
sendDMsToUsers ( sender + '\n' + message . content + '\n' + link ) ;
2023-09-04 15:38:04 -06:00
}
2023-09-08 19:31:10 -06:00
}
bot . on ( 'messageCreate' , ( message ) => {
checkMessage ( message ) ;
} ) ;
bot . on ( 'messageUpdate' , ( oldMessage , newMessage ) => {
checkMessage ( newMessage ) ;
2022-01-20 13:05:23 -07:00
} ) ;
2022-01-08 14:38:24 -07:00
//make sure this line is the last line
2023-09-04 00:15:07 -06:00
bot . login ( process . env . CLIENT _TOKEN ) ; //login bot using token