You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Fox-V3/lseen/lseen.py

95 lines
3.2 KiB

7 years ago
from datetime import datetime
from typing import Literal
7 years ago
import dateutil.parser
import discord
from redbot.core import Config, commands
7 years ago
from redbot.core.bot import Red
from redbot.core.commands import Cog
from redbot.core.utils import AsyncIter
7 years ago
class LastSeen(Cog):
7 years ago
"""
Report when a user was last seen online
7 years ago
"""
online_status = discord.Status.online
offline_status = discord.Status.offline
def __init__(self, bot: Red):
super().__init__()
7 years ago
self.bot = bot
self.config = Config.get_conf(self, identifier=9811198108111121, force_registration=True)
default_global = {}
default_guild = {"enabled": False}
6 years ago
default_member = {"seen": None}
7 years ago
self.config.register_global(**default_global)
self.config.register_guild(**default_guild)
self.config.register_member(**default_member)
async def red_delete_data_for_user(
self,
*,
requester: Literal["discord_deleted_user", "owner", "user", "user_strict"],
user_id: int,
):
all_members = await self.config.all_members()
async for guild_id, guild_data in AsyncIter(all_members.items(), steps=100):
if user_id in guild_data:
await self.config.member_from_ids(guild_id, user_id).clear()
7 years ago
@staticmethod
def get_date_time(s):
d = dateutil.parser.parse(s)
return d
6 years ago
@commands.group(aliases=["setlseen"], name="lseenset")
async def lset(self, ctx: commands.Context):
7 years ago
"""Change settings for lseen"""
if ctx.invoked_subcommand is None:
7 years ago
pass
7 years ago
@lset.command(name="toggle")
async def lset_toggle(self, ctx: commands.Context):
7 years ago
"""Toggles tracking seen for this server"""
enabled = not await self.config.guild(ctx.guild).enabled()
6 years ago
await self.config.guild(ctx.guild).enabled.set(enabled)
7 years ago
await ctx.maybe_send_embed(
6 years ago
"Seen for this server is now {}".format("Enabled" if enabled else "Disabled")
)
7 years ago
6 years ago
@commands.command(aliases=["lastseen"])
async def lseen(self, ctx: commands.Context, member: discord.Member):
7 years ago
"""
Just says the time the user was last seen
"""
if member.status != self.offline_status:
last_seen = datetime.utcnow()
else:
last_seen = await self.config.member(member).seen()
if last_seen is None:
await ctx.maybe_send_embed(embed=discord.Embed(description="I've never seen this user"))
7 years ago
return
last_seen = self.get_date_time(last_seen)
# embed = discord.Embed(
# description="{} was last seen at this date and time".format(member.display_name),
6 years ago
# timestamp=last_seen)
7 years ago
embed = discord.Embed(timestamp=last_seen)
await ctx.send(embed=embed)
@commands.Cog.listener()
7 years ago
async def on_member_update(self, before: discord.Member, after: discord.Member):
if before.status != self.offline_status and after.status == self.offline_status:
if not await self.config.guild(before.guild).enabled():
return
await self.config.member(before).seen.set(datetime.utcnow().isoformat())