Start working on roles

This commit is contained in:
Hri7566 2023-12-01 20:40:01 -05:00
parent 39cd018692
commit 747dde52db
9 changed files with 185 additions and 14 deletions

View File

@ -5,6 +5,6 @@ agents:
wss://mppclone.com:
- id: "✧𝓓𝓔𝓥 𝓡𝓸𝓸𝓶✧"
# - id: "Anime/Touhou & Modern"
- id: "test/awkward"
# - id: "test/awkward"
# - id: "wizardposting"
# - id: Room163249719601

26
config/roles.yml Normal file
View File

@ -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:
- "*"

View File

@ -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) {
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
name: msg.p.name,
role: Role.NONE
});
}
user = await readUser(msg.p._id);
if (!user)

View File

@ -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`;
}
);

View File

@ -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
);

View File

@ -1,6 +1,8 @@
import { loadCommands } from "./commands";
import { loadRoleConfig } from "./permissions";
import { ServiceLoader } from "./services";
loadRoleConfig();
loadCommands();
ServiceLoader.loadServices();

98
src/permissions/index.ts Normal file
View File

@ -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<Role, TRole>();
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<Role, TRole>;
let config: Record<Role, TRole>;
export function loadRoleConfig() {
config = loadConfig("config/roles.yml", defaultConfig);
console.log(config);
}

View File

@ -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 <expr> | /exit | /list | /view <index> | /unview";
return "Microcommands: /help | /js <expr> | /exit | /list | /view <index> | /unview | /admin+ <id>";
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;
}
}

View File

@ -78,6 +78,7 @@ export class ConsoleAgent extends ServiceAgent<readline.ReadLine> {
} else {
this.emit("send chat", message.a);
}
this.client.prompt();
});
}