|
1 | | -import { Message } from "../types/message" |
| 1 | +import { Message } from "../types/message"; |
2 | 2 | import log from "../components/utils/log"; |
| 3 | +import { isAdmin, setAdmin } from "../components/services/user"; |
| 4 | +import client from "../components/client"; |
3 | 5 |
|
4 | 6 | export const info = { |
5 | 7 | command: "demote", |
6 | | - description: "Demote mentioned users from admin.", |
| 8 | + description: "Demote mentioned users from group or bot admin.", |
7 | 9 | usage: "demote <@user>", |
8 | 10 | example: "demote @user123", |
9 | | - role: "admin", |
| 11 | + role: "super-admin", |
10 | 12 | cooldown: 5000, |
11 | 13 | }; |
12 | 14 |
|
13 | 15 | export default async function (msg: Message): Promise<void> { |
| 16 | + const query = msg.body.replace(/^demote\b\s*/i, "").trim(); |
| 17 | + |
| 18 | + const bot = /--bot/i.test(query); |
| 19 | + const group = /--group/i.test(query); |
| 20 | + |
| 21 | + if (!bot && !group) { |
| 22 | + await msg.reply("Please provide a valid flag: `--bot` or `--admin`."); |
| 23 | + return; |
| 24 | + } |
| 25 | + |
14 | 26 | if (msg.mentionedIds.length === 0) { |
15 | 27 | await msg.reply("Please mention a user to demote."); |
16 | 28 | return; |
17 | 29 | } |
18 | 30 |
|
19 | | - const chat = await msg.getChat(); |
| 31 | + if (group) { |
| 32 | + const chat = await msg.getChat(); |
| 33 | + |
| 34 | + if (!chat.isGroup) { |
| 35 | + await msg.reply("This command only works in groups."); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + const groupChat = chat as any; |
20 | 40 |
|
21 | | - if (!chat.isGroup) { |
22 | | - await msg.reply("This command only works in groups."); |
| 41 | + try { |
| 42 | + for (const userId of msg.mentionedIds) { |
| 43 | + await groupChat.demoteParticipants([userId]); |
| 44 | + } |
| 45 | + } catch (err) { |
| 46 | + log.error("Demote", err); |
| 47 | + await msg.reply("Failed to demote user. Make sure I am an admin."); |
| 48 | + } |
23 | 49 | return; |
24 | 50 | } |
25 | 51 |
|
26 | | - const groupChat = chat as any; |
| 52 | + const jid = msg.mentionedIds[0]; |
| 53 | + const lid = jid.split("@")[0]; |
27 | 54 |
|
28 | | - try { |
29 | | - for (const userId of msg.mentionedIds) { |
30 | | - await groupChat.demoteParticipants([userId]); |
31 | | - } |
32 | | - } catch (err) { |
33 | | - log.error("Demote", err); |
34 | | - await msg.reply("Failed to demote user. Make sure I am an admin."); |
| 55 | + const isUserAdmin = await isAdmin(lid); |
| 56 | + if (isUserAdmin) { |
| 57 | + await Promise.allSettled([ |
| 58 | + setAdmin(lid, false), |
| 59 | + msg.reply("The user now demoted as bot admin."), |
| 60 | + ]); |
| 61 | + return; |
35 | 62 | } |
| 63 | + |
| 64 | + await msg.reply("The user is not an admin."); |
36 | 65 | } |
0 commit comments