-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.py
More file actions
208 lines (165 loc) · 6.79 KB
/
Copy pathadmin.py
File metadata and controls
208 lines (165 loc) · 6.79 KB
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
200
201
202
203
204
205
206
207
208
"""Admin system for MulyonoW Bot using Supabase."""
from telegram import Update
from telegram.ext import ContextTypes
from utils.db import supabase
# Owner ID (hardcoded fallback)
OWNER_ID = 7265116685 # Ganti dengan user ID kamu
async def _is_admin(user_id: int) -> bool:
"""Check if user is admin via Supabase."""
if user_id == OWNER_ID:
return True
try:
result = supabase.table("bot_users").select("is_admin").eq("user_id", user_id).execute()
if result.data and result.data[0].get("is_admin"):
return True
except Exception:
pass
return False
# ── Admin Commands ─────────────────────────────────────────────────────
async def admin_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""/admin — Cek status admin & info bot."""
user = update.effective_user
is_admin = await _is_admin(user.id)
if user.id == OWNER_ID:
status = "OWNER"
elif is_admin:
status = "ADMIN"
else:
status = "USER"
await update.message.reply_text(
f"Admin Check\n\n"
f"Status: {status}\n"
f"User ID: {user.id}\n"
f"Username: @{user.username or 'N/A'}\n"
f"Name: {user.first_name} {user.last_name or ''}"
)
async def stats_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""/stats — Statistik bot (admin only)."""
user = update.effective_user
if not await _is_admin(user.id):
await update.message.reply_text("Command ini hanya untuk admin.")
return
try:
users_result = supabase.table("bot_users").select("user_id", count="exact").execute()
total_users = users_result.count or 0
banned_result = supabase.table("bot_users").select("user_id", count="exact").eq("is_banned", True).execute()
total_banned = banned_result.count or 0
stats_result = supabase.table("bot_stats").select("id", count="exact").execute()
total_commands = stats_result.count or 0
await update.message.reply_text(
f"Statistik MulyonoW Bot\n\n"
f"Total Users: {total_users}\n"
f"Banned: {total_banned}\n"
f"Total Commands: {total_commands}"
)
except Exception as e:
await update.message.reply_text(f"Error: {e}")
async def broadcast_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""/broadcast <pesan> — Kirim pesan ke semua user (admin only)."""
user = update.effective_user
if not await _is_admin(user.id):
await update.message.reply_text("Command ini hanya untuk admin.")
return
if not context.args:
await update.message.reply_text(
"Cara pakai: /broadcast <pesan>\n"
"Contoh: /broadcast Bot akan maintenance jam 22:00"
)
return
message = " ".join(context.args)
try:
result = supabase.table("bot_users").select("user_id").eq("is_banned", False).execute()
users = result.data or []
if not users:
await update.message.reply_text("Tidak ada user di database.")
return
sent = 0
failed = 0
bot = context.bot
await update.message.reply_text(f"Mengirim broadcast ke {len(users)} user...")
for u in users:
try:
await bot.send_message(
chat_id=u["user_id"],
text=f"BROADCAST\n\n{message}"
)
sent += 1
except Exception:
failed += 1
await update.message.reply_text(
f"Broadcast Selesai!\n\n"
f"Terkirim: {sent}\n"
f"Gagal: {failed}"
)
except Exception as e:
await update.message.reply_text(f"Error: {e}")
async def ban_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""/ban <user_id> — Ban user dari bot (admin only)."""
user = update.effective_user
if not await _is_admin(user.id):
await update.message.reply_text("Command ini hanya untuk admin.")
return
if not context.args:
await update.message.reply_text(
"Cara pakai: /ban <user_id>\n"
"Contoh: /ban 123456789"
)
return
try:
target_id = int(context.args[0])
except ValueError:
await update.message.reply_text("User ID harus angka.")
return
if target_id == OWNER_ID:
await update.message.reply_text("Tidak bisa ban owner!")
return
try:
existing = supabase.table("bot_users").select("user_id").eq("user_id", target_id).execute()
if not existing.data:
await update.message.reply_text(f"User {target_id} tidak ditemukan di database.")
return
supabase.table("bot_users").update({"is_banned": True}).eq("user_id", target_id).execute()
await update.message.reply_text(f"User {target_id} telah di-ban.")
except Exception as e:
await update.message.reply_text(f"Error: {e}")
async def unban_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""/unban <user_id> — Unban user (admin only)."""
user = update.effective_user
if not await _is_admin(user.id):
await update.message.reply_text("Command ini hanya untuk admin.")
return
if not context.args:
await update.message.reply_text("Cara pakai: /unban <user_id>\nContoh: /unban 123456789")
return
try:
target_id = int(context.args[0])
except ValueError:
await update.message.reply_text("User ID harus angka.")
return
try:
supabase.table("bot_users").update({"is_banned": False}).eq("user_id", target_id).execute()
await update.message.reply_text(f"User {target_id} telah di-unban.")
except Exception as e:
await update.message.reply_text(f"Error: {e}")
async def addadmin_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""/addadmin <user_id> — Tambah admin baru (owner only)."""
user = update.effective_user
if user.id != OWNER_ID:
await update.message.reply_text("Command ini hanya untuk owner.")
return
if not context.args:
await update.message.reply_text("Cara pakai: /addadmin <user_id>\nContoh: /addadmin 987654321")
return
try:
target_id = int(context.args[0])
except ValueError:
await update.message.reply_text("User ID harus angka.")
return
try:
supabase.table("bot_users").upsert({
"user_id": target_id,
"is_admin": True,
}).execute()
await update.message.reply_text(f"User {target_id} sekarang adalah admin.")
except Exception as e:
await update.message.reply_text(f"Error: {e}")