mekasu0124

Untitled

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