forked from degenRobot/pyliza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
268 lines (210 loc) · 7.98 KB
/
main.py
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import os
import sys
import discord
from discord.ext import tasks
from fastapi import FastAPI
from twitter.twitterClient import TwitterClient
from twitter.twitterInteractions import TwitterInteractionHandler
from helpers import getResponse, prepareContext, log_message, reflectThoughts, getUserContext, updateUserContext, fetch_context
import chromadb
import time
from dotenv import load_dotenv
import config
import json
from scrape import updateContext
chroma_db_path = os.path.join(os.getcwd(), "chromadb")
chromaClient = chromadb.PersistentClient(path=chroma_db_path)
app = FastAPI()
load_dotenv()
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
class MyBot(discord.Client):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.bg_tasks = []
async def setup_hook(self):
# Start background tasks
self.bg_tasks.append(ponderThoughts.start())
self.bg_tasks.append(search_tweets.start())
self.bg_tasks.append(reply_guy.start())
self.bg_tasks.append(tweet_to_followers.start())
async def on_ready(self):
print(f'Bot logged in as {self.user}')
async def on_disconnect(self):
print("Bot disconnected. Attempting to reconnect...")
async def on_message(self, message):
if self.user.mentioned_in(message):
try:
context = ""
response = getResponse(message.content, additionalContext="")
await send_long_message(message.channel, response)
except Exception as e:
print(f"Error handling message: {e}")
await send_long_message(message.channel, "REEEEEEEEEE I'M BROKEN")
@app.get("/")
async def hello_fly():
return 'hello from fly.io'
def split_message(message, limit=2000):
"""Split a message into chunks of maximum length."""
if len(message) <= limit:
return [message]
chunks = []
while message:
if len(message) <= limit:
chunks.append(message)
break
# Find the last space before the limit
split_index = message.rfind(' ', 0, limit)
if split_index == -1:
split_index = limit
chunks.append(message[:split_index])
message = message[split_index:].lstrip()
return chunks
async def send_long_message(channel, message, updateChannel=""):
chunks = split_message(message)
for chunk in chunks:
await channel.send(chunk)
print(chunk)
def getCurrentThoughts():
thoughtProcess = json.load(open("initial_thoughts.json"))
thoughts = thoughtProcess["thought_process"]
return thoughts
@tasks.loop(seconds=config.ponderFrequency)
async def ponderThoughts():
last_tweet = json.load(open("last_tweet.json"))
last_tweet_time = last_tweet["last_tweet"]
print("Time since last tweet: ", time.time() - last_tweet_time)
if time.time() - last_tweet_time < config.postFrequency:
print("Not posting tweet, too soon...")
return
print("Updating context...")
thoughts = getCurrentThoughts()
updateContext(thoughtProcess=thoughts)
thoughts = getCurrentThoughts()
print("Pondering thoughts...")
try:
reflectThoughts(additionalContext=thoughts)
client = TwitterClient(
username=config.userName,
password=os.getenv('TWITTER_PASSWORD'),
email=os.getenv('TWITTER_EMAIL'),
)
### Log history to Chroma DB
#log_message(chromaClient, thoughts, "user")
print("Posting thoughts.....")
tweet = getResponse(config.postPrompt, additionalContext=thoughts)
print("Tweet: ", tweet)
client.send_tweet(tweet)
last_tweet["last_tweet"] = time.time()
with open("last_tweet.json", "w") as f:
json.dump(last_tweet, f)
except Exception as e:
print(f"Error: {e}")
@tasks.loop(seconds=config.postFrequency)
async def post_tweet():
last_tweet = json.load(open("last_tweet.json"))
last_tweet_time = last_tweet["last_tweet"]
if time.time() - last_tweet_time < config.postFrequency:
print("Not posting tweet, too soon...")
return
print("Posting tweet...")
context = prepareContext(getCurrentThoughts(), chromaClient)
try :
tweet = getResponse(config.postPrompt, additionalContext=context)
print("Tweet: ", tweet)
client = TwitterClient(
username=os.getenv('TWITTER_USERNAME'),
password=os.getenv('TWITTER_PASSWORD'),
email=os.getenv('TWITTER_EMAIL'),
)
### Log history to Chroma DB
log_message(chromaClient, tweet, "user")
client.send_tweet(tweet)
except Exception as e:
print(f"Error: {e}")
@tasks.loop(seconds=config.tweetToFollowersFrequency)
async def tweet_to_followers():
print("Tweeting to followers...")
try:
client = TwitterClient(
username=config.userName,
password=os.getenv('TWITTER_PASSWORD'),
email=os.getenv('TWITTER_EMAIL'),
chroma_client=chromaClient
)
interaction_handler = TwitterInteractionHandler(
client,
response_generator=getResponse,
chroma_client=chromaClient,
getUserContext=getUserContext,
updateUserContext=updateUserContext,
fetchContext=fetch_context
)
interaction_handler.tweet_to_followers()
except Exception as e:
print(f"Error: {e}")
@tasks.loop(seconds=config.replyGuyFrequency)
async def reply_guy():
print("Replying to reply guy targets...")
try:
client = TwitterClient(
username=os.getenv('TWITTER_USERNAME'),
password=os.getenv('TWITTER_PASSWORD'),
email=os.getenv('TWITTER_EMAIL'),
poll_interval=int(os.getenv('TWITTER_POLL_INTERVAL', 120)),
chroma_client=chromaClient
)
interaction_handler = TwitterInteractionHandler(
client,
response_generator=getResponse,
chroma_client=chromaClient,
search_terms=config.search_terms,
reply_targets=config.reply_targets,
getUserContext=getUserContext,
updateUserContext=updateUserContext,
fetchContext=fetch_context
)
context = prepareContext(getCurrentThoughts(), chromaClient)
interaction_handler.reply_guy(additionalContext=context)
except Exception as e:
print(f"Error: {e}")
@tasks.loop(seconds=config.searchFrequency)
async def search_tweets():
try:
client = TwitterClient(
username=os.getenv('TWITTER_USERNAME'),
password=os.getenv('TWITTER_PASSWORD'),
email=os.getenv('TWITTER_EMAIL'),
poll_interval=int(os.getenv('TWITTER_POLL_INTERVAL', 120)),
chroma_client=chromaClient,
)
interaction_handler = TwitterInteractionHandler(
client,
response_generator=getResponse,
chroma_client=chromaClient,
search_terms=config.search_terms,
getUserContext=getUserContext,
updateUserContext=updateUserContext,
fetchContext=fetch_context
)
context = prepareContext(getCurrentThoughts(), chromaClient)
interaction_handler.monitor_mentions(additionalContext=context)
except Exception as e:
print(f"Error: {e}")
# Create bot instance with reconnect enabled
intents = discord.Intents.default()
intents.message_content = True
bot = MyBot(intents=intents, reconnect=False)
# Error handling for the bot
@bot.event
async def on_error(event, *args, **kwargs):
print(f"Error in {event}: {sys.exc_info()}")
while True:
try:
bot.run(DISCORD_TOKEN)
except Exception as e:
print(f"Bot crashed with error: {e}")
# Create a new bot instance and run it
print("Restarting bot...")
time.sleep(10)
bot = MyBot(intents=discord.Intents.default(), reconnect=False)
bot.run(DISCORD_TOKEN)