Skip to content

Commit e9863db

Browse files
committed
refactor: rename bot-admin role to admin and update related commands
1 parent ae69d6a commit e9863db

8 files changed

Lines changed: 21 additions & 35 deletions

File tree

src/commands/admin.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Message } from "../types/message";
2-
import { setBotAdmin } from "../components/services/user";
2+
import { setAdmin } from "../components/services/user";
33

44
export const info = {
55
command: "admin",
6-
description: "Grant or revoke bot-admin status to mentioned users.",
6+
description: "Grant or revoke admin (bot-level) status to mentioned users.",
77
usage: "admin <@user>",
88
example: "admin @user123",
99
role: "super-admin",
@@ -12,15 +12,15 @@ export const info = {
1212

1313
export default async function (msg: Message): Promise<void> {
1414
if (msg.mentionedIds.length === 0) {
15-
await msg.reply("Please mention a user to toggle bot-admin status.");
15+
await msg.reply("Please mention a user to grant or revoke admin.");
1616
return;
1717
}
1818

19+
const revoke = /--remove|--revoke|remove|revoke/i.test(msg.body);
1920
const lids = msg.mentionedIds.map((id) => id.split("@")[0]);
2021

2122
for (const lid of lids) {
22-
// Toggle: fetch current user and flip. To keep simple we'll set to true.
23-
await setBotAdmin(lid, true);
23+
await setAdmin(lid, !revoke);
2424
}
2525

2626
await msg.react("✅");

src/commands/block.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const info = {
66
description: "Block users from the bot.",
77
usage: "block <@user>",
88
example: "block @user123",
9-
role: "bot-admin",
9+
role: "admin",
1010
cooldown: 5000,
1111
};
1212

src/commands/help.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,6 @@ export default async function (msg: Message): Promise<void> {
9696
return;
9797
}
9898

99-
// help bot-admin
100-
if (/^bot-admin$/i.test(query)) {
101-
const botCommands = Object.values(commands)
102-
.filter((cmd: CommandType) => cmd.role === "bot-admin")
103-
.map((cmd: CommandType) => cmd.command)
104-
.sort((a, b) => a.localeCompare(b));
105-
await msg.reply(buildAdminPage(botCommands));
106-
return;
107-
}
108-
10999
if (!/^[1-9]\d*$/.test(query) && query != "") {
110100
await msg.reply("Please type a valid page number.");
111101
return;
@@ -125,7 +115,6 @@ export default async function (msg: Message): Promise<void> {
125115

126116
userCommands.unshift("admin");
127117
userCommands.unshift("super-admin");
128-
userCommands.unshift("bot-admin");
129118

130119
if (Object.values(commands).length === 0) {
131120
await msg.reply(`The *${page}* is obviously is not our bot bounds.`);

src/commands/reload.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const info = {
88
description: "Reload a specific command or all commands.",
99
usage: "reload | [command]",
1010
example: "reload ai",
11-
role: "bot-admin",
11+
role: "admin",
1212
cooldown: 5000,
1313
};
1414

src/commands/unblock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const info = {
77
description: "Unblock the users from the bot & rate limiter.",
88
usage: "unblock <@user>",
99
example: "unblock @user123",
10-
role: "bot-admin",
10+
role: "admin",
1111
cooldown: 5000,
1212
};
1313

src/commands/unload.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const info = {
88
description: "Unload a specific command.",
99
usage: "unload [command]",
1010
example: "unload ai",
11-
role: "bot-admin",
11+
role: "admin",
1212
cooldown: 5000,
1313
};
1414

src/components/events/message.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,12 @@ export default async function (msg: Message, type: string): Promise<void> {
233233
return;
234234
}
235235
}
236-
} else if (handler.role === "bot-admin") {
236+
} else if (handler.role === "admin") {
237237
if (!msg.fromMe) {
238+
// admin role now covers previously 'bot-admin' privileges (stored in redis)
238239
try {
239-
const { isBotAdmin } = await import("../services/user");
240-
const ok = await isBotAdmin(lid);
240+
const { isAdmin } = await import("../services/user");
241+
const ok = await isAdmin(lid);
241242
if (!ok) return;
242243
} catch (err) {
243244
return;

src/components/services/user.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -258,29 +258,25 @@ export async function unblockUser(lid: string): Promise<void> {
258258
}
259259
}
260260

261-
export async function setBotAdmin(lid: string, value: boolean): Promise<void> {
261+
export async function setAdmin(lid: string, value: boolean): Promise<void> {
262262
try {
263-
const key = `botadmin:${lid}`;
264-
if (value) {
265-
// set without TTL so privilege persists
266-
await redis.set(key, "1");
267-
} else {
268-
await redis.del(key);
269-
}
263+
const key = `admin:${lid}`;
264+
if (value) await redis.set(key, "1");
265+
else await redis.del(key);
270266
} catch (error) {
271267
Sentry.captureException(error);
272-
log.error("Redis", `Failed to set bot admin for: ${lid}`, error);
268+
log.error("Redis", `Failed to set admin for: ${lid}`, error);
273269
}
274270
}
275271

276-
export async function isBotAdmin(lid: string): Promise<boolean> {
272+
export async function isAdmin(lid: string): Promise<boolean> {
277273
try {
278-
const key = `botadmin:${lid}`;
274+
const key = `admin:${lid}`;
279275
const val = await redis.get(key);
280276
return val !== null;
281277
} catch (error) {
282278
Sentry.captureException(error);
283-
log.error("Redis", `Failed to check bot admin: ${lid}`, error);
279+
log.error("Redis", `Failed to check admin: ${lid}`, error);
284280
}
285281
return false;
286282
}

0 commit comments

Comments
 (0)