2023-08-14 10:36:29 -04:00
import Discord from 'discord.js' ;
import TClient from '../client.js' ;
2023-08-29 20:21:53 -04:00
import MessageTool from '../helpers/MessageTool.js' ;
2023-08-14 10:36:29 -04:00
export default {
async autocomplete ( client : TClient , interaction : Discord.AutocompleteInteraction ) {
const array = ( await client . tags ? . _content . find ( ) ) ? . map ( x = > x . _id ) . filter ( c = > c . startsWith ( interaction . options . getFocused ( ) ) ) ;
await interaction ? . respond ( array ? . map ( c = > ( { name : c , value : c } ) ) ) ;
// If you question all those '?.', let me tell you: Discord.JS is fricking stupid and I am too stressed to find a solution for it.
} ,
async run ( client : TClient , interaction : Discord.ChatInputCommandInteraction < 'cached' > ) {
2023-08-14 11:51:35 -04:00
if ( interaction . options . getSubcommandGroup ( ) === 'management' && ! client . isStaff ( interaction . member ) && ! client . config . whitelist . includes ( interaction . member . id ) ) return client . youNeedRole ( interaction , 'dcmod' ) ;
2023-08-14 10:36:29 -04:00
const tagData = async ( ) = > await client . tags . _content . findOne ( { _id : interaction.options.getString ( 'name' ) } ) ;
const tagMeta = {
isEmbedTrue : async ( ) = > ( await tagData ( ) ) . embedBool ,
title : async ( ) = > ( await tagData ( ) ) . _id ,
message : async ( ) = > ( await tagData ( ) ) . message ,
creatorName : async ( ) = > ( await client . users . fetch ( ( await tagData ( ) ) . user . _id ) ) . displayName
} ;
( {
send : async ( ) = > {
2023-08-14 13:15:23 -04:00
if ( ! await tagData ( ) ) return interaction . reply ( { content : 'This tag is not available in the database.' , ephemeral :true } ) ;
2023-08-14 10:36:29 -04:00
let targetField = '' ;
const targetMember = interaction . options . getMember ( 'target_user' ) ;
if ( targetMember ) targetField = ` *This tag is for <@ ${ targetMember . id } >* ` ;
const embedTemplate = new client . embed ( ) . setColor ( client . config . embedColor ) . setTitle ( await tagMeta . title ( ) ) . setDescription ( await tagMeta . message ( ) ) . setFooter ( { text : ` Tag creator: ${ await tagMeta . creatorName ( ) } ` } ) ;
2023-08-29 20:21:53 -04:00
const messageTemplate = MessageTool . concatMessage (
2023-08-14 10:36:29 -04:00
targetField ? targetField : '' ,
` ** ${ await tagMeta . title ( ) } ** ` ,
await tagMeta . message ( ) ,
'' ,
` Tag creator: ** ${ await tagMeta . creatorName ( ) } ** `
2023-08-29 20:21:53 -04:00
) ;
2023-08-14 10:36:29 -04:00
if ( await tagMeta . isEmbedTrue ( ) ) return interaction . reply ( { content : targetField ? targetField : null , embeds : [ embedTemplate ] , allowedMentions : { parse : [ 'users' ] } } ) ;
else return interaction . reply ( { content : messageTemplate , allowedMentions : { parse : [ 'users' ] } } )
} ,
create : async ( ) = > await client . tags . _content . create ( {
_id : interaction.options.getString ( 'name' ) ,
2023-08-14 11:51:35 -04:00
message : interaction.options.getString ( 'message' ) . replaceAll ( /\\n/g , '\n' ) ,
2023-08-14 10:36:29 -04:00
embedBool : interaction.options.getBoolean ( 'embed' ) ,
user : {
_id : interaction.member.id ,
name : interaction.user.username
}
} )
. then ( ( ) = > interaction . reply ( 'Tag is now created and available to use.' ) )
. catch ( err = > interaction . reply ( ` There was an error while trying to create your tag: \ n \` \` \` ${ err } \` \` \` ` ) ) ,
delete : async ( ) = > await client . tags . _content . findByIdAndDelete ( interaction . options . getString ( 'name' ) ) . then ( ( ) = > interaction . reply ( 'Tag successfully deleted.' ) ) . catch ( err = > interaction . reply ( ` Failed to delete the tag: \ n \` \` \` ${ err } \` \` \` ` ) ) ,
edit : async ( ) = > await client . tags . _content . findByIdAndUpdate ( interaction . options . getString ( 'name' ) , {
$set : {
2023-08-14 11:51:35 -04:00
message : interaction.options.getString ( 'new-message' ) . replaceAll ( /\\n/g , '\n' ) ,
2023-08-14 10:36:29 -04:00
embedBool : interaction.options.getBoolean ( 'embed' )
}
} )
. then ( ( ) = > interaction . reply ( 'Tag successfully updated, enjoy!' ) )
. catch ( err = > interaction . reply ( ` Tag couldn \ 't be updated: \ n \` \` \` ${ err } \` \` \` ` ) )
} as any ) [ interaction . options . getSubcommand ( ) ? ? interaction . options . getSubcommandGroup ( ) ] ( ) ;
} ,
data : new Discord . SlashCommandBuilder ( )
. setName ( 'tag' )
. setDescription ( 'Send user the resources/FAQ provided in the tag' )
. addSubcommand ( x = > x
. setName ( 'send' )
. setDescription ( 'Send a resource tag' )
. addStringOption ( x = > x
. setName ( 'name' )
. setDescription ( 'Name of an existing tag to send' )
. setAutocomplete ( true )
. setRequired ( true ) )
. addUserOption ( x = > x
. setName ( 'target_user' )
. setDescription ( 'Directly mention the target with this tag' ) ) )
. addSubcommandGroup ( x = > x
. setName ( 'management' )
. setDescription ( 'Add a new tag or delete/edit your current tag' )
. addSubcommand ( x = > x
. setName ( 'create' )
. setDescription ( 'Create a new tag' )
. addStringOption ( x = > x
. setName ( 'name' )
. setDescription ( 'Name of your tag, must be within 3-25 characters' )
. setMinLength ( 3 )
. setMaxLength ( 25 )
. setRequired ( true ) )
. addStringOption ( x = > x
. setName ( 'message' )
2023-08-14 11:51:35 -04:00
. setDescription ( 'Message to be included in your tag; e.g, you\'re giving the user some instructions, newline: \\n' )
2023-08-14 10:36:29 -04:00
. setMinLength ( 6 )
. setMaxLength ( 2048 )
. setRequired ( true ) )
. addBooleanOption ( x = > x
. setName ( 'embed' )
. setDescription ( 'Toggle this option if you want your message to be inside the embed or not' )
. setRequired ( true ) ) )
. addSubcommand ( x = > x
. setName ( 'delete' )
. setDescription ( 'Delete a tag' )
. addStringOption ( x = > x
. setName ( 'name' )
. setDescription ( 'Name of the tag to be deleted' )
. setAutocomplete ( true )
. setRequired ( true ) ) )
. addSubcommand ( x = > x
. setName ( 'edit' )
. setDescription ( 'Edit an existing tag' )
. addStringOption ( x = > x
. setName ( 'name' )
. setDescription ( 'Name of the tag to be edited' )
. setAutocomplete ( true )
. setRequired ( true ) )
. addStringOption ( x = > x
. setName ( 'new-message' )
2023-08-14 11:51:35 -04:00
. setDescription ( 'Replace the current tag\'s message with a new one, newline: \\n' )
2023-08-14 10:36:29 -04:00
. setRequired ( true ) )
. addBooleanOption ( x = > x
. setName ( 'embed' )
. setDescription ( 'Toggle this option on an existing tag to be updated with embed or not' )
. setRequired ( true ) ) ) )
}