From 747dde52dbd6b41a705e23cf06ca602b8fe3880c Mon Sep 17 00:00:00 2001 From: Hri7566 Date: Fri, 1 Dec 2023 20:40:01 -0500 Subject: [PATCH] Start working on roles --- config/mpp_net_channels.yml | 2 +- config/roles.yml | 26 +++++++ src/commands/CommandHandler.ts | 19 +++-- src/commands/commands/general/about.ts | 2 +- src/commands/commands/utility/id.ts | 11 +-- src/index.ts | 2 + src/permissions/index.ts | 98 ++++++++++++++++++++++++++ src/services/console/MicroHandler.ts | 38 +++++++++- src/services/console/index.ts | 1 + 9 files changed, 185 insertions(+), 14 deletions(-) create mode 100644 config/roles.yml create mode 100644 src/permissions/index.ts diff --git a/config/mpp_net_channels.yml b/config/mpp_net_channels.yml index 212b978..1d85c21 100644 --- a/config/mpp_net_channels.yml +++ b/config/mpp_net_channels.yml @@ -5,6 +5,6 @@ agents: wss://mppclone.com: - id: "βœ§π““π“”π“₯ π“‘π“Έπ“Έπ“Άβœ§" # - id: "Anime/Touhou & Modern" - - id: "test/awkward" + # - id: "test/awkward" # - id: "wizardposting" # - id: Room163249719601 diff --git a/config/roles.yml b/config/roles.yml new file mode 100644 index 0000000..d0597dc --- /dev/null +++ b/config/roles.yml @@ -0,0 +1,26 @@ +NONE: + displayName: None + permissions: + - cosmic.commandGroup.general + - cosmic.command.inventory + - cosmic.command.magic8ball + - cosmic.command.color + - cosmic.command.id + - cosmic.command.math + - cosmic.command.memory +MODERATOR: + displayName: Moderator + inherits: NONE + permissions: + - cosmic.commandGroup.economy + - cosmic.command.msg + - cosmic.command.memory +ADMINISTRATOR: + displayName: Administrator + inherits: MODERATOR + permissions: + - cosmic.commandGroup. +OWNER: + displayName: Owner + permissions: + - "*" diff --git a/src/commands/CommandHandler.ts b/src/commands/CommandHandler.ts index fa39e22..1852468 100644 --- a/src/commands/CommandHandler.ts +++ b/src/commands/CommandHandler.ts @@ -1,4 +1,4 @@ -import { Inventory, User } from "@prisma/client"; +import { Inventory, Role, User } from "@prisma/client"; import { createUser, readUser } from "../data/user"; import { ServiceAgent } from "../services/ServiceAgent"; import { Command } from "./Command"; @@ -52,12 +52,17 @@ export class CommandHandler { let user = await readUser(msg.p._id); if (!user) { - await createUser({ - id: msg.p._id, - platform: agent.platform, - platformId: msg.p.platformId, - name: msg.p.name - }); + let role = Role.NONE; + + if (agent.platform == "console" && msg.p._id == "console") { + await createUser({ + id: msg.p._id, + platform: agent.platform, + platformId: msg.p.platformId, + name: msg.p.name, + role: Role.NONE + }); + } user = await readUser(msg.p._id); if (!user) diff --git a/src/commands/commands/general/about.ts b/src/commands/commands/general/about.ts index f24be00..c0a3b6b 100644 --- a/src/commands/commands/general/about.ts +++ b/src/commands/commands/general/about.ts @@ -6,6 +6,6 @@ export const about = new Command( "get about bozo", "about", (msg, agent) => { - return `This is a dumb chat bot`; + return `πŸ’« This space bot was made by Hri7566.\nπŸš€ This bot is made possible by users like you. Thank you.\n🌌 Discord: @hri7566`; } ); diff --git a/src/commands/commands/utility/id.ts b/src/commands/commands/utility/id.ts index a12d687..a64f0ac 100644 --- a/src/commands/commands/utility/id.ts +++ b/src/commands/commands/utility/id.ts @@ -7,10 +7,13 @@ export const id = new Command( "get your id bozo", "id", (msg, agent) => { - if (!(agent as MPPAgent).client.isConnected) return; - return `ID: \`${(msg.originalMessage as any).p._id}\` Cosmic ID: \`${ - msg.p._id - }\``; + if (agent.platform == "mpp") { + return `ID: \`${ + (msg.originalMessage as any).p._id + }\` Cosmic ID: \`${msg.p._id}\``; + } else { + return `Cosmic ID: \`${msg.p._id}\``; + } }, false ); diff --git a/src/index.ts b/src/index.ts index aeb6ae7..9577e05 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,8 @@ import { loadCommands } from "./commands"; +import { loadRoleConfig } from "./permissions"; import { ServiceLoader } from "./services"; +loadRoleConfig(); loadCommands(); ServiceLoader.loadServices(); diff --git a/src/permissions/index.ts b/src/permissions/index.ts new file mode 100644 index 0000000..7df8919 --- /dev/null +++ b/src/permissions/index.ts @@ -0,0 +1,98 @@ +import { Role } from "@prisma/client"; +import { loadConfig } from "../util/config"; + +/** + * Check two permission strings to see if they match + * @param node1 Permission string + * @param node2 Permission string + */ +export function handlePermission(node1: string, node2: string) { + const hierarchy1 = node1.split("."); + const hierarchy2 = node2.split("."); + + for (let i = 0; i < hierarchy1.length; i++) { + if (i == hierarchy1.length - 1 || i == hierarchy2.length) { + if (hierarchy1[i] == hierarchy2[i]) return true; + if (hierarchy1[i] == "*") return true; + if (hierarchy2[i] == "*") return true; + } else { + if (hierarchy1[i] == hierarchy2[i]) continue; + if (hierarchy1[i] == "*") return true; + if (hierarchy2[i] == "*") return true; + } + + return false; + } + + return false; +} + +console.log(handlePermission("*", "*")); + +/** + * Check if a group has a permission + * @param role Prisma role + * @param permission Permission to check + */ +export function hasPermission(role: Role, permission: string) { + // TODO hasPermission + return; +} + +export const roles = new Map(); + +export type TRole = { + displayName: string; + permissions: string[]; +} & ( + | { + permissions: string[]; + } + | { + inherits: Role; + permissions?: string[]; + } +); + +let defaultConfig = { + NONE: { + displayName: "None", + permissions: [ + "cosmic.commandGroup.general", + + "cosmic.command.inventory", + + "cosmic.command.magic8ball", + + "cosmic.command.color", + "cosmic.command.id", + "cosmic.command.math", + "cosmic.command.memory" + ] + }, + MODERATOR: { + displayName: "Moderator", + inherits: "NONE", + permissions: [ + "cosmic.commandGroup.economy", + "cosmic.command.msg", + "cosmic.command.memory" + ] + }, + ADMINISTRATOR: { + displayName: "Administrator", + inherits: "MODERATOR", + permissions: ["cosmic.commandGroup."] + }, + OWNER: { + displayName: "Owner", + permissions: ["*"] + } +} as Record; + +let config: Record; + +export function loadRoleConfig() { + config = loadConfig("config/roles.yml", defaultConfig); + console.log(config); +} diff --git a/src/services/console/MicroHandler.ts b/src/services/console/MicroHandler.ts index 2a95935..20d7e23 100644 --- a/src/services/console/MicroHandler.ts +++ b/src/services/console/MicroHandler.ts @@ -1,7 +1,9 @@ +import { Role } from "@prisma/client"; import { ConsoleAgent } from "."; import { ServiceLoader } from ".."; import { scopedEval } from "../.."; import { BaseCommandMessage } from "../../commands/CommandHandler"; +import { readUser, updateUser } from "../../data/user"; import { CosmicColor } from "../../util/CosmicColor"; import { ServiceAgent } from "../ServiceAgent"; import { MPPAgent } from "../mpp"; @@ -53,7 +55,7 @@ export class MicroHandler { case "commands": case "cmds": default: - return "Microcommands: /help | /js | /exit | /list | /view | /unview"; + return "Microcommands: /help | /js | /exit | /list | /view | /unview | /admin+ "; break; case "js": case "eval": @@ -155,6 +157,40 @@ export class MicroHandler { p.color }, ${new CosmicColor(p.color).getName()})` )}`; + break; + case "admin+": + const userId = command.argv + .slice(1, command.argv.length) + .join(" "); + + let user = await readUser(userId); + if (!user) return "No such user."; + + user.role = Role.ADMINISTRATOR; + await updateUser(user); + + return `Made user "${user.name}" [${user.platformId.substring( + 0, + 6 + )}...] an administrator`; + + break; + case "admin-": + const userId2 = command.argv + .slice(1, command.argv.length) + .join(" "); + + let user2 = await readUser(userId2); + if (!user2) return "No such user."; + + user2.role = Role.NONE; + await updateUser(user2); + + return `Made user "${user2.name}" [${user2.platformId.substring( + 0, + 6 + )}...] a normal user.`; + break; } } diff --git a/src/services/console/index.ts b/src/services/console/index.ts index c14e15d..2138fe1 100644 --- a/src/services/console/index.ts +++ b/src/services/console/index.ts @@ -78,6 +78,7 @@ export class ConsoleAgent extends ServiceAgent { } else { this.emit("send chat", message.a); } + this.client.prompt(); }); }