From c8918719f3d0555fcc290555789fc01b0fe5f6a7 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sun, 7 Aug 2022 09:56:15 +0530 Subject: [PATCH 1/3] Add levelling system --- database/levels.json | 1 + main.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 database/levels.json diff --git a/database/levels.json b/database/levels.json new file mode 100644 index 00000000..9e26dfee --- /dev/null +++ b/database/levels.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/main.py b/main.py index d6a8d335..5ef54547 100644 --- a/main.py +++ b/main.py @@ -31,6 +31,7 @@ with open('database/items.json', 'r') as f: items = json.load(f) with open('config/shop.json', 'r') as f: shopitem = json.load(f) with open('database/presence.json', 'r') as f: user_presence = json.load(f) +with open('database/levels.json', 'r') as f: levels = json.load(f) #Pre-Initialization Commands def timenow(): datetime.datetime.now().strftime("%H:%M:%S") @@ -39,6 +40,7 @@ def save(): with open('database/warnings.json', 'w+') as f: json.dump(warnings, f, indent=4) with open('database/items.json', 'w+') as f: json.dump(items, f, indent=4) with open('database/presence.json', 'w+') as f: json.dump(user_presence, f, indent=4) + with open('database/levels.json', 'w+') as f: json.dump(levels, f, indent=4) if not os.path.isdir("logs"): os.mkdir('logs') @@ -82,6 +84,7 @@ async def on_message(ctx): if str(ctx.author.id) not in warnings: warnings[str(ctx.guild.id)] = {} if str(ctx.author.id) not in warnings[str(ctx.guild.id)]: warnings[str(ctx.guild.id)][str(ctx.author.id)] = [] if str(ctx.author.id) not in items: items[str(ctx.author.id)] = {} + if str(ctx.author.id) not in levels: levels[str(ctx.author.id)] = {"xp": 0, "level": 0} for z in shopitem: if z in items[str(ctx.author.id)]: pass else: items[str(ctx.author.id)][str(z)] = 0 @@ -98,6 +101,22 @@ async def on_message(ctx): m1 = await ctx.channel.send(f"Welcome back {ctx.author.mention}. Your AFK has been removed.") await asyncio.sleep(5) await m1.delete() + if not ctx.author.bot: + levels[str(ctx.author.id)]["xp"] += randint(1, 5) + # if levelupchannel[str(message.guild.id)] == 0: + # channelid = message.channel + # else: + # channelid = client.get_channel(levelupchannel[str(message.guild.id)]) + # ^ Saving for later when server-based leveling implementation is added + xpreq = 0 + for level in range(int(levels[str(ctx.author.id)]["level"])): + xpreq += 50 + if xpreq >= 5000: break + if levels[str(ctx.author.id)]["xp"] >= xpreq: + levels[str(ctx.author.id)]["xp"] = 0 + levels[str(ctx.author.id)]["level"] += 1 + await ctx.channel.send(f"{ctx.author.mention}, you are now level **{levels[str(message.guild.id)][str(message.author.id)]}**. Nice!") + save() #Error handler @client.event From 3fd413d7c71471e420199cedbaf9d5da68ed6e60 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sun, 7 Aug 2022 10:23:40 +0530 Subject: [PATCH 2/3] Add `/rank` command This command can be used to display your rank, or another user's rank. --- main.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/main.py b/main.py index 5ef54547..0670b751 100644 --- a/main.py +++ b/main.py @@ -921,6 +921,22 @@ async def afk_mod_remove(ctx:SlashContext, user:discord.User): except KeyError: return await ctx.send("That user isn't AFK.", hidden=True) +@slash.slash( + name="rank", + description="Shows your rank or another user's rank", + options=[ + create_option(name="user", description="Who's rank do you want to view?", option_type=6, required=False) + ] +) +async def rank(ctx:SlashContext, user:discord.User=None): + if user == None: user = ctx.author.id + try: + localembed = discord.Embed(title="{user.display_name}'s rank", color=discord.Color.random()) + localembed.add_field(name="Level", value=levels[str(user.id)]["level"]) + localembed.add_field(name="XP", value=levels[str(user.id)]["xp"]) + localembed.set_footer(text="Keep chatting to earn levels!", icon_url=ctx.author.avatar_url) + except KeyError: return await ctx.send("Looks like that user isn't indexed yet. Try again later.", hidden=True) + # Initialization utils.ping.host() client.run(api.auth.get_token()) From 335ec5c2cb7dbfa2d97204a3fd920e594480421b Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sun, 7 Aug 2022 10:36:52 +0530 Subject: [PATCH 3/3] Add `/edit_rank` and `/edit_xp` dev commands For the devs ONLY. --- main.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/main.py b/main.py index 0670b751..30fa363e 100644 --- a/main.py +++ b/main.py @@ -937,6 +937,36 @@ async def rank(ctx:SlashContext, user:discord.User=None): localembed.set_footer(text="Keep chatting to earn levels!", icon_url=ctx.author.avatar_url) except KeyError: return await ctx.send("Looks like that user isn't indexed yet. Try again later.", hidden=True) +@slash.slash( + name="edit_rank", + description="Edits a user's rank. (DEV ONLY)", + options=[ + create_option(name="user", description="Who's rank do you want to edit?", option_type=6, required=True), + create_option(name="new_rank", decription="The new rank you want to set for the user", option_type=4, required=True) + ] +) +async def edit_rank(ctx:SlashContext, user:discord.User, new_rank:int): + if ctx.author.id != 738290097170153472: return await ctx.send("This command isn't for you.", hidden=True) + try: + levels[str(user.id)]["level"] = new_rank + await ctx.reply(f"{user.display_name}\'s rank successfully edited. `New Rank: {levels[str(user.id)]['level']}`") + except KeyError: return await ctx.reply("That user isn't indexed yet.", hidden=True) + +@slash.slash( + name="edit_xp", + description="Edits a user's XP. (DEV ONLY)", + options=[ + create_option(name="user", description="Who's rank do you want to edit?", option_type=6, required=True), + create_option(name="new_xp", decription="The new xp count you want to set for the user", option_type=4, required=True) + ] +) +async def edit_rank(ctx:SlashContext, user:discord.User, new_xp:int): + if ctx.author.id != 738290097170153472: return await ctx.send("This command isn't for you.", hidden=True) + try: + levels[str(user.id)]["xp"] = new_xp + await ctx.reply(f"{user.display_name}\'s XP count successfully edited. `New XP: {levels[str(user.id)]['xp']}`") + except KeyError: return await ctx.reply("That user isn't indexed yet.", hidden=True) + # Initialization utils.ping.host() client.run(api.auth.get_token())