Advertisement
LyndenSylvester

Untitled

Jan 5th, 2025
61
0
5 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.61 KB | None | 0 0
  1. import discord
  2. from discord import app_commands
  3. from discord.ext import commands, tasks
  4. import sqlite3
  5. import os
  6. import asyncio
  7. from datetime import datetime, timedelta
  8. import shutil
  9. from typing import Optional
  10. from discord.ui import View, Button
  11. import csv
  12. from io import StringIO
  13. import yaml
  14. import datetime
  15. from dotenv import load_dotenv
  16.  
  17. from pathlib import Path
  18. from Utils import Utils
  19. from ConfirmView import ConfirmView
  20. from Feedback import Feedback
  21.  
  22. def load_faq_data(file_path):
  23.     with open(file_path, 'r') as stream:
  24.         try:
  25.             return yaml.safe_load(stream)
  26.         except yaml.YAMLError as exc:
  27.             print(f"Error parsing YAML file: {exc}")
  28.             return None
  29. print(f"Path is: {os.path.basename(Path(Path(__file__).parent.name) / 'FAQ.yml')}")
  30. faq_data = load_faq_data(os.path.basename(Path(Path(__file__).parent.name) / 'FAQ.yml'))
  31.  
  32. class Paginator(discord.ui.View):
  33.     def __init__(self, data, sep=5):
  34.         super().__init__()
  35.         self.current_page = 1
  36.         self.sep = sep
  37.         self.data = data
  38.  
  39.     async def send(self, interaction: discord.Interaction):
  40.         self.interaction = interaction
  41.         self.message = await interaction.response.send_message(embed=self.create_embed(self.data), view=self, ephemeral=True)
  42.         await self.update_message(self.data[:self.sep])
  43.  
  44.     def create_embed(self, data):
  45.         embed = discord.Embed(title=f"FAQ Page {self.current_page} / {len(self.data) // self.sep + 1}")
  46.         for dictionary in data:
  47.             for name, value in dictionary.items():
  48.                 embed.add_field(name=name, value=value, inline=False)
  49.         return embed
  50.    
  51.     async def update_message(self, data):
  52.         self.update_buttons()
  53.         await self.interaction.edit_original_response(embed=self.create_embed(data), view=self)
  54.  
  55.     def update_buttons(self):
  56.         if self.current_page == 1:
  57.             self.first_page_button.disabled = True
  58.             self.prev_button.disabled = True
  59.             self.first_page_button.style = discord.ButtonStyle.gray
  60.             self.prev_button.style = discord.ButtonStyle.gray
  61.         else:
  62.             self.first_page_button.disabled = False
  63.             self.prev_button.disabled = False
  64.             self.first_page_button.style = discord.ButtonStyle.green
  65.             self.prev_button.style = discord.ButtonStyle.primary
  66.  
  67.         if self.current_page == len(self.data) // self.sep + 1:
  68.             self.next_button.disabled = True
  69.             self.last_page_button.disabled = True
  70.             self.last_page_button.style = discord.ButtonStyle.gray
  71.             self.next_button.style = discord.ButtonStyle.gray
  72.         else:
  73.             self.next_button.disabled = False
  74.             self.last_page_button.disabled = False
  75.             self.last_page_button.style = discord.ButtonStyle.green
  76.             self.next_button.style = discord.ButtonStyle.primary
  77.  
  78.     @discord.ui.button(label="|<",
  79.                        style=discord.ButtonStyle.primary)
  80.     async def first_page_button(self, interaction : discord.Interaction, button : discord.ui.Button):
  81.         await interaction.response.defer()
  82.         self.current_page = 1
  83.         until_item = self.current_page * self.sep
  84.         from_item = until_item - self.sep
  85.         await self.update_message(self.data[:until_item])
  86.  
  87.     @discord.ui.button(label="<",
  88.                         style=discord.ButtonStyle.primary)
  89.     async def prev_button(self, interaction : discord.Interaction, button : discord.ui.Button):
  90.         await interaction.response.defer()
  91.         self.current_page -= 1
  92.         until_item = self.current_page * self.sep
  93.         from_item = until_item - self.sep
  94.         await self.update_message(self.data[from_item:until_item])
  95.  
  96.     @discord.ui.button(label=">",
  97.                        style=discord.ButtonStyle.primary)
  98.     async def next_button(self, interaction : discord.Interaction, button : discord.ui.Button):
  99.         await interaction.response.defer()
  100.         self.current_page += 1
  101.         until_item = self.current_page * self.sep
  102.         from_item = until_item - self.sep
  103.         await self.update_message(self.data[from_item:until_item])
  104.        
  105.     @discord.ui.button(label=">|",
  106.                        style=discord.ButtonStyle.primary)
  107.     async def last_page_button(self, interaction : discord.Interaction, button : discord.ui.Button):
  108.         await interaction.response.defer()
  109.         self.current_page = int(len(self.data) / self.sep) + 1
  110.         until_item = self.current_page * self.sep
  111.         from_item = until_item - self.sep
  112.         await self.update_message(self.data[from_item:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement