Refactor monolithic bot into modular cog system with hot-reloading capabilities#3
Refactor monolithic bot into modular cog system with hot-reloading capabilities#3
Conversation
Co-authored-by: UN7X <163171152+UN7X@users.noreply.github.com>
| try: | ||
| # Attempt to compile and evaluate as an expression. | ||
| code_obj = compile(code_str, "<stdin>", "eval") | ||
| result = eval(code_obj, globals()) |
There was a problem hiding this comment.
security (python.lang.security.audit.eval-detected): Detected the use of eval(). eval() can be dangerous if used to evaluate dynamic content. If this content can be input from outside the program, this may be a code injection vulnerability. Ensure evaluated content is not definable by external sources.
Source: opengrep
| code_obj = compile(code_str, "<stdin>", "exec") | ||
| with io.StringIO() as buffer: | ||
| with contextlib.redirect_stdout(buffer): | ||
| exec(code_obj, globals()) |
There was a problem hiding this comment.
security (python.lang.security.audit.exec-detected): Detected the use of exec(). exec() can be dangerous if used to evaluate dynamic content. If this content can be input from outside the program, this may be a code injection vulnerability. Ensure evaluated content is not definable by external sources.
Source: opengrep
| try: | ||
| # First, try to compile as an expression. | ||
| compiled = compile(code, "<eval>", "eval") | ||
| result = eval(compiled, global_env) |
There was a problem hiding this comment.
security (python.lang.security.audit.eval-detected): Detected the use of eval(). eval() can be dangerous if used to evaluate dynamic content. If this content can be input from outside the program, this may be a code injection vulnerability. Ensure evaluated content is not definable by external sources.
Source: opengrep
| compiled = compile(code, "<exec>", "exec") | ||
| with io.StringIO() as buffer: | ||
| with contextlib.redirect_stdout(buffer): | ||
| exec(compiled, global_env) |
There was a problem hiding this comment.
security (python.lang.security.audit.exec-detected): Detected the use of exec(). exec() can be dangerous if used to evaluate dynamic content. If this content can be input from outside the program, this may be a code injection vulnerability. Ensure evaluated content is not definable by external sources.
Source: opengrep
| try: | ||
| # try expression first, then statements | ||
| compiled = compile(src, "<repl>", "eval") | ||
| result = eval(compiled, env) |
There was a problem hiding this comment.
security (python.lang.security.audit.eval-detected): Detected the use of eval(). eval() can be dangerous if used to evaluate dynamic content. If this content can be input from outside the program, this may be a code injection vulnerability. Ensure evaluated content is not definable by external sources.
Source: opengrep
| result = await result | ||
| except SyntaxError: | ||
| compiled = compile(src, "<repl>", "exec") | ||
| exec(compiled, env) |
There was a problem hiding this comment.
security (python.lang.security.audit.exec-detected): Detected the use of exec(). exec() can be dangerous if used to evaluate dynamic content. If this content can be input from outside the program, this may be a code injection vulnerability. Ensure evaluated content is not definable by external sources.
Source: opengrep
| try: | ||
| # Attempt to compile and evaluate as an expression. | ||
| code_obj = compile(code_str, "<stdin>", "eval") | ||
| result = eval(code_obj, globals()) |
There was a problem hiding this comment.
security (python.lang.security.audit.eval-detected): Detected the use of eval(). eval() can be dangerous if used to evaluate dynamic content. If this content can be input from outside the program, this may be a code injection vulnerability. Ensure evaluated content is not definable by external sources.
Source: opengrep
| code_obj = compile(code_str, "<stdin>", "exec") | ||
| with io.StringIO() as buffer: | ||
| with contextlib.redirect_stdout(buffer): | ||
| exec(code_obj, globals()) |
There was a problem hiding this comment.
security (python.lang.security.audit.exec-detected): Detected the use of exec(). exec() can be dangerous if used to evaluate dynamic content. If this content can be input from outside the program, this may be a code injection vulnerability. Ensure evaluated content is not definable by external sources.
Source: opengrep
| try: | ||
| # First, try to compile as an expression. | ||
| compiled = compile(code, "<eval>", "eval") | ||
| result = eval(compiled, global_env) |
There was a problem hiding this comment.
security (python.lang.security.audit.eval-detected): Detected the use of eval(). eval() can be dangerous if used to evaluate dynamic content. If this content can be input from outside the program, this may be a code injection vulnerability. Ensure evaluated content is not definable by external sources.
Source: opengrep
| compiled = compile(code, "<exec>", "exec") | ||
| with io.StringIO() as buffer: | ||
| with contextlib.redirect_stdout(buffer): | ||
| exec(compiled, global_env) |
There was a problem hiding this comment.
security (python.lang.security.audit.exec-detected): Detected the use of exec(). exec() can be dangerous if used to evaluate dynamic content. If this content can be input from outside the program, this may be a code injection vulnerability. Ensure evaluated content is not definable by external sources.
Source: opengrep
| try: | ||
| # try expression first, then statements | ||
| compiled = compile(src, "<repl>", "eval") | ||
| result = eval(compiled, env) |
There was a problem hiding this comment.
security (python.lang.security.audit.eval-detected): Detected the use of eval(). eval() can be dangerous if used to evaluate dynamic content. If this content can be input from outside the program, this may be a code injection vulnerability. Ensure evaluated content is not definable by external sources.
Source: opengrep
| result = await result | ||
| except SyntaxError: | ||
| compiled = compile(src, "<repl>", "exec") | ||
| exec(compiled, env) |
There was a problem hiding this comment.
security (python.lang.security.audit.exec-detected): Detected the use of exec(). exec() can be dangerous if used to evaluate dynamic content. If this content can be input from outside the program, this may be a code injection vulnerability. Ensure evaluated content is not definable by external sources.
Source: opengrep
Co-authored-by: UN7X <163171152+UN7X@users.noreply.github.com>
This PR completely restructures the 7xBot from a monolithic 1954-line file into a modular cog system that enables runtime reloading and selective feature toggling without requiring full bot restarts.
Problem Solved
The original
7xbot.pywas a single massive file that required restarting the entire Python process for any code changes or bug fixes. This caused downtime and made development difficult, especially when testing changes or temporarily disabling problematic features.Solution Overview
New Architecture
bot_main.py: New modular entry point with cog management systemcogs/: Organized modules for different bot functionalityrestart_bot.sh: External monitoring script for emergency full restartsCog Organization
admin.py: Owner commands (shutdown, eval, repl, debug)utility.py: Basic commands (tc, echo, man, beta management)points.py: Points system and shop functionalityevents.py: Event handlers (on_ready, on_message, status rotation)database.py: Shared database utilitiesNew Management Commands
Key Benefits
For Development
7/reload_cogFor Production
7/unload_cogExample Workflow
Backward Compatibility
The original
7xbot.pyfile is preserved unchanged as a backup. All existing commands continue to function identically, but now benefit from the modular architecture.Files Added/Modified
bot_main.py- New modular bot entry pointcogs/directory with 7 cog modulesrestart_bot.sh- External restart monitoringstart_bot.sh- Convenient startup script.env.example- Environment configuration template.gitignore- Added restart files and environment configTesting
Includes
demo_cog_system.pythat demonstrates all functionality without requiring Discord credentials, andtest_cogs.pyfor development testing.This transformation enables the bot to be "effectively rebooted" in portions as requested, while also providing the backup full restart capability via the external shell script monitoring system.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.