2022-08-14 05:51:45 -04:00
/* eslint-disable max-len */
2022-10-19 21:14:29 -04:00
const { MessageEmbed } = require ( 'discord.js' ) ;
2022-08-14 05:51:45 -04:00
function add ( command , interval , bot ) {
const id = setInterval ( ( ) => bot . core . run ( command ) , interval ) ;
bot . cloops . push ( { id , interval , command } ) ;
}
function remove ( item ) {
clearInterval ( bot . cloops [ item ] . id ) ;
bot . cloops . splice ( item , 1 ) ;
}
function clear ( ) {
for ( const interval of bot . cloops ) clearInterval ( interval . id ) ;
bot . cloops = [ ] ;
}
2022-10-19 21:14:29 -04:00
function list ( bot , discord , channeldc ) {
2022-08-14 05:51:45 -04:00
const message = [ ] ;
2022-10-19 21:14:29 -04:00
if ( discord ) {
for ( const [ id , cloop ] of Object . entries ( bot . cloops ) ) {
message . push ( id ) ;
message . push ( ' > ' ) ;
message . push ( ` \` ${ cloop . command } \` ` ) ;
message . push ( ' - ' ) ;
message . push ( cloop . interval ) ;
message . push ( '\n' ) ;
}
message . pop ( ) ;
const Embed = new MessageEmbed ( )
. setColor ( '#FFFF00' )
. setTitle ( 'Cloops' )
. setDescription ( message . join ( '' ) ) ;
channeldc . send ( { embeds : [ Embed ] } ) ;
return ;
}
2022-08-14 05:51:45 -04:00
message . push ( { text : 'Cloops:' , color : 'green' } ) ;
2022-08-14 07:18:52 -04:00
message . push ( '\n' ) ;
2022-08-14 05:51:45 -04:00
for ( const [ id , cloop ] of Object . entries ( bot . cloops ) ) {
message . push ( { text : id , color : 'aqua' } ) ;
message . push ( { text : ' > ' , color : 'gold' } ) ;
message . push ( { text : cloop . command , color : 'green' } ) ;
message . push ( { text : ' - ' , color : 'gold' } ) ;
message . push ( { text : cloop . interval , color : 'green' } ) ;
message . push ( '\n' ) ;
}
message . pop ( ) ;
bot . core . run ( 'minecraft:tellraw @a ' + JSON . stringify ( message ) ) ;
}
module . exports = {
name : 'cloop' ,
alias : [ ] ,
description : 'Loop commands' ,
usage : '<hash> <add|remove|removeall|list> <interval|index> <command>' ,
trusted : 1 ,
execute : function ( bot , username , usernameraw , sender , prefix , args ) {
2022-10-19 07:17:41 -04:00
if ( ! bot . cloops ) bot . cloops = [ ] ;
2022-08-14 05:51:45 -04:00
if ( args [ 1 ] === 'add' ) {
if ( args [ 0 ] === bot . hash ) {
2022-10-19 07:17:41 -04:00
if ( args [ 3 ] ) {
2022-10-24 04:03:18 -04:00
if ( ! Number ( args [ 2 ] ) && Number ( args [ 2 ] ) !== 0 ) throw new SyntaxError ( 'Invalid interval' ) ;
2022-08-14 05:51:45 -04:00
add ( args . slice ( 3 ) . join ( ' ' ) , args [ 2 ] , bot ) ;
bot . core . run ( 'minecraft:tellraw @a ' + JSON . stringify ( [ { text : 'Added command ' , color : 'white' } , { text : ` ${ args . slice ( 3 ) . join ( ' ' ) } ` , color : 'aqua' } , { text : ' with interval ' , color : 'white' } , { text : ` ${ args [ 2 ] } ` , color : 'green' } , { text : ' to the cloops' , color : 'white' } ] ) ) ;
}
} else {
throw new Error ( 'Invalid hash' ) ;
}
return ;
}
if ( args [ 1 ] === 'list' ) {
if ( args [ 0 ] === bot . hash ) {
list ( bot ) ;
} else {
throw new Error ( 'Invalid hash' ) ;
}
return ;
}
if ( args [ 1 ] === 'remove' ) {
if ( args [ 0 ] === bot . hash ) {
remove ( args [ 2 ] ) ;
bot . core . run ( 'minecraft:tellraw @a ' + JSON . stringify ( [ '' , { text : 'Removed cloop ' } , { text : args [ 2 ] , color : 'aqua' } ] ) ) ;
} else {
throw new Error ( 'Invalid hash' ) ;
}
return ;
}
if ( args [ 1 ] === 'removeall' ) {
if ( args [ 0 ] === bot . hash ) {
clear ( ) ;
bot . core . run ( 'minecraft:tellraw @a ' + JSON . stringify ( [ '' , { text : 'Removed all looped commands' , color : 'white' } ] ) ) ;
} else {
throw new Error ( 'Invalid hash' ) ;
}
return ;
}
} ,
2022-10-19 21:14:29 -04:00
discordExecute : function ( bot , username , usernameraw , sender , prefix , args , channeldc , message ) {
if ( ! bot . cloops ) bot . cloops = [ ] ;
if ( args [ 0 ] === 'add' ) {
if ( message . member . roles . cache . some ( ( role ) => role . name === 'Trusted' ) ) {
if ( args [ 2 ] ) {
2022-10-29 05:49:51 -04:00
if ( ! Number ( args [ 1 ] ) && Number ( args [ 1 ] ) !== 0 ) throw new SyntaxError ( 'Invalid interval' ) ;
2022-10-19 21:14:29 -04:00
add ( args . slice ( 2 ) . join ( ' ' ) , args [ 1 ] , bot ) ;
const Embed = new MessageEmbed ( )
. setColor ( '#FFFF00' )
. setTitle ( 'Cloop' )
. setDescription ( ` Added cloop \` ${ args . slice ( 2 ) . join ( ' ' ) } \` with interval ${ args [ 1 ] } to the cloops ` ) ;
channeldc . send ( { embeds : [ Embed ] } ) ;
}
} else {
throw new Error ( 'You\'re not in the trusted role!' ) ;
}
return ;
}
if ( args [ 0 ] === 'list' ) {
if ( message . member . roles . cache . some ( ( role ) => role . name === 'Trusted' ) ) {
list ( bot , true , channeldc ) ;
} else {
throw new Error ( 'You\'re not in the trusted role!' ) ;
}
return ;
}
if ( args [ 0 ] === 'remove' ) {
if ( message . member . roles . cache . some ( ( role ) => role . name === 'Trusted' ) ) {
remove ( args [ 1 ] ) ;
const Embed = new MessageEmbed ( )
. setColor ( '#FFFF00' )
. setTitle ( 'Cloop' )
. setDescription ( ` Removed cloop \` ${ args [ 1 ] } \` ` ) ;
channeldc . send ( { embeds : [ Embed ] } ) ;
} else {
throw new Error ( 'You\'re not in the trusted role!' ) ;
}
return ;
}
if ( args [ 0 ] === 'removeall' ) {
if ( message . member . roles . cache . some ( ( role ) => role . name === 'Trusted' ) ) {
clear ( ) ;
const Embed = new MessageEmbed ( )
. setColor ( '#FFFF00' )
. setTitle ( 'Cloop' )
. setDescription ( 'Removed all looped commands' ) ;
channeldc . send ( { embeds : [ Embed ] } ) ;
} else {
throw new Error ( 'You\'re not in the trusted role!' ) ;
}
return ;
}
} ,
2022-08-14 05:51:45 -04:00
} ;