Add setrole command

This commit is contained in:
Aslan 2025-12-26 12:02:19 +01:00
parent 52916c4432
commit 735847bb63
4 changed files with 32 additions and 13 deletions

View file

@ -1,6 +1,6 @@
{ {
"name": "aslobot-matrix", "name": "aslobot-matrix",
"version": "0.5.1", "version": "0.5.2",
"description": "", "description": "",
"license": "ISC", "license": "ISC",
"author": "", "author": "",

View file

@ -1,7 +1,8 @@
import { MatrixClient } from "matrix-js-sdk"; import { MatrixClient } from "matrix-js-sdk";
import type { ICallbackStore } from "../types.js"; import type { ICallbackStore } from "../types.js";
import { config } from "../../config.js"; import { config } from "../../config.js";
import { load, save } from "../../store/store.js"; import { load, save, state } from "../../store/store.js";
import type { TRole } from "../../store/types.js";
let client: MatrixClient; let client: MatrixClient;
@ -11,6 +12,11 @@ const registerModuleAdmin = (
) => { ) => {
client = matrixClient; client = matrixClient;
callbackStore.messageCallbacks.push({
startConditions: [`${config.app.triggerPrefix}setrole`],
allowedRoles: ["ADMIN"],
callbackFunc: onSetRole,
});
callbackStore.messageCallbacks.push({ callbackStore.messageCallbacks.push({
startConditions: [`${config.app.triggerPrefix}shutdown`], startConditions: [`${config.app.triggerPrefix}shutdown`],
allowedRoles: ["ADMIN"], allowedRoles: ["ADMIN"],
@ -28,25 +34,38 @@ const registerModuleAdmin = (
}); });
}; };
const onShutdown = (text: string, roomId: string) => { const onSetRole = async (text: string, roomId: string) => {
const words = text.split(" ");
const userId = words[1];
const role = words[2] as TRole;
const user = state.users.find((user) => user.id === userId);
if (!user || !role) {
return;
}
user.role = role;
client.sendTextMessage(roomId, `Role for ${user} has been set to ${role}`);
};
const onShutdown = async (text: string, roomId: string) => {
if (!text.includes("nosave")) { if (!text.includes("nosave")) {
client.sendTextMessage(roomId, "Saving data..."); await client.sendTextMessage(roomId, "Saving data...");
save(); save();
} }
const sendPromise = client.sendTextMessage(roomId, "Shutting down..."); await client.sendTextMessage(roomId, "Shutting down...");
sendPromise.finally(() => { process.exit(0);
process.exit(0);
});
}; };
const onLoadData = (_text: string, roomId: string) => { const onLoadData = async (_text: string, roomId: string) => {
client.sendTextMessage(roomId, "Loading data..."); await client.sendTextMessage(roomId, "Loading data...");
load(); load();
}; };
const onSaveData = (_text: string, roomId: string) => { const onSaveData = async (_text: string, roomId: string) => {
client.sendTextMessage(roomId, "Saving data..."); await client.sendTextMessage(roomId, "Saving data...");
save(); save();
}; };

View file

@ -2,7 +2,6 @@ import { MatrixClient, MsgType } from "matrix-js-sdk";
import type { ICallbackStore } from "../types.js"; import type { ICallbackStore } from "../types.js";
import { config } from "../../config.js"; import { config } from "../../config.js";
import { getTextGemini, getImageGemini } from "../../services/ai/ai.js"; import { getTextGemini, getImageGemini } from "../../services/ai/ai.js";
import type { IAnimal } from "./types.js";
import { animals } from "./animals.js"; import { animals } from "./animals.js";
let client: MatrixClient; let client: MatrixClient;

View file

@ -82,6 +82,7 @@ const onHelp = (_text: string, roomId: string) => {
<hr/> <hr/>
<h3>Role: Admin</h3> <h3>Role: Admin</h3>
<ul> <ul>
<li><b>!setrole {user} {role}</b> - Set role for a user</li>
<li><b>!shutdown</b> - Shutdown bot</li> <li><b>!shutdown</b> - Shutdown bot</li>
</ul> </ul>
<h4>AsloBot version ${packageConfig.version}</h4>`, <h4>AsloBot version ${packageConfig.version}</h4>`,