help_cache.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import discord
  2. from discord.ext import commands
  3. from discord.commands import Option
  4. from discord.commands import slash_command
  5. import json
  6. from pathlib import Path
  7. class helpcache(commands.Cog):
  8. def __init__(self, bot: discord.Bot):
  9. self.bot = bot
  10. #Command initialization
  11. @slash_command(name="help_cache", description= "Get Infos")
  12. async def help_cache(
  13. self,
  14. ctx,
  15. ):
  16. server = ctx.guild
  17. json_path = Path(__file__).resolve().parent.parent.joinpath("json_files", "help_cache.json")
  18. if not json_path.exists():
  19. await ctx.respond("The .json file is missing.")
  20. return
  21. try:
  22. with json_path.open("r", encoding="utf-8") as f:
  23. json_data = json.load(f)
  24. except json.JSONDecodeError:
  25. await ctx.respond("The .json file is not valid JSON.")
  26. return
  27. if isinstance(json_data, dict):
  28. entries = [json_data]
  29. elif isinstance(json_data, list):
  30. entries = json_data
  31. else:
  32. await ctx.respond("The .json file has an unexpected structure.")
  33. return
  34. if not entries or not isinstance(entries[0], dict):
  35. await ctx.respond("The .json file has an unexpected structure.")
  36. return
  37. if not entries:
  38. await ctx.respond("The .json file is empty.")
  39. return
  40. entry = entries[0]
  41. jstitle = entry.get("title", "Help")
  42. jsdesc = entry.get("desc", "No description provided.")
  43. embed = discord.Embed(
  44. title=f"{jstitle}",
  45. description=f"{jsdesc}",
  46. color=discord.Color.yellow()
  47. )
  48. embed.set_thumbnail(url=server.icon)
  49. embed.set_author(name="VicePD", icon_url="https://i.imgur.com/6QteFrg.png")
  50. embed.set_footer(text="VicePD - Bot | Made by BaumSplitter41")
  51. await ctx.respond(embed=embed)
  52. def setup(bot: discord.Bot):
  53. bot.add_cog(helpcache(bot))