Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # main.py code
- from datetime import datetime
- from colorama import Fore, Style
- from telegram import Update
- from telegram.ext import Application, CommandHandler, CallbackContext
- from pathlib import Path
- import configparser
- import os
- bot_name = "NullByte_bot"
- async def help(update: Update, context: CallbackContext):
- await update.message.reply_text("""Text """)
- async def error_handler(update: Update, context: CallbackContext) -> None:
- await context.bot.send_message(update.effective_chat.id, text="An error occurred.")
- def load_modules():
- modules = []
- for module_name in os.listdir("modules"):
- if not os.path.isdir(os.path.join("modules", module_name)):
- continue
- modules.append(module_name)
- return modules
- def load_handlers(module):
- handlers = []
- module_path = f"modules.{module}"
- for command_file in os.listdir(os.path.join("modules", module)):
- if command_file.endswith(".py") and not command_file.startswith("__"):
- command_name = command_file[:-3] # Remove the ".py" extension
- handlers.append((command_name, module_path))
- return handlers
- class Code:
- def __init__(self, token):
- self.application = Application.builder().token(token).build()
- @staticmethod
- def read_token():
- config = configparser.ConfigParser()
- config_path = Path(__file__).parent / 'setup' / 'config.ini'
- config.read(config_path)
- return config['Telegram']['Token']
- def run_bot(self):
- modules = load_modules()
- for module in modules:
- handlers = load_handlers(module)
- for command, module_path in handlers:
- module_full_path = f"{module_path}.{command}"
- module_obj = __import__(module_full_path, fromlist=[command])
- handler_function = getattr(module_obj, f"{command}_handler")
- self.application.add_handler(CommandHandler(command, handler_function))
- self.application.add_handler(CommandHandler('help', help))
- self.application.add_error_handler(error_handler)
- current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
- print(f"{Fore.WHITE}[{Fore.BLUE}*{Fore.WHITE}] {Fore.GREEN}Logged in as '{Fore.MAGENTA}{bot_name}' {Fore.GREEN}| {Fore.WHITE}{current_time}{Style.RESET_ALL}")
- self.application.run_polling()
- if __name__ == "__main__":
- directory_path = 'setup'
- config_file_name = 'config.ini'
- config_file_path = os.path.join(directory_path, config_file_name)
- if os.path.exists(config_file_path):
- TOKEN = Code.read_token()
- if TOKEN:
- Code(TOKEN).run_bot()
- else:
- print(f"Please enter a token in {config_file_path}")
- else:
- configer = configparser.ConfigParser()
- configer['Telegram'] = {'Token': ''}
- with open(config_file_path, 'w') as config_file:
- configer.write(config_file)
- print("Created default config.ini file. Please pass a token")
- # this is code from kick.py inside moderation directory in 'modules'
- # kick.py
- from telegram import Update
- from telegram.ext import CallbackContext
- import asyncio
- async def kick_handler(update: Update, context: CallbackContext):
- await update.message.reply_text(f"User has been kicked!")
Advertisement
Add Comment
Please, Sign In to add comment