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-04 23:49:06 -07:00
sendMessage
2021-02-19 17:44:38 -08:00
} 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
const n = < number > await stdin . read ( buf ) ;
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
2021-01-07 05:34:14 -08:00
const fullCmd = await ask ( "cmd> " ) ;
2021-01-07 11:00:46 -08:00
// Split the args off of the command and prep the command
2021-01-07 05:34:14 -08:00
const args = fullCmd . split ( " " ) ;
const command = args . shift ( ) ? . toLowerCase ( ) ;
2021-01-07 11:00:46 -08:00
// All commands below here
// exit or e
// Fully closes the bot
2021-01-07 05:34:14 -08:00
if ( command === "exit" || command === "e" ) {
console . log ( ` ${ botName } Shutting down. \ n \ nGoodbye. ` ) ;
done = true ;
Deno . exit ( 0 ) ;
2021-01-07 11:00:46 -08:00
}
// stop
// Closes the CLI only, leaving the bot running truly headless
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 ;
2021-01-07 11:00:46 -08:00
}
// m [channel] [message]
// Sends [message] to specified [channel]
else if ( command === "m" ) {
2021-01-07 05:34:14 -08:00
try {
2021-11-21 19:14:57 -08:00
const channelId = args . shift ( ) || "" ;
2021-01-07 05:34:14 -08:00
const message = args . join ( " " ) ;
2021-01-07 11:00:46 -08:00
2021-11-21 19:14:57 -08:00
sendMessage ( BigInt ( channelId ) , message ) . catch ( reason = > {
2021-03-27 21:29:06 -07:00
console . error ( reason ) ;
} ) ;
2021-01-07 05:34:14 -08:00
}
catch ( e ) {
console . error ( e ) ;
}
2021-01-07 11:00:46 -08:00
}
// ml [message]
// Sends a message to the specified log channel
else if ( command === "ml" ) {
2021-01-07 05:34:14 -08:00
const message = args . join ( " " ) ;
2021-01-07 11:00:46 -08:00
2021-03-27 21:29:06 -07:00
sendMessage ( logChannel , message ) . catch ( reason = > {
console . error ( reason ) ;
} ) ;
2021-01-07 11:00:46 -08:00
}
// help or h
// Shows a basic help menu
else if ( command === "help" || command === "h" ) {
2021-01-07 05:34:14 -08:00
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 ` ) ;
2021-01-07 11:00:46 -08:00
}
// Unhandled commands die here
else {
2021-01-07 05:34:14 -08:00
console . log ( "undefined command" ) ;
}
}
} ;
2022-05-04 23:49:06 -07:00
export default { cmdPrompt } ;