updates

parent 20dd61e8
......@@ -44,10 +44,10 @@ class Personal:
@blacklist_group.command(name="add", brief="add to blacklist", no_pm=True)
@checks.is_owner()
async def blacklist_add(self, ctx, value:str, *, obj:MultiMention):
if isinstance(obj, discord.guild):
if isinstance(obj, discord.Guild):
kwargs = dict(guild_id=int(obj.id))
elif isinstance(obj, discord.Channel):
elif isinstance(obj, discord.TextChannel):
kwargs = dict(channel_id=int(obj.id))
elif isinstance(obj, discord.Role):
......@@ -70,10 +70,10 @@ class Personal:
@blacklist_group.command(name="remove", brief="remove from blacklist", no_pm=True)
@checks.is_owner()
async def blacklist_remove(self, ctx, value:str, *, obj:MultiMention):
if isinstance(obj, discord.guild):
if isinstance(obj, discord.Guild):
kwargs = dict(guild_id=int(obj.id))
elif isinstance(obj, discord.Channel):
elif isinstance(obj, discord.TextChannel):
kwargs = dict(channel_id=int(obj.id))
elif isinstance(obj, discord.Role):
......
......@@ -98,8 +98,7 @@ class TagOverrides(tag_parser.TagFunctions):
tag_dict = sql.TagVariable(tag_name=self.tag.name, data=json.dumps({}))
session.add(tag_dict)
temp_data = json.loads(tag_dict.data)
return temp_data.get(key, default)
return tag_dict.data[key]
def set(self, key, value):
with self.bot.db_scope() as session:
......@@ -108,9 +107,7 @@ class TagOverrides(tag_parser.TagFunctions):
tag_dict = sql.TagVariable(tag_name=self.tag.name, data=json.dumps({}))
session.add(tag_dict)
temp_data = json.loads(tag_dict.data)
temp_data[key] = value
tag_dict.data = json.dumps(temp_data)
tag_dict.data[key] = value
self.bot.db.flag(tag_dict, "data") # force it to re-commit
......
......@@ -8,7 +8,7 @@ CHANNEL_ID = re.compile(r"^<#(?P<id>[0-9]{,23})>$")
MEMBER_ID = re.compile(r"^<@!?(?P<id>[0-9]{,23})>$")
class MultiMention(commands.Converter):
async def convert(self, ctx, argument): # its never going to be a private channel
async def convert(self, ctx, argument): # it's never going to be a private channel
message = ctx.message
channel = message.channel
guild = channel.guild
......
from sqlalchemy import ForeignKey, INTEGER, TEXT, BOOLEAN, Column, create_engine
from sqlalchemy import ForeignKey, Integer, BigInteger, String, Date, Boolean, Column, create_engine
from sqlalchemy.orm import relationship, sessionmaker, scoped_session
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy.orm.attributes import flag_modified
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
# Start mapping classesS
# Start mapping classes
class Tag(Base):
__tablename__ = "tags"
name = Column(TEXT, primary_key=True) # limit of 50
author_id = Column(INTEGER, ForeignKey("users.id"))
name = Column(String(50), primary_key=True)
author_id = Column(BigInteger, ForeignKey("users.id"))
author = relationship("User", back_populates="tags")
content = Column(TEXT) # limit of 1500
uses = Column(INTEGER)
timestamp = Column(TEXT)
content = Column(String(1500))
uses = Column(Integer)
timestamp = Column(String)
def __repr__(self):
return f"<Tag(name='{self.name}', author_id={self.author_id}, uses={self.uses}, timestamp='{self.timestamp}')>"
......@@ -24,13 +25,13 @@ class Tag(Base):
class Permission(Base):
__tablename__ = "permissions"
pk = Column(INTEGER, primary_key=True)
guild_id = Column(INTEGER)
channel_id = Column(INTEGER)
role_id = Column(INTEGER)
pk = Column(Integer, primary_key=True)
guild_id = Column(BigInteger)
channel_id = Column(BigInteger)
role_id = Column(BigInteger)
user = relationship("User", back_populates="permissions")
user_id = Column(INTEGER, ForeignKey("users.id"))
bits = Column(INTEGER)
user_id = Column(BigInteger, ForeignKey("users.id"))
bits = Column(BigInteger)
def __repr__(self):
return f"<Permission(guild_id={self.guild_id}, channel_id={self.channel_id}, user_id={self.user_id}, bits={self.bits})>"
......@@ -38,10 +39,10 @@ class Permission(Base):
class User(Base):
__tablename__ = "users"
id = Column(INTEGER, primary_key=True)
name = Column(TEXT) # limit of 40
bot = Column(BOOLEAN)
discrim = Column(TEXT) # limit of 4
id = Column(BigInteger, primary_key=True)
name = Column(String(40))
bot = Column(Boolean)
discrim = Column(String(4))
permissions = relationship("Permission", back_populates="user", cascade="all, delete, delete-orphan")
messages = relationship("Message", back_populates="author", cascade="all, delete, delete-orphan")
tags = relationship("Tag", back_populates="author", cascade="all, delete, delete-orphan")
......@@ -52,13 +53,13 @@ class User(Base):
class Blacklist(Base):
__tablename__ = "blacklist"
pk = Column(INTEGER, primary_key=True)
guild_id = Column(INTEGER)
channel_id = Column(INTEGER)
role_id = Column(INTEGER)
user_id = Column(INTEGER)
pk = Column(Integer, primary_key=True)
guild_id = Column(BigInteger)
channel_id = Column(BigInteger)
role_id = Column(BigInteger)
user_id = Column(BigInteger)
data = Column(TEXT)
data = Column(String)
def __repr__(self):
return f"<Blacklist(guild_id={self.guild_id}, channel_id={self.channel_id}, role_id={self.role_id}, user_id={self.user_id})>"
......@@ -66,13 +67,13 @@ class Blacklist(Base):
class Whitelist(Base):
__tablename__ = "whitelist"
pk = Column(INTEGER, primary_key=True)
guild_id = Column(INTEGER)
channel_id = Column(INTEGER)
role_id = Column(INTEGER)
user_id = Column(INTEGER)
pk = Column(Integer, primary_key=True)
guild_id = Column(BigInteger)
channel_id = Column(BigInteger)
role_id = Column(BigInteger)
user_id = Column(BigInteger)
data = Column(TEXT)
data = Column(String)
def __repr__(self):
return f"<Whitelist(guild_id={self.guild_id}, channel_id={self.channel_id}, role_id={self.role_id}, user_id={self.user_id})>"
......@@ -80,8 +81,8 @@ class Whitelist(Base):
class TagVariable(Base):
__tablename__ = "tag_values"
tag_name = Column(TEXT, primary_key=True) # limit of 50
data = Column(TEXT)
tag_name = Column(String(50), primary_key=True)
data = Column(JSONB) # JSONb as key:value pairs
def __repr__(self):
return f"<TagVariable(tag_name='{self.tag_name}, values={self.data})>"
......@@ -89,15 +90,15 @@ class TagVariable(Base):
class Message(Base):
__tablename__ = "chat_logs"
pk = Column(INTEGER, primary_key=True)
id = Column(INTEGER)
timestamp = Column(TEXT)
author_id = Column(INTEGER, ForeignKey("users.id"))
pk = Column(Integer, primary_key=True)
id = Column(BigInteger)
timestamp = Column(String)
author_id = Column(BigInteger, ForeignKey("users.id"))
author = relationship("User", back_populates="messages")
channel_id = Column(INTEGER)
guild_id = Column(INTEGER)
content = Column(TEXT) # limit of 2000
action = Column(TEXT) # limit of 15
channel_id = Column(BigInteger)
guild_id = Column(BigInteger)
content = Column(String(2000))
action = Column(String(15))
def __repr__(self):
return f"<Message(id={self.id}, timestamp='{self.timestamp}', author_id={self.author_id}, channel_id={self.channel_id}, guild_id={self.guild_id}, action='{self.action}')>"
......@@ -105,8 +106,8 @@ class Message(Base):
class Command(Base):
__tablename__ = "command_stats"
command_name = Column(TEXT, primary_key=True) # limit of 40
uses = Column(INTEGER)
command_name = Column(String(40), primary_key=True)
uses = Column(Integer)
def __repr__(self):
return f"<Command(command_name='{self.command_name}', uses={self.uses})>"
......@@ -114,8 +115,8 @@ class Command(Base):
class Prefix(Base):
__tablename__ = "prefixes"
guild_id = Column(INTEGER, primary_key=True)
prefix = Column(TEXT) # limit of 25
guild_id = Column(BigInteger, primary_key=True)
prefix = Column(String(25))
def __repr__(self):
return f"<Prefix(guild_id={self.guild_id}, prefix='{self.prefix}')>"
......@@ -125,8 +126,10 @@ class Prefix(Base):
class SQL:
def __init__(self, *args, **kwargs):
self.db_name = kwargs.get("db_name")
self.db_api = kwargs.get("db_api", "pysqlite")
self.engine = create_engine(f"sqlite+{self.db_api}:///{self.db_name}.db?check_same_thread=False", echo=False)
self.db_username = kwargs.get("db_username")
self.db_password = kwargs.get("db_password")
self.db_api = kwargs.get("db_api", "pypostgresql")
self.engine = create_engine(f"postgresql+{self.db_api}://{self.db_username}:{self.db_password}@localhost:5432/{self.db_name}", echo=False)
self.Session = sessionmaker(bind=self.engine)
Base.metadata.create_all(self.engine)
......
......@@ -195,7 +195,7 @@ class SnakeBot(commands.Bot):
)
self.aio_session = aiohttp.ClientSession()
self.db = sql.SQL(db_name="snake")
self.db = sql.SQL(db_name="snake", db_username=os.environ.get("SNAKE_DB_USERNAME"), db_password=os.environ.get("SNAKE_DB_PASSWORD"))
self.boot_time = datetime.now()
for filename in os.listdir("cogs"):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment