2023-03-31 00:34:58 -05:00
// Require the necessary discord.js classes
2023-04-03 19:43:50 -05:00
const { Client , Events , GatewayIntentBits , REST , Routes , Collection , FLAGS , MessageMentionOptions } = require ( 'discord.js' ) ;
2023-03-31 00:34:58 -05:00
const Discord = require ( 'discord.js' ) ;
const { clientId , guildId , token } = require ( './key.json' ) ;
const fs = require ( 'node:fs' ) ;
const path = require ( 'node:path' ) ;
// Create a new client instance
const client = new Discord . Client ( {
intents : [
GatewayIntentBits . Guilds ,
GatewayIntentBits . GuildMessages ,
GatewayIntentBits . MessageContent ,
GatewayIntentBits . GuildMembers ,
] ,
} ) ;
// When the client is ready, run this code (only once)
// We use 'c' for the event parameter to keep it separate from the already defined 'client'
client . once ( Events . ClientReady , c = > {
console . log ( ` Ready! Logged in as ${ c . user . tag } ` ) ;
} ) ;
// Log in to Discord with your client's token
client . login ( token ) ;
// Retrieve commands
client . commands = new Collection ( ) ;
const commandsPath = path . join ( __dirname , 'commands' ) ;
const commandFiles = fs . readdirSync ( commandsPath ) . filter ( file = > file . endsWith ( '.ts' ) ) ;
for ( const file of commandFiles ) {
const filePath = path . join ( commandsPath , file ) ;
const command = require ( filePath ) ;
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ( 'data' in command && 'execute' in command ) {
client . commands . set ( command . data . name , command ) ;
} else {
console . log ( ` [WARNING] The command at ${ filePath } is missing a required "data" or "execute" property. ` ) ;
}
}
client . on ( Events . InteractionCreate , async interaction = > {
if ( ! interaction . isChatInputCommand ( ) ) return ;
const command = interaction . client . commands . get ( interaction . commandName ) ;
if ( ! command ) {
console . error ( ` No command matching ${ interaction . commandName } was found. ` ) ;
return ;
}
try {
2023-04-04 17:19:28 -05:00
await command . execute ( interaction , client ) ;
2023-03-31 00:34:58 -05:00
} catch ( error ) {
console . error ( error ) ;
if ( interaction . replied || interaction . deferred ) {
await interaction . followUp ( { content : 'There was an error while executing this command!' , ephemeral : true } ) ;
} else {
await interaction . reply ( { content : 'There was an error while executing this command!' , ephemeral : true } ) ;
}
}
} ) ;
// My code
var abbreviationKey = require ( "./abbreviation_key.json" ) ;
2023-03-31 12:10:42 -05:00
function arrayRotate ( arr , reverse , amount ) {
for ( var i = 0 ; i < amount ; i ++ ) {
2023-03-31 13:20:24 -05:00
if ( reverse ) arr . unshift ( arr . pop ( ) ) ;
else arr . push ( arr . shift ( ) ) ;
return arr ;
2023-03-31 12:10:42 -05:00
}
2023-03-31 00:34:58 -05:00
}
2023-03-31 12:10:42 -05:00
function matchAbbr ( abbrTarget ) {
2023-03-31 12:29:46 -05:00
console . log ( "Looking for: " + abbrTarget ) ;
2023-03-31 12:10:42 -05:00
for ( var abbr in abbreviationKey . target_phrases ) {
2023-03-31 12:46:02 -05:00
if ( abbreviationKey . target_phrases [ abbr ] == abbrTarget ) {
2023-03-31 21:39:39 -05:00
//console.log("abbrTarget: " + typeof (abbrTarget));
//console.log("abbr: " + typeof (abbr));
2023-03-31 12:10:42 -05:00
return abbr ;
} else {
2023-03-31 21:39:39 -05:00
//console.log("abbrTarget: " + typeof (abbrTarget));
//console.log("abbr: " + typeof (abbr));
2023-03-31 12:10:42 -05:00
}
}
2023-03-31 13:20:24 -05:00
return "" ;
2023-03-31 12:10:42 -05:00
}
2023-03-31 00:34:58 -05:00
2023-04-01 12:15:12 -05:00
function replyMessage ( message , correctedMessage , abbrsUsed ) {
var plural = "" ;
if ( abbrsUsed == 1 ) {
plural = "an acronym" ;
} else {
plural = abbrsUsed + " acronyms"
}
2023-04-04 18:01:12 -05:00
var replyString = "Your message contains " + plural + "! Let me fix that for you: \n > " + correctedMessage + "\n \n \|\|btw I was written by Caleb Fontenot\|\|"
var stringLength = replyString . length ;
2023-04-04 16:28:07 -05:00
if ( stringLength > 2000 ) {
2023-04-04 16:28:53 -05:00
replyString = "Sorry, I detected " + plural + ", but the resultant response was " + stringLength + " characters long, and Discord only allows a maximum of 2000."
2023-04-04 16:28:07 -05:00
}
message . reply ( { content : replyString , allowedMentions : { repliedUser : false } } ) ;
2023-04-03 19:30:11 -05:00
2023-03-31 12:10:42 -05:00
}
2023-03-31 00:34:58 -05:00
client . on ( 'messageCreate' , message = > {
2023-04-01 18:41:35 -05:00
checkMessage ( message ) ;
2023-04-02 18:56:10 -05:00
checkInsult ( message ) ;
2023-04-01 18:41:35 -05:00
} ) ;
2023-04-01 18:44:08 -05:00
client . on ( 'messageUpdate' , ( undefined , newMessage ) = > {
checkMessage ( newMessage ) ;
2023-04-02 18:56:10 -05:00
checkInsult ( newMessage ) ;
2023-04-01 18:41:35 -05:00
} ) ;
function checkMessage ( message ) {
2023-03-31 00:34:58 -05:00
console . log ( ` ${ message . author . tag } in # ${ message . channel . name } sent: ${ message . content } ` ) ;
2023-04-02 15:40:04 -05:00
//console.log(message.author);
if ( message . author . id == "1091120267851485215" ) {
return ;
}
2023-04-02 09:34:20 -05:00
2023-04-02 21:53:18 -05:00
var matchMessageArray = message . content . toLowerCase ( ) . split ( /[ ,!?\"@#$%^&*().;:|]+/ ) ;
2023-03-31 12:10:42 -05:00
console . log ( matchMessageArray ) ;
2023-03-31 21:24:57 -05:00
let correctedMessage = message . content ;
let matchDetected = false ;
2023-04-01 12:15:12 -05:00
var abbrsUsed = 0 ;
2023-03-31 12:10:42 -05:00
for ( let i = 0 ; i < matchMessageArray . length ; ++ i ) {
2023-04-01 12:04:36 -05:00
var rebuildMessageArray = [ ] ;
2023-03-31 21:47:23 -05:00
//console.log("Corrected Message: " + correctedMessage);
2023-04-02 15:40:04 -05:00
2023-04-02 15:37:01 -05:00
if ( abbreviationKey . target_phrases [ matchMessageArray [ i ] ] !== undefined ) {
2023-04-04 13:05:18 -05:00
checkIfGirlfriend ( message ) ;
2023-04-04 12:59:58 -05:00
if ( checkIfExempt ( message ) ) {
return ;
2023-04-02 15:37:01 -05:00
}
2023-03-31 21:24:57 -05:00
matchDetected = true ;
2023-04-01 12:15:12 -05:00
abbrsUsed ++ ;
2023-03-31 12:10:42 -05:00
//Return key
var phrase = abbreviationKey . target_phrases [ matchMessageArray [ i ] ] ;
2023-03-31 13:20:24 -05:00
var abbr = matchAbbr ( phrase ) ; //abbreviationKey.target_phrases[phrase];
2023-04-01 15:37:35 -05:00
//console.log(typeof(abbr));
2023-03-31 12:10:42 -05:00
console . log ( "Found abbreviation: " + abbr ) ;
console . log ( "Phrase that matches used acronym: " + phrase ) ;
2023-03-31 12:33:50 -05:00
if ( abbr !== "" ) {
2023-04-01 12:04:36 -05:00
rebuildMessageArray = correctedMessage . split ( new RegExp ( abbr , 'i' ) ) ;
2023-04-01 15:37:35 -05:00
console . log ( "rebuildMessageArray: " + rebuildMessageArray ) ;
2023-03-31 12:33:50 -05:00
} else {
message . reply ( "Detected abbreviation, but regex matching broke. Caleb is working on this..." ) ;
2023-04-04 12:59:58 -05:00
return ;
2023-03-31 12:33:50 -05:00
}
2023-04-04 12:26:22 -05:00
//https://gitea.calebfontenot.com/CCF_100/NoMoreAcronyms/issues/1
2023-03-31 12:10:42 -05:00
arrayRotate ( rebuildMessageArray , true , 1 ) ;
2023-04-03 18:01:47 -05:00
rebuildMessageArray . unshift ( "`" + phrase + "`" ) ;
2023-03-31 12:10:42 -05:00
arrayRotate ( rebuildMessageArray , true , 2 ) ;
console . log ( rebuildMessageArray ) ;
// Build into string and set to variable!
2023-03-31 21:39:39 -05:00
correctedMessage = "" ; // clear old message.
2023-03-31 12:10:42 -05:00
for ( let j = 0 ; j < rebuildMessageArray . length ; j ++ ) {
correctedMessage += rebuildMessageArray [ j ] ;
}
console . log ( correctedMessage ) ;
2023-04-02 15:40:04 -05:00
2023-03-31 12:10:42 -05:00
//break;
2023-03-31 00:34:58 -05:00
}
}
2023-03-31 21:24:57 -05:00
if ( matchDetected ) {
2023-04-01 12:15:12 -05:00
replyMessage ( message , correctedMessage , abbrsUsed ) ;
2023-03-31 21:24:57 -05:00
}
2023-04-02 15:40:04 -05:00
2023-04-02 18:56:10 -05:00
}
2023-04-04 13:05:18 -05:00
function checkIfGirlfriend ( message ) {
if ( message . author . id == "698360641731166238" ) {
message . react ( '☺️' ) ;
message . react ( '❤️' ) ;
}
}
2023-04-04 12:59:58 -05:00
function checkIfExempt ( message ) {
console . log ( "Has role? " + message . member . roles . cache . some ( role = > role . name == "Exempt from NoMoreAcronyms" ) ) ;
if ( ! message . author . bot ) {
if ( message . member . roles . cache . some ( role = > role . name == "Exempt from NoMoreAcronyms" ) ) {
if ( ! message . member . roles . cache . some ( role = > role . name == "NoReactions" ) ) {
message . react ( '🇵' ) ;
message . react ( '🇴' ) ;
message . react ( '🇬' ) ;
return true ;
}
return true ;
}
}
return false ;
}
2023-04-02 18:56:10 -05:00
function checkInsult ( message ) {
2023-04-02 19:18:10 -05:00
if ( ! message . author . bot ) {
if ( ! message . member . roles . cache . some ( role = > role . name == "Exempt from NoMoreAcronyms" ) ) {
if ( message . content . includes ( "bot" , "annoying" ) ) {
message . react ( '🇳' ) ;
message . react ( '🇴' ) ;
message . react ( '❌' ) ;
message . react ( '🇺' ) ;
}
2023-04-02 19:02:58 -05:00
}
2023-04-02 18:56:10 -05:00
}
2023-04-01 18:41:35 -05:00
}