diff --git a/config/mpp_net_channels.yml b/config/mpp_net_channels.yml index b49d1f3..a659096 100644 --- a/config/mpp_net_channels.yml +++ b/config/mpp_net_channels.yml @@ -1,5 +1,5 @@ desiredUser: - name: "🟇 𝙎𝙪𝙥𝙚𝙧 Cosmic (*help)" + name: "🟇 𝙎𝙪𝙥𝙚𝙧 Cosmic" color: "#1d0054" agents: wss://mppclone.com: diff --git a/config/prefixes.yml b/config/prefixes.yml index 6f3d0d4..7ef975a 100644 --- a/config/prefixes.yml +++ b/config/prefixes.yml @@ -1,5 +1,5 @@ prefixes: - - id: cosmic - spaced: true - - id: "*" + - id: "**" spaced: false + - id: cdebug + spaced: true diff --git a/src/commands/CommandHandler.ts b/src/commands/CommandHandler.ts index fdba2d6..0412235 100644 --- a/src/commands/CommandHandler.ts +++ b/src/commands/CommandHandler.ts @@ -1,5 +1,5 @@ import { Inventory, Role, User } from "@prisma/client"; -import { createUser, readUser } from "../data/user"; +import { createUser, readUser, updateUser } from "../data/user"; import { ServiceAgent } from "../services/ServiceAgent"; import { Command } from "./Command"; import { CommandGroup } from "./CommandGroup"; @@ -12,13 +12,13 @@ import { loadConfig } from "../util/config"; const prefixConfig = loadConfig("config/prefixes.yml", { prefixes: [ - { - id: "cosmic", - spaced: true - }, { id: "*", spaced: false + }, + { + id: "cosmic", + spaced: true } ] }); @@ -77,6 +77,11 @@ export class CommandHandler { return "Somehow, something has gone terribly wrong and I can't create user data for you. I can't run your command now."; } + if (user.name !== msg.p.name) { + user.name = msg.p.name; + await updateUser(user); + } + // Get inventory data let inventory = await readInventory(msg.p._id); @@ -93,12 +98,15 @@ export class CommandHandler { } let usedPrefix: Prefix | undefined; + let isSpacedAndNonMatching = false; for (const prefix of this.prefixes) { if (msg.argv[0].startsWith(prefix.id)) { usedPrefix = prefix; if (prefix.spaced) { + if (prefix.id !== msg.argv[0]) + isSpacedAndNonMatching = true; msg.argv.splice(0, 1); msg.argc--; } @@ -108,6 +116,7 @@ export class CommandHandler { } if (!usedPrefix) return; + if (isSpacedAndNonMatching) return; let usedAlias = msg.argv[0]; if (!usedPrefix.spaced) diff --git a/src/commands/commands/utility/branch.ts b/src/commands/commands/utility/branch.ts new file mode 100644 index 0000000..cfc990b --- /dev/null +++ b/src/commands/commands/utility/branch.ts @@ -0,0 +1,12 @@ +import { getGitBranch } from "../../../util/git"; +import { Command } from "../../Command"; + +export const branch = new Command( + "branch", + ["branch"], + "get the git branch bozo", + "branch", + async (msg, agent) => { + return `Current git branch: \`${await getGitBranch()}\``; + } +); diff --git a/src/commands/commands/utility/permissions.ts b/src/commands/commands/utility/permissions.ts new file mode 100644 index 0000000..803ea73 --- /dev/null +++ b/src/commands/commands/utility/permissions.ts @@ -0,0 +1,28 @@ +import { + TRole, + fuzzyFindRoleByDisplayName, + getRole +} from "../../../permissions"; +import { Command } from "../../Command"; + +export const permissions = new Command( + "permissions", + ["permissions", "perms", "perm"], + "list permissions of a role bozo", + "permissions [role]", + (msg, agent) => { + const userRole = getRole(msg.user.role); + let role: TRole | undefined; + + if (userRole) role = userRole; + if (msg.argv[1]) role = fuzzyFindRoleByDisplayName(msg.argv[1]); + + if (role) { + return `Permissions for role "${ + role.displayName + }": ${role.permissions.join(" | ")}`; + } else { + return `No role found.`; + } + } +); diff --git a/src/commands/index.ts b/src/commands/index.ts index d943b33..f9d7d6f 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -14,6 +14,8 @@ import { role } from "./commands/utility/role"; import { ic } from "./commands/utility/ic"; import { uptime } from "./commands/utility/uptime"; import { balance } from "./commands/economy/balance"; +import { permissions } from "./commands/utility/permissions"; +import { branch } from "./commands/utility/branch"; export function loadCommands() { // cringe @@ -39,7 +41,9 @@ export function loadCommands() { color, role, ic, - uptime + uptime, + permissions, + branch ]); CommandHandler.addCommandGroup(utility); } diff --git a/src/permissions/index.ts b/src/permissions/index.ts index 3c86b0b..caf452f 100644 --- a/src/permissions/index.ts +++ b/src/permissions/index.ts @@ -88,3 +88,10 @@ export function loadRoleConfig() { export function getRole(role: Role) { return roles.get(role); } + +export function fuzzyFindRoleByDisplayName(fuzzy: string) { + for (const [key, val] of roles) { + if (val.displayName.toLowerCase().includes(fuzzy.toLowerCase())) + return val; + } +} diff --git a/src/services/index.ts b/src/services/index.ts index cc01c95..21ebd45 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -31,7 +31,7 @@ interface MPPNetConfig { const mppConfig = loadConfig("config/mpp_net_channels.yml", { desiredUser: { - name: "🟇 𝙎𝙪𝙥𝙚𝙧 Cosmic (*help)", + name: "🟇 𝙎𝙪𝙥𝙚𝙧 Cosmic", color: "#1d0054" }, agents: { diff --git a/src/services/mpp/index.ts b/src/services/mpp/index.ts index 8b5d793..39428e8 100644 --- a/src/services/mpp/index.ts +++ b/src/services/mpp/index.ts @@ -3,6 +3,7 @@ import { ServiceAgent } from "../ServiceAgent"; import { CommandHandler } from "../../commands/CommandHandler"; import { Cursor } from "./Cursor"; import { ChatMessage } from "../console/MicroHandler"; +import { help as helpCommand } from "../../commands/commands/general/help"; export class MPPAgent extends ServiceAgent { public cursor: Cursor; @@ -40,8 +41,10 @@ export class MPPAgent extends ServiceAgent { }); this.client.on("a", async msg => { + // Construct database ID const _id = "MPP_" + this.client.uri + "_" + msg.p._id; + // Emit chat event (used by microhandler) this.emit("chat", { m: "a", a: msg.a, @@ -54,8 +57,10 @@ export class MPPAgent extends ServiceAgent { originalMessage: msg } as ChatMessage); + // Construct command arguments let args = msg.a.split(" "); + // Run command and get output const str = await CommandHandler.handleCommand( { m: "command", @@ -73,6 +78,7 @@ export class MPPAgent extends ServiceAgent { this ); + // Send message in chat if (str) { if (typeof str == "string") { if (str.includes("\n")) { @@ -95,32 +101,62 @@ export class MPPAgent extends ServiceAgent { } }); - this.on("send chat", text => { - this.client.sendArray([ - { - m: "a", - message: `\u034f${text}` + this.on("send chat", (text: string) => { + // Send message in chat + if (text.length > 512) { + // Split every 512 chars + for (let i = 0; i < text.length; i += 512) { + let small = text.substring(i, i + 512); + + this.client.sendArray([ + { + m: "a", + message: `\u034f${small}` + } + ]); } - ]); + } else { + this.client.sendArray([ + { + m: "a", + message: `\u034f${text}` + } + ]); + } }); } public fixUser() { if (!this.client.user) return; + let after = ""; + + if (CommandHandler.prefixes[0]) + after = ` (${CommandHandler.prefixes[0].id}${ + CommandHandler.prefixes[0].spaced ? " " : "" + }${helpCommand.aliases[0]})`; + if ( - this.client.user.name !== this.desiredUser.name || + !this.client.user.name.startsWith(this.desiredUser.name + after) || this.client.user.color !== this.desiredUser.color ) { this.client.sendArray([ { m: "userset", - set: this.desiredUser + set: { + name: this.desiredUser.name + after, + color: this.desiredUser.color + } } ]); } } + /** + * Get a participant object with part of their name or user ID + * @param fuzzy Part of username or user ID + * @returns Found participant or undefined + */ public getParticipant(fuzzy: string) { for (const p of Object.values(this.client.ppl)) { if (!p._id.includes(fuzzy) && !p.name.includes(fuzzy)) return; diff --git a/src/util/git.ts b/src/util/git.ts new file mode 100644 index 0000000..402755d --- /dev/null +++ b/src/util/git.ts @@ -0,0 +1,17 @@ +import { exec } from "child_process"; + +// https://stackoverflow.com/questions/62225567/get-current-git-branch-with-node-js +export function getGitBranch() { + return new Promise((resolve, reject) => { + exec("git rev-parse --abbrev-ref HEAD", (err, stdout, stderr) => { + if (err || typeof stdout !== "string") reject(err); + + resolve(stdout.split("\n").join("")); + }); + }); +} + +export async function isMainBranch() { + const branch = await getGitBranch(); + return branch === "main" || branch === "master"; +}