-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathbot.py
More file actions
289 lines (249 loc) · 13.2 KB
/
bot.py
File metadata and controls
289 lines (249 loc) · 13.2 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# 𝕽𝕺𝕏𝖄•𝔹𝕒𝕤𝕚𝕔ℕ𝕖𝕖𝕕𝔹𝕠𝕥 ⚡️
# Created by: RoxyBasicNeedBot
# GitHub: https://github.com/RoxyBasicNeedBot
# Telegram: https://t.me/roxybasicneedbot1
# Website: https://roxybasicneedbot.unaux.com/?i=1
# YouTube: @roxybasicneedbot
# Instagram: roxybasicneedbot1
# Portfolio: https://aratt.ai/@roxybasicneedbot
#
# Bot & Website Developer 🤖
# Creator of Roxy BasicNeedBot & many automation tools ⚡
# Skilled in Python, APIs, and Web Development
#
# © 2025 RoxyBasicNeedBot. All Rights Reserved.
import asyncio
import logging
import requests
from datetime import datetime
from pyrogram import Client
from pyrogram.types import BotCommand
from pyrogram.errors import ChatWriteForbidden, ChatIdInvalid, ChannelPrivate
from config import RoxyBotConfig
from ROXYBASICNEEDBOT.modules.roxybot_database import roxybot_db
from ROXYBASICNEEDBOT.modules.roxybot_keepalive import roxybot_start_server
# Configure logging with detailed format
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - [%(filename)s:%(lineno)d] - %(message)s',
handlers=[
logging.StreamHandler(),
logging.FileHandler('roxybot.log', encoding='utf-8')
]
)
logger = logging.getLogger(__name__)
# ASCII Art Banner
ROXYBOT_BANNER = """
╔═══════════════════════════════════════════════════════════╗
║ ║
║ ██████╗ ██████╗ ██╗ ██╗██╗ ██╗ ██████╗ ██████╗ ║
║ ██╔══██╗██╔═══██╗╚██╗██╔╝╚██╗ ██╔╝ ██╔══██╗██╔═══██╗║
║ ██████╔╝██║ ██║ ╚███╔╝ ╚████╔╝ ██████╔╝██║ ██║║
║ ██╔══██╗██║ ██║ ██╔██╗ ╚██╔╝ ██╔══██╗██║ ██║║
║ ██║ ██║╚██████╔╝██╔╝ ██╗ ██║ ██████╔╝╚██████╔╝║
║ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ║
║ ║
║ 🤖 ROXY ZIP MAKER BOT ⚡ ║
║ Created by RoxyBasicNeedBot ║
║ Version 1.0.0 ║
║ ║
╚═══════════════════════════════════════════════════════════╝
"""
# 𝕽𝕺𝕏𝖄•𝔹𝕒𝕤𝕚𝕔ℕ𝕖𝕖𝕕𝔹𝕠𝕥 ⚡️
# Main Bot Application
class RoxyZipMakerBot:
"""Main Bot Class"""
def __init__(self):
logger.info("🔧 RoxyBot: Initializing bot configuration...")
# Validate configuration
RoxyBotConfig.roxybot_validate_config()
logger.info("✅ RoxyBot: Configuration validated successfully")
# Initialize Pyrogram Client
self.roxybot_app = Client(
name="RoxyZipMakerBot",
api_id=RoxyBotConfig.ROXYBOT_API_ID,
api_hash=RoxyBotConfig.ROXYBOT_API_HASH,
bot_token=RoxyBotConfig.ROXYBOT_BOT_TOKEN,
plugins=dict(root="ROXYBASICNEEDBOT/plugins"),
workdir="."
)
logger.info("✅ RoxyBot: Pyrogram client initialized")
logger.info(f"📂 RoxyBot: Plugins directory: ROXYBASICNEEDBOT/plugins")
def roxybot_clear_webhook(self):
"""Force clear webhook before starting"""
try:
url = f"https://api.telegram.org/bot{RoxyBotConfig.ROXYBOT_BOT_TOKEN}/deleteWebhook?drop_pending_updates=False"
response = requests.get(url, timeout=10)
result = response.json()
logger.info(f"🔄 RoxyBot: Webhook clear status: {result}")
return result.get('ok', False)
except Exception as e:
logger.error(f"⚠️ RoxyBot: Failed to clear webhook: {e}")
return False
async def roxybot_send_log_message(self, message: str):
"""Send message to log channel"""
log_channel = RoxyBotConfig.ROXYBOT_LOG_CHANNEL
if not log_channel or log_channel == 0:
logger.warning("⚠️ RoxyBot: LOG_CHANNEL not configured, skipping log message")
return False
try:
logger.info(f"📤 RoxyBot: Sending log message to channel {log_channel}")
await self.roxybot_app.send_message(
chat_id=log_channel,
text=message,
disable_web_page_preview=True
)
logger.info(f"✅ RoxyBot: Log message sent successfully to {log_channel}")
return True
except ChatWriteForbidden:
logger.error(f"❌ RoxyBot: Bot cannot write to log channel {log_channel}. Make sure bot is admin!")
except ChatIdInvalid:
logger.error(f"❌ RoxyBot: Invalid log channel ID: {log_channel}")
except ChannelPrivate:
logger.error(f"❌ RoxyBot: Log channel {log_channel} is private or bot was kicked")
except Exception as e:
logger.error(f"❌ RoxyBot: Failed to send log message: {type(e).__name__}: {e}")
return False
async def roxybot_start(self):
"""Start the bot"""
print(ROXYBOT_BANNER)
print("\n🔄 RoxyBot: Initializing...")
logger.info("=" * 60)
logger.info("🚀 RoxyBot: Starting Roxy Zip Maker Bot...")
logger.info("=" * 60)
# Clear webhook first
webhook_cleared = self.roxybot_clear_webhook()
logger.info(f"🔄 RoxyBot: Webhook cleared: {webhook_cleared}")
# Start Flask keep-alive server
roxybot_start_server()
logger.info("✅ RoxyBot: Keep-alive server started")
# Connect to database
db_connected = await roxybot_db.roxybot_connect()
if db_connected:
print("✅ RoxyBot: Database connected!")
logger.info("✅ RoxyBot: MongoDB database connected successfully")
else:
print("⚠️ RoxyBot: Database connection failed (will retry)")
logger.warning("⚠️ RoxyBot: MongoDB connection failed")
# Start bot
logger.info("🔄 RoxyBot: Starting Pyrogram client...")
await self.roxybot_app.start()
logger.info("✅ RoxyBot: Pyrogram client started successfully")
# Register Commands with Telegram (auto-appear in menu when user types /)
try:
# Define all bot commands
bot_commands = [
BotCommand("start", "⚡ Sᴛᴀʀᴛ Tʜᴇ Bᴏᴛ"),
BotCommand("help", "🆘 Gᴇᴛ Hᴇʟᴘ & Gᴜɪᴅᴇ"),
BotCommand("create", "📦 Cʀᴇᴀᴛᴇ Aʀᴄʜɪᴠᴇ (ZIP/RAR/7z)"),
BotCommand("files", "📁 Vɪᴇᴡ Qᴜᴇᴜᴇᴅ Fɪʟᴇꜱ"),
BotCommand("addthumb", "🖼️ Sᴇᴛ Cᴜꜱᴛᴏᴍ Tʜᴜᴍʙɴᴀɪʟ"),
BotCommand("delthumb", "🗑️ Dᴇʟᴇᴛᴇ Tʜᴜᴍʙɴᴀɪʟ"),
BotCommand("viewthumb", "👀 Vɪᴇᴡ Yᴏᴜʀ Tʜᴜᴍʙɴᴀɪʟ"),
BotCommand("stats", "📊 Bᴏᴛ Sᴛᴀᴛɪꜱᴛɪᴄꜱ"),
BotCommand("cancel", "🚫 Cᴀɴᴄᴇʟ Oᴘᴇʀᴀᴛɪᴏɴ"),
BotCommand("ban", "⛔ Bᴀɴ Uꜱᴇʀ (Aᴅᴍɪɴ)"),
BotCommand("unban", "✅ Uɴʙᴀɴ Uꜱᴇʀ (Aᴅᴍɪɴ)"),
BotCommand("cast", "📢 Bʀᴏᴀᴅᴄᴀꜱᴛ (Aᴅᴍɪɴ)")
]
# Register commands with specific scope
from pyrogram.types import BotCommandScopeAllPrivateChats
# Register using AllPrivateChats scope which is main one for bots
try:
await self.roxybot_app.set_bot_commands(
commands=bot_commands,
scope=BotCommandScopeAllPrivateChats()
)
logger.info(f"✅ RoxyBot: Commands registered with ScopeAllPrivateChats")
print(f"✅ RoxyBot: {len(bot_commands)} commands registered successfully!")
except Exception as e:
logger.warning(f"⚠️ RoxyBot: Start scope failed, trying default: {e}")
# Fallback to default
await self.roxybot_app.set_bot_commands(
commands=bot_commands
)
print(f"✅ RoxyBot: Commands registered (Default scope)")
except Exception as e:
logger.error(f"❌ RoxyBot: Failed to register commands: {type(e).__name__}: {e}", exc_info=True)
print(f"❌ RoxyBot: Command registration error: {e}")
# Get bot info
roxy_me = await self.roxybot_app.get_me()
logger.info(f"✅ RoxyBot: Bot info retrieved - @{roxy_me.username} (ID: {roxy_me.id})")
print("✅ RoxyBot: Bot started successfully!")
print(f"✅ RoxyBot: @{roxy_me.username} is now running!")
print(f"🆔 Bot ID: {roxy_me.id}")
# Send startup message to log channel
startup_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
startup_message = f"""
<b>🚀 ROXY ZIP MAKER BOT STARTED</b>
<blockquote>🤖 <b>Bot Info:</b>
├ Username: @{roxy_me.username}
├ Bot ID: <code>{roxy_me.id}</code>
├ Version: {RoxyBotConfig.ROXYBOT_VERSION}
└ Started: {startup_time}</blockquote>
<blockquote>📊 <b>System Status:</b>
├ Database: {'✅ Connected' if db_connected else '❌ Failed'}
├ Webhook: {'✅ Cleared' if webhook_cleared else '⚠️ Issue'}
└ Commands: ✅ Registered</blockquote>
<blockquote>⚡ RᴏxʏBᴀꜱɪᴄNᴇᴇᴅBᴏᴛ</blockquote>
"""
log_sent = await self.roxybot_send_log_message(startup_message)
if log_sent:
print("✅ RoxyBot: Startup message sent to log channel!")
else:
print("⚠️ RoxyBot: Could not send startup message to log channel")
# Check if plugins are loaded
if hasattr(self.roxybot_app, 'plugins'):
print(f"✅ RoxyBot: Plugins loaded from 'ROXYBASICNEEDBOT/plugins'")
logger.info("✅ RoxyBot: Plugins loaded successfully")
else:
print("⚠️ RoxyBot: Warning - Plugins may not be loaded!")
logger.warning("⚠️ RoxyBot: Plugins attribute not found")
print("\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
print("⚡ RoxyBasicNeedBot - Zip Maker Bot")
print("📱 Telegram: https://t.me/roxybasicneedbot1")
print("🌐 Website: https://roxybasicneedbot.unaux.com")
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
print("💡 Send /start to your bot to test!")
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n")
logger.info("=" * 60)
logger.info("🎉 RoxyBot: Bot is fully operational and waiting for messages!")
logger.info("=" * 60)
# Keep bot running
await asyncio.Event().wait()
async def roxybot_stop(self):
"""Stop the bot"""
print("\n🛑 RoxyBot: Stopping bot...")
logger.info("🛑 RoxyBot: Shutting down...")
# Send shutdown message to log channel
shutdown_message = f"""
🛑 **ROXY ZIP MAKER BOT STOPPED**
━━━━━━━━━━━━━━━━━━━━━━
📅 Stopped: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
━━━━━━━━━━━━━━━━━━━━━━
"""
await self.roxybot_send_log_message(shutdown_message)
await self.roxybot_app.stop()
print("✅ RoxyBot: Bot stopped successfully!")
logger.info("✅ RoxyBot: Bot stopped successfully")
# 𝕽𝕺𝕏𝖄•𝔹𝕒𝕤𝕚𝕔ℕ𝕖𝕖𝕕𝔹𝕠𝕥 ⚡️
# Main Entry Point
async def main():
"""Main function"""
logger.info("🔧 RoxyBot: Entering main function...")
roxybot = RoxyZipMakerBot()
try:
await roxybot.roxybot_start()
except KeyboardInterrupt:
logger.info("⌨️ RoxyBot: Keyboard interrupt received")
await roxybot.roxybot_stop()
except Exception as e:
logger.critical(f"❌ RoxyBot: Fatal error: {e}", exc_info=True)
print(f"❌ RoxyBot: Fatal error: {e}")
if __name__ == "__main__":
asyncio.run(main())
# 𝕽𝕺𝕏𝖄•𝔹𝕒𝕤𝕚𝕔ℕ𝕖𝕖𝕕𝔹𝕠𝕥 ⚡️
# Follow me on:
# YouTube: @roxybasicneedbot | Instagram: roxybasicneedbot1
# Telegram: https://t.me/roxybasicneedbot1
# © 2025 RoxyBasicNeedBot. All Rights Reserved.