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