forked from Desenvolvimento-de-Software/mslovelace_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Restrict.ts
161 lines (136 loc) · 4.09 KB
/
Restrict.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* Ada Lovelace Telegram Bot
*
* This file is part of Ada Lovelace Telegram Bot.
* You are free to modify and share this project or its files.
*
* @package mslovelace_bot
* @author Marcos Leandro <mleandrojr@yggdrasill.com.br>
* @license GPLv3 <http://www.gnu.org/licenses/gpl-3.0.en.html>
*/
import Command from "./Command.js";
import Context from "../library/telegram/context/Context.js";
import CommandContext from "../library/telegram/context/Command.js";
import BotCommand from "../library/telegram/type/BotCommand.js";
import ChatConfigs from "../model/ChatConfigs.js";
import ChatHelper from "../helper/Chat.js";
import Lang from "../helper/Lang.js";
export default class Restrict extends Command {
/**
* Commands list.
*
* @author Marcos Leandro
* @since 2024-05-03
*
* @var {BotCommand[]}
*/
public static readonly commands: BotCommand[] = [
{ command: "restrict", description: "Shows the new users restriction status. Manages new users from sending messages with [on | off]." }
];
/**
* The constructor.
*
* @author Marcos Leandro
* @since 1.0.0
*/
public constructor(context: Context) {
super(context);
this.setParams(["index", "on", "off"]);
}
/**
* Runs the command.
*
* @author Marcos Leandro
* @since 2023-06-13
*
* @param payload
*/
public async run(command: CommandContext): Promise<void> {
if (!await this.context.user.isAdmin()) {
return;
}
const params = command.getParams();
let action = "index";
if (params && params.length) {
action = this.isRegisteredParam(params[0]) ? params[0] : "index";
}
this.context.message.delete();
const method = action as keyof typeof Restrict.prototype;
await this[method](true as never);
}
/**
* Returns the restriction status.
*
* @author Marcos Leandro
* @since 2023-06-13
*/
private async index(): Promise<void> {
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
if (!chat || !chat.id) {
return;
}
const chatConfig = new ChatConfigs();
chatConfig
.select()
.where("chat_id").equal(chat.id);
const config = await chatConfig.execute();
if (!config.length) {
return;
}
Lang.set(chat.language || "us");
const restrictStatus = Lang.get(parseInt(config[0].restrict_new_users) === 1 ? "textEnabled" : "textDisabled");
const restrictMessage = Lang.get("restrictStatus").replace("{status}", restrictStatus);
this.context.chat.sendMessage(restrictMessage);
}
/**
* Activates the new users restriction.
*
* @author Marcos Leandro
* @since 2023-06-13
*
* @return {Promise<void>}
*/
private async on(): Promise<void> {
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
if (!chat || !chat.id) {
return;
}
const result = await this.update(chat.id, 1);
this.index();
}
/**
* Deactivates the new users restriction.
*
* @author Marcos Leandro
* @since 2023-06-13
*
* @return {Promise<void>}
*/
private async off(): Promise<void> {
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
if (!chat || !chat.id) {
return;
}
const result = await this.update(chat.id, 0);
this.index();
}
/**
* Updates the restriction status.
*
* @author Marcos Leandro
* @since 2023-06-13
*
* @param {number} chatId
* @param {number} status
*
* @return {Promise<any>}
*/
private async update(chatId: number, status: number): Promise<any> {
const chatConfig = new ChatConfigs();
chatConfig
.update()
.set("restrict_new_users", status)
.where("chat_id").equal(chatId);
return chatConfig.execute();
}
}