rewrite: Localisation support.

This commit is contained in:
2022-11-23 13:11:41 +02:00
parent 2eea40f679
commit 0d5e801945
18 changed files with 666 additions and 27 deletions

View File

@@ -11,6 +11,8 @@ from settings.groups import SettingGroup
from .data import CoreData
from .lion import Lions
from .guild_settings import GuildSettings
from .user_settings import UserSettings
class CoreCog(LionCog):
@@ -31,12 +33,18 @@ class CoreCog(LionCog):
self.guild_setting_groups: list[SettingGroup] = []
self.user_setting_groups: list[SettingGroup] = []
# Some ModelSetting registries
# These are for more convenient direct access
self.guild_settings = GuildSettings
self.user_settings = UserSettings
self.app_cmd_cache: list[discord.app_commands.AppCommand] = []
self.cmd_name_cache: dict[str, discord.app_commands.AppCommand] = {}
async def bot_check_once(self, ctx: LionContext):
async def bot_check_once(self, ctx: LionContext): # type: ignore
lion = await self.lions.fetch(ctx.guild.id if ctx.guild else 0, ctx.author.id)
await lion.touch_discord_models(ctx.author)
if ctx.guild:
await lion.touch_discord_models(ctx.author) # type: ignore # Type checker doesn't recognise guard
ctx.alion = lion
return True

View File

@@ -73,7 +73,9 @@ class CoreData(Registry, name="core"):
API_timestamp BIGINT,
gems INTEGER DEFAULT 0,
first_seen TIMESTAMPTZ DEFAULT now(),
last_seen TIMESTAMPTZ
last_seen TIMESTAMPTZ,
locale TEXT,
locale_hint TEXT
);
"""
@@ -89,6 +91,8 @@ class CoreData(Registry, name="core"):
gems = Integer()
first_seen = Timestamp()
last_seen = Timestamp()
locale = String()
locale_hint = String()
class Guild(RowModel):
"""
@@ -132,7 +136,9 @@ class CoreData(Registry, name="core"):
pomodoro_channel BIGINT,
name TEXT,
first_joined_at TIMESTAMPTZ DEFAULT now(),
left_at TIMESTAMPTZ
left_at TIMESTAMPTZ,
locale TEXT,
force_locale BOOLEAN
);
"""
@@ -191,6 +197,9 @@ class CoreData(Registry, name="core"):
first_joined_at = Timestamp()
left_at = Timestamp()
locale = String()
force_locale = Bool()
unranked_rows = Table('unranked_rows')
donator_roles = Table('donator_roles')

View File

@@ -0,0 +1,7 @@
from settings.groups import ModelSettings, SettingDotDict
from .data import CoreData
class GuildSettings(ModelSettings):
_settings = SettingDotDict()
model = CoreData.Guild

View File

@@ -9,6 +9,9 @@ from data import WeakCache
from .data import CoreData
from .user_settings import UserSettings
from .guild_settings import GuildSettings
class Lion:
"""
@@ -25,9 +28,10 @@ class Lion:
There is no guarantee that a corresponding discord Member actually exists.
"""
__slots__ = ('data', 'user_data', 'guild_data', '_member', '__weakref__')
__slots__ = ('bot', 'data', 'user_data', 'guild_data', '_member', '__weakref__')
def __init__(self, data: CoreData.Member, user_data: CoreData.User, guild_data: CoreData.Guild):
def __init__(self, bot: LionBot, data: CoreData.Member, user_data: CoreData.User, guild_data: CoreData.Guild):
self.bot = bot
self.data = data
self.user_data = user_data
self.guild_data = guild_data
@@ -52,6 +56,15 @@ class Lion:
def get(cls, guildid, userid):
return cls._cache_.get((guildid, userid), None)
# ModelSettings interfaces
@property
def guild_settings(self):
return GuildSettings(self.guildid, self.guild_data, bot=self.bot)
@property
def user_settings(self):
return UserSettings(self.userid, self.user_data, bot=self.bot)
# Setting interfaces
# Each of these return an initialised member setting
@@ -74,7 +87,7 @@ class Lion:
# Discord data cache
async def touch_discord_models(self, member: discord.Member):
"""
Update the stored discord data from the givem member.
Update the stored discord data from the given user or member object.
Intended to be used when we get member data from events that may not be available in cache.
"""
# Can we do these in one query?
@@ -115,7 +128,7 @@ class Lions(LionCog):
guild = await data.Guild.fetch_or_create(guildid)
user = await data.User.fetch_or_create(userid)
member = await data.Member.fetch_or_create(guildid, userid)
lion = Lion(member, user, guild)
lion = Lion(self.bot, member, user, guild)
self._cache_[(guildid, userid)] = lion
return lion

View File

@@ -0,0 +1,7 @@
from settings.groups import ModelSettings, SettingDotDict
from .data import CoreData
class UserSettings(ModelSettings):
_settings = SettingDotDict()
model = CoreData.User