Skip to content

Commit

Permalink
fix: some bugs
Browse files Browse the repository at this point in the history
[BugFix #58 #61]
  • Loading branch information
Zero6992 authored Dec 14, 2022
2 parents 52e0dd1 + a0e6819 commit 9019904
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def check_verion() -> None:
name, version = installed.project_name, installed.version
# Compare the version number to see if it matches the one in requirements.txt
if package != f'{name}=={version}':
logger.exception(f'{name} version {version} is installed but does not match the requirements')
logger.error(f'{name} version {version} is installed but does not match the requirements')
sys.exit();

if __name__ == '__main__':
Expand Down
41 changes: 26 additions & 15 deletions src/bot.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import discord
from discord.ext import commands
from discord import app_commands
from src import responses
from src import log

logger = log.setup_logger(__name__)

data = responses.get_config()
config = responses.get_config()

isPrivate = False


class aclient(discord.Client):
def __init__(self) -> None:
super().__init__(intents=discord.Intents.default())
self.tree = app_commands.CommandTree(self)

async def on_ready(self) -> None:
await self.wait_until_ready()
await self.tree.sync()
await send_start_prompt()
logger.info(f'{self.user} is now running!')


async def send_message(message, user_message):
await message.response.defer(ephemeral=isPrivate)
try:
Expand All @@ -28,26 +40,27 @@ async def send_message(message, user_message):
formatted_code_block = ""
for line in code_block:
while len(line) > 1900:
# Split the line at the 50th character
# Split the line at the 50th character
formatted_code_block += line[:1900] + "\n"
line = line[1900:]
formatted_code_block += line + "\n" # Add the line and seperate with new line
formatted_code_block += line + "\n" # Add the line and seperate with new line

# Send the code block in a separate message
if (len(formatted_code_block) > 2000):
code_block_chunks = [formatted_code_block[i:i+1900] for i in range(0, len(formatted_code_block), 1900)]
code_block_chunks = [formatted_code_block[i:i+1900]
for i in range(0, len(formatted_code_block), 1900)]
for chunk in code_block_chunks:
await message.followup.send("```" + chunk + "```")
else:
await message.followup.send("```" + formatted_code_block + "```")
await message.followup.send("```" + formatted_code_block + "```")

# Send the remaining of the response in another message

if len(parts) >= 3:
await message.followup.send(parts[2])
else:
response_chunks = [response[i:i+1900]
for i in range(0, len(response), 1900)]
for i in range(0, len(response), 1900)]
for chunk in response_chunks:
await message.followup.send(chunk)
else:
Expand All @@ -57,7 +70,7 @@ async def send_message(message, user_message):
logger.exception(f"Error while sending message: {e}")


async def send_start_prompt() :
async def send_start_prompt():
import os
import os.path

Expand All @@ -76,11 +89,9 @@ async def send_start_prompt() :
except Exception as e:
logger.exception(f"Error while sending starting prompt: {e}")


def run_discord_bot():
intents = discord.Intents.default()
intents.message_content = True
activity = discord.Activity(type=discord.ActivityType.watching, name="/chat | /private | /public | /reset")
client = commands.Bot(command_prefix='!', intents=intents, activity=activity)
client = aclient()

@client.event
async def on_ready():
Expand Down Expand Up @@ -132,6 +143,6 @@ async def reset(interaction: discord.Interaction):
"\x1b[31mChatGPT bot has been successfully reset\x1b[0m")
await send_start_prompt()

TOKEN = data['discord_bot_token']

TOKEN = config['discord_bot_token']
client.run(TOKEN)
8 changes: 4 additions & 4 deletions src/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ def get_config() -> dict:
config_path = os.path.join(config_dir, config_name)

with open(config_path, 'r') as f:
data = json.load(f)
config = json.load(f)

return data
return config


data = get_config()
config = get_config()

config = {
"session_token" : data['session_token']
"session_token" : config['session_token']
}

chatbot = Chatbot(config, conversation_id=None)

0 comments on commit 9019904

Please sign in to comment.