From 23023a891632bf2a6f7cc87b678f1306e8bab582 Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Mon, 6 Mar 2023 04:23:23 +0000 Subject: [PATCH 1/6] Move all AFK system commands to a cog --- cogs/afk.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 37 --------------------------------- 2 files changed, 59 insertions(+), 37 deletions(-) create mode 100644 cogs/afk.py diff --git a/cogs/afk.py b/cogs/afk.py new file mode 100644 index 00000000..7176236e --- /dev/null +++ b/cogs/afk.py @@ -0,0 +1,59 @@ +# Imports +import discord +import json +import time +from discord import option, ApplicationContext, SlashCommandGroup +from discord.ext import commands + +# Variables +with open("database/presence.json", 'r') as f: presence = json.load(f) + +def save(): + with open("database/presence.json", 'w+') as f: presence = json.dump(presence, f) + +# Commands +class Presence(commands.Cog): + def __init__(self, bot): + self.bot = bot + + afk_system = SlashCommandGroup("afk", "Commands related to managing your AFK status.") + + @afk_system.command( + name="set", + description="Sets your AFK status with a custom response" + ) + @option(name="response", description="What do you want your AFK response to be?", type=str, default="I'm AFK") + async def afk_set(self, ctx: ApplicationContext, response:str="I'm AFK"): + exctime = time.time() + if str(ctx.guild.id) not in presence: presence[str(ctx.guild.id)] = {} + presence[str(ctx.guild.id)][str(ctx.author.id)] = {"type": "afk", "time": exctime, "response": response} + save() + localembed = discord.Embed(title=f"{ctx.author.display_name} is now AFK.", description=f"Response: {response}", color=discord.Color.dark_orange()) + await ctx.respond(embed=localembed) + + @afk_system.command( + name="remove", + description="Removes your AFK status" + ) + async def afk_remove(self, ctx: ApplicationContext): + try: + del presence[str(ctx.guild.id)][str(ctx.author.id)] + save() + await ctx.respond(f"Alright {ctx.author.mention}, I've removed your AFK.") + except KeyError: return await ctx.respond("You weren't previously AFK.", ephemeral=True) + + @afk_system.command( + name="mod_remove", + description="Removes an AFK status for someone else" + ) + @option(name="user", description="Whose AFK status do you want to remove?", type=discord.User) + async def afk_mod_remove(self, ctx: ApplicationContext, user:discord.User): + if not ctx.author.guild_permissions.manage_messages: return await ctx.respond("You don't have the required permissions to use this.", ephemeral=True) + try: + del presence[str(ctx.guild.id)][str(user.id)] + save() + await ctx.respond(f"{user.display_name}'s AFK has been removed.") + except KeyError: return await ctx.respond("That user isn't AFK.", ephemeral=True) + +# Cog Initialization +def setup(bot): bot.add_cog(Presence(bot)) diff --git a/main.py b/main.py index a8b6c852..4c365ef4 100644 --- a/main.py +++ b/main.py @@ -333,43 +333,6 @@ async def reload(ctx: ApplicationContext, cog: str): # AFK System Commands afk_system = client.create_group("afk", "Commands for interacting with the built-in AFK system.") -@afk_system.command( - name="set", - description="Sets your AFK status with a custom response" -) -@option(name="response", description="What do you want your AFK response to be?", type=str, default="I'm AFK") -async def afk_set(ctx: ApplicationContext, response:str="I'm AFK"): - exctime = time.time() - if str(ctx.guild.id) not in presence: presence[str(ctx.guild.id)] = {} - presence[str(ctx.guild.id)][str(ctx.author.id)] = {"type": "afk", "time": exctime, "response": response} - save() - localembed = discord.Embed(title=f"{ctx.author.display_name} is now AFK.", description=f"Response: {response}", color=discord.Color.dark_orange()) - await ctx.respond(embed=localembed) - -@afk_system.command( - name="remove", - description="Removes your AFK status" -) -async def afk_remove(ctx: ApplicationContext): - try: - del presence[str(ctx.guild.id)][str(ctx.author.id)] - save() - await ctx.respond(f"Alright {ctx.author.mention}, I've removed your AFK.") - except KeyError: return await ctx.respond("You weren't previously AFK.", ephemeral=True) - -@afk_system.command( - name="mod_remove", - description="Removes an AFK status for someone else" -) -@option(name="user", description="Whose AFK status do you want to remove?", type=discord.User) -async def afk_mod_remove(ctx: ApplicationContext, user:discord.User): - if not ctx.author.guild_permissions.manage_messages: return await ctx.respond("You don't have the required permissions to use this.", ephemeral=True) - try: - del presence[str(ctx.guild.id)][str(user.id)] - save() - await ctx.respond(f"{user.display_name}'s AFK has been removed.") - except KeyError: return await ctx.respond("That user isn't AFK.", ephemeral=True) - # IsoCoins commands isocoin_system = client.create_group("isocoin", "Commands related to the IsoCoin rewards system.") From 7928c702f073a7df8d16eab00e3d3da268f9223c Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Mon, 6 Mar 2023 04:24:59 +0000 Subject: [PATCH 2/6] Remove AFK slash command group in main file --- cogs/afk.py | 2 +- main.py | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/cogs/afk.py b/cogs/afk.py index 7176236e..11f1b3f0 100644 --- a/cogs/afk.py +++ b/cogs/afk.py @@ -16,7 +16,7 @@ class Presence(commands.Cog): def __init__(self, bot): self.bot = bot - afk_system = SlashCommandGroup("afk", "Commands related to managing your AFK status.") + afk_system = SlashCommandGroup("afk", "Commands for interacting with the AFK system.") @afk_system.command( name="set", diff --git a/main.py b/main.py index 4c365ef4..7e263974 100644 --- a/main.py +++ b/main.py @@ -330,9 +330,6 @@ async def reload(ctx: ApplicationContext, cog: str): await ctx.respond(embed=discord.Embed(description=f"{cog} cog successfully reloaded.", color=discord.Color.green())) except: await ctx.respond(embed=discord.Embed(description=f"{cog} cog not found.", color=discord.Color.red())) -# AFK System Commands -afk_system = client.create_group("afk", "Commands for interacting with the built-in AFK system.") - # IsoCoins commands isocoin_system = client.create_group("isocoin", "Commands related to the IsoCoin rewards system.") From 36948e243bf3fa28676cc7ed21d44b0801e8b84a Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Mon, 6 Mar 2023 04:25:53 +0000 Subject: [PATCH 3/6] Add AFK cog loading on bot startup --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 7e263974..7b6d0f09 100644 --- a/main.py +++ b/main.py @@ -360,7 +360,7 @@ async def isocoin_shop(ctx: ApplicationContext): await ctx.respond("IsoCoin shop is coming soon! Check back later for new items.") # Initialization -active_cogs = ["economy", "maths", "moderation", "reddit", "minigames", "automod", "isobank", "levelling", "fun"] +active_cogs = ["economy", "maths", "moderation", "reddit", "minigames", "automod", "isobank", "levelling", "fun", "afk"] i = 1 cog_errors = 0 for x in active_cogs: From aded0b51fc67c5e030b390f9fb9301791d203f8b Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Mon, 6 Mar 2023 04:26:52 +0000 Subject: [PATCH 4/6] Spread out active cogs list variable --- main.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 7b6d0f09..dec4659e 100644 --- a/main.py +++ b/main.py @@ -360,7 +360,18 @@ async def isocoin_shop(ctx: ApplicationContext): await ctx.respond("IsoCoin shop is coming soon! Check back later for new items.") # Initialization -active_cogs = ["economy", "maths", "moderation", "reddit", "minigames", "automod", "isobank", "levelling", "fun", "afk"] +active_cogs = [ + "economy", + "maths", + "moderation", + "reddit", + "minigames", + "automod", + "isobank", + "levelling", + "fun", + "afk" +] i = 1 cog_errors = 0 for x in active_cogs: From c5de6165e53612298069c1361c375394799033a5 Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Mon, 6 Mar 2023 04:40:34 +0000 Subject: [PATCH 5/6] Add function to get user presence status and data --- cogs/afk.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/cogs/afk.py b/cogs/afk.py index 11f1b3f0..d1713c7c 100644 --- a/cogs/afk.py +++ b/cogs/afk.py @@ -11,6 +11,18 @@ def save(): with open("database/presence.json", 'w+') as f: presence = json.dump(presence, f) +# Functions +def get_presence(user_id: int, guild_id: int) -> dict: + """Returns a `dict` of the specified user's current AFK status in the guild.""" + if str(user_id) in presence[str(guild_id)]: + return { + "afk": True, + "response": presence[str(guild_id)][str(user_id)]['response'], + "time": presence[str(guild_id)][str(user_id)]['time'] + } + else: + return False + # Commands class Presence(commands.Cog): def __init__(self, bot): From 3f065151500e69ecb5c8516da422d5d7c003ede0 Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Mon, 6 Mar 2023 04:47:42 +0000 Subject: [PATCH 6/6] Move `/whoami` from main file to utils cog --- cogs/utils.py | 32 ++++++++++++++++++++++++++++++++ main.py | 29 ----------------------------- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/cogs/utils.py b/cogs/utils.py index a018dc69..afd1bc24 100644 --- a/cogs/utils.py +++ b/cogs/utils.py @@ -2,11 +2,13 @@ import discord import os import psutil +import math import framework.isobot.embedengine from discord import option, ApplicationContext from discord.ext import commands from cogs.economy import get_wallet, get_bank, get_user_networth, get_user_count from cogs.levelling import get_level, get_xp +from cogs.afk import get_presence # Variables color = discord.Color.random() @@ -48,6 +50,36 @@ async def embedbuilder(self, ctx: ApplicationContext, title: str, description: s await ctx.respond("Embed Built!", ephemeral=True) await ctx.channel.send(embed=framework.isobot.embedengine.embed(title, description, image=image_url, thumbnail=thumbnail_url, color=color, footer_text=footer_text, footer_img=footer_icon_url)) + @commands.slash_command( + name='whoami', + description='Shows information on a user' + ) + @option(name="user", description="Who do you want to know about?", type=discord.User, default=None) + async def whoami(self, ctx: ApplicationContext, user: discord.User=None): + if user == None: user = ctx.author + username = user + displayname = user.display_name + registered = user.joined_at.strftime("%b %d, %Y, %T") + pfp = user.avatar + localembed_desc = f"`AKA` {displayname}" + presence = get_presence(ctx.author.id, ctx.guild.id) + if presence != False: localembed_desc += f"\n`🌙 AFK` {presence['response']} - " + localembed = discord.Embed( + title=f'User Info on {username}', + description=localembed_desc + ) + localembed.set_thumbnail(url=pfp) + localembed.add_field(name='Username', value=username, inline=True) + localembed.add_field(name='Display Name', value=displayname, inline=True) + localembed.add_field(name='Joined Discord on', value=registered, inline=False) + localembed.add_field(name='Avatar URL', value=f"[here!]({pfp})", inline=True) + role_render = "" + for p in user.roles: + if p != user.roles[0]: role_render += f"<@&{p.id}> " + localembed.add_field(name='Roles', value=role_render, inline=False) + localembed.add_field(name="Net worth", value=f"{get_user_networth(user.id)} coins", inline=False) + await ctx.respond(embed=localembed) + @commands.slash_command( name="profile", description="Shows basic stats about your isobot profile, or someone else's profile stats" diff --git a/main.py b/main.py index dec4659e..6a7eebad 100644 --- a/main.py +++ b/main.py @@ -239,35 +239,6 @@ async def help(ctx: ApplicationContext, command:str=None): await user.send(embed=localembed) await ctx.respond("Check your direct messages.", ephemeral=True) -@client.slash_command( - name='whoami', - description='Shows information on a user' -) -@option(name="user", description="Who do you want to know about?", type=discord.User, default=None) -async def whoami(ctx: ApplicationContext, user: discord.User=None): - if user == None: user = ctx.author - username = user - displayname = user.display_name - registered = user.joined_at.strftime("%b %d, %Y, %T") - pfp = user.avatar - localembed_desc = f"`AKA` {displayname}" - if str(user.id) in presence[str(ctx.guild.id)]: localembed_desc += f"\n`🌙 AFK` {presence[str(ctx.guild.id)][str(user.id)]['response']} - " - localembed = discord.Embed( - title=f'User Info on {username}', - description=localembed_desc - ) - localembed.set_thumbnail(url=pfp) - localembed.add_field(name='Username', value=username, inline=True) - localembed.add_field(name='Display Name', value=displayname, inline=True) - localembed.add_field(name='Joined Discord on', value=registered, inline=False) - localembed.add_field(name='Avatar URL', value=f"[here!]({pfp})", inline=True) - role_render = "" - for p in user.roles: - if p != user.roles[0]: role_render += f"<@&{p.id}> " - localembed.add_field(name='Roles', value=role_render, inline=False) - localembed.add_field(name="Net worth", value=f"{get_user_networth(user.id)} coins", inline=False) - await ctx.respond(embed=localembed) - # DevTools commands @client.slash_command( name='sync',