2021-01-07 22:02:38 -08:00
/ * T h e A r t i f i c e r w a s b u i l t i n m e m o r y o f B a b k a
* With love , Ean
*
* December 21 , 2020
* /
2021-02-19 17:44:38 -08:00
import {
// Discordeno deps
2022-05-20 01:43:22 -07:00
sendMessage ,
} from '../deps.ts' ;
2021-01-07 05:34:14 -08:00
2021-01-07 11:00:46 -08:00
// ask(prompt) returns string
// ask prompts the user at command line for message
const ask = async ( question : string , stdin = Deno . stdin , stdout = Deno . stdout ) : Promise < string > = > {
2021-01-07 05:34:14 -08:00
const buf = new Uint8Array ( 1024 ) ;
// Write question to console
await stdout . write ( new TextEncoder ( ) . encode ( question ) ) ;
// Read console's input into answer
2022-05-20 01:43:22 -07:00
const n = < number > await stdin . read ( buf ) ;
2021-01-07 05:34:14 -08:00
const answer = new TextDecoder ( ) . decode ( buf . subarray ( 0 , n ) ) ;
return answer . trim ( ) ;
} ;
2021-11-21 19:14:57 -08:00
// cmdPrompt(logChannel, botName) returns nothing
2021-01-07 11:00:46 -08:00
// cmdPrompt creates an interactive CLI for the bot, commands can vary
2021-11-21 19:14:57 -08:00
const cmdPrompt = async ( logChannel : bigint , botName : string ) : Promise < void > = > {
2021-01-07 05:34:14 -08:00
let done = false ;
while ( ! done ) {
2021-01-07 11:00:46 -08:00
// Get a command and its args
2022-05-20 01:43:22 -07:00
const fullCmd = await ask ( 'cmd> ' ) ;
2021-01-07 05:34:14 -08:00
2021-01-07 11:00:46 -08:00
// Split the args off of the command and prep the command
2022-05-20 01:43:22 -07:00
const args = fullCmd . split ( ' ' ) ;
2021-01-07 05:34:14 -08:00
const command = args . shift ( ) ? . toLowerCase ( ) ;
2021-01-07 11:00:46 -08:00
// All commands below here
// exit or e
// Fully closes the bot
2022-05-20 01:43:22 -07:00
if ( command === 'exit' || command === 'e' ) {
2021-01-07 05:34:14 -08:00
console . log ( ` ${ botName } Shutting down. \ n \ nGoodbye. ` ) ;
done = true ;
Deno . exit ( 0 ) ;
2022-05-20 01:43:22 -07:00
} // stop
2021-01-07 11:00:46 -08:00
// Closes the CLI only, leaving the bot running truly headless
2022-05-20 01:43:22 -07:00
else if ( command === 'stop' ) {
2021-01-07 05:34:14 -08:00
console . log ( ` Closing ${ botName } CLI. Bot will continue to run. \ n \ nGoodbye. ` ) ;
done = true ;
2022-05-20 01:43:22 -07:00
} // m [channel] [message]
2021-01-07 11:00:46 -08:00
// Sends [message] to specified [channel]
2022-05-20 01:43:22 -07:00
else if ( command === 'm' ) {
2021-01-07 05:34:14 -08:00
try {
2022-05-20 01:43:22 -07:00
const channelId = args . shift ( ) || '' ;
const message = args . join ( ' ' ) ;
2021-01-07 11:00:46 -08:00
2022-05-20 01:43:22 -07:00
sendMessage ( BigInt ( channelId ) , message ) . catch ( ( reason ) = > {
2021-03-27 21:29:06 -07:00
console . error ( reason ) ;
} ) ;
2022-05-20 01:43:22 -07:00
} catch ( e ) {
2021-01-07 05:34:14 -08:00
console . error ( e ) ;
}
2022-05-20 01:43:22 -07:00
} // ml [message]
2021-01-07 11:00:46 -08:00
// Sends a message to the specified log channel
2022-05-20 01:43:22 -07:00
else if ( command === 'ml' ) {
const message = args . join ( ' ' ) ;
2021-01-07 11:00:46 -08:00
2022-05-20 01:43:22 -07:00
sendMessage ( logChannel , message ) . catch ( ( reason ) = > {
2021-03-27 21:29:06 -07:00
console . error ( reason ) ;
} ) ;
2022-05-20 01:43:22 -07:00
} // help or h
2021-01-07 11:00:46 -08:00
// Shows a basic help menu
2022-05-20 01:43:22 -07:00
else if ( command === 'help' || command === 'h' ) {
console . log (
` ${ botName } CLI Help: \ n \ nAvailable Commands: \ n exit - closes bot \ n stop - closes the CLI \ n m [ChannelID] [messgae] - sends message to specific ChannelID as the bot \ n ml [message] sends a message to the specified botlog \ n help - this message ` ,
) ;
} // Unhandled commands die here
2021-01-07 11:00:46 -08:00
else {
2022-05-20 01:43:22 -07:00
console . log ( 'undefined command' ) ;
2021-01-07 05:34:14 -08:00
}
}
} ;
2022-05-04 23:49:06 -07:00
export default { cmdPrompt } ;