(core): Add member name caching.

This commit is contained in:
2022-01-20 12:12:17 +02:00
parent b21224812e
commit 4a67736adc
6 changed files with 68 additions and 8 deletions

View File

@@ -105,8 +105,8 @@ class LionModule(Module):
await ctx.reply("I need permission to send embeds in this channel before I can run any commands!") await ctx.reply("I need permission to send embeds in this channel before I can run any commands!")
raise SafeCancellation(details='I cannot send embeds in this channel.') raise SafeCancellation(details='I cannot send embeds in this channel.')
# Ensure Lion exists # Ensure Lion exists and cached data is up to date
ctx.alion ctx.alion.update_saved_data(ctx.author)
# Start typing # Start typing
await ctx.ch.trigger_typing() await ctx.ch.trigger_typing()

View File

@@ -14,7 +14,7 @@ meta = RowTable(
user_config = RowTable( user_config = RowTable(
'user_config', 'user_config',
('userid', 'timezone', 'topgg_vote_reminder'), ('userid', 'timezone', 'topgg_vote_reminder', 'avatar_hash'),
'userid', 'userid',
cache=TTLCache(5000, ttl=60*5) cache=TTLCache(5000, ttl=60*5)
) )
@@ -33,7 +33,8 @@ guild_config = RowTable(
'video_studyban', 'video_grace_period', 'video_studyban', 'video_grace_period',
'greeting_channel', 'greeting_message', 'returning_message', 'greeting_channel', 'greeting_message', 'returning_message',
'starting_funds', 'persist_roles', 'starting_funds', 'persist_roles',
'pomodoro_channel'), 'pomodoro_channel',
'name'),
'guildid', 'guildid',
cache=TTLCache(2500, ttl=60*5) cache=TTLCache(2500, ttl=60*5)
) )
@@ -51,6 +52,7 @@ lions = RowTable(
'revision_mute_count', 'revision_mute_count',
'last_study_badgeid', 'last_study_badgeid',
'video_warned', 'video_warned',
'display_name',
'_timestamp' '_timestamp'
), ),
('guildid', 'userid'), ('guildid', 'userid'),

View File

@@ -1,4 +1,5 @@
import pytz import pytz
import discord
from functools import reduce from functools import reduce
from datetime import datetime, timedelta from datetime import datetime, timedelta
@@ -48,6 +49,8 @@ class Lion:
# TODO: Debug log # TODO: Debug log
lion = tb.lions.fetch(key) lion = tb.lions.fetch(key)
if not lion: if not lion:
tb.user_config.fetch_or_create(userid)
tb.guild_config.fetch_or_create(guildid)
tb.lions.create_row( tb.lions.create_row(
guildid=guildid, guildid=guildid,
userid=userid, userid=userid,
@@ -75,10 +78,24 @@ class Lion:
@property @property
def data(self): def data(self):
""" """
The Row corresponding to this user. The Row corresponding to this member.
""" """
return tb.lions.fetch(self.key) return tb.lions.fetch(self.key)
@property
def user_data(self):
"""
The Row corresponding to this user.
"""
return tb.user_config.fetch_or_create(self.userid)
@property
def guild_data(self):
"""
The Row corresponding to this guild.
"""
return tb.guild_config.fetch_or_create(self.guildid)
@property @property
def settings(self): def settings(self):
""" """
@@ -229,6 +246,33 @@ class Lion:
return remaining return remaining
@property
def name(self):
"""
Returns the best local name possible.
"""
if self.member:
name = self.member.display_name
elif self.data.display_name:
name = self.data.display_name
else:
name = str(self.userid)
return name
def update_saved_data(self, member: discord.Member):
"""
Update the stored discord data from the givem member.
Intended to be used when we get member data from events that may not be available in cache.
"""
if self.guild_data.name != member.guild.name:
self.guild_data.name = member.guild.name
if self.user_data.avatar_hash != member.avatar:
self.user_data.avatar_hash = member.avatar
if self.data.display_name != member.display_name:
self.data.display_name = member.display_name
def localize(self, naive_utc_dt): def localize(self, naive_utc_dt):
""" """
Localise the provided naive UTC datetime into the user's timezone. Localise the provided naive UTC datetime into the user's timezone.

View File

@@ -254,7 +254,7 @@ async def session_voice_tracker(client, member, before, after):
return return
guild = member.guild guild = member.guild
Lion.fetch(guild.id, member.id) Lion.fetch(guild.id, member.id).update_saved_data(member)
session = Session.get(guild.id, member.id) session = Session.get(guild.id, member.id)
if before.channel == after.channel: if before.channel == after.channel:

View File

@@ -73,4 +73,15 @@ AS $$
$$ LANGUAGE PLPGSQL; $$ LANGUAGE PLPGSQL;
-- }}} -- }}}
ALTER TABLE user_config
ADD COLUMN avatar_hash TEXT;
ALTER TABLE guild_config
ADD COLUMN name TEXT;
ALTER TABLE members
ADD COLUMN display_name TEXT;
INSERT INTO VersionHistory (version, author) VALUES (9, 'v8-v9 migration'); INSERT INTO VersionHistory (version, author) VALUES (9, 'v8-v9 migration');

View File

@@ -42,7 +42,8 @@ CREATE TABLE global_guild_blacklist(
CREATE TABLE user_config( CREATE TABLE user_config(
userid BIGINT PRIMARY KEY, userid BIGINT PRIMARY KEY,
timezone TEXT, timezone TEXT,
topgg_vote_reminder topgg_vote_reminder,
avatar_hash TEXT
); );
-- }}} -- }}}
@@ -80,7 +81,8 @@ CREATE TABLE guild_config(
starting_funds INTEGER, starting_funds INTEGER,
persist_roles BOOLEAN, persist_roles BOOLEAN,
daily_study_cap INTEGER, daily_study_cap INTEGER,
pomodoro_channel BIGINT pomodoro_channel BIGINT,
name TEXT
); );
CREATE TABLE ignored_members( CREATE TABLE ignored_members(
@@ -405,6 +407,7 @@ CREATE TABLE members(
last_workout_start TIMESTAMP, last_workout_start TIMESTAMP,
last_study_badgeid INTEGER REFERENCES study_badges ON DELETE SET NULL, last_study_badgeid INTEGER REFERENCES study_badges ON DELETE SET NULL,
video_warned BOOLEAN DEFAULT FALSE, video_warned BOOLEAN DEFAULT FALSE,
display_name TEXT,
_timestamp TIMESTAMP DEFAULT (now() at time zone 'utc'), _timestamp TIMESTAMP DEFAULT (now() at time zone 'utc'),
PRIMARY KEY(guildid, userid) PRIMARY KEY(guildid, userid)
); );