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 ) => {
2019-11-05 15:52:46 +00:00
const guild = ( await database . guilds . find ( { id : message . channel . guild . id } ) . exec ( ) ) [ 0 ] ;
2019-10-28 20:21:06 +00:00
const tags = guild . tags ;
2019-09-29 03:32:43 +00:00
const blacklist = [ "add" , "edit" , "remove" , "delete" , "list" , "random" ] ;
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! ` ;
if ( blacklist . indexOf ( args [ 1 ] . toLowerCase ( ) ) > - 1 ) return ` ${ message . author . mention } , you can't make a tag with that name! ` ;
2019-11-05 15:52:46 +00:00
if ( tags . has ( args [ 1 ] . toLowerCase ( ) ) ) return ` ${ message . author . mention } , this tag already exists! ` ;
2019-10-28 20:21:06 +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 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! ` ;
2019-11-05 15:52:46 +00:00
if ( ! tags . has ( args [ 1 ] . toLowerCase ( ) ) ) return ` ${ message . author . mention } , this tag doesn't exist! ` ;
2019-11-13 00:09:06 +00:00
if ( tags . get ( args [ 1 ] . toLowerCase ( ) ) . author !== message . author . id && tags . get ( args [ 1 ] . toLowerCase ( ) ) . author !== process . env . OWNER ) return ` ${ message . author . mention } , you don't own this tag! ` ;
2019-10-28 20:21:06 +00:00
tags . set ( args [ 1 ] . toLowerCase ( ) , undefined ) ;
await guild . save ( ) ;
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! ` ;
2019-11-05 15:52:46 +00:00
if ( ! tags . has ( args [ 1 ] . toLowerCase ( ) ) ) return ` ${ message . author . mention } , this tag doesn't exist! ` ;
2019-11-13 00:09:06 +00:00
if ( tags . get ( args [ 1 ] . toLowerCase ( ) ) . author !== message . author . id && tags . get ( args [ 1 ] . toLowerCase ( ) ) . author !== process . env . OWNER ) return ` ${ message . author . mention } , you don't own this tag! ` ;
2019-10-28 20:21:06 +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! ` ;
case "list" :
if ( ! message . channel . guild . members . get ( client . user . id ) . permission . 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 ) . permission . has ( "embedLinks" ) && ! message . channel . permissionsOf ( client . user . id ) . has ( "embedLinks" ) ) return ` ${ message . author . mention } , I don't have the \` Embed Links \` permission! ` ;
var pageSize = 15 ;
var embeds = [ ] ;
2019-10-28 20:21:06 +00:00
var groups = Array . from ( tags . keys ( ) ) . map ( ( item , index ) => {
return index % pageSize === 0 ? Array . from ( tags . keys ( ) ) . 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" ) ,
"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" :
2019-10-28 20:21:06 +00:00
return tags [ random ( Object . keys ( tags ) ) ] . content ;
2019-09-29 03:32:43 +00:00
default :
if ( args . length === 0 ) return ` ${ message . author . mention } , you need to specify the name of the tag you want to view! ` ;
2019-11-05 15:52:46 +00:00
if ( ! tags . has ( args [ 0 ] . toLowerCase ( ) ) ) return ` ${ message . author . mention } , this tag doesn't exist! ` ;
return tags . get ( args [ 0 ] . toLowerCase ( ) ) . content ;
2019-09-29 03:32:43 +00:00
}
} ;
2019-10-28 20:21:06 +00:00
const setTag = async ( content , name , message , guild ) => {
2019-09-29 03:32:43 +00:00
if ( content === undefined || content . length === 0 ) return ` ${ message . author . mention } , you need to provide the content of the tag! ` ;
if ( message . attachments . length !== 0 ) {
2019-10-28 20:21:06 +00:00
guild . tags . set ( name , { content : ` ${ content } ${ message . attachments [ 0 ] . url } ` , author : message . author . id } ) ;
2019-09-29 03:32:43 +00:00
} else {
2019-10-28 20:21:06 +00:00
guild . tags . set ( name , { content : content , author : message . author . id } ) ;
2019-09-29 03:32:43 +00:00
}
2019-10-28 20:21:06 +00:00
await guild . save ( ) ;
2019-09-29 03:32:43 +00:00
} ;
exports . aliases = [ "t" , "tag" , "ta" ] ;