from models.user import User from models.level import Level from models.item import Item from models.inventory import Inventory from mee6_py_api import API from base import db import datetime import discord import yaml import logging logger = logging.getLogger("peewee") logger.addHandler(logging.StreamHandler()) logger.setLevel(logging.DEBUG) with open("config.yaml", "r") as yamlfile: config = yaml.load(yamlfile, Loader=yaml.CLoader) print("Read config successful") config = config intents = discord.Intents.default() intents.members = True client = discord.Client(intents=intents) # Runs once bot has connected to discord @client.event async def on_ready(): db.create_tables([User, Level, Item, Inventory]) time_now = datetime.datetime.utcnow() print(f"{time_now}: {client.user} has connected to Discord!") for guild in client.guilds: if guild.id == config["discord_server_id"]: print(f"{time_now}: {client.user} has joined {guild.name}!") await populate_users(guild.members) # cur_exp = 0 # for i in range(10): # print(f"level {i}: {cur_exp} exp") # level = Level(level=i, exp_required=cur_exp) # level.save() # print(f"{cur_exp} + 5 * {(pow(i,2))} + {(50 * i)} + 100") # cur_exp = cur_exp + 5 * pow(i, 2) + (50 * i) + 100 populate_levels() populate_items() async def populate_users(members: list): users = [] for member in 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() def populate_levels(): cur_exp = 0 for i in range(100): reward_currency = i * 10 if i % 10 == 0: reward_currency *= 1.5 print(f"level {i}: {cur_exp} exp, {reward_currency} coins") level = Level(level=i, exp_required=cur_exp, reward_currency=reward_currency) level.save(force_insert=True) print(f"{cur_exp} + 5 * {(pow(i,2))} + {(50 * i)} + 100") cur_exp = cur_exp + 5 * pow(i, 2) + (50 * i) + 100 def populate_items(): with open("shop.yaml", "r") as yamlfile: shop = yaml.load(yamlfile, Loader=yaml.CLoader) print("Read shop successful") for name, shop_item in shop["items"].items(): print(f"{name}: {shop_item}") if "rank" in shop_item.keys(): item = Item( name=name, price=shop_item["price"], text=shop_item["text"], rank=shop_item["rank"] ) else: item = Item(name=name, price=shop_item["price"], text=shop_item["text"]) item.save() client.run(config["discord_token"])