2019-10-28 20:21:06 +00:00
const database = require ( "../utils/database.js" ) ;
2019-09-29 03:32:43 +00:00
const client = require ( "../utils/client.js" ) ;
const paginator = require ( "../utils/pagination/pagination.js" ) ;
const { random } = require ( "../utils/misc.js" ) ;
exports . run = async ( message , args ) => {
2020-09-01 22:10:19 +00:00
if ( ! message . channel . guild ) return ` ${ message . author . mention } , this command only works in servers! ` ;
2020-12-18 02:32:19 +00:00
const guild = await database . getGuild ( message . channel . guild . id ) ;
if ( ( guild . tagsDisabled || guild . tags _disabled ) && args [ 0 ] . toLowerCase ( ) !== ( "enable" || "disable" ) ) return ;
2020-09-03 19:26:39 +00:00
if ( args . length === 0 ) return ` ${ message . author . mention } , you need to specify the name of the tag you want to view! ` ;
2020-12-18 02:32:19 +00:00
const tags = guild . tags instanceof Map ? Object . fromEntries ( guild . tags ) : guild . tags ;
const blacklist = [ "add" , "edit" , "remove" , "delete" , "list" , "random" , "own" , "owner" , "enable" , "disable" ] ;
2019-09-29 03:32:43 +00:00
switch ( args [ 0 ] . toLowerCase ( ) ) {
case "add" :
if ( args [ 1 ] === undefined ) return ` ${ message . author . mention } , you need to provide the name of the tag you want to add! ` ;
2019-11-23 23:23:28 +00:00
if ( blacklist . includes ( args [ 1 ] . toLowerCase ( ) ) ) return ` ${ message . author . mention } , you can't make a tag with that name! ` ;
2020-12-18 02:32:19 +00:00
if ( tags [ args [ 1 ] . toLowerCase ( ) ] ) return ` ${ message . author . mention } , this tag already exists! ` ;
2020-07-27 00:52:13 +00:00
var result = await setTag ( args . slice ( 2 ) . join ( " " ) , args [ 1 ] . toLowerCase ( ) , message , guild ) ;
2019-11-30 15:48:05 +00:00
if ( result ) return result ;
2019-09-29 03:32:43 +00:00
return ` ${ message . author . mention } , the tag \` ${ args [ 1 ] . toLowerCase ( ) } \` has been added! ` ;
case "delete" :
case "remove" :
if ( args [ 1 ] === undefined ) return ` ${ message . author . mention } , you need to provide the name of the tag you want to delete! ` ;
2020-12-18 02:32:19 +00:00
if ( ! tags [ args [ 1 ] . toLowerCase ( ) ] ) return ` ${ message . author . mention } , this tag doesn't exist! ` ;
2021-01-28 04:57:13 +00:00
if ( tags [ args [ 1 ] . toLowerCase ( ) ] . author !== message . author . id && ! message . member . permissions . has ( "manageMessages" ) && message . author . id !== process . env . OWNER ) return ` ${ message . author . mention } , you don't own this tag! ` ;
2020-12-18 02:32:19 +00:00
await database . removeTag ( args [ 1 ] . toLowerCase ( ) , message . channel . guild ) ;
2019-09-29 03:32:43 +00:00
return ` ${ message . author . mention } , the tag \` ${ args [ 1 ] . toLowerCase ( ) } \` has been deleted! ` ;
case "edit" :
if ( args [ 1 ] === undefined ) return ` ${ message . author . mention } , you need to provide the name of the tag you want to edit! ` ;
2020-12-18 02:32:19 +00:00
if ( ! tags [ args [ 1 ] . toLowerCase ( ) ] ) return ` ${ message . author . mention } , this tag doesn't exist! ` ;
2021-01-28 04:57:13 +00:00
if ( tags [ args [ 1 ] . toLowerCase ( ) ] . author !== message . author . id && ! message . member . permissions . has ( "manageMessages" ) && message . author . id !== process . env . OWNER ) return ` ${ message . author . mention } , you don't own this tag! ` ;
2020-07-27 00:52:13 +00:00
await setTag ( args . slice ( 2 ) . join ( " " ) , args [ 1 ] . toLowerCase ( ) , message , guild ) ;
2019-09-29 03:32:43 +00:00
return ` ${ message . author . mention } , the tag \` ${ args [ 1 ] . toLowerCase ( ) } \` has been edited! ` ;
2020-01-27 13:41:13 +00:00
case "own" :
case "owner" :
if ( args [ 1 ] === undefined ) return ` ${ message . author . mention } , you need to provide the name of the tag you want to check the owner of! ` ;
2020-12-18 02:32:19 +00:00
if ( ! tags [ args [ 1 ] . toLowerCase ( ) ] ) return ` ${ message . author . mention } , this tag doesn't exist! ` ;
return ` ${ message . author . mention } , this tag is owned by ** ${ client . users . get ( tags [ args [ 1 ] . toLowerCase ( ) ] . author ) . username } # ${ client . users . get ( tags [ args [ 1 ] . toLowerCase ( ) ] . author ) . discriminator } ** ( \` ${ tags [ args [ 1 ] . toLowerCase ( ) ] . author } \` ). ` ;
2019-09-29 03:32:43 +00:00
case "list" :
2021-01-28 04:57:13 +00:00
if ( ! message . channel . guild . members . get ( client . user . id ) . permissions . has ( "addReactions" ) && ! message . channel . permissionsOf ( client . user . id ) . has ( "addReactions" ) ) return ` ${ message . author . mention } , I don't have the \` Add Reactions \` permission! ` ;
if ( ! message . channel . guild . members . get ( client . user . id ) . permissions . has ( "embedLinks" ) && ! message . channel . permissionsOf ( client . user . id ) . has ( "embedLinks" ) ) return ` ${ message . author . mention } , I don't have the \` Embed Links \` permission! ` ;
2019-09-29 03:32:43 +00:00
var pageSize = 15 ;
var embeds = [ ] ;
2020-12-18 02:32:19 +00:00
var groups = Object . keys ( tags ) . map ( ( item , index ) => {
return index % pageSize === 0 ? Object . keys ( tags ) . slice ( index , index + pageSize ) : null ;
2019-11-05 15:52:46 +00:00
} ) . filter ( ( item ) => {
return item ;
2019-09-29 03:32:43 +00:00
} ) ;
for ( const [ i , value ] of groups . entries ( ) ) {
embeds . push ( {
"embed" : {
"title" : "Tag List" ,
"color" : 16711680 ,
"footer" : {
"text" : ` Page ${ i + 1 } of ${ groups . length } `
} ,
"description" : value . join ( "\n" ) ,
2019-12-19 16:36:33 +00:00
"fields" : process . env . NODE _ENV === "development" ? [ { "name" : "Note" , "value" : "Tags created in this version of esmBot will not carry over to the final release." } ] : null ,
2019-09-29 03:32:43 +00:00
"author" : {
"name" : message . author . username ,
"icon_url" : message . author . avatarURL
}
}
} ) ;
}
if ( embeds . length === 0 ) return ` ${ message . author . mention } , I couldn't find any tags! ` ;
return paginator ( message , embeds ) ;
case "random" :
2020-12-18 02:32:19 +00:00
return tags [ random ( Object . keys ( tags ) ) ] . content ;
2020-09-03 19:26:39 +00:00
case "enable" :
case "disable" :
2021-01-28 04:57:13 +00:00
if ( ! message . member . permissions . has ( "manageMessages" ) && message . author . id !== process . env . OWNER ) return ` ${ message . author . mention } , you don't have permission to disable tags! ` ;
2020-12-18 02:32:19 +00:00
var toggleResult = await database . toggleTags ( message . channel . guild ) ;
return ` ${ message . author . mention } , tags for this guild have been ${ toggleResult ? "disabled" : "enabled" } . To ${ toggleResult ? "enable" : "disable" } them again, run ${ guild . prefix } tags ${ toggleResult ? "enable" : "disable" } . ` ;
2019-09-29 03:32:43 +00:00
default :
2020-12-18 02:32:19 +00:00
if ( ! tags [ args [ 0 ] . toLowerCase ( ) ] ) return ` ${ message . author . mention } , this tag doesn't exist! ` ;
return tags [ args [ 0 ] . toLowerCase ( ) ] . content ;
2019-09-29 03:32:43 +00:00
}
} ;
2020-12-18 02:32:19 +00:00
const setTag = async ( content , name , message ) => {
2019-11-30 15:48:05 +00:00
if ( ( ! content || content . length === 0 ) && message . attachments . length === 0 ) return ` ${ message . author . mention } , you need to provide the content of the tag! ` ;
if ( message . attachments . length !== 0 && content ) {
2020-12-18 02:32:19 +00:00
await database . setTag ( name , { content : ` ${ content } ${ message . attachments [ 0 ] . url } ` , author : message . author . id } , message . channel . guild ) ;
2019-11-30 15:48:05 +00:00
} else if ( message . attachments . length !== 0 ) {
2020-12-18 02:32:19 +00:00
await database . setTag ( name , { content : message . attachments [ 0 ] . url , author : message . author . id } , message . channel . guild ) ;
2019-09-29 03:32:43 +00:00
} else {
2020-12-18 02:32:19 +00:00
await database . setTag ( name , { content : content , author : message . author . id } , message . channel . guild ) ;
2019-09-29 03:32:43 +00:00
}
2019-11-30 15:48:05 +00:00
return ;
2019-09-29 03:32:43 +00:00
} ;
2019-12-02 20:47:22 +00:00
exports . aliases = [ "t" , "tag" , "ta" ] ;
exports . category = 3 ;
2019-12-06 00:15:28 +00:00
exports . help = {
default : "Gets a tag" ,
add : "Adds a tag" ,
delete : "Deletes a tag" ,
edit : "Edits a tag" ,
list : "Lists all tags in the server" ,
2020-09-03 19:26:39 +00:00
random : "Gets a random tag" ,
owner : "Gets the owner of a tag" ,
disable : "Disables/Enables the tag system"
2019-12-06 00:15:28 +00:00
} ;
exports . params = {
default : "[name]" ,
add : "[name] [content]" ,
delete : "[name]" ,
2020-09-03 19:26:39 +00:00
edit : "[name] [content]" ,
owner : "[name]"
2019-12-10 15:53:43 +00:00
} ;