Guest User

Untitled

a guest
Jan 8th, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. # main.py code
  2. from datetime import datetime
  3. from colorama import Fore, Style
  4. from telegram import Update
  5. from telegram.ext import Application, CommandHandler, CallbackContext
  6. from pathlib import Path
  7. import configparser
  8. import os
  9.  
  10. bot_name = "NullByte_bot"
  11.  
  12.  
  13. async def help(update: Update, context: CallbackContext):
  14. await update.message.reply_text("""Text """)
  15.  
  16.  
  17. async def error_handler(update: Update, context: CallbackContext) -> None:
  18. await context.bot.send_message(update.effective_chat.id, text="An error occurred.")
  19.  
  20.  
  21. def load_modules():
  22. modules = []
  23. for module_name in os.listdir("modules"):
  24. if not os.path.isdir(os.path.join("modules", module_name)):
  25. continue
  26. modules.append(module_name)
  27. return modules
  28.  
  29.  
  30. def load_handlers(module):
  31. handlers = []
  32. module_path = f"modules.{module}"
  33. for command_file in os.listdir(os.path.join("modules", module)):
  34. if command_file.endswith(".py") and not command_file.startswith("__"):
  35. command_name = command_file[:-3] # Remove the ".py" extension
  36. handlers.append((command_name, module_path))
  37. return handlers
  38.  
  39.  
  40. class Code:
  41. def __init__(self, token):
  42. self.application = Application.builder().token(token).build()
  43.  
  44. @staticmethod
  45. def read_token():
  46. config = configparser.ConfigParser()
  47. config_path = Path(__file__).parent / 'setup' / 'config.ini'
  48. config.read(config_path)
  49. return config['Telegram']['Token']
  50.  
  51. def run_bot(self):
  52. modules = load_modules()
  53.  
  54. for module in modules:
  55. handlers = load_handlers(module)
  56.  
  57. for command, module_path in handlers:
  58. module_full_path = f"{module_path}.{command}"
  59. module_obj = __import__(module_full_path, fromlist=[command])
  60. handler_function = getattr(module_obj, f"{command}_handler")
  61. self.application.add_handler(CommandHandler(command, handler_function))
  62.  
  63. self.application.add_handler(CommandHandler('help', help))
  64. self.application.add_error_handler(error_handler)
  65. current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  66. 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}")
  67. self.application.run_polling()
  68.  
  69.  
  70. if __name__ == "__main__":
  71. directory_path = 'setup'
  72. config_file_name = 'config.ini'
  73.  
  74. config_file_path = os.path.join(directory_path, config_file_name)
  75.  
  76. if os.path.exists(config_file_path):
  77. TOKEN = Code.read_token()
  78.  
  79. if TOKEN:
  80. Code(TOKEN).run_bot()
  81. else:
  82. print(f"Please enter a token in {config_file_path}")
  83. else:
  84. configer = configparser.ConfigParser()
  85. configer['Telegram'] = {'Token': ''}
  86.  
  87. with open(config_file_path, 'w') as config_file:
  88. configer.write(config_file)
  89. print("Created default config.ini file. Please pass a token")
  90.  
  91. # this is code from kick.py inside moderation directory in 'modules'
  92.  
  93. # kick.py
  94.  
  95. from telegram import Update
  96. from telegram.ext import CallbackContext
  97. import asyncio
  98.  
  99.  
  100. async def kick_handler(update: Update, context: CallbackContext):
  101. await update.message.reply_text(f"User has been kicked!")
  102.  
Advertisement
Add Comment
Please, Sign In to add comment