2022-12-05 03:16:48 -05:00
2022-11-27 02:35:28 -05:00
const { MessageEmbed } = require ( 'discord.js' )
function add ( command , interval , bot ) {
const id = setInterval ( ( ) => bot . core . run ( command ) , interval )
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
bot . cloops . push ( { id , interval , command } )
2022-08-14 05:51:45 -04:00
}
2022-11-27 02:35:28 -05:00
function remove ( item , bot ) {
clearInterval ( bot . cloops [ item ] . id )
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
bot . cloops . splice ( item , 1 )
2022-08-14 05:51:45 -04:00
}
2022-11-27 02:35:28 -05:00
function clear ( bot ) {
for ( const interval of bot . cloops ) clearInterval ( interval . id )
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
bot . cloops = [ ]
2022-08-14 05:51:45 -04:00
}
2022-12-01 05:15:30 -05:00
function list ( bot , discord , channeldc , selector , config ) {
2022-11-27 02:35:28 -05:00
const message = [ ]
2022-08-14 05:51:45 -04:00
2022-10-19 21:14:29 -04:00
if ( discord ) {
for ( const [ id , cloop ] of Object . entries ( bot . cloops ) ) {
2022-11-27 02:35:28 -05:00
message . push ( id )
message . push ( ' > ' )
message . push ( ` \` ${ cloop . command } \` ` )
message . push ( ' - ' )
message . push ( cloop . interval )
message . push ( '\n' )
2022-10-19 21:14:29 -04:00
}
2022-11-27 02:35:28 -05:00
message . pop ( )
2022-10-19 21:14:29 -04:00
const Embed = new MessageEmbed ( )
2022-12-01 05:15:30 -05:00
. setColor ( config . discord . embedsColors . normal )
2022-11-27 02:35:28 -05:00
. setTitle ( 'Cloops' )
. setDescription ( message . join ( '' ) )
channeldc . send ( { embeds : [ Embed ] } )
return
2022-10-19 21:14:29 -04:00
}
2022-11-27 02:35:28 -05:00
message . push ( { text : 'Cloops:' , color : 'green' } )
message . push ( '\n' )
2022-08-14 05:51:45 -04:00
for ( const [ id , cloop ] of Object . entries ( bot . cloops ) ) {
2022-11-27 02:35:28 -05:00
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' )
2022-08-14 05:51:45 -04:00
}
2022-11-27 02:35:28 -05:00
message . pop ( )
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
bot . tellraw ( selector , message )
2022-08-14 05:51:45 -04:00
}
module . exports = {
name : 'cloop' ,
alias : [ ] ,
description : 'Loop commands' ,
2022-11-13 02:41:59 -05:00
usage : [
'<hash> <add> <interval> <command>' ,
'<hash> <remove> <index>' ,
'<hash> <removeall>' ,
2022-11-27 02:35:28 -05:00
'<hash> <list>'
2022-11-13 02:41:59 -05:00
] ,
2022-08-14 05:51:45 -04:00
trusted : 1 ,
2022-11-28 06:32:49 -05:00
execute ( bot , username , usernameraw , sender , prefix , args , config , hash , ownerhash , selector ) {
2022-11-27 02:35:28 -05:00
if ( ! bot . cloops ) bot . cloops = [ ]
2022-11-23 04:24:00 -05:00
if ( args [ 1 ] === 'add' && args [ 3 ] ) {
2022-11-27 02:35:28 -05:00
if ( ! Number ( args [ 2 ] ) && Number ( args [ 2 ] ) !== 0 ) throw new SyntaxError ( 'Invalid interval' )
add ( args . slice ( 3 ) . join ( ' ' ) , args [ 2 ] , bot )
bot . tellraw ( selector , [ { 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' } ] )
2022-11-23 04:24:00 -05:00
} else if ( args [ 1 ] === 'list' ) {
2022-11-27 02:35:28 -05:00
list ( bot , false , null , selector )
2022-11-23 04:24:00 -05:00
} else if ( args [ 1 ] === 'remove' ) {
2022-11-27 02:35:28 -05:00
remove ( args [ 2 ] , bot )
bot . tellraw ( selector , [ { text : 'Removed cloop ' } , { text : args [ 2 ] , color : 'aqua' } ] )
2022-11-23 04:24:00 -05:00
} else if ( args [ 1 ] === 'removeall' ) {
2022-11-27 02:35:28 -05:00
clear ( bot )
bot . tellraw ( selector , [ { text : 'Removed all looped commands' , color : 'white' } ] )
2022-11-07 08:08:29 -05:00
} else {
2022-11-27 02:35:28 -05:00
throw new SyntaxError ( 'Invalid argument' )
2022-08-14 05:51:45 -04:00
}
} ,
2022-12-01 05:15:30 -05:00
discordExecute ( bot , username , usernameraw , sender , prefix , args , channeldc , message , config ) {
2022-11-27 02:35:28 -05:00
if ( ! bot . cloops ) bot . cloops = [ ]
2022-11-23 06:10:45 -05:00
if ( args [ 0 ] === 'add' && args [ 2 ] ) {
2022-11-27 02:35:28 -05:00
if ( ! Number ( args [ 1 ] ) && Number ( args [ 1 ] ) !== 0 ) throw new SyntaxError ( 'Invalid interval' )
add ( args . slice ( 2 ) . join ( ' ' ) , args [ 1 ] , bot )
2022-11-23 06:10:45 -05:00
const Embed = new MessageEmbed ( )
2022-12-01 05:15:30 -05:00
. setColor ( config . discord . embedsColors . normal )
2022-11-27 02:35:28 -05:00
. setTitle ( 'Cloop' )
. setDescription ( ` Added cloop \` ${ args . slice ( 2 ) . join ( ' ' ) } \` with interval ${ args [ 1 ] } to the cloops ` )
channeldc . send ( { embeds : [ Embed ] } )
2022-11-23 06:10:45 -05:00
} else if ( args [ 0 ] === 'list' ) {
2022-12-01 05:15:30 -05:00
list ( bot , true , channeldc , '@a' , config )
2022-11-23 06:10:45 -05:00
} else if ( args [ 0 ] === 'remove' ) {
2022-11-27 02:35:28 -05:00
remove ( args [ 1 ] , bot )
2022-11-23 06:10:45 -05:00
const Embed = new MessageEmbed ( )
2022-12-01 05:15:30 -05:00
. setColor ( config . discord . embedsColors . normal )
2022-11-27 02:35:28 -05:00
. setTitle ( 'Cloop' )
. setDescription ( ` Removed cloop \` ${ args [ 1 ] } \` ` )
channeldc . send ( { embeds : [ Embed ] } )
2022-11-23 06:10:45 -05:00
} else if ( args [ 0 ] === 'removeall' ) {
2022-11-27 02:35:28 -05:00
clear ( bot )
2022-11-23 06:10:45 -05:00
const Embed = new MessageEmbed ( )
2022-12-01 05:15:30 -05:00
. setColor ( config . discord . embedsColors . normal )
2022-11-27 02:35:28 -05:00
. setTitle ( 'Cloop' )
. setDescription ( 'Removed all looped commands' )
channeldc . send ( { embeds : [ Embed ] } )
2022-11-07 08:08:29 -05:00
} else {
2022-11-27 02:35:28 -05:00
throw new Error ( 'Invalid argument' )
2022-10-19 21:14:29 -04:00
}
2022-11-27 02:35:28 -05:00
}
}