-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbot.py
More file actions
196 lines (167 loc) · 8.53 KB
/
Copy pathbot.py
File metadata and controls
196 lines (167 loc) · 8.53 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
# ═══════════════════════════════════════════════════════════════
# 𝕽𝕺𝕏𝖄•𝔹𝕒𝕤𝕚𝕔ℕ𝕖𝕖𝕕𝔹𝕠𝕥 ⚡️
# Created by: RoxyBasicNeedBot
# GitHub: https://github.com/RoxyBasicNeedBot
# Telegram: https://t.me/roxybasicneedbot1
# Website: https://roxybasicneedbot.unaux.com/?i=1
# YouTube: @roxybasicneedbot
#
# Portfolio: https://aratt.ai/@roxybasicneedbot
#
# Bot & Website Developer 🤖
# Creator of RoxyBasicNeedBot & many automation tools ⚡
# Skilled in Python, APIs, and Web Development
#
# © 2026 RoxyBasicNeedBot. All Rights Reserved.
# ═══════════════════════════════════════════════════════════════
from pyrogram import Client, filters
from pyrogram.enums import ParseMode
from pyrogram.types import Message
from config import (
API_ID,
API_HASH,
BOT_TOKEN,
ADMIN_IDS,
MONGODB_URL,
DB_NAME,
USERS_COLLECTION
)
from ROXYBASICNEEDBOT.database.mongodb import RoxyBotDB
import logging
import os
import asyncio
import pymongo
from datetime import datetime, timedelta
# Import commands and handlers
from ROXYBASICNEEDBOT.commands.start import roxybot_start_command
from ROXYBASICNEEDBOT.commands.admin import roxybot_broadcast_command
from ROXYBASICNEEDBOT.commands.stats_command import roxybot_stats_command
from ROXYBASICNEEDBOT.commands.user_info import roxybot_id_command
from ROXYBASICNEEDBOT.commands.moderation import (
roxybot_ban_command,
roxybot_unban_command,
roxybot_banned_list
)
from ROXYBASICNEEDBOT.handlers.callbacks import roxybot_handle_callback, roxybot_handle_inline
from ROXYBASICNEEDBOT.utils.message_handlers import (
roxybot_handle_forwarded,
roxybot_handle_message
)
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
roxybot_logger = logging.getLogger(__name__)
class RoxyBasicNeedBot:
def __init__(self):
# Initialize Pyrogram client
self.roxybot_client = Client(
"chat_id_bot",
api_id=API_ID,
api_hash=API_HASH,
bot_token=BOT_TOKEN,
parse_mode=ParseMode.HTML
)
# Initialize database
self.roxybot_db = RoxyBotDB()
self.roxybot_setup_mongodb()
# Register message handlers
self.roxybot_register_handlers()
def roxybot_setup_mongodb(self):
"""Initialize MongoDB connection and indexes"""
try:
self.roxybot_mongo_client = pymongo.MongoClient(MONGODB_URL)
self.roxybot_db_connection = self.roxybot_mongo_client[DB_NAME]
self.roxybot_users_collection = self.roxybot_db_connection[USERS_COLLECTION]
# Create index on user_id to ensure uniqueness
self.roxybot_users_collection.create_index("user_id", unique=True)
roxybot_logger.info("RoxyBot: MongoDB connection established successfully")
except Exception as e:
roxybot_logger.error(f"RoxyBot: Error connecting to MongoDB: {e}")
raise
def roxybot_register_handlers(self):
"""Register all message handlers"""
# Import additional commands
from ROXYBASICNEEDBOT.commands.help import roxybot_help_command
from ROXYBASICNEEDBOT.commands.utility import roxybot_ping_command, roxybot_restart_command
# Command handlers
self.roxybot_client.on_message(filters.command("start"))(roxybot_start_command)
self.roxybot_client.on_message(filters.command("help"))(roxybot_help_command)
self.roxybot_client.on_message(filters.command("ping"))(roxybot_ping_command)
self.roxybot_client.on_message(filters.command("restart") & filters.user(ADMIN_IDS))(roxybot_restart_command)
self.roxybot_client.on_message(filters.command(["broadcast", "bcast"]) & filters.user(ADMIN_IDS))(roxybot_broadcast_command)
self.roxybot_client.on_message(filters.command(["stats", "statistics"]) & filters.user(ADMIN_IDS))(roxybot_stats_command)
self.roxybot_client.on_message(filters.command("id"))(roxybot_id_command)
# Admin commands
self.roxybot_client.on_message(filters.command("ban") & filters.user(ADMIN_IDS))(roxybot_ban_command)
self.roxybot_client.on_message(filters.command("unban") & filters.user(ADMIN_IDS))(roxybot_unban_command)
self.roxybot_client.on_message(filters.command("banned") & filters.user(ADMIN_IDS))(roxybot_banned_list)
# Message handlers
self.roxybot_client.on_message(filters.private & filters.forwarded)(roxybot_handle_forwarded)
self.roxybot_client.on_message(filters.private)(roxybot_handle_message)
# Callback and inline handlers
self.roxybot_client.on_callback_query()(roxybot_handle_callback)
self.roxybot_client.on_inline_query()(roxybot_handle_inline)
roxybot_logger.info("RoxyBot: All handlers registered successfully")
async def roxybot_set_bot_commands(self):
"""Register bot commands with Telegram for private chats"""
from pyrogram.types import BotCommand, BotCommandScopeAllPrivateChats, BotCommandScopeChat
# Commands visible to all users in private chats
roxybot_user_commands = [
BotCommand("start", "🚀 Start the bot"),
BotCommand("help", "❓ Show help message"),
BotCommand("ping", "🏓 Check bot status"),
BotCommand("id", "🆔 Get user ID by username"),
]
# Additional commands for admins
roxybot_admin_commands = roxybot_user_commands + [
BotCommand("broadcast", "📢 Broadcast message to all users"),
BotCommand("stats", "📊 View bot statistics"),
BotCommand("ban", "🚫 Ban a user"),
BotCommand("unban", "✅ Unban a user"),
BotCommand("banned", "📋 List banned users"),
BotCommand("restart", "🔄 Restart the bot"),
]
try:
# Register commands with AllPrivateChats scope (main scope for bots)
await self.roxybot_client.set_bot_commands(
commands=roxybot_user_commands,
scope=BotCommandScopeAllPrivateChats()
)
roxybot_logger.info("RoxyBot: User commands registered for all private chats")
# Set admin commands for each admin using BotCommandScopeChat
for roxybot_admin_id in ADMIN_IDS:
try:
await self.roxybot_client.set_bot_commands(
commands=roxybot_admin_commands,
scope=BotCommandScopeChat(chat_id=roxybot_admin_id)
)
roxybot_logger.info(f"RoxyBot: Admin commands registered for user {roxybot_admin_id}")
except Exception as e:
roxybot_logger.warning(f"RoxyBot: Could not set admin commands for {roxybot_admin_id}: {e}")
except Exception as e:
roxybot_logger.error(f"RoxyBot: Error setting bot commands: {e}")
def roxybot_run(self):
"""Start the bot"""
try:
roxybot_logger.info("Starting RoxyBasicNeedBot...")
# Register commands on startup
self.roxybot_client.start()
self.roxybot_client.loop.run_until_complete(self.roxybot_set_bot_commands())
roxybot_logger.info("RoxyBot: Bot commands registered with Telegram")
# Keep bot running
from pyrogram import idle
idle()
self.roxybot_client.stop()
except Exception as e:
roxybot_logger.error(f"RoxyBot: Error starting bot: {e}")
raise
if __name__ == "__main__":
# Create and run RoxyBasicNeedBot instance
roxybot_instance = RoxyBasicNeedBot()
roxybot_instance.roxybot_run()
# ═══════════════════════════════════════════════════════════════
# 𝕽𝕺𝕏𝖄•𝔹𝕒𝕤𝕚𝕔ℕ𝕖𝕖𝕕𝔹𝕠𝕥 ⚡️
# © 2026 RoxyBasicNeedBot. All Rights Reserved.
# ═══════════════════════════════════════════════════════════════