From 090a3b92ac7db6a35348d1626ef0bc2f8dcb2c2b Mon Sep 17 00:00:00 2001 From: "William C." Date: Wed, 6 Mar 2024 20:04:30 -0500 Subject: [PATCH] fixed richest, added afk, some other bug fixes --- cogs/economy.py | 2 +- cogs/utility.py | 49 +++++++++++++++++++++++++++++++------ main.py | 65 ++++++++++++++++++++++++++++++++----------------- 3 files changed, 85 insertions(+), 31 deletions(-) diff --git a/cogs/economy.py b/cogs/economy.py index f72c806..5892856 100644 --- a/cogs/economy.py +++ b/cogs/economy.py @@ -419,7 +419,7 @@ async def work(ctx): async def richest(ctx): lb = [] count = 0 - d = userdb.find().sort([("levels.totalxp", pymongo.DESCENDING)]).limit(10) + d = userdb.find().sort([("economy.total", pymongo.DESCENDING)]).limit(10) for doc in (await d.to_list(length=10)): total = doc['economy']['wallet'] + doc['economy']['bank'] count += 1 diff --git a/cogs/utility.py b/cogs/utility.py index ec86535..3a8bfe2 100644 --- a/cogs/utility.py +++ b/cogs/utility.py @@ -348,8 +348,8 @@ async def suggest(ctx, *, message): description="Get your personal line of prefixes!", ) async def prefixes(ctx): - if userdb.find_one({'userid':ctx.author.id}): - prefixes = userdb.find_one({'userid':ctx.author.id})['prefixes'] + if (await userdb.find_one({'userid':ctx.author.id})): + prefixes = (await userdb.find_one({'userid':ctx.author.id}))['prefixes'] embed = voltage.SendableEmbed( title="Your prefixes!", description=f"Your prefixes are:{sep} ```{sep}{f'{sep}'.join(prefixes)}{sep}```", @@ -365,14 +365,14 @@ async def prefixes(ctx): description="Add a prefix to your personal line of prefixes!", ) async def addprefix(ctx, *, prefix): - if userdb.find_one({'userid':ctx.author.id}): + if (await userdb.find_one({'userid':ctx.author.id})): if prefix[0] == " ": return await ctx.send("Prefix cannot start with a space!") - elif any(x in prefix for x in userdb.find_one({'userid':ctx.author.id})['prefixes']): + elif any(x in prefix for x in (await userdb.find_one({'userid':ctx.author.id}))['prefixes']): return await ctx.send("This prefix is already in your list of prefixes!") else: - userdb.update_one({'userid':ctx.author.id}, {'$push':{'prefixes':prefix}}) - prefixes = userdb.find_one({'userid':ctx.author.id})['prefixes'] + await userdb.update_one({'userid':ctx.author.id}, {'$push':{'prefixes':prefix}}) + prefixes = (await userdb.find_one({'userid':ctx.author.id}))['prefixes'] embed = voltage.SendableEmbed( title="Added a new prefix to your list!", description=f"Added `{prefix}` to your list of {len(prefixes)}!{sep}To see your prefixes, type `m!prefixes` or alternatively; mention me!", @@ -388,10 +388,10 @@ async def addprefix(ctx, *, prefix): description="Remove a prefix from your personal line of prefixes!", ) async def removeprefix(ctx, *, prefix): - if userdb.find_one({'userid':ctx.author.id}): + if (await userdb.find_one({'userid':ctx.author.id})): if len((await userdb.find_one({'userid':ctx.author.id})['prefixes'])) > 1 : if prefix in (await userdb.find_one({'userid':ctx.author.id}))['prefixes']: - userdb.update_one({'userid':ctx.author.id}, {'$pull':{'prefixes':prefix}}) + await userdb.update_one({'userid':ctx.author.id}, {'$pull':{'prefixes':prefix}}) prefixes = (await userdb.find_one({'userid':ctx.author.id}))['prefixes'] embed = voltage.SendableEmbed( title="Removed a prefix from your list!", @@ -514,6 +514,39 @@ async def familyfriendly(ctx, toggle): ) await ctx.reply(embed=embed) + @utility.command(name="afk", description="Set your AFK status!", aliases=["awayfromkeyboard", 'brb']) + async def afk(ctx, *, reason: str = None): + if (await userdb.find_one({'userid': ctx.author.id})): + await userdb.update_one( + { + 'userid': ctx.author.id + }, + { + '$set': { + f'status.afk.{ctx.server.id}': { + f'reason': reason, + f'lastseen': int(time.time()), + f'afk': True + } + } + } + ) + embed = voltage.SendableEmbed( + title=ctx.author.display_name, + icon_url=ctx.author.display_avatar.url, + description=f"Set your AFK status to:{sep}{reason}", + color="#00FF00" + ) + await ctx.reply(embed=embed) + else: + embed = voltage.SendableEmbed( + title=ctx.author.display_name, + icon_url=ctx.author.display_avatar.url, + description="Please create an account with `m!add`!", + color="#dc3545", + ) + await ctx.reply(embed=embed) + @utility.command(name="inbox", description="View your inbox!") async def inbox(ctx, page: int = 1): notifications_per_page = 5 diff --git a/main.py b/main.py index 86909c7..e97ba56 100644 --- a/main.py +++ b/main.py @@ -135,28 +135,25 @@ async def serverupdate(): import time -"""for i in await userdb.find({}): - userdb.bulk_write( - [ - pymongo.UpdateOne( - {'userid':i['userid']}, - {'$set':{'notifications.inbox': { - "1":{ - "message": f"Welcome to Mecha, {i['username']}!{sep}To get started, type `m!help` in this server to get started!", - "date": time.time(), - "title": "Welcome To Mecha!", - "type": "bot", - "read": False +async def upd(): + for i in (await userdb.find({})): + userdb.bulk_write( + [ + pymongo.UpdateOne( + {'userid':i['userid']}, + {'$set': + { + "status.afk": {} + } } - }}} - ), - ] - ) - print(f"Updated {i['username']}!") -""" + ), + ] + ) + print(f"Updated {i['username']}!") + async def update_level(user:voltage.User): - if await userdb.find_one({'userid':user.id}): + if (await userdb.find_one({'userid':user.id})): user_data = await userdb.find_one({'userid':user.id}) lvl = user_data['levels']['level'] xp = user_data['levels']['xp'] @@ -302,8 +299,8 @@ async def get_user(user: voltage.User): async def give_xp(user: voltage.User, xp:int): await userdb.update_one( - {"userid": user.id}, - {"$inc": {"levels.xp": xp}} + {"userid": user.id}, + {"$inc": {"levels.xp": xp}} ) prefixes = ["m!"] @@ -451,8 +448,31 @@ async def stayon(): await channel.send(embed=embed) await asyncio.sleep(60*60) i += 1 - +async def afkCheck(message): + try: + if (await userdb.find_one({"userid": message.author.id})): + if (await userdb.find_one({"userid": message.author.id}))['status']['afk'][message.server.id]['afk']: + if (await userdb.find_one({"userid": message.author.id}))['status']['afk'][message.server.id]['lastseen'] + 2 < int(time.time()): + await userdb.update_one( + {"userid": message.author.id}, + {"$set": {"status.afk.{}".format(message.server.id): {"afk": False}}} + ) + embed = voltage.SendableEmbed( + title="AFK!", + description=f"Welcome back, {message.author.mention}!{sep}I've removed your AFK status!", + colour="#00ff00", + icon_url=message.author.avatar.url + ) + await message.reply(embed=embed, delete_after=5) + else: + return "TooEarly" + else: + return "NotAfk" + else: + return "DoesntExist" + except: + return @client.listen("ready") async def ready(): @@ -554,6 +574,7 @@ async def on_message(message): if message.channel.id == message.author.id: return asyncio.create_task(levelstuff(message)) # pièce de résistance + asyncio.create_task(afkCheck(message)) asyncio.create_task(loggingstuff(message)) await client.handle_commands(message) # so everything else doesnt trip over its clumsy ass selves."