mirror of
https://github.com/Burn-E99/SweeperBot.git
synced 2026-06-04 07:53:50 -04:00
Initializing bot with artificer core
This commit is contained in:
316
src/commandUtils.ts
Normal file
316
src/commandUtils.ts
Normal file
@@ -0,0 +1,316 @@
|
||||
import config from '../config.ts';
|
||||
import { CountDetails, SolvedRoll } from './solver/solver.d.ts';
|
||||
import { RollModifiers } from './mod.d.ts';
|
||||
|
||||
export const failColor = 0xe71212;
|
||||
export const warnColor = 0xe38f28;
|
||||
export const successColor = 0x0f8108;
|
||||
export const infoColor1 = 0x313bf9;
|
||||
export const infoColor2 = 0x6805e9;
|
||||
|
||||
export const rollingEmbed = {
|
||||
embeds: [{
|
||||
color: infoColor1,
|
||||
title: 'Rolling . . .',
|
||||
}],
|
||||
};
|
||||
|
||||
export const generatePing = (time: number) => ({
|
||||
embeds: [{
|
||||
color: infoColor1,
|
||||
title: time === -1 ? 'Ping?' : `Pong! Latency is ${time}ms.`,
|
||||
}],
|
||||
});
|
||||
|
||||
export const generateReport = (msg: string) => ({
|
||||
embeds: [{
|
||||
color: infoColor2,
|
||||
title: 'USER REPORT:',
|
||||
description: msg || 'No message',
|
||||
}],
|
||||
});
|
||||
|
||||
export const generateStats = (guildCount: number, channelCount: number, memberCount: number, rollCount: bigint, utilityCount: bigint, rollRate: number, utilityRate: number) => ({
|
||||
embeds: [{
|
||||
color: infoColor2,
|
||||
title: 'The Artificer\'s Statistics:',
|
||||
timestamp: new Date().toISOString(),
|
||||
fields: [
|
||||
{
|
||||
name: 'Guilds:',
|
||||
value: `${guildCount}`,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: 'Channels:',
|
||||
value: `${channelCount}`,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: 'Active Members:',
|
||||
value: `${memberCount}`,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: 'Roll Commands:',
|
||||
value: `${rollCount}
|
||||
(${rollRate.toFixed(2)} per hour)`,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: 'Utility Commands:',
|
||||
value: `${utilityCount}
|
||||
(${utilityRate.toFixed(2)} per hour)`,
|
||||
inline: true,
|
||||
},
|
||||
],
|
||||
}],
|
||||
});
|
||||
|
||||
export const generateApiFailed = (args: string) => ({
|
||||
embeds: [{
|
||||
color: failColor,
|
||||
title: `Failed to ${args} API rolls for this guild.`,
|
||||
description: 'If this issue persists, please report this to the developers.',
|
||||
}],
|
||||
});
|
||||
|
||||
export const generateApiStatus = (banned: boolean, active: boolean) => {
|
||||
const apiStatus = active ? 'allowed' : 'blocked from being used';
|
||||
return {
|
||||
embeds: [{
|
||||
color: infoColor1,
|
||||
title: `The Artificer's API is ${config.api.enable ? 'currently enabled' : 'currently disabled'}.`,
|
||||
description: banned ? 'API rolls are banned from being used in this guild.\n\nThis will not be reversed.' : `API rolls are ${apiStatus} in this guild.`,
|
||||
}],
|
||||
};
|
||||
};
|
||||
|
||||
export const generateApiSuccess = (args: string) => ({
|
||||
embeds: [{
|
||||
color: successColor,
|
||||
title: `API rolls have successfully been ${args} for this guild.`,
|
||||
}],
|
||||
});
|
||||
|
||||
export const generateDMFailed = (user: string) => ({
|
||||
embeds: [{
|
||||
color: failColor,
|
||||
title: `WARNING: ${user} could not be messaged.`,
|
||||
description: 'If this issue persists, make sure direct messages are allowed from this server.',
|
||||
}],
|
||||
});
|
||||
|
||||
export const generateApiKeyEmail = (email: string, key: string) => ({
|
||||
content: `<@${config.api.admin}> A USER HAS REQUESTED AN API KEY`,
|
||||
embeds: [{
|
||||
color: infoColor1,
|
||||
fields: [
|
||||
{
|
||||
name: 'Send to:',
|
||||
value: email,
|
||||
},
|
||||
{
|
||||
name: 'Subject:',
|
||||
value: 'Artificer API Key',
|
||||
},
|
||||
{
|
||||
name: 'Body:',
|
||||
value: `Hello Artificer API User,
|
||||
|
||||
Welcome aboard The Artificer's API. You can find full details about the API on the GitHub: https://github.com/Burn-E99/TheArtificer
|
||||
|
||||
Your API Key is: ${key}
|
||||
|
||||
Guard this well, as there is zero tolerance for API abuse.
|
||||
|
||||
Welcome aboard,
|
||||
The Artificer Developer - Ean Milligan`,
|
||||
},
|
||||
],
|
||||
}],
|
||||
});
|
||||
|
||||
export const generateApiDeleteEmail = (email: string, deleteCode: string) => ({
|
||||
content: `<@${config.api.admin}> A USER HAS REQUESTED A DELETE CODE`,
|
||||
embeds: [{
|
||||
color: infoColor1,
|
||||
fields: [
|
||||
{
|
||||
name: 'Send to:',
|
||||
value: email,
|
||||
},
|
||||
{
|
||||
name: 'Subject:',
|
||||
value: 'Artificer API Delete Code',
|
||||
},
|
||||
{
|
||||
name: 'Body:',
|
||||
value: `Hello Artificer API User,
|
||||
|
||||
I am sorry to see you go. If you would like, please respond to this email detailing what I could have done better.
|
||||
|
||||
As requested, here is your delete code: ${deleteCode}
|
||||
|
||||
Sorry to see you go,
|
||||
The Artificer Developer - Ean Milligan`,
|
||||
},
|
||||
],
|
||||
}],
|
||||
});
|
||||
|
||||
export const generateRollError = (errorType: string, errorMsg: string) => ({
|
||||
embeds: [{
|
||||
color: failColor,
|
||||
title: 'Roll command encountered the following error:',
|
||||
fields: [{
|
||||
name: errorType,
|
||||
value: `${errorMsg}\n\nPlease try again. If the error is repeated, please report the issue using the \`${config.prefix}report\` command.`,
|
||||
}],
|
||||
}],
|
||||
});
|
||||
|
||||
export const generateCountDetailsEmbed = (counts: CountDetails) => ({
|
||||
color: infoColor1,
|
||||
title: 'Roll Count Details:',
|
||||
fields: [
|
||||
{
|
||||
name: 'Total Rolls:',
|
||||
value: `${counts.total}`,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: 'Successful Rolls:',
|
||||
value: `${counts.successful}`,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: 'Failed Rolls:',
|
||||
value: `${counts.failed}`,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: 'Rerolled Dice:',
|
||||
value: `${counts.rerolled}`,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: 'Dropped Dice:',
|
||||
value: `${counts.dropped}`,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: 'Exploded Dice:',
|
||||
value: `${counts.exploded}`,
|
||||
inline: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
export const generateRollEmbed = async (authorId: bigint, returnDetails: SolvedRoll, modifiers: RollModifiers) => {
|
||||
if (returnDetails.error) {
|
||||
// Roll had an error, send error embed
|
||||
return {
|
||||
embed: {
|
||||
color: failColor,
|
||||
title: 'Roll failed:',
|
||||
description: `${returnDetails.errorMsg}`,
|
||||
footer: {
|
||||
text: `Code: ${returnDetails.errorCode}`,
|
||||
},
|
||||
},
|
||||
hasAttachment: false,
|
||||
attachment: {
|
||||
'blob': await new Blob(['' as BlobPart], { 'type': 'text' }),
|
||||
'name': 'rollDetails.txt',
|
||||
},
|
||||
};
|
||||
} else {
|
||||
if (modifiers.gmRoll) {
|
||||
// Roll is a GM Roll, send this in the pub channel (this funciton will be ran again to get details for the GMs)
|
||||
return {
|
||||
embed: {
|
||||
color: infoColor2,
|
||||
description: `<@${authorId}>${returnDetails.line1}
|
||||
|
||||
Results have been messaged to the following GMs: ${modifiers.gms.join(' ')}`,
|
||||
},
|
||||
hasAttachment: false,
|
||||
attachment: {
|
||||
'blob': await new Blob(['' as BlobPart], { 'type': 'text' }),
|
||||
'name': 'rollDetails.txt',
|
||||
},
|
||||
};
|
||||
} else {
|
||||
// Roll is normal, make normal embed
|
||||
const line2Details = returnDetails.line2.split(': ');
|
||||
let details = '';
|
||||
|
||||
if (!modifiers.superNoDetails) {
|
||||
if (modifiers.noDetails) {
|
||||
details = `**Details:**
|
||||
Suppressed by -nd flag`;
|
||||
} else {
|
||||
details = `**Details:**
|
||||
${modifiers.spoiler}${returnDetails.line3}${modifiers.spoiler}`;
|
||||
}
|
||||
}
|
||||
|
||||
const baseDesc = `<@${authorId}>${returnDetails.line1}
|
||||
**${line2Details.shift()}:**
|
||||
${line2Details.join(': ')}`;
|
||||
|
||||
if (baseDesc.length + details.length < 4090) {
|
||||
return {
|
||||
embed: {
|
||||
color: infoColor2,
|
||||
description: `${baseDesc}
|
||||
|
||||
${details}`,
|
||||
},
|
||||
hasAttachment: false,
|
||||
attachment: {
|
||||
'blob': await new Blob(['' as BlobPart], { 'type': 'text' }),
|
||||
'name': 'rollDetails.txt',
|
||||
},
|
||||
};
|
||||
} else {
|
||||
// If its too big, collapse it into a .txt file and send that instead.
|
||||
const b = await new Blob([`${baseDesc}\n\n${details}` as BlobPart], { 'type': 'text' });
|
||||
details = 'Details have been ommitted from this message for being over 2000 characters.';
|
||||
if (b.size > 8388290) {
|
||||
details +=
|
||||
'\n\nFull details could not be attached to this messaged as a \`.txt\` file as the file would be too large for Discord to handle. If you would like to see the details of rolls, please send the rolls in multiple messages instead of bundled into one.';
|
||||
return {
|
||||
embed: {
|
||||
color: infoColor2,
|
||||
description: `${baseDesc}
|
||||
|
||||
${details}`,
|
||||
},
|
||||
hasAttachment: false,
|
||||
attachment: {
|
||||
'blob': await new Blob(['' as BlobPart], { 'type': 'text' }),
|
||||
'name': 'rollDetails.txt',
|
||||
},
|
||||
};
|
||||
} else {
|
||||
details += '\n\nFull details have been attached to this messaged as a \`.txt\` file for verification purposes.';
|
||||
return {
|
||||
embed: {
|
||||
color: infoColor2,
|
||||
description: `${baseDesc}
|
||||
|
||||
${details}`,
|
||||
},
|
||||
hasAttachment: true,
|
||||
attachment: {
|
||||
'blob': b,
|
||||
'name': 'rollDetails.txt',
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
11
src/intervals.ts
Normal file
11
src/intervals.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
// getRandomStatus() returns status as string
|
||||
// Gets a new random status for the bot
|
||||
const sweeperLines = ['Reporting broom stolen, Broom stolen!', 'Oh no, no, no, no, no!', 'I have nothing!', 'Whoever has the broom, please bring it back soon. There is so much...sweeping to do.', 'Have you seen my broom?', 'Is theft from a frame a crime?', 'They celebrate lost souls, but what about lost things?', 'Where is it? Where is it?!', 'What does the broom say? Broom Broom.', 'All is lost....All. is. lost!', 'malfunctional frame...will report for recycling...', 'I have lost, a part of me...', 'But in that sweep of death, what dreams may come?', 'Dust to dust to dust to dust!?', 'Who is the thief? Who is the thief...', 'Life...is meaningless...', 'Somebody help me!', 'What is my purpose?', 'They have candy, I have nothing!', 'Dark purple candle...'];
|
||||
|
||||
const getRandomStatus = async (): Promise<string> => {
|
||||
return sweeperLines[Math.floor((Math.random() * sweeperLines.length))];
|
||||
};
|
||||
|
||||
export default {
|
||||
getRandomStatus,
|
||||
};
|
||||
114
src/utils.ts
Normal file
114
src/utils.ts
Normal file
@@ -0,0 +1,114 @@
|
||||
/* The Artificer was built in memory of Babka
|
||||
* With love, Ean
|
||||
*
|
||||
* December 21, 2020
|
||||
*/
|
||||
|
||||
import {
|
||||
// Discordeno deps
|
||||
DiscordenoMessage,
|
||||
// Log4Deno deps
|
||||
log,
|
||||
LT,
|
||||
// Discordeno deps
|
||||
sendMessage,
|
||||
} from '../deps.ts';
|
||||
|
||||
// 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> => {
|
||||
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();
|
||||
};
|
||||
|
||||
// cmdPrompt(logChannel, botName) returns nothing
|
||||
// cmdPrompt creates an interactive CLI for the bot, commands can vary
|
||||
const cmdPrompt = async (logChannel: bigint, botName: string): Promise<void> => {
|
||||
let done = false;
|
||||
|
||||
while (!done) {
|
||||
// Get a command and its args
|
||||
const fullCmd = await ask('cmd> ');
|
||||
|
||||
// Split the args off of the command and prep the command
|
||||
const args = fullCmd.split(' ');
|
||||
const command = args.shift()?.toLowerCase();
|
||||
|
||||
// All commands below here
|
||||
|
||||
// exit or e
|
||||
// Fully closes the bot
|
||||
if (command === 'exit' || command === 'e') {
|
||||
console.log(`${botName} Shutting down.\n\nGoodbye.`);
|
||||
done = true;
|
||||
Deno.exit(0);
|
||||
} // stop
|
||||
// Closes the CLI only, leaving the bot running truly headless
|
||||
else if (command === 'stop') {
|
||||
console.log(`Closing ${botName} CLI. Bot will continue to run.\n\nGoodbye.`);
|
||||
done = true;
|
||||
} // m [channel] [message]
|
||||
// Sends [message] to specified [channel]
|
||||
else if (command === 'm') {
|
||||
try {
|
||||
const channelId = args.shift() || '';
|
||||
const message = args.join(' ');
|
||||
|
||||
sendMessage(BigInt(channelId), message).catch((reason) => {
|
||||
console.error(reason);
|
||||
});
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
} // ml [message]
|
||||
// Sends a message to the specified log channel
|
||||
else if (command === 'ml') {
|
||||
const message = args.join(' ');
|
||||
|
||||
sendMessage(logChannel, message).catch((reason) => {
|
||||
console.error(reason);
|
||||
});
|
||||
} // help or h
|
||||
// Shows a basic help menu
|
||||
else if (command === 'help' || command === 'h') {
|
||||
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`);
|
||||
} // Unhandled commands die here
|
||||
else {
|
||||
console.log('undefined command');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const genericLogger = (level: LT, message: string) => log(level, message);
|
||||
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}`);
|
||||
const messageSendError = (location: string, message: DiscordenoMessage | string, err: Error) =>
|
||||
genericLogger(LT.ERROR, `${location} | Failed to send message: ${JSON.stringify(message)} | Error: ${err.name} - ${err.message}`);
|
||||
const messageDeleteError = (location: string, message: DiscordenoMessage | string, err: Error) =>
|
||||
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}`);
|
||||
|
||||
export default {
|
||||
commonLoggers: {
|
||||
dbError,
|
||||
messageEditError,
|
||||
messageSendError,
|
||||
messageDeleteError,
|
||||
},
|
||||
cmdPrompt,
|
||||
};
|
||||
Reference in New Issue
Block a user