forked from johndotpub/DiscordianAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
435 lines (370 loc) · 14.5 KB
/
bot.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# Standard library imports
import argparse
import asyncio
import configparser
import logging
import os
import re
import sys
import time
from logging.handlers import RotatingFileHandler
# Third-party imports
import discord
from openai import OpenAI
from websockets.exceptions import ConnectionClosed
# Define the function to parse command-line arguments
def parse_arguments() -> argparse.Namespace:
try:
parser = argparse.ArgumentParser(description='GPT-based Discord bot.')
parser.add_argument('--conf', help='Configuration file path')
args = parser.parse_args()
return args
except Exception as e:
logger.error(f"Error parsing arguments: {e}")
raise
# Define the function to load the configuration
def load_configuration(config_file: str) -> configparser.ConfigParser:
try:
config = configparser.ConfigParser()
# Check if the configuration file exists
if os.path.exists(config_file):
config.read(config_file)
else:
# Fall back to environment variables
config.read_dict(
{section: dict(os.environ) for section in config.sections()}
)
return config
except Exception as e:
logger.error(f"Error loading configuration: {e}")
raise
def set_activity_status(
activity_type: str,
activity_status: str
) -> discord.Activity:
"""
Return discord.Activity object with specified activity type and status
"""
try:
activity_types = {
'playing': discord.ActivityType.playing,
'streaming': discord.ActivityType.streaming,
'listening': discord.ActivityType.listening,
'watching': discord.ActivityType.watching,
'custom': discord.ActivityType.custom,
'competing': discord.ActivityType.competing
}
return discord.Activity(
type=activity_types.get(
activity_type, discord.ActivityType.listening
),
name=activity_status
)
except Exception as e:
logger.error(f"Error setting activity status: {e}")
raise
# Define the function to get the conversation summary
def get_conversation_summary(conversation: list[dict]) -> list[dict]:
"""
Conversation summary from combining user messages and assistant responses
"""
try:
summary = []
user_messages = [
message for message in conversation if message["role"] == "user"
]
assistant_responses = [
message for message in conversation if message["role"] == "assistant"
]
# Combine user messages and assistant responses into a summary
for user_message, assistant_response in zip(
user_messages, assistant_responses
):
summary.append(user_message)
summary.append(assistant_response)
return summary
except Exception as e:
logger.error(f"Error getting conversation summary: {e}")
raise
async def check_rate_limit(
user: discord.User,
logger: logging.Logger = None
) -> bool:
"""
Check if a user has exceeded the rate limit for sending messages.
"""
if logger is None:
logger = logging.getLogger(__name__)
try:
current_time = time.time()
last_command_timestamp = last_command_timestamps.get(user.id, 0)
last_command_count_user = last_command_count.get(user.id, 0)
if current_time - last_command_timestamp > RATE_LIMIT_PER:
last_command_timestamps[user.id] = current_time
last_command_count[user.id] = 1
logger.info(f"Rate limit passed for user: {user}")
return True
if last_command_count_user < RATE_LIMIT:
last_command_count[user.id] += 1
logger.info(f"Rate limit passed for user: {user}")
return True
logger.info(f"Rate limit exceeded for user: {user}")
return False
except Exception as e:
logger.error(f"Error checking rate limit: {e}")
raise
async def process_input_message(
input_message: str,
user: discord.User,
conversation_summary: list[dict]
) -> str:
"""
Process an input message using OpenAI's GPT model.
"""
try:
logger.info("Sending prompt to OpenAI API.")
conversation = conversation_history.get(user.id, [])
conversation.append({"role": "user", "content": input_message})
conversation_tokens = sum(
len(message["content"].split()) for message in conversation
)
if conversation_tokens >= GPT_TOKENS * 0.8:
conversation_summary = get_conversation_summary(conversation)
conversation_tokens_summary = sum(
len(message["content"].split())
for message in conversation_summary
)
max_tokens = GPT_TOKENS - conversation_tokens_summary
else:
max_tokens = GPT_TOKENS - conversation_tokens
# Log the current conversation history
# logger.info(f"Current conversation history: {conversation}")
def call_openai_api():
return client.chat.completions.create(
model=GPT_MODEL,
messages=[
{"role": "system", "content": SYSTEM_MESSAGE},
*conversation_summary,
{"role": "user", "content": input_message}
],
max_tokens=max_tokens,
temperature=0.7
)
response = await asyncio.to_thread(call_openai_api)
try:
# Extracting the response content from the new API response format
if response.choices:
response_content = response.choices[0].message.content.strip()
else:
response_content = None
except AttributeError:
logger.error(
"Failed to get response from OpenAI API: "
"Invalid response format."
)
return "Sorry, an error occurred while processing the message."
if response_content:
logger.info("Received response from OpenAI API.")
# Debugging: Log the raw response
# logger.info(f"Raw API response: {response}")
logger.info(f"Sent the response: {response_content}")
conversation.append(
{"role": "assistant", "content": response_content}
)
conversation_history[user.id] = conversation
return response_content
else:
logger.error("OpenAI API error: No response text.")
return "Sorry, I didn't get that. Can you rephrase or ask again?"
except ConnectionClosed as error:
logger.error(f"WebSocket connection closed: {error}")
logger.info("Reconnecting in 5 seconds...")
await asyncio.sleep(5)
await bot.login(DISCORD_TOKEN)
await bot.connect(reconnect=True)
except Exception as error:
logger.error("An error processing message: %s", error)
return "An error occurred while processing the message."
# Executes the argparse code only when the file is run directly
if __name__ == "__main__": # noqa: C901 (ignore complexity in main function)
# Parse command-line arguments
args = parse_arguments()
# Load configuration
config = load_configuration(args.conf)
# Retrieve configuration details from the configuration file
DISCORD_TOKEN = config.get('Discord', 'DISCORD_TOKEN')
ALLOWED_CHANNELS = config.get(
'Discord', 'ALLOWED_CHANNELS', fallback=''
).split(',')
BOT_PRESENCE = config.get('Discord', 'BOT_PRESENCE', fallback='online')
# ACTIVITY_TYPE playing, streaming, listening, watching, custom, competing
ACTIVITY_TYPE = config.get(
'Discord', 'ACTIVITY_TYPE', fallback='listening'
)
ACTIVITY_STATUS = config.get(
'Discord', 'ACTIVITY_STATUS', fallback='Humans'
)
OPENAI_API_KEY = config.get('OpenAI', 'OPENAI_API_KEY')
OPENAI_TIMEOUT = config.getint('OpenAI', 'OPENAI_TIMEOUT', fallback='30')
GPT_MODEL = config.get(
'OpenAI', 'GPT_MODEL', fallback='gpt-3.5-turbo-1106'
)
GPT_TOKENS = config.getint('OpenAI', 'GPT_TOKENS', fallback=4096)
SYSTEM_MESSAGE = config.get(
'OpenAI', 'SYSTEM_MESSAGE', fallback='You are a helpful assistant.'
)
RATE_LIMIT = config.getint('Limits', 'RATE_LIMIT', fallback=10)
RATE_LIMIT_PER = config.getint('Limits', 'RATE_LIMIT_PER', fallback=60)
LOG_FILE = config.get('Logging', 'LOG_FILE', fallback='bot.log')
LOG_LEVEL = config.get('Logging', 'LOG_LEVEL', fallback='INFO')
# Set up logging
logger = logging.getLogger('discord')
logger.setLevel(getattr(logging, LOG_LEVEL.upper()))
# File handler
file_handler = RotatingFileHandler(
LOG_FILE, maxBytes=5 * 1024 * 1024, backupCount=5
)
file_handler.setLevel(getattr(logging, LOG_LEVEL.upper()))
file_formatter = logging.Formatter(
'%(asctime)s [%(levelname)s] %(name)s: %(message)s'
)
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)
# Set a global exception handler
def handle_unhandled_exception(exc_type, exc_value, exc_traceback):
if issubclass(exc_type, KeyboardInterrupt):
sys.__excepthook__(exc_type, exc_value, exc_traceback)
return
logger.error("Unhandled exception", exc_info=(exc_type, exc_value, exc_traceback))
sys.excepthook = handle_unhandled_exception
# Set the intents for the bot
intents = discord.Intents.default()
intents.typing = False
intents.presences = False
# Create a dictionary to store the last command timestamp for each user
last_command_timestamps = {}
last_command_count = {}
# Create a dictionary to store conversation history for each user
conversation_history = {}
# Create the bot instance
bot = discord.Client(intents=intents)
# Create the OpenAI client instance
client = OpenAI(
api_key=OPENAI_API_KEY
)
@bot.event
async def on_ready():
"""
Event handler for when the bot is ready to receive messages.
"""
logger.info(f'We have logged in as {bot.user}')
logger.info(f'Configured bot presence: {BOT_PRESENCE}')
logger.info(f'Configured activity type: {ACTIVITY_TYPE}')
logger.info(f'Configured activity status: {ACTIVITY_STATUS}')
activity = set_activity_status(ACTIVITY_TYPE, ACTIVITY_STATUS)
await bot.change_presence(
activity=activity,
status=discord.Status(BOT_PRESENCE)
)
@bot.event
async def on_disconnect():
"""
Event handler for when the bot disconnects from the Discord server.
"""
logger.info('Bot has disconnected')
@bot.event
async def on_resumed():
"""
Event handler for when the bot resumes its session.
"""
logger.info('Bot has resumed session')
@bot.event
async def on_shard_ready(shard_id):
"""
Event handler for when a shard is ready.
"""
logger.info(f'Shard {shard_id} is ready')
@bot.event
async def on_message(message):
"""
Event handler for when a message is received.
"""
try:
if message.author == bot.user:
return
if isinstance(message.channel, discord.DMChannel):
await process_dm_message(message)
elif (
isinstance(message.channel, discord.TextChannel)
and message.channel.name in ALLOWED_CHANNELS
and bot.user in message.mentions
):
await process_channel_message(message)
except Exception as e:
logger.error(f"An error occurred in on_message: {e}")
async def process_dm_message(message):
"""
Process a direct message.
"""
logger.info(
f'Received DM from {message.author}: {message.content}'
)
if not await check_rate_limit(message.author):
await message.channel.send(
f"{message.author.mention} Exceeded the Rate Limit! Please slow down!"
)
logger.warning(f"Rate Limit Exceed by DM from {message.author}")
return
conversation_summary = get_conversation_summary(
conversation_history.get(message.author.id, [])
)
response = await process_input_message(
message.content, message.author, conversation_summary
)
await send_split_message(message.channel, response)
async def process_channel_message(message):
"""
Process a message in a channel.
"""
logger.info(
'Received message in {} from {}: {}'.format(
str(message.channel),
str(message.author),
re.sub(r'<@\d+>', '', message.content)
)
)
if not await check_rate_limit(message.author):
await message.channel.send(
f"{message.author.mention} Exceeded the Rate Limit! Please slow down!"
)
logger.warning(f"Rate Limit Exceeded in {message.channel} by {message.author}")
return
conversation_summary = get_conversation_summary(
conversation_history.get(message.author.id, [])
)
response = await process_input_message(
message.content, message.author, conversation_summary
)
await send_split_message(message.channel, response)
async def send_split_message(channel, message):
"""
Send a message to a channel. If the message is longer than 2000 characters,
it is split into multiple messages at the nearest newline character around
the middle of the message.
"""
if len(message) <= 2000:
await channel.send(message)
else:
# Find the nearest newline character around the middle of the message
middle_index = len(message) // 2
split_index = message.rfind('\n', 0, middle_index)
if split_index == -1: # No newline character found
split_index = middle_index # Split at the middle of the message
# Split the message into two parts
message_part1 = message[:split_index]
message_part2 = message[split_index:]
# Send the two parts as separate messages
await channel.send(message_part1)
await channel.send(message_part2)
# Run the bot
bot.run(DISCORD_TOKEN)