Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import disnake
- import json
- from disnake.ext import commands
- from disnake.enums import ButtonStyle
- from services.paginator import CategoryPaginator
- from server.database import DatabaseEngine
- with open("./server/config.json", 'r', encoding="utf-8-sig") as f:
- data = json.load(f)
- guild_id = [data["guild_id"],]
- class GeneralCommands(commands.Cog):
- def __init__(self, bot):
- self.bot = bot
- self.db_engine = DatabaseEngine()
- @commands.slash_command(
- name = "meks_hub",
- description = "Returns a paginator embed as a \"flip book\" of github repo's",
- guild_ids = guild_id
- )
- async def meks_hub(self, inter):
- await inter.response.defer()
- guild = self.bot.get_guild(guild_id[0])
- # build table of contents embed
- embed = disnake.Embed(
- color = disnake.Colour.random(),
- title = f"{guild.name} Of Applications",
- description = "Select A Category Below To View Those Projects"
- ).set_thumbnail(url = guild.icon)
- topics = ["Beginner Projects", "Intermediate Projects", "WebScraping Projects",
- "Automation Projects", "TKinter Projects", "Turtle Projects", "OpenCV Projects",
- "PythonDjango Projects", "Python TTS Projects", "Other Projects"]
- options = [
- disnake.SelectOption(label = topic, value=str(i)) for i, topic in enumerate(topics, start=1)
- ]
- select = disnake.ui.Select(
- placeholder="Select A Category",
- options=options,
- max_values=1,
- custom_id = "meks_hub_category_selector"
- )
- await inter.edit_original_message(embed=embed, components=select)
- @commands.Cog.listener(disnake.Event.dropdown)
- async def topic_select(self, inter: disnake.MessageInteraction):
- await inter.response.defer()
- if inter.component.custom_id != "meks_hub_category_selector":
- return
- if inter.author.id != inter.message.interaction.author:
- return
- # obtain the wanted list of embeds
- index = int(inter.values[0]) - 1
- all_embeds = await get_embeds(self.db_engine)
- embeds = all_embeds[index]
- await inter.edit_original_message(embed=embeds[0],view=CategoryPaginator(embeds, inter.message.interaction.author))
- async def get_embeds(db):
- # create list of embeds that will be passed to the paginator
- beginner = []
- intermediate = []
- web_scraping = []
- automation = []
- tkinter = []
- turtle = []
- opencv = []
- py_django = []
- python_tts = []
- other = []
- # make a database call to obtain the list of tuples of all the projects
- all_projects = db.get_projects()
- # iterate through each tuple in the list of tuples
- for i in all_projects:
- # unpack the tuple into it's variabels
- iden, name, date, github_link, imgur_link, category = i
- # build the embed for the unpacked project
- embed = disnake.Embed(
- color = disnake.Colour.random(),
- title = category,
- description = f"Project #{iden}"
- ).add_field(
- name = "Project Name",
- value = name,
- inline = False
- ).add_field(
- name = "Project Production Date",
- value = date,
- inline = False
- ).add_field(
- name = "Project GitHub Link",
- value = github_link,
- inline = False
- ).set_thumbnail(url=imgur_link)
- # determine which list the page gets added to based off the category
- if category == "Beginner Projects":
- beginner.append(embed)
- elif category == "Intermediate Projects":
- intermediate.append(embed)
- elif category == "WebScraping Projects":
- web_scraping.append(embed)
- elif category == "Automation Projects":
- automation.append(embed)
- elif category == "TKinter Projects":
- tkinter.append(embed)
- elif category == "Turtle Projects":
- turtle.append(embed)
- elif category == "OpenCV Projects":
- opencv.append(embed)
- elif category == "PythonDjango Projects":
- py_django.append(embed)
- elif category == "Python Text To Speech Projects":
- python_tts.append(embed)
- elif category == "Other Projects":
- other.append(embed)
- else:
- raise
- return (embed, beginner, intermediate, web_scraping, automation, tkinter, turtle, opencv, py_django, python_tts, other)
- def setup(bot):
- bot.add_cog(GeneralCommands(bot))
Advertisement
Add Comment
Please, Sign In to add comment