2021-01-07 22:02:38 -08:00
|
|
|
/* The Artificer was built in memory of Babka
|
|
|
|
* With love, Ean
|
|
|
|
*
|
|
|
|
* December 21, 2020
|
|
|
|
*/
|
|
|
|
|
2021-02-19 17:44:38 -08:00
|
|
|
import {
|
2022-06-22 18:43:57 -07:00
|
|
|
// Discordeno deps
|
|
|
|
DiscordenoMessage,
|
|
|
|
// Log4Deno deps
|
|
|
|
log,
|
|
|
|
LT,
|
2021-02-19 17:44:38 -08:00
|
|
|
// 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') {
|
2022-06-22 18:43:57 -07:00
|
|
|
console.log(`${botName} CLI Help:
|
|
|
|
|
|
|
|
Available Commands:
|
|
|
|
exit - closes bot
|
|
|
|
stop - closes the CLI
|
|
|
|
m [ChannelID] [messgae] - sends message to specific ChannelID as the bot
|
|
|
|
ml [message] sends a message to the specified botlog
|
|
|
|
help - this message`);
|
2022-05-20 01:43:22 -07:00
|
|
|
} // 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-06-22 18:43:57 -07:00
|
|
|
const genericLogger = (level: LT, message: string) => log(level, message);
|
2022-06-24 17:52:56 -07:00
|
|
|
const messageEditError = (location: string, message: DiscordenoMessage | string, err: Error) =>
|
|
|
|
genericLogger(LT.ERROR, `${location} | Failed to edit message: ${JSON.stringify(message)} | Error: ${err.name} - ${err.message}`);
|
2022-06-22 18:45:40 -07:00
|
|
|
const messageSendError = (location: string, message: DiscordenoMessage | string, err: Error) =>
|
2022-06-24 17:52:56 -07:00
|
|
|
genericLogger(LT.ERROR, `${location} | Failed to send message: ${JSON.stringify(message)} | Error: ${err.name} - ${err.message}`);
|
2022-06-22 18:45:40 -07:00
|
|
|
const messageDeleteError = (location: string, message: DiscordenoMessage | string, err: Error) =>
|
2022-06-24 17:52:56 -07:00
|
|
|
genericLogger(LT.ERROR, `${location} | Failed to delete message: ${JSON.stringify(message)} | Error: ${err.name} - ${err.message}`);
|
|
|
|
const dbError = (location: string, type: string, err: Error) => genericLogger(LT.ERROR, `${location} | Failed to ${type} database | Error: ${err.name} - ${err.message}`);
|
2022-06-22 18:43:57 -07:00
|
|
|
|
|
|
|
export default {
|
|
|
|
commonLoggers: {
|
2022-06-24 17:52:56 -07:00
|
|
|
dbError,
|
|
|
|
messageEditError,
|
2022-06-22 18:43:57 -07:00
|
|
|
messageSendError,
|
|
|
|
messageDeleteError,
|
|
|
|
},
|
|
|
|
cmdPrompt,
|
|
|
|
};
|