Cleanup 1

This commit is contained in:
Hri7566 2023-12-02 17:21:01 -05:00
parent 59edf1601c
commit 58706b4eb8
19 changed files with 68 additions and 78 deletions

View File

@ -7,6 +7,7 @@ import { Prefix } from "./Prefix";
import { createInventory, readInventory } from "../data/inventory"; import { createInventory, readInventory } from "../data/inventory";
import { hasPermission } from "../permissions"; import { hasPermission } from "../permissions";
import { Logger } from "../util/Logger"; import { Logger } from "../util/Logger";
import { balanceConfig } from "../economy/Balance";
export interface CommandMessage<T = unknown> { export interface CommandMessage<T = unknown> {
m: "command"; m: "command";
@ -77,7 +78,8 @@ export class CommandHandler {
if (!inventory) { if (!inventory) {
await createInventory({ await createInventory({
userId: msg.p._id, userId: msg.p._id,
items: [] items: [],
balance: balanceConfig.defaultBalance || 0
}); });
inventory = await readInventory(msg.p._id); inventory = await readInventory(msg.p._id);

View File

@ -1,5 +1,4 @@
import { formatBalance } from "../../../economy/Balance"; import { formatBalance } from "../../../economy/Balance";
import { Item, StackableItem } from "../../../economy/Item";
import { Command } from "../../Command"; import { Command } from "../../Command";
export const balance = new Command( export const balance = new Command(
@ -7,7 +6,7 @@ export const balance = new Command(
["balance", "bal", "money"], ["balance", "bal", "money"],
"get bozo's balance", "get bozo's balance",
"balance", "balance",
(msg, agent) => { msg => {
const bal = msg.inventory.balance; const bal = msg.inventory.balance;
return `Balance: ${formatBalance(bal)}`; return `Balance: ${formatBalance(bal)}`;
} }

View File

@ -1,13 +1,12 @@
import { Item, StackableItem } from "../../../economy/Item"; import { Item, StackableItem } from "../../../economy/Item";
import { Command } from "../../Command"; import { Command } from "../../Command";
import { CommandHandler } from "../../CommandHandler";
export const inventory = new Command( export const inventory = new Command(
"inventory", "inventory",
["inventory", "inv"], ["inventory", "inv"],
"get bozo's inventory", "get bozo's inventory",
"inventory", "inventory",
(msg, agent) => { msg => {
const items = msg.inventory.items as unknown as Item[]; const items = msg.inventory.items as unknown as Item[];
const list = items const list = items
.map( .map(

View File

@ -3,26 +3,26 @@ import crypto from "crypto";
// Possible answers // Possible answers
const answers = [ const answers = [
"It is certain", "It is certain",
"It is decidedly so", "It is decidedly so",
"Without a doubt", "Without a doubt",
"Yes, definitely", "Yes, definitely",
"You may rely on it", "You may rely on it",
"As I see it, yes", "As I see it, yes",
"Most likely", "Most likely",
"Outlook good", "Outlook good",
"Yes", "Yes",
"Signs point to yes", "Signs point to yes",
"Reply hazy, try again", "Reply hazy, try again",
"Ask again later", "Ask again later",
"Better not tell you now", "Better not tell you now",
"Cannot predict now", "Cannot predict now",
"Concentrate and ask again", "Concentrate and ask again",
`Don't count on it`, `Don't count on it`,
"My reply is no", "My reply is no",
"My sources say no", "My sources say no",
"Outlook not so good", "Outlook not so good",
"Very doubtful" "Very doubtful"
]; ];
export const magic8ball = new Command( export const magic8ball = new Command(
@ -30,37 +30,37 @@ export const magic8ball = new Command(
["magic8ball", "8ball", "8"], ["magic8ball", "8ball", "8"],
"magic8ball bozo", "magic8ball bozo",
"magic8ball <question>", "magic8ball <question>",
(msg, agent) => { msg => {
/** /**
* Magic 8 ball command * Magic 8 ball command
* *
* Returns a unique answer for every question asked. * Returns a unique answer for every question asked.
*/ */
// Check arguments // Check arguments
// We're not checking for question marks since the user might think that's too much work // We're not checking for question marks since the user might think that's too much work
if (!msg.argv[1]) return `🎱 Ask me a question, dummy`; if (!msg.argv[1]) return `🎱 Ask me a question, dummy`;
// Hash the question (the user will get the same response for repeated questions) // Hash the question (the user will get the same response for repeated questions)
// We use a predictable but unique salt (security by obscurity?) // We use a predictable but unique salt (security by obscurity?)
const hash = crypto.createHash("sha-256"); const hash = crypto.createHash("sha-256");
hash.update("magic" + msg.argv[1] + "8ball"); hash.update("magic" + msg.argv[1] + "8ball");
// Use the question hash as the index // Use the question hash as the index
const hex = hash.digest("hex"); const hex = hash.digest("hex");
let index = parseInt(hex[0] + hex[1] + hex[2], 16); let index = parseInt(hex[0] + hex[1] + hex[2], 16);
// Make sure the index is within the bounds of the answers array // Make sure the index is within the bounds of the answers array
while (index >= answers.length) { while (index >= answers.length) {
index -= answers.length; index -= answers.length;
} }
while (index <= 0) { while (index <= 0) {
index += answers.length; index += answers.length;
} }
// Answer the user's question // Answer the user's question
let answer = answers[index]; let answer = answers[index];
return `🎱 ${answer}, ${msg.p.name}`; return `🎱 ${answer}, ${msg.p.name}`;
} }
); );

View File

@ -5,7 +5,7 @@ export const about = new Command(
["about", "info"], ["about", "info"],
"get about bozo", "get about bozo",
"about", "about",
(msg, agent) => { () => {
return `💫 This space bot was made by Hri7566.\n🚀 This bot is made possible by users like you. Thank you.\n🌌 Discord: @hri7566`; return `💫 This space bot was made by Hri7566.\n🚀 This bot is made possible by users like you. Thank you.\n🌌 Discord: @hri7566`;
} }
); );

View File

@ -7,7 +7,7 @@ export const help = new Command(
["help", "h", "commands", "cmds"], ["help", "h", "commands", "cmds"],
"get help bozo", "get help bozo",
"help [command]", "help [command]",
(msg, agent) => { msg => {
if (msg.argv[1]) { if (msg.argv[1]) {
// Get command usage // Get command usage
let command: Command | undefined; let command: Command | undefined;

View File

@ -1,4 +1,3 @@
import { MPPAgent } from "../../../services/mpp";
import { CosmicColor } from "../../../util/CosmicColor"; import { CosmicColor } from "../../../util/CosmicColor";
import { Command } from "../../Command"; import { Command } from "../../Command";
@ -7,7 +6,7 @@ export const color = new Command(
["color"], ["color"],
"colors, bozo", "colors, bozo",
"color [<r> <g> <b> | <hex>]", "color [<r> <g> <b> | <hex>]",
(msg, agent) => { msg => {
if (msg.argv[3]) { if (msg.argv[3]) {
// test for rgb // test for rgb
try { try {

View File

@ -1,4 +1,4 @@
import { MPPAgent } from "../../../services/mpp"; import type { MPPAgent } from "../../../services/mpp";
import { Command } from "../../Command"; import { Command } from "../../Command";
export const cursor = new Command( export const cursor = new Command(
@ -7,7 +7,7 @@ export const cursor = new Command(
"set the cursor bozo", "set the cursor bozo",
"cursor <mode>", "cursor <mode>",
(msg, agent) => { (msg, agent) => {
if (!(agent as MPPAgent).client.isConnected) return; if (agent.platform !== "mpp") return;
if (!msg.argv[1]) return "Specify a mode."; if (!msg.argv[1]) return "Specify a mode.";
const cursor = (agent as MPPAgent).cursor; const cursor = (agent as MPPAgent).cursor;

View File

@ -6,7 +6,7 @@ export const math = new Command(
["math"], ["math"],
"math bozo", "math bozo",
"math <expression>", "math <expression>",
(msg, agent) => { msg => {
try { try {
const argcat = msg.argv.slice(1, msg.argv.length).join(" "); const argcat = msg.argv.slice(1, msg.argv.length).join(" ");
const answer = evaluate(argcat); const answer = evaluate(argcat);

View File

@ -5,7 +5,7 @@ export const memory = new Command(
["memory", "mem"], ["memory", "mem"],
"get the memory bozo", "get the memory bozo",
"memory", "memory",
(msg, agent) => { () => {
return `${(process.memoryUsage().heapUsed / 1000 / 1000).toFixed( return `${(process.memoryUsage().heapUsed / 1000 / 1000).toFixed(
2 2
)} MB used / ${(process.memoryUsage().heapTotal / 1000 / 1000).toFixed( )} MB used / ${(process.memoryUsage().heapTotal / 1000 / 1000).toFixed(

View File

@ -6,7 +6,7 @@ export const role = new Command(
["role"], ["role"],
"get your role bozo", "get your role bozo",
"role", "role",
(msg, agent) => { msg => {
const role = getRole(msg.user.role); const role = getRole(msg.user.role);
if (!role) return `Your role: ${msg.user.role} (this role is broken)`; if (!role) return `Your role: ${msg.user.role} (this role is broken)`;
return `Your role: ${role.displayName} [${msg.user.role}]`; return `Your role: ${role.displayName} [${msg.user.role}]`;

View File

@ -1,4 +1,3 @@
import { MicroHandler } from "../../../services/console/MicroHandler";
import { padNum } from "../../../util/Logger"; import { padNum } from "../../../util/Logger";
import { Command } from "../../Command"; import { Command } from "../../Command";

View File

@ -1,4 +1,4 @@
import { Inventory, Prisma, User } from "@prisma/client"; import { Inventory } from "@prisma/client";
import { prisma } from "./prisma"; import { prisma } from "./prisma";
export async function createInventory(data: Omit<Inventory, "id">) { export async function createInventory(data: Omit<Inventory, "id">) {

View File

@ -1,4 +1,4 @@
import { Prisma, User } from "@prisma/client"; import { User } from "@prisma/client";
import { prisma } from "./prisma"; import { prisma } from "./prisma";
export async function createUser(data: User) { export async function createUser(data: User) {

View File

@ -1,16 +1,17 @@
import { loadConfig } from "../util/config"; import { loadConfig } from "../util/config";
const config = loadConfig("config/balance.yml", { export const balanceConfig = loadConfig("config/balance.yml", {
symbol: " star bits", symbol: " star bits",
after: true, after: true,
cutoff: 0 cutoff: 0,
defaultBalance: 0
}); });
export function formatBalance( export function formatBalance(
balance: number, balance: number,
symbol: string = config.symbol, symbol: string = balanceConfig.symbol,
after: boolean = config.after, after: boolean = balanceConfig.after,
cutoff: number = config.cutoff cutoff: number = balanceConfig.cutoff
) { ) {
if (after) return `${balance.toFixed(cutoff)}${symbol}`; if (after) return `${balance.toFixed(cutoff)}${symbol}`;
else return `${symbol}${balance.toFixed(cutoff)}`; else return `${symbol}${balance.toFixed(cutoff)}`;

View File

@ -2,16 +2,11 @@ import {
BaseCommandMessage, BaseCommandMessage,
CommandHandler CommandHandler
} from "../../commands/CommandHandler"; } from "../../commands/CommandHandler";
import { loadConfig } from "../../util/config";
import { ServiceAgent } from "../ServiceAgent"; import { ServiceAgent } from "../ServiceAgent";
import readline from "readline"; import readline from "readline";
import { MicroHandler } from "./MicroHandler"; import { MicroHandler } from "./MicroHandler";
import { Logger } from "../../util/Logger"; import { Logger } from "../../util/Logger";
const config = loadConfig("config/switchchat.yml", {
ownerOnly: false
});
export class ConsoleAgent extends ServiceAgent<readline.ReadLine> { export class ConsoleAgent extends ServiceAgent<readline.ReadLine> {
public desiredUser = { public desiredUser = {
name: "🟇 𝙎𝙪𝙥𝙚𝙧 Cosmic", name: "🟇 𝙎𝙪𝙥𝙚𝙧 Cosmic",

View File

@ -1,9 +1,7 @@
import EventEmitter from "events";
import { MPPAgent } from "./mpp"; import { MPPAgent } from "./mpp";
import env from "../util/env"; import env from "../util/env";
import { ServiceAgent } from "./ServiceAgent"; import { ServiceAgent } from "./ServiceAgent";
import { loadConfig } from "../util/config"; import { loadConfig } from "../util/config";
import { z } from "zod";
import { SwitchChatAgent } from "./switchchat"; import { SwitchChatAgent } from "./switchchat";
import { ConsoleAgent } from "./console"; import { ConsoleAgent } from "./console";

View File

@ -1,7 +1,6 @@
import { import {
BaseCommandMessage, BaseCommandMessage,
CommandHandler, CommandHandler
CommandMessage
} from "../../commands/CommandHandler"; } from "../../commands/CommandHandler";
import { loadConfig } from "../../util/config"; import { loadConfig } from "../../util/config";
import { ServiceAgent } from "../ServiceAgent"; import { ServiceAgent } from "../ServiceAgent";

View File

@ -1,6 +1,5 @@
import { existsSync, readFileSync, writeFileSync } from "fs"; import { existsSync, readFileSync, writeFileSync } from "fs";
import { parse, stringify } from "yaml"; import { parse, stringify } from "yaml";
import { z } from "zod";
/** /**
* Load a YAML config file and set default values if config path is nonexistent * Load a YAML config file and set default values if config path is nonexistent