Permissions & branch commands, git branch function, prefix QoL

This commit is contained in:
Hri7566 2023-12-09 17:00:36 -05:00
parent d3499d2bbd
commit c61c3daa2c
10 changed files with 132 additions and 19 deletions

View File

@ -1,5 +1,5 @@
desiredUser:
name: "🟇 𝙎𝙪𝙥𝙚𝙧 Cosmic (*help)"
name: "🟇 𝙎𝙪𝙥𝙚𝙧 Cosmic"
color: "#1d0054"
agents:
wss://mppclone.com:

View File

@ -1,5 +1,5 @@
prefixes:
- id: cosmic
spaced: true
- id: "*"
- id: "**"
spaced: false
- id: cdebug
spaced: true

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -31,7 +31,7 @@ interface MPPNetConfig {
const mppConfig = loadConfig<MPPNetConfig>("config/mpp_net_channels.yml", {
desiredUser: {
name: "🟇 𝙎𝙪𝙥𝙚𝙧 Cosmic (*help)",
name: "🟇 𝙎𝙪𝙥𝙚𝙧 Cosmic",
color: "#1d0054"
},
agents: {

View File

@ -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<Client> {
public cursor: Cursor;
@ -40,8 +41,10 @@ export class MPPAgent extends ServiceAgent<Client> {
});
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<Client> {
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<Client> {
this
);
// Send message in chat
if (str) {
if (typeof str == "string") {
if (str.includes("\n")) {
@ -95,32 +101,62 @@ export class MPPAgent extends ServiceAgent<Client> {
}
});
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;

17
src/util/git.ts Normal file
View File

@ -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";
}