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-12-06 18:47:40 +00:00
if ( args . length === 0 ) return ` ${ message . author . mention } , you need to specify the name of the tag you want to view! ` ;
2020-07-10 17:07:24 +00:00
const guildDB = await database . query ( "SELECT * FROM guilds WHERE guild_id = $1" , [ message . channel . guild . id ] ) ;
const tags = guildDB . rows [ 0 ] . tags ;
2019-09-29 03:32:43 +00:00
const blacklist = [ "add" , "edit" , "remove" , "delete" , "list" , "random" ] ;
switch ( args [ 0 ] . toLowerCase ( ) ) {
2020-07-10 17:07:24 +00:00
case "create" :
2019-09-29 03:32:43 +00:00
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-07-10 17:07:24 +00:00
if ( tags [ args [ 1 ] . toLowerCase ( ) ] ) return ` ${ message . author . mention } , this tag already exists! ` ;
var result = await setTag ( args . slice ( 2 ) . join ( " " ) , args [ 1 ] . toLowerCase ( ) , message , guildDB ) ;
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-07-10 17:07:24 +00:00
if ( ! tags [ args [ 1 ] . toLowerCase ( ) ] ) return ` ${ message . author . mention } , this tag doesn't exist! ` ;
2020-07-16 14:28:09 +00:00
if ( tags [ args [ 1 ] . toLowerCase ( ) ] . author !== message . author . id && ! message . member . permission . has ( "administrator" ) && message . author . id !== process . env . OWNER ) return ` ${ message . author . mention } , you don't own this tag! ` ;
2020-07-10 17:07:24 +00:00
delete tags [ args [ 1 ] . toLowerCase ( ) ] ;
await database . query ( "UPDATE guilds SET tags = $1 WHERE guild_id = $2" , [ tags , message . channel . guild . id ] ) ;
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-07-10 17:07:24 +00:00
if ( ! tags [ args [ 1 ] . toLowerCase ( ) ] ) return ` ${ message . author . mention } , this tag doesn't exist! ` ;
if ( tags [ args [ 1 ] . toLowerCase ( ) ] . author !== message . author . id && tags [ args [ 1 ] . toLowerCase ( ) ] . author !== process . env . OWNER ) return ` ${ message . author . mention } , you don't own this tag! ` ;
await setTag ( args . slice ( 2 ) . join ( " " ) , args [ 1 ] . toLowerCase ( ) , message , guildDB ) ;
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-07-10 17:07:24 +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" :
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 = [ ] ;
2020-07-10 17:07:24 +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-03-10 22:24:57 +00:00
return random ( [ ... tags ] ) [ 1 ] . content ;
2019-09-29 03:32:43 +00:00
default :
2020-07-10 17:07:24 +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-07-10 17:07:24 +00:00
const setTag = async ( content , name , message , guildDB ) => {
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-07-10 17:07:24 +00:00
guildDB . rows [ 0 ] . tags [ name ] = { content : ` ${ content } ${ message . attachments [ 0 ] . url } ` , author : message . author . id } ;
2019-11-30 15:48:05 +00:00
} else if ( message . attachments . length !== 0 ) {
2020-07-10 17:07:24 +00:00
guildDB . rows [ 0 ] . tags [ name ] = { content : message . attachments [ 0 ] . url , author : message . author . id } ;
2019-09-29 03:32:43 +00:00
} else {
2020-07-10 17:07:24 +00:00
guildDB . rows [ 0 ] . tags [ name ] = { content : content , author : message . author . id } ;
2019-09-29 03:32:43 +00:00
}
2020-07-10 17:07:24 +00:00
await database . query ( "UPDATE guilds SET tags = $1 WHERE guild_id = $2" , [ guildDB . rows [ 0 ] . tags , message . channel . guild . id ] ) ;
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" ,
random : "Gets a random tag"
} ;
exports . params = {
default : "[name]" ,
add : "[name] [content]" ,
delete : "[name]" ,
edit : "[name] [content]"
2019-12-10 15:53:43 +00:00
} ;