-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathmiddleware.js
177 lines (160 loc) · 6.44 KB
/
middleware.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
// Impor modul dan dependensi yang diperlukan
const {
Cooldown,
monospace,
quote
} = require("@mengkodingan/ckptw");
// Fungsi untuk mengecek apakah pengguna memiliki cukup koin sebelum menggunakan perintah tertentu
async function checkCoin(requiredCoin, senderId) {
const userDb = await db.get(`user.${senderId}`) || {};
if (tools.general.isOwner(senderId) || userDb.premium) return false;
if ((userDb.coin || 0) < requiredCoin) return true;
await db.subtract(`user.${senderId}.coin`, requiredCoin);
return false;
}
// Middleware utama bot
module.exports = (bot) => {
bot.use(async (ctx, next) => {
// Variabel umum
const isGroup = ctx.isGroup();
const isPrivate = !isGroup;
const senderJid = ctx.sender.jid;
const senderId = tools.general.getID(senderJid);
const groupJid = isGroup ? ctx.id : null;
const groupId = isGroup ? tools.general.getID(groupJid) : null;
const isOwner = tools.general.isOwner(senderId);
// Mengambil data bot, pengguna, dan grup dari database
const botDb = await db.get("bot") || {};
const userDb = await db.get(`user.${senderId}`) || {};
const groupDb = await db.get(`group.${groupId}`) || {};
// Pengecekan mode bot (group, private, self) dan mode mute grup
if ((botDb.mode === "group" && !isGroup) || (botDb.mode === "private" && isGroup) || (botDb.mode === "self" && !isOwner)) return;
if (groupDb.mute && ctx.used.command !== "unmute") return;
if (config.system.autoTypingOnCmd) await ctx.simulateTyping(); // Simulasi mengetik jika diaktifkan dalam konfigurasi
// Menambah XP pengguna dan menangani level-up
const xpGain = 10;
const xpToLevelUp = 100;
let newUserXp = (userDb?.xp || 0) + xpGain;
if (newUserXp >= xpToLevelUp) {
let newUserLevel = (userDb?.level || 0) + 1;
newUserXp -= xpToLevelUp;
const profilePictureUrl = await ctx.core.profilePictureUrl(ctx.sender.jid, "image").catch(() => "https://i.pinimg.com/736x/70/dd/61/70dd612c65034b88ebf474a52ccc70c4.jpg");
if (userDb?.autolevelup) await ctx.reply({
text: `${quote(`Selamat! Kamu telah naik ke level ${newUserLevel}!`)}\n` +
`${config.msg.readmore}\n` +
quote(tools.msg.generateNotes([`Terganggu? Ketik ${monospace(`${ctx.used.prefix}setprofile autolevelup`)} untuk menonaktifkan pesan autolevelup.`])),
contextInfo: {
externalAdReply: {
mediaType: 1,
previewType: 0,
mediaUrl: config.bot.website,
title: config.msg.watermark,
renderLargerThumbnail: true,
thumbnailUrl: profilePictureUrl || config.bot.thumbnail,
sourceUrl: config.bot.website
}
}
});
await db.set(`user.${senderId}.xp`, newUserXp);
await db.set(`user.${senderId}.level`, newUserLevel);
} else {
await db.set(`user.${senderId}.xp`, newUserXp);
}
// Pengecekan kondisi pengguna
const restrictions = [{
condition: userDb.banned,
msg: config.msg.banned,
reaction: "🚫",
key: "banned",
},
{
condition: !isOwner && !userDb.premium && new Cooldown(ctx, config.system.cooldown).onCooldown,
msg: config.msg.cooldown,
reaction: "💤",
key: "cooldown",
},
{
condition: config.system.requireBotGroupMembership && ctx.used.command !== "botgroup" && !isOwner && !userDb.premium && !(await ctx.group(config.bot.groupJid).members()).some(member => tools.general.getID(member.id) === senderId),
msg: config.msg.botGroupMembership,
reaction: "🚫",
key: "requireBotGroupMembership",
}
];
for (const {
condition,
msg,
reaction,
key
}
of restrictions) {
if (condition) {
if (!userDb.hasSentMsg?.[key]) {
await ctx.reply(msg);
await db.set(`user.${senderId}.hasSentMsg.${key}`, true);
} else {
await ctx.react(ctx.id, reaction);
}
return;
}
}
// Pengecekan izin
const command = [...ctx.bot.cmd.values()].find(cmd => [cmd.name, ...(cmd.aliases || [])].includes(ctx.used.command));
if (!command) return next();
const {
permissions = {}
} = command;
const permissionChecks = [{
key: "admin",
condition: isGroup && !await ctx.group().isSenderAdmin(),
msg: config.msg.admin,
},
{
key: "botAdmin",
condition: isGroup && !await ctx.group().isBotAdmin(),
msg: config.msg.botAdmin,
},
{
key: "coin",
condition: permissions.coin && config.system.useCoin && await checkCoin(permissions.coin, senderId),
msg: config.msg.coin,
},
{
key: "group",
condition: !isGroup,
msg: config.msg.group,
},
{
key: "owner",
condition: !isOwner,
msg: config.msg.owner,
},
{
key: "premium",
condition: !isOwner && !userDb.premium,
msg: config.msg.premium,
},
{
key: "private",
condition: isGroup,
msg: config.msg.private,
},
{
key: "restrict",
condition: config.system.restrict,
msg: config.msg.restrict,
}
];
for (const {
key,
condition,
msg
}
of permissionChecks) {
if (permissions[key] && condition) {
await ctx.reply(msg);
return;
}
}
await next(); // Lanjut ke proses berikutnya jika semua kondisi pengguna dan izin terpenuhi
});
};