mekasu0124

Untitled

Feb 10th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.69 KB | None | 0 0
  1. import disnake
  2. import json
  3.  
  4. from disnake.ext import commands
  5. from disnake.enums import ButtonStyle
  6. from services.paginator import CategoryPaginator
  7. from server.database import DatabaseEngine
  8.  
  9. with open("./server/config.json", 'r', encoding="utf-8-sig") as f:
  10.     data = json.load(f)
  11.  
  12. guild_id = [data["guild_id"],]
  13.  
  14. class GeneralCommands(commands.Cog):
  15.     def __init__(self, bot):
  16.         self.bot = bot
  17.         self.db_engine = DatabaseEngine()
  18.  
  19.     @commands.slash_command(
  20.         name = "meks_hub",
  21.         description = "Returns a paginator embed as a \"flip book\" of github repo's",
  22.         guild_ids = guild_id
  23.     )
  24.     async def meks_hub(self, inter):
  25.         await inter.response.defer()
  26.  
  27.         guild = self.bot.get_guild(guild_id[0])
  28.  
  29.         # build table of contents embed
  30.         embed = disnake.Embed(
  31.             color = disnake.Colour.random(),
  32.             title = f"{guild.name} Of Applications",
  33.             description = "Select A Category Below To View Those Projects"
  34.         ).set_thumbnail(url = guild.icon)
  35.  
  36.         topics = ["Beginner Projects", "Intermediate Projects", "WebScraping Projects",
  37.                     "Automation Projects", "TKinter Projects", "Turtle Projects", "OpenCV Projects",
  38.                     "PythonDjango Projects", "Python TTS Projects", "Other Projects"]
  39.  
  40.         options = [
  41.             disnake.SelectOption(label = topic, value=str(i)) for i, topic in enumerate(topics, start=1)
  42.         ]
  43.  
  44.         select = disnake.ui.Select(
  45.             placeholder="Select A Category",
  46.             options=options,
  47.             max_values=1,
  48.             custom_id = "meks_hub_category_selector"
  49.         )
  50.  
  51.         await inter.edit_original_message(embed=embed, components=select)
  52.  
  53.     @commands.Cog.listener(disnake.Event.dropdown)
  54.     async def topic_select(self, inter: disnake.MessageInteraction):
  55.         await inter.response.defer()
  56.  
  57.         if inter.component.custom_id != "meks_hub_category_selector":
  58.             return
  59.        
  60.         if inter.author.id != inter.message.interaction.author:
  61.             return
  62.  
  63.         # obtain the wanted list of embeds
  64.         index = int(inter.values[0]) - 1
  65.         all_embeds = await get_embeds(self.db_engine)
  66.         embeds = all_embeds[index]
  67.  
  68.         await inter.edit_original_message(embed=embeds[0],view=CategoryPaginator(embeds, inter.message.interaction.author))
  69.  
  70.  
  71.  
  72. async def get_embeds(db):
  73.     # create list of embeds that will be passed to the paginator
  74.     beginner = []
  75.     intermediate = []
  76.     web_scraping = []
  77.     automation = []
  78.     tkinter = []
  79.     turtle = []
  80.     opencv = []
  81.     py_django = []
  82.     python_tts = []
  83.     other = []
  84.  
  85.     # make a database call to obtain the list of tuples of all the projects
  86.     all_projects = db.get_projects()
  87.  
  88.     # iterate through each tuple in the list of tuples
  89.     for i in all_projects:
  90.         # unpack the tuple into it's variabels
  91.         iden, name, date, github_link, imgur_link, category = i
  92.  
  93.         # build the embed for the unpacked project
  94.         embed = disnake.Embed(
  95.             color = disnake.Colour.random(),
  96.             title = category,
  97.             description = f"Project #{iden}"
  98.         ).add_field(
  99.             name = "Project Name",
  100.             value = name,
  101.             inline = False
  102.         ).add_field(
  103.             name = "Project Production Date",
  104.             value = date,
  105.             inline = False
  106.         ).add_field(
  107.             name = "Project GitHub Link",
  108.             value = github_link,
  109.             inline = False
  110.         ).set_thumbnail(url=imgur_link)
  111.  
  112.         # determine which list the page gets added to based off the category
  113.         if category == "Beginner Projects":
  114.             beginner.append(embed)
  115.         elif category == "Intermediate Projects":
  116.             intermediate.append(embed)
  117.         elif category == "WebScraping Projects":
  118.             web_scraping.append(embed)
  119.         elif category == "Automation Projects":
  120.             automation.append(embed)
  121.         elif category == "TKinter Projects":
  122.             tkinter.append(embed)
  123.         elif category == "Turtle Projects":
  124.             turtle.append(embed)
  125.         elif category == "OpenCV Projects":
  126.             opencv.append(embed)
  127.         elif category == "PythonDjango Projects":
  128.             py_django.append(embed)
  129.         elif category == "Python Text To Speech Projects":
  130.             python_tts.append(embed)
  131.         elif category == "Other Projects":
  132.             other.append(embed)
  133.         else:
  134.             raise
  135.  
  136.     return (embed, beginner, intermediate, web_scraping, automation, tkinter, turtle, opencv, py_django, python_tts, other)
  137.  
  138. def setup(bot):
  139.     bot.add_cog(GeneralCommands(bot))
Advertisement
Add Comment
Please, Sign In to add comment