Skip to content

Instantly share code, notes, and snippets.

@zekroTJA
Last active October 12, 2021 03:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zekroTJA/7502d0e867edc75c3450c3d86414c54e to your computer and use it in GitHub Desktop.
Save zekroTJA/7502d0e867edc75c3450c3d86414c54e to your computer and use it in GitHub Desktop.
Get the oldest user account from a dgrs cache.
import redis
import argparse
import json
import datetime
def inject_creation_date(u):
uid = int(u.get('id'))
ts = (uid >> 22) + 1420070400000
u['created_date'] = datetime.datetime.fromtimestamp(
ts / 1000).strftime('%Y-%m-%d %H:%M:%S')
def main():
parser = argparse.ArgumentParser('oldest-discord-account')
parser.add_argument('--redis-connection', '-rc', type=str,
help='Redis connection string')
parser.add_argument('--guild', '-g', type=str,
help='A guild ID to pick members from')
parser.add_argument('--number', '-n', type=int,
default=5, help='Number of accounts shown')
args = parser.parse_args()
r = redis.from_url(args.redis_connection)
keys = r.keys(f'dgrs:member:{args.guild}:*') \
if args.guild else r.keys('dgrs:user:*')
uids_str = [str(k).split(':')[-1][:-1] for k in keys]
uids = sorted([int(uid_str) for uid_str in uids_str if uid_str != '@me'])
oldest_uids = uids[:args.number]
resolved_users = [
json.loads(r.get(f'dgrs:user:{uid}'))
for uid in oldest_uids
]
[inject_creation_date(u) for u in resolved_users]
for i, u in enumerate(resolved_users):
username = u.get('username')
discriminator = u.get('discriminator')
uid = u.get('id')
created_date = u.get('created_date')
print(
f'[{i+1}] Username: {username}#{discriminator}\n'
f' ID: {uid}\n'
f' Created: {created_date}'
)
r.close()
pass
if __name__ == '__main__':
main()

How to Use

This script is ment to be used with a Redis instance connected to a Discord bot which utilizes zekrotja/drgs for cache management.

  1. Clone the gist in an empty directory.
    git clone https://gist.github.com/zekroTJA/7502d0e867edc75c3450c3d86414c54e.git .
    
  2. Run a temporary python:alpine Docker container inside the network of your Redis instance with the script mounted and execute the script inside it.
    docker run --rm \
      -v $PWD/dgrs-oldest-user.py:/tmp/dgrs-oldest-user.py:ro \
      --network <the network ID your Redis container is in> \
      python:alpine \
        /bin/ash -c 'python3 -m pip install redis && python3 /tmp/dgrs-oldest-user.py -rs redis://<your Redis address>:6379/0'
    

If you want to list your Docker networks available, use the following command.

docker network ls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment