mirror of
https://github.com/Paillat-dev/Botator.git
synced 2026-01-02 09:16:19 +00:00
🐛 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
This commit is contained in:
102
src/guild.py
Normal file
102
src/guild.py
Normal file
@@ -0,0 +1,102 @@
|
||||
import orjson
|
||||
import discord
|
||||
|
||||
from src.utils.SqlConnector import sql
|
||||
from datetime import datetime
|
||||
from src.utils.variousclasses import models, characters
|
||||
|
||||
class Guild:
|
||||
def __init__(self, id: int):
|
||||
self.id = id
|
||||
self.load()
|
||||
|
||||
def getDbData(self):
|
||||
with sql.mainDb as con:
|
||||
curs_data = con.cursor()
|
||||
curs_data.execute("SELECT * FROM setup_data WHERE guild_id = ?", (self.id,))
|
||||
data = curs_data.fetchone()
|
||||
self.isInDb = data is not None
|
||||
if not self.isInDb:
|
||||
self.updateDbData()
|
||||
with sql.mainDb as con:
|
||||
curs_data = con.cursor()
|
||||
curs_data.execute("SELECT * FROM setup_data WHERE guild_id = ?", (self.id,))
|
||||
data = curs_data.fetchone()
|
||||
data = orjson.loads(data[1])
|
||||
self.premium = data["premium"]
|
||||
self.channels = data["channels"]
|
||||
self.api_keys = data["api_keys"]
|
||||
if self.premium:
|
||||
self.premium_expiration = datetime.fromisoformat(data.get("premium_expiration", None))
|
||||
self.checkPremiumExpires()
|
||||
else:
|
||||
self.premium_expiration = None
|
||||
|
||||
def checkPremiumExpires(self):
|
||||
if self.premium_expiration is None:
|
||||
self.premium = False
|
||||
return
|
||||
if self.premium_expiration < datetime.now():
|
||||
self.premium = False
|
||||
self.premium_expiration = None
|
||||
self.updateDbData()
|
||||
|
||||
def updateDbData(self):
|
||||
if self.isInDb:
|
||||
data = {
|
||||
"guild_id": self.id,
|
||||
"premium": self.premium,
|
||||
"channels": self.channels,
|
||||
"api_keys": self.api_keys,
|
||||
"premium_expiration": self.premium_expiration.isoformat() if self.premium else None,
|
||||
}
|
||||
else:
|
||||
data = {
|
||||
"guild_id": self.id,
|
||||
"premium": False,
|
||||
"channels": {},
|
||||
"api_keys": {},
|
||||
"premium_expiration": None,
|
||||
}
|
||||
with sql.mainDb as con:
|
||||
curs_data = con.cursor()
|
||||
if self.isInDb:
|
||||
curs_data.execute("UPDATE setup_data SET guild_settings = ? WHERE guild_id = ?", (orjson.dumps(data), self.id))
|
||||
else:
|
||||
curs_data.execute("INSERT INTO setup_data (guild_id, guild_settings) VALUES (?, ?)", (self.id, orjson.dumps(data)))
|
||||
self.isInDb = True
|
||||
|
||||
def load(self):
|
||||
self.getDbData()
|
||||
|
||||
def addChannel(self, channel: discord.TextChannel, model: str, character: str):
|
||||
print(f"Adding channel {channel.id} to guild {self.id} with model {model} and character {character}")
|
||||
self.channels[str(channel.id)] = {
|
||||
"model": model,
|
||||
"character": character,
|
||||
}
|
||||
self.updateDbData()
|
||||
|
||||
def delChannel(self, channel: discord.TextChannel):
|
||||
del self.channels[str(channel.id)]
|
||||
self.updateDbData()
|
||||
|
||||
@property
|
||||
def sanitizedChannels(self) -> dict:
|
||||
if self.premium:
|
||||
return self.channels
|
||||
if len(self.channels) == 0:
|
||||
return {}
|
||||
return {
|
||||
list(self.channels.keys())[0]: {
|
||||
"model": models.matchingDict[models.default],
|
||||
"character": characters.matchingDict[characters.default],
|
||||
}
|
||||
}
|
||||
|
||||
def getChannelInfo(self, channel: str):
|
||||
return self.sanitizedChannels.get(channel, None)
|
||||
|
||||
def addApiKey(self, api: str, key: str):
|
||||
self.api_keys[api] = key
|
||||
self.updateDbData()
|
||||
Reference in New Issue
Block a user