Switch to custom group for approved use and added data migration

lseen_develop
bobloy 4 years ago
parent 49dd52c8dc
commit 9347578fbc

@ -1,5 +1,7 @@
from .lseen import LastSeen from .lseen import LastSeen
def setup(bot): async def setup(bot):
bot.add_cog(LastSeen(bot)) seen = LastSeen(bot)
bot.add_cog(seen)
await seen.initialize()

@ -1,4 +1,5 @@
import asyncio import asyncio
import logging
from datetime import datetime from datetime import datetime
from typing import Literal from typing import Literal
@ -9,6 +10,8 @@ from redbot.core.bot import Red
from redbot.core.commands import Cog from redbot.core.commands import Cog
from redbot.core.utils import AsyncIter from redbot.core.utils import AsyncIter
log = logging.getLogger("red.fox_v3.lseen")
class LastSeen(Cog): class LastSeen(Cog):
""" """
@ -23,13 +26,15 @@ class LastSeen(Cog):
super().__init__() super().__init__()
self.bot = bot self.bot = bot
self.config = Config.get_conf(self, identifier=9811198108111121, force_registration=True) self.config = Config.get_conf(self, identifier=9811198108111121, force_registration=True)
default_global = {} default_global = {"migrated_v2": False}
default_guild = {"enabled": None} default_guild = {"enabled": None}
default_member = {"seen": None} default_member = {"seen": None}
self.config.register_global(**default_global) self.config.register_global(**default_global)
self.config.register_guild(**default_guild) self.config.register_guild(**default_guild)
self.config.register_member(**default_member) # self.config.register_member(**default_member)
self.config.init_custom("CustomMember", 2)
self.config.register_custom("CustomMember", **default_member)
async def red_delete_data_for_user( async def red_delete_data_for_user(
self, self,
@ -42,13 +47,20 @@ class LastSeen(Cog):
async for guild_id, guild_data in AsyncIter(all_members.items(), steps=100): async for guild_id, guild_data in AsyncIter(all_members.items(), steps=100):
if user_id in guild_data: if user_id in guild_data:
await self.config.member_from_ids(guild_id, user_id).clear() await self.config.custom("CustomMember", guild_id, str(user_id)).clear()
async def _initalize_tracking(self, guild: discord.Guild): async def _initalize_tracking(self, guild: discord.Guild):
now = datetime.utcnow().isoformat() now = datetime.utcnow().isoformat()
for member in guild.members: online_members = {
if member.status != self.offline_status: member.id: {"seen": now}
await self.config.member(member).seen.set(now) for member in guild.members
if member.status != self.offline_status
}
async with self.config.custom("CustomMember", guild.id).all() as cm:
cm.update(online_members)
# for member in guild.members:
# if member.status != self.offline_status:
# await self.config.custom("CustomMember", guild.id, member.id).seen.set(now)
@staticmethod @staticmethod
def get_date_time(s): def get_date_time(s):
@ -90,9 +102,11 @@ class LastSeen(Cog):
if member.status != self.offline_status: if member.status != self.offline_status:
last_seen = datetime.utcnow() last_seen = datetime.utcnow()
if await self.config.guild(ctx.guild).enabled(): if await self.config.guild(ctx.guild).enabled():
await self.config.member(member).seen.set(last_seen.isoformat()) await self.config.custom("CustomMember", member.guild.id, member.id).seen.set(
last_seen.isoformat()
)
else: else:
last_seen = await self.config.member(member).seen() last_seen = await self.config.custom("CustomMember", member.guild.id, member.id).seen()
if last_seen is None: if last_seen is None:
await ctx.maybe_send_embed("I've never seen this user") await ctx.maybe_send_embed("I've never seen this user")
return return
@ -112,4 +126,20 @@ class LastSeen(Cog):
return return
if not await self.config.guild(after.guild).enabled(): if not await self.config.guild(after.guild).enabled():
return return
await self.config.member(before).seen.set(datetime.utcnow().isoformat()) await self.config.custom("CustomMember", before.guild.id, before.id).seen.set(
datetime.utcnow().isoformat()
)
async def initialize(self):
if not await self.config.migrated_v2():
all_members = await self.config.all_members()
async for guild_id, guild_data in AsyncIter(all_members.items(), steps=100):
async with self.config.custom("CustomMember").all() as cm:
cm.update(
{
str(guild_id): {str(member_id): {"seen": member_data["seen"]}}
for member_id, member_data in guild_data.items()
}
)
log.info("LastSeen migrated to V2")
await self.config.migrated_v2.set(True)

Loading…
Cancel
Save