Skip to content

Commit

Permalink
Config summary owner command
Browse files Browse the repository at this point in the history
  • Loading branch information
stekc committed Oct 9, 2024
1 parent 55e0f8b commit ee5df5b
Showing 1 changed file with 74 additions and 1 deletion.
75 changes: 74 additions & 1 deletion cogs/owner.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from discord.ext import commands
from discord.ext.commands import Context

from utils.jsons import ConfigJSON
from utils.jsons import ConfigJSON, SocialsJSON, TrackingJSON


class Owner(commands.Cog, name="owner"):
Expand Down Expand Up @@ -244,6 +244,79 @@ async def setpfp(self, context: Context, *, image: discord.Attachment) -> None:
)
await context.send(embed=embed)

@commands.hybrid_command(
name="configsummary",
description="Summarize the configuration database.",
)
@app_commands.guilds(discord.Object(id=config["main_guild_id"]))
@commands.is_owner()
async def config_summary(self, context: Context) -> None:
config_cog = self.bot.get_cog("Config")
if not config_cog:
await context.send("Config cog not found.")
return

async with config_cog.db.execute("SELECT * FROM guild_config") as cursor:
guild_rows = await cursor.fetchall()

async with config_cog.db.execute("SELECT * FROM user_config") as cursor:
user_rows = await cursor.fetchall()

socials = SocialsJSON().load_json()
tracking = TrackingJSON().load_json()

total_servers = len(self.bot.guilds)
summary = {"Services": {}, "Link Tracking": {}}

for social in socials:
summary["Services"][social] = {"enabled": total_servers, "disabled": 0}

for tracker in tracking:
summary["Link Tracking"][tracker] = {"disabled": 0}

for row in guild_rows:
guild_id, config_json = row
config = json.loads(config_json)

for social in socials:
if social in config:
if not config[social].get("enabled", True):
summary["Services"][social]["enabled"] -= 1
summary["Services"][social]["disabled"] += 1

for row in user_rows:
user_id, config_json = row
config = json.loads(config_json)

for tracker in tracking:
if tracker in config:
if not config[tracker].get("enabled", True):
summary["Link Tracking"][tracker]["disabled"] += 1

embed = discord.Embed(title="Configuration Summary", color=0xBEBEFE)

services_content = []
for service, counts in summary["Services"].items():
services_content.append(
f"{service.title().replace('Tiktok', 'TikTok').replace('Imdb', 'IMDb')}: {counts['enabled']} enabled, {counts['disabled']} disabled"
)
embed.add_field(
name="Services", value="\n".join(services_content), inline=False
)

tracking_content = []
for tracker, counts in summary["Link Tracking"].items():
tracking_content.append(
f"{tracker.title().replace('Tiktok', 'TikTok')}: {counts['disabled']} users disabled"
)
embed.add_field(
name="Link Tracking",
value="\n".join(tracking_content),
inline=False,
)

await context.send(embed=embed)


async def setup(bot) -> None:
await bot.add_cog(Owner(bot))

0 comments on commit ee5df5b

Please sign in to comment.