Fix user and snowflake commands

This commit is contained in:
Dustin Pianalto 2019-12-15 15:23:22 -09:00
parent 945a8b850a
commit 2a00c2f07d
2 changed files with 16 additions and 5 deletions

View File

@ -1,7 +1,7 @@
import discord import discord
from discord.ext import commands from discord.ext import commands
from datetime import datetime, timedelta from datetime import datetime, timedelta
from geeksbot.imports.utils import process_snowflake from geeksbot.imports.utils import process_snowflake, create_date_string
class Inspect(commands.Cog): class Inspect(commands.Cog):
@ -22,10 +22,10 @@ class Inspect(commands.Cog):
value=f'{member.bot}', value=f'{member.bot}',
inline=True) inline=True)
em.add_field(name=f'Joined Guild:', em.add_field(name=f'Joined Guild:',
value=f'{self.create_date_string(member.joined_at, ctx.message.created_at)}', value=f'{create_date_string(member.joined_at, ctx.message.created_at)}',
inline=False) inline=False)
em.add_field(name=f'Joined Discord:', em.add_field(name=f'Joined Discord:',
value=f'{self.create_date_string(member.created_at, ctx.message.created_at)}', value=f'{create_date_string(member.created_at, ctx.message.created_at)}',
inline=False) inline=False)
em.add_field(name=f'Current Status:', em.add_field(name=f'Current Status:',
value=f'{member.status}', value=f'{member.status}',
@ -49,8 +49,10 @@ class Inspect(commands.Cog):
snowflake = int(snowflake) snowflake = int(snowflake)
except ValueError: except ValueError:
await ctx.send('That is not a valid snowflake') await ctx.send('That is not a valid snowflake')
if len(bin(snowflake))-2 < 63 or len(bin(snowflake))-2 > 64: return
if len(bin(snowflake))-2 < 23 or len(bin(snowflake))-2 > 64:
await ctx.send('That is not a valid snowflake') await ctx.send('That is not a valid snowflake')
return
creation_time, worker, process, counter = process_snowflake(snowflake) creation_time, worker, process, counter = process_snowflake(snowflake)
em = discord.Embed(title=str(snowflake), em = discord.Embed(title=str(snowflake),
description=f'Created At: {creation_time.strftime("%c")}\n' description=f'Created At: {creation_time.strftime("%c")}\n'

View File

@ -8,6 +8,15 @@ async def get_guild_config(bot, guild_id):
guild_config = bot.cache.get() guild_config = bot.cache.get()
def create_date_string(time, time_now):
diff = (time_now - time)
date_str = time.strftime('%Y-%m-%d %H:%M:%S')
return f"{diff.days} {'day' if diff.days == 1 else 'days'} " \
f"{diff.seconds // 3600} {'hour' if diff.seconds // 3600 == 1 else 'hours'} " \
f"{diff.seconds % 3600 // 60} {'minute' if diff.seconds % 3600 // 60 == 1 else 'minutes'} " \
f"{diff.seconds % 3600 % 60} {'second' if diff.seconds % 3600 % 60 == 1 else 'seconds'} ago.\n{date_str}"
def process_snowflake(snowflake: int) -> typing.Tuple[datetime, int, int, int]: def process_snowflake(snowflake: int) -> typing.Tuple[datetime, int, int, int]:
DISCORD_EPOCH = 1420070400000 DISCORD_EPOCH = 1420070400000
TIME_BITS_LOC = 22 TIME_BITS_LOC = 22
@ -16,7 +25,7 @@ def process_snowflake(snowflake: int) -> typing.Tuple[datetime, int, int, int]:
PROCESS_ID_LOC = 12 PROCESS_ID_LOC = 12
PROCESS_ID_MASK = 0x1F000 PROCESS_ID_MASK = 0x1F000
INCREMENT_MASK = 0xFFF INCREMENT_MASK = 0xFFF
creation_time = datetime.fromtimestamp((snowflake >> TIME_BITS_LOC) + DISCORD_EPOCH) creation_time = datetime.fromtimestamp(((snowflake >> TIME_BITS_LOC) + DISCORD_EPOCH) / 1000.0)
worker_id = (snowflake >> WORKER_ID_LOC) & WORKER_ID_MASK worker_id = (snowflake >> WORKER_ID_LOC) & WORKER_ID_MASK
process_id = (snowflake >> PROCESS_ID_LOC) & PROCESS_ID_MASK process_id = (snowflake >> PROCESS_ID_LOC) & PROCESS_ID_MASK
counter = snowflake & INCREMENT_MASK counter = snowflake & INCREMENT_MASK