-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcommandAdmin.js
199 lines (169 loc) · 9.68 KB
/
commandAdmin.js
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import error_handling from './module/error_handling.js';
import get_database_telegram from './module/get_database_telegram.js';
export default async function commandAdmin(client, config) {
try {
// Command to send multiple photos to all specified users
client.command('sendphoto', async (ctx) => {
try {
const message_id = ctx?.message?.message_id;
// Check if the command is sent by the bot owner
if (!isBotOwner(ctx)) {
return await ctx.reply('This command can only be executed by the bot owner.', { parse_mode: 'HTML', reply_to_message_id: message_id });
}
const photo = ctx?.message?.reply_to_message?.photo?.[0] || ctx?.message?.photo?.[0];
if (!photo) return
const caption = ctx.message.caption || ctx.message.reply_to_message.caption || '';
const userType = ctx.message.text.split(' ')[1];
if (!userType || !['all', 'private', 'group', 'supergroup', 'channel'].includes(userType)) {
return await ctx.reply('Invalid user type. Usage: /sendphoto <userType>', { parse_mode: 'HTML', reply_to_message_id: message_id });
}
const users = await getDatabaseUsers(userType);
await sendMediaToAll(users, 'photo', photo, caption); // Pass photo array instead of single photo
} catch (error) {
console.log(error);
}
});
// Command to send multiple videos to all specified users
client.command('sendvideo', async (ctx) => {
try {
const message_id = ctx?.message?.message_id;
// Check if the command is sent by the bot owner
if (!isBotOwner(ctx)) {
return await ctx.reply('This command can only be executed by the bot owner.', { parse_mode: 'HTML', reply_to_message_id: message_id });
}
const video = ctx?.message?.reply_to_message?.video || ctx?.message?.video;
if (!video) return
const caption = ctx.message.caption || ctx.message.reply_to_message.caption || '';
const userType = ctx.message.text.split(' ')[1]; // Get the user type argument
if (!userType || !['all', 'private', 'group', 'supergroup', 'channel'].includes(userType)) {
return await ctx.reply('Invalid user type. Usage: /sendvideo <userType>', { parse_mode: 'HTML', reply_to_message_id: message_id });
}
const users = await getDatabaseUsers(userType);
await sendMediaToAll(users, 'video', video, caption); // Pass video as array with single item
} catch (error) {
console.log(error);
}
});
// Command to send multiple audio files to all specified users
client.command('sendaudio', async (ctx) => {
try {
const message_id = ctx?.message?.message_id;
// Check if the command is sent by the bot owner
if (!isBotOwner(ctx)) {
return await ctx.reply('This command can only be executed by the bot owner.', { parse_mode: 'HTML', reply_to_message_id: message_id });
}
const audio = ctx?.message?.reply_to_message?.audio || ctx?.message?.audio;
if (!audio) return
const caption = ctx.message.caption || ctx.message.reply_to_message.caption || '';
const userType = ctx.message.text.split(' ')[1]; // Get the user type argument
if (!userType || !['all', 'private', 'group', 'supergroup', 'channel'].includes(userType)) {
return await ctx.reply('Invalid user type. Usage: /sendaudio <userType>', { parse_mode: 'HTML', reply_to_message_id: message_id });
}
const users = await getDatabaseUsers(userType);
await sendMediaToAll(users, 'audio', audio, caption); // Pass audio as array with single item
} catch (error) {
console.log(error);
}
});
// Command to send multiple documents (PDFs) to all specified users
client.command('senddocument', async (ctx) => {
try {
const message_id = ctx?.message?.message_id;
// Check if the command is sent by the bot owner
if (!isBotOwner(ctx)) {
return await ctx.reply('This command can only be executed by the bot owner.', { parse_mode: 'HTML', reply_to_message_id: message_id });
}
const document = ctx?.message?.reply_to_message?.document || ctx?.message?.document;
if (!document) return
const caption = ctx.message.caption || ctx.message.reply_to_message.caption || '';
const userType = ctx.message.text.split(' ')[1]; // Get the user type argument
if (!userType || !['all', 'private', 'group', 'supergroup', 'channel'].includes(userType)) {
return await ctx.reply('Invalid user type. Usage: /senddocument <userType>', { parse_mode: 'HTML', reply_to_message_id: message_id });
}
const users = await getDatabaseUsers(userType);
await sendMediaToAll(users, 'document', document, caption); // Pass document as array with single item
} catch (error) {
console.log(error);
}
});
// Command to send text message to all specified users
client.command('sendtext', async (ctx) => {
try {
const message_id = ctx?.message?.message_id;
// Check if the command is sent by the bot owner
if (!isBotOwner(ctx)) {
return await ctx.reply('This command can only be executed by the bot owner.', { parse_mode: 'HTML', reply_to_message_id: message_id });
}
const message = ctx.message.text.split(' ').slice(2).join(' ') || ctx.message.reply_to_message?.text // Get text message from command
if (!message) return
const userType = ctx.message.text.split(' ')[1]; // Get the user type argument
if (!userType || !['all', 'private', 'group', 'supergroup', 'channel'].includes(userType)) {
return await ctx.reply('Invalid user type. Usage: /sendtext <userType>', { parse_mode: 'HTML', reply_to_message_id: message_id });
}
const users = await getDatabaseUsers(userType);
await sendMessageToAll(users, message); // Send text message to all users
} catch (error) {
console.log(error);
}
});
// Function to fetch users from database based on user type
async function getDatabaseUsers(userType) {
const GetAllUsers = await get_database_telegram(userType);
return GetAllUsers.filter(user => user?.permissions?.canSendMessages || user?.type === "private");
}
// Function to send multiple media files to all users
async function sendMediaToAll(users, type, media, caption = '') {
for (const user of users) {
try {
await sendMediaWithRetry(user.id, media.file_id, `send${type.charAt(0).toUpperCase() + type.slice(1)}`, caption);
} catch (error) {
await error_handling(error, client);
}
}
}
// Function to send text message to all users
async function sendMessageToAll(users, message) {
for (const user of users) {
try {
await sendMessageWithRetry(user.id, message, 'sendMessage');
} catch (error) {
await error_handling(error, client);
}
}
}
// Function to send media with retry mechanism
async function sendMediaWithRetry(chatId, media, method, caption = '') {
try {
await client.telegram[method](chatId, media, { parse_mode: 'HTML', caption });
} catch (error) {
if (error.response && error.response.ok === false && error.response.error_code === 504) {
console.log("Network timeout, retry after a delay (e.g., 5 seconds)");
setTimeout(() => sendMediaWithRetry(chatId, media, method, caption), 5000);
} else {
await error_handling(error, client);
}
}
}
// Function to send message with retry mechanism
async function sendMessageWithRetry(chatId, message, method) {
try {
await client.telegram[method](chatId, message, { parse_mode: 'HTML' });
} catch (error) {
if (error.response && error.response.ok === false && error.response.error_code === 504) {
console.log("Network timeout, retry after a delay (e.g., 5 seconds)");
setTimeout(() => sendMessageWithRetry(chatId, message, method), 5000);
} else {
await error_handling(error, client);
}
}
}
// Function to check if the command is sent by the bot owner
function isBotOwner(ctx) {
const BOT_OWNER_USERNAME = config.BOT_OWNER_USERNAME; // Replace with your bot's username
const commandSenderUsername = ctx.message.from.username.toLowerCase();
return commandSenderUsername === BOT_OWNER_USERNAME.toLowerCase();
}
} catch (error) {
console.log(error);
}
}