Files
Botator/src/ChatProcess.py
Paillat 3a955d4379 🐛 fix(main.py): handle on_application_command_error with proper error handling and response
 feat(main.py): add ChatProcess module for handling chat-related functionality
🔧 refactor(main.py): import necessary modules and update bot.add_cog calls
🔧 refactor(server.ts): change port variable case from lowercase port to uppercase PORT to improve semantics
 feat(server.ts): add support for process.env.PORT environment variable to be able to run app on a configurable port
🔧 refactor(cogs/__init__.py): import ChannelSetup cog
 feat(cogs/channelSetup.py): add ChannelSetup cog for setting up channels and server-wide settings
🔧 refactor(cogs/setup.py): import SlashCommandGroup and guild_only from discord module
 feat(cogs/setup.py): add setup_channel command for adding and removing channels
 feat(cogs/setup.py): add api command for setting API keys
 feat(cogs/setup.py): add premium command for setting guild to premium
🔧 refactor(cogs/settings.py): temporarily disable images command due to maintenance
🔧 refactor(config.py): remove unnecessary code related to moderation table
 feat(guild.py): add Guild class for managing guild-specific data and settings
 feat(SqlConnector.py): add SQLConnection and _sql classes for managing SQLite connections
 feat(variousclasses.py): add models, characters, and apis classes for autocomplete functionality in slash commands
2023-08-19 14:16:30 +02:00

86 lines
3.1 KiB
Python

import asyncio
import os
import re
import discord
import datetime
import json
from src.utils.misc import moderate, ModerationError, Hasher
from src.utils.variousclasses import models, characters, apis
from src.guild import Guild
from src.utils.openaicaller import openai_caller
from src.functionscalls import (
call_function,
functions,
server_normal_channel_functions,
FuntionCallError,
)
class Chat:
def __init__(self, bot, message: discord.Message):
self.bot = bot
self.message: discord.Message = message
self.guild = Guild(self.message.guild.id)
self.author = message.author
self.is_bots_thread = False
async def getSupplementaryData(self) -> None:
"""
This function gets various contextual data that will be needed later on
- The original message (if the message is a reply to a previous message from the bot)
- The channel the message was sent in (if the message was sent in a thread)
"""
if isinstance(self.message.channel, discord.Thread):
if self.message.channel.owner_id == self.bot.user.id:
self.is_bots_thread = True
self.channelIdForSettings = self.message.channel.parent_id
else:
self.channelIdForSettings = self.message.channel.id
try:
self.original_message = await self.message.channel.fetch_message(
self.message.reference.message_id
)
except:
self.original_message = None
if (
self.original_message != None
and self.original_message.author.id == self.bot.user.id
):
self.original_message = None
async def preExitCriteria(self) -> bool:
"""
Returns True if any of the exit criterias are met
This checks if the guild has the needed settings for the bot to work
"""
returnCriterias = []
returnCriterias.append(self.message.author.id == self.bot.user.id)
returnCriterias.append(self.api_key == None)
returnCriterias.append(self.is_active == 0)
return any(returnCriterias)
async def postExitCriteria(self) -> bool:
"""
Returns True if any of the exit criterias are met (their opposite is met but there is a not in front of the any() function)
This checks if the bot should actuallly respond to the message or if the message doesn't concern the bot
"""
returnCriterias = []
returnCriterias.append(self.guild.sanitizedChannels.get(str(self.message.channel.id), None) != None)
returnCriterias.append(
self.message.content.find("<@" + str(self.bot.user.id) + ">") != -1
)
returnCriterias.append(self.original_message != None)
returnCriterias.append(self.is_bots_thread)
return not any(returnCriterias)
async def getSettings(self):
self.settings = self.guild.getChannelInfo(str(self.channelIdForSettings))
self.model = self.settings["model"]
self.character = self.settings["character"]
self.openai_api_key = self.guild.api_keys.get("openai", None)