rewrite: Core components.
This commit is contained in:
@@ -1,12 +1,28 @@
|
||||
from enum import Enum
|
||||
from itertools import chain
|
||||
from psycopg import sql
|
||||
from cachetools import TTLCache
|
||||
|
||||
from data import Table, Registry, Column, RowModel
|
||||
from data import Table, Registry, Column, RowModel, RegisterEnum
|
||||
from data.models import WeakCache
|
||||
from data.columns import Integer, String, Bool, Timestamp
|
||||
|
||||
|
||||
class RankType(Enum):
|
||||
"""
|
||||
Schema
|
||||
------
|
||||
CREATE TYPE RankType AS ENUM(
|
||||
'XP',
|
||||
'VOICE',
|
||||
'MESSAGE'
|
||||
);
|
||||
"""
|
||||
XP = 'XP',
|
||||
VOICE = 'VOICE',
|
||||
MESSAGE = 'MESSAGE',
|
||||
|
||||
|
||||
class CoreData(Registry, name="core"):
|
||||
class AppConfig(RowModel):
|
||||
"""
|
||||
@@ -141,7 +157,10 @@ class CoreData(Registry, name="core"):
|
||||
left_at TIMESTAMPTZ,
|
||||
locale TEXT,
|
||||
timezone TEXT,
|
||||
force_locale BOOLEAN
|
||||
force_locale BOOLEAN,
|
||||
season_start TIMESTAMPTZ,
|
||||
xp_per_period INTEGER,
|
||||
xp_per_centiword INTEGER
|
||||
);
|
||||
|
||||
"""
|
||||
@@ -205,7 +224,16 @@ class CoreData(Registry, name="core"):
|
||||
locale = String()
|
||||
force_locale = Bool()
|
||||
|
||||
unranked_rows = Table('unranked_rows')
|
||||
season_start = Timestamp()
|
||||
rank_type: Column[RankType] = Column()
|
||||
rank_channel = Integer()
|
||||
dm_ranks = Bool()
|
||||
|
||||
xp_per_period = Integer()
|
||||
xp_per_centiword = Integer()
|
||||
coins_per_centixp = Integer()
|
||||
|
||||
allow_transfers = Bool()
|
||||
|
||||
donator_roles = Table('donator_roles')
|
||||
|
||||
|
||||
@@ -78,6 +78,35 @@ class Lions(LionCog):
|
||||
self.lion_guilds[guildid] = lguild
|
||||
return lguild
|
||||
|
||||
async def fetch_guilds(self, *guildids) -> dict[int, LionGuild]:
|
||||
"""
|
||||
Fetch (or create) multiple LionGuilds simultaneously, using cache where possible.
|
||||
"""
|
||||
guild_map = {}
|
||||
missing = set()
|
||||
for guildid in guildids:
|
||||
lguild = self.lion_guilds.get(guildid, None)
|
||||
guild_map[guildid] = lguild
|
||||
if lguild is None:
|
||||
missing.add(guildid)
|
||||
|
||||
if missing:
|
||||
rows = await self.data.Guild.fetch_where(guildid=list(missing))
|
||||
missing.difference_update(row.guildid for row in rows)
|
||||
|
||||
if missing:
|
||||
new_rows = await self.data.Guild.table.insert_many(
|
||||
('guildid',),
|
||||
*((guildid,) for guildid in missing)
|
||||
).with_adapter(self.data.Guild._make_rows)
|
||||
rows = (*rows, *new_rows)
|
||||
|
||||
for row in rows:
|
||||
guildid = row.guildid
|
||||
self.lion_guilds[guildid] = guild_map[guildid] = LionGuild(self.bot, row)
|
||||
|
||||
return guild_map
|
||||
|
||||
async def fetch_member(self, guildid, userid, member: Optional[discord.Member] = None) -> LionMember:
|
||||
"""
|
||||
Fetch the given LionMember, using cache for data if possible.
|
||||
|
||||
@@ -71,7 +71,7 @@ class LionGuild(Timezoned):
|
||||
@property
|
||||
def guild_mode(self):
|
||||
# TODO: Configuration, data, and settings for this...
|
||||
return GuildMode.StudyGuild
|
||||
return GuildMode.VoiceGuild
|
||||
|
||||
@property
|
||||
def timezone(self) -> pytz.timezone:
|
||||
|
||||
Reference in New Issue
Block a user