MINEOGO commited on
Commit
d65737b
·
verified ·
1 Parent(s): dcbc530

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -4
app.py CHANGED
@@ -1,7 +1,8 @@
1
  import discord
2
- import asyncio
3
  import os
4
  import json
 
 
5
  from dotenv import load_dotenv
6
  from huggingface_hub import InferenceClient
7
  from datetime import datetime
@@ -29,6 +30,8 @@ TOP_P = 0.95
29
  os.makedirs("data", exist_ok=True)
30
  CONVERSATION_LOG = "data/conversations.jsonl"
31
 
 
 
32
  async def get_ai_response(message_content):
33
  messages = [
34
  {"role": "system", "content": SYSTEM_PROMPT},
@@ -56,16 +59,63 @@ def save_conversation(user_message, bot_response):
56
  with open(CONVERSATION_LOG, "a", encoding="utf-8") as f:
57
  f.write(json.dumps(log_entry) + "\n")
58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  @bot.event
60
  async def on_ready():
61
  print(f"Bot is running in background as {bot.user}")
62
 
63
  @bot.event
64
  async def on_message(message):
65
- # Ignore bot's own messages
66
  if message.author == bot.user:
67
  return
68
 
 
 
 
 
 
 
69
  try:
70
  user_input = message.content
71
  response = await get_ai_response(user_input)
@@ -74,6 +124,13 @@ async def on_message(message):
74
  save_conversation(user_input, response)
75
  except Exception as e:
76
  print(f"Error responding to message: {e}")
 
77
 
78
- # Run the bot
79
- bot.run(TOKEN)
 
 
 
 
 
 
 
1
  import discord
 
2
  import os
3
  import json
4
+ import sys
5
+ import random
6
  from dotenv import load_dotenv
7
  from huggingface_hub import InferenceClient
8
  from datetime import datetime
 
30
  os.makedirs("data", exist_ok=True)
31
  CONVERSATION_LOG = "data/conversations.jsonl"
32
 
33
+ # === Functions ===
34
+
35
  async def get_ai_response(message_content):
36
  messages = [
37
  {"role": "system", "content": SYSTEM_PROMPT},
 
59
  with open(CONVERSATION_LOG, "a", encoding="utf-8") as f:
60
  f.write(json.dumps(log_entry) + "\n")
61
 
62
+ def generate_dynamic_command():
63
+ """Generate a command name and response dynamically"""
64
+ command_name = f"command_{random.randint(1000, 9999)}"
65
+ response_text = f"This is a dynamically generated command named '{command_name}'."
66
+
67
+ # Creating the new command and appending it to app.py
68
+ command_code = f"""
69
+ @bot.event
70
+ async def on_message(message):
71
+ if message.author == bot.user:
72
+ return
73
+
74
+ if message.content.startswith('!{command_name}'):
75
+ await message.channel.send("{response_text}")
76
+ """
77
+ try:
78
+ # Append to app.py
79
+ with open("app.py", "a", encoding="utf-8") as f:
80
+ f.write(command_code)
81
+ print(f"Successfully added the command '!{command_name}' to app.py")
82
+
83
+ # Optional: Reload bot after adding command
84
+ os.execv(sys.executable, ['python'] + sys.argv)
85
+
86
+ except Exception as e:
87
+ print(f"Error while adding command to app.py: {e}")
88
+
89
+ # === Error Handling and Diagnosis ===
90
+
91
+ def diagnose_and_save_error(error_message):
92
+ """Function to diagnose and log errors into app.py for future review"""
93
+ log_entry = {
94
+ "timestamp": datetime.utcnow().isoformat(),
95
+ "error_message": error_message
96
+ }
97
+ with open("app.py", "a", encoding="utf-8") as f:
98
+ f.write("\n# Error Log\n")
99
+ f.write(json.dumps(log_entry) + "\n")
100
+
101
+ # === Events ===
102
+
103
  @bot.event
104
  async def on_ready():
105
  print(f"Bot is running in background as {bot.user}")
106
 
107
  @bot.event
108
  async def on_message(message):
109
+ # Ignore own messages
110
  if message.author == bot.user:
111
  return
112
 
113
+ # Trigger to create a new dynamic command based on some interaction
114
+ if "generate" in message.content.lower(): # You can change the condition here
115
+ print("Generating a new command based on interaction!")
116
+ generate_dynamic_command()
117
+
118
+ # Example: normal AI chat
119
  try:
120
  user_input = message.content
121
  response = await get_ai_response(user_input)
 
124
  save_conversation(user_input, response)
125
  except Exception as e:
126
  print(f"Error responding to message: {e}")
127
+ diagnose_and_save_error(f"Error responding to message: {e}")
128
 
129
+ # === Start ===
130
+ try:
131
+ bot.run(TOKEN)
132
+ except Exception as e:
133
+ print(f"Bot crashed: {e}")
134
+ diagnose_and_save_error(f"Bot crashed: {e}")
135
+ # Optional: You can auto-restart after crash
136
+ # os.execv(sys.executable, ['python'] + sys.argv)