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,7 +1,8 @@
import { MatrixClient } from "matrix-js-sdk";
import type { ICallbackStore } from "../types.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;
@ -11,6 +12,11 @@ const registerModuleAdmin = (
) => {
client = matrixClient;
callbackStore.messageCallbacks.push({
startConditions: [`${config.app.triggerPrefix}setrole`],
allowedRoles: ["ADMIN"],
callbackFunc: onSetRole,
});
callbackStore.messageCallbacks.push({
startConditions: [`${config.app.triggerPrefix}shutdown`],
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")) {
client.sendTextMessage(roomId, "Saving data...");
await client.sendTextMessage(roomId, "Saving data...");
save();
}
const sendPromise = client.sendTextMessage(roomId, "Shutting down...");
sendPromise.finally(() => {
process.exit(0);
});
await client.sendTextMessage(roomId, "Shutting down...");
process.exit(0);
};
const onLoadData = (_text: string, roomId: string) => {
client.sendTextMessage(roomId, "Loading data...");
const onLoadData = async (_text: string, roomId: string) => {
await client.sendTextMessage(roomId, "Loading data...");
load();
};
const onSaveData = (_text: string, roomId: string) => {
client.sendTextMessage(roomId, "Saving data...");
const onSaveData = async (_text: string, roomId: string) => {
await client.sendTextMessage(roomId, "Saving data...");
save();
};

View file

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

View file

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