help_team.py 2.0 KB

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