2022-11-24 15:36:33 +01:00
import { ChatInputCommandInteraction , PermissionFlagsBits , SlashCommandBuilder } from "discord.js" ;
import { database , Badword } from "../data" ;
import { IsNull } from "typeorm" ;
import { getGuildSetting , isPremiumActive } from "../tools/data" ;
import getDefaultEmbed , { getFailedEmbed , getSuccessEmbed } from "../tools/defaultEmbeds" ;
import { getGuildChannel } from "../tools/discord" ;
2022-11-24 16:25:20 +01:00
import { Color , Emoji } from "../tools/design" ;
2022-11-24 15:36:33 +01:00
const builder = new SlashCommandBuilder ( ) ;
builder . setName ( "blocklist" ) ;
builder . setDescription ( "Configures the servers blocklist" ) ;
builder . setDefaultMemberPermissions ( PermissionFlagsBits . ManageGuild ) ;
builder . setDMPermission ( false ) ;
builder . addSubcommand ( ( builder ) = > {
builder . setName ( "add" ) ;
builder . setDescription ( "Adds a word to the servers blocklist" ) ;
builder . addStringOption ( ( option ) = > {
option . setName ( "word" ) ;
option . setDescription ( "The word to add" ) ;
option . setRequired ( true ) ;
option . setMaxLength ( 50 ) ;
return option ;
} ) ;
return builder ;
} ) ;
builder . addSubcommand ( ( builder ) = > {
builder . setName ( "remove" ) ;
builder . setDescription ( "Removes a word from the servers blocklist" ) ;
builder . addStringOption ( ( option ) = > {
option . setName ( "word" ) ;
option . setDescription ( "The word to remove" ) ;
option . setRequired ( true ) ;
option . setMaxLength ( 50 ) ;
return option ;
} ) ;
return builder ;
} ) ;
builder . addSubcommand ( ( builder ) = > {
builder . setName ( "get" ) ;
builder . setDescription ( "Returns all words from the servers blocklist" ) ;
return builder ;
} ) ;
async function execute ( interaction : ChatInputCommandInteraction ) : Promise < void > {
if ( ! interaction . guildId ) throw new Error ( "Command was executed in DM context" ) ;
const settings = await getGuildSetting ( interaction . guildId ) ;
const isPremium = isPremiumActive ( settings . isPremiumUntil ) ;
const logChannel = settings . notificationChannelID ? await getGuildChannel ( interaction . guildId , settings . notificationChannelID ) : null ;
switch ( interaction . options . getSubcommand ( true ) ) {
case "get" : {
const guildBadWords = await database . getRepository ( Badword ) . find ( {
select : {
value : true
} ,
where : {
guildID : interaction.guildId
}
} ) ;
const globalBadWords = await database . getRepository ( Badword ) . find ( {
where : {
guildID : IsNull ( )
}
} ) ;
interaction . reply ( {
content : ` \` \` \` Global bad word list \` \` \` \ n|| ${ globalBadWords . map ( ( word ) = > word . value ) . reduce ( ( prev , next ) = > prev + ", " + next , "" ) . slice ( 2 ) } || \ n \` \` \` Local server bad word list ( ${ guildBadWords . length } / ${ isPremium ? 100 : 10 } ) \` \` \` \ n|| ${ guildBadWords . map ( ( word ) = > word . value ) . reduce ( ( prev , next ) = > prev + ", " + next , "" ) . slice ( 2 ) } || ` ,
ephemeral : true
} ) . catch ( ) ;
break ;
}
case "add" : {
const count = await database . getRepository ( Badword ) . count ( {
where : {
guildID : interaction.guildId
}
} ) ;
const limit = isPremium ? 100 : 10 ;
if ( count >= limit ) {
const embed = getFailedEmbed ( ) ;
2022-11-24 16:25:20 +01:00
embed . setDescription ( ` You reached the word limit for your guild. Please delete a word before adding a new one ` ) ;
2022-11-24 15:36:33 +01:00
interaction . reply ( {
embeds : [ embed ] ,
ephemeral : true
} ) . catch ( ) ;
return ;
}
const word = interaction . options . getString ( "word" , true ) . toLowerCase ( ) ;
const exists = await database . getRepository ( Badword ) . count ( {
where : [
{ guildID : interaction.guildId , value : word } ,
{ guildID : IsNull ( ) , value : word }
]
} ) > 0 ;
if ( exists ) {
const embed = getFailedEmbed ( ) ;
embed . setDescription ( ` " ${ word } " already exists in the blocklist ` ) ;
interaction . reply ( {
embeds : [ embed ] ,
ephemeral : true
} ) . catch ( ) ;
return ;
}
const entry = new Badword ( ) ;
entry . guildID = interaction . guildId ;
entry . value = word ;
await database . getRepository ( Badword ) . save ( entry ) ;
const embed = getSuccessEmbed ( ) ;
embed . setDescription ( ` " ${ word } " has been added to the blocklist ` ) ;
interaction . reply ( {
embeds : [ embed ] ,
ephemeral : true
} ) ;
const logMessage = getDefaultEmbed ( ) ;
2022-11-24 16:25:20 +01:00
logMessage . setTitle ( ` ${ Emoji . SETTINGS } Word added ` ) ;
logMessage . setColor ( Color . INFORMING_BLUE ) ;
2022-11-24 15:36:33 +01:00
logMessage . setDescription ( ` "|| ${ word } ||" has been added to the blocklist ` ) ;
logMessage . addFields ( {
name : "This action was performed by" ,
value : ` ${ interaction . user . tag } ( ${ interaction . user . id } ) `
} ) ;
if ( logChannel && logChannel . isTextBased ( ) ) {
logChannel . send ( {
embeds : [ logMessage ]
} ) . catch ( ) ;
}
break ;
}
case "remove" : {
const word = interaction . options . getString ( "word" , true ) . toLowerCase ( ) ;
const entry = await database . getRepository ( Badword ) . findOne ( {
where : {
guildID : interaction.guildId ,
value : word
}
} ) ;
if ( ! entry ) {
const embed = getFailedEmbed ( ) ;
embed . setDescription ( ` " ${ word } " was not found in the blocklist ` ) ;
interaction . reply ( {
embeds : [ embed ] ,
ephemeral : true
} ) . catch ( ) ;
return ;
}
await database . getRepository ( Badword ) . delete ( {
id : entry.id
} ) ;
const embed = getSuccessEmbed ( ) ;
embed . setDescription ( ` " ${ word } " has been removed from the blocklist ` ) ;
interaction . reply ( {
embeds : [ embed ] ,
ephemeral : true
} ) . catch ( ) ;
const logMessage = getDefaultEmbed ( ) ;
2022-11-24 16:25:20 +01:00
logMessage . setTitle ( ` ${ Emoji . SETTINGS } Word removed ` ) ;
logMessage . setColor ( Color . INFORMING_BLUE ) ;
2022-11-24 15:36:33 +01:00
logMessage . setDescription ( ` "|| ${ word } ||" has been removed from the blocklist ` ) ;
logMessage . addFields ( {
name : "This action was performed by" ,
value : ` ${ interaction . user . tag } ( ${ interaction . user . id } ) `
} ) ;
if ( logChannel && logChannel . isTextBased ( ) ) {
logChannel . send ( {
embeds : [ logMessage ]
} ) . catch ( ) ;
}
break ;
}
default : {
throw new Error ( ` " ${ interaction . options . getSubcommand ( true ) } " cannot be executed ` ) ;
}
}
}
export {
builder ,
execute
}