From 439022f058cb2b9bbf6e2b46c067f4c828a1cab0 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Thu, 25 Nov 2021 03:25:44 +0000 Subject: Initial commit --- initialise_db.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 initialise_db.py (limited to 'initialise_db.py') diff --git a/initialise_db.py b/initialise_db.py new file mode 100644 index 0000000..62e6316 --- /dev/null +++ b/initialise_db.py @@ -0,0 +1,43 @@ +from models.user import User +from mee6_py_api import API +from base import db, config, client +import datetime + + +# Runs once bot has connected to discord +@client.event +async def on_ready(): + + db.create_tables([User]) + time_now = datetime.datetime.utcnow() + print(f"{time_now}: {client.user} has connected to Discord!") + users = [] + for guild in client.guilds: + if guild.id == config["discord_server_id"]: + print(f"{time_now}: {client.user} has joined {guild.name}!") + for member in guild.members: + print(member) + print(member.display_name) + print(member.nick) + print(member.joined_at) + users.append( + User( + username=member.name, + display_name=member.display_name, + discord_id=member.id, + joined_date=member.joined_at, + ) + ) + mee6API = API(config["discord_server_id"]) + mee6_levels = await mee6API.levels.get_leaderboard_page(0) + for user in users: + for mee6_user in mee6_levels["players"]: + if mee6_user["username"] == user.username: + user.level = mee6_user["level"] + user.exp = mee6_user["xp"] + user.message_count = mee6_user["message_count"] + print(user) + user.save() + + +client.run(config["discord_token"]) -- cgit v1.2.3