Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import discord
- from discord import app_commands
- from discord.ext import commands, tasks
- import sqlite3
- import os
- import asyncio
- from datetime import datetime, timedelta
- import shutil
- from typing import Optional
- from discord.ui import View, Button
- import csv
- from io import StringIO
- import yaml
- import datetime
- from dotenv import load_dotenv
- from pathlib import Path
- from Utils import Utils
- from ConfirmView import ConfirmView
- from Feedback import Feedback
- def load_faq_data(file_path):
- with open(file_path, 'r') as stream:
- try:
- return yaml.safe_load(stream)
- except yaml.YAMLError as exc:
- print(f"Error parsing YAML file: {exc}")
- return None
- print(f"Path is: {os.path.basename(Path(Path(__file__).parent.name) / 'FAQ.yml')}")
- faq_data = load_faq_data(os.path.basename(Path(Path(__file__).parent.name) / 'FAQ.yml'))
- class Paginator(discord.ui.View):
- def __init__(self, data, sep=5):
- super().__init__()
- self.current_page = 1
- self.sep = sep
- self.data = data
- async def send(self, interaction: discord.Interaction):
- self.interaction = interaction
- self.message = await interaction.response.send_message(embed=self.create_embed(self.data), view=self, ephemeral=True)
- await self.update_message(self.data[:self.sep])
- def create_embed(self, data):
- embed = discord.Embed(title=f"FAQ Page {self.current_page} / {len(self.data) // self.sep + 1}")
- for dictionary in data:
- for name, value in dictionary.items():
- embed.add_field(name=name, value=value, inline=False)
- return embed
- async def update_message(self, data):
- self.update_buttons()
- await self.interaction.edit_original_response(embed=self.create_embed(data), view=self)
- def update_buttons(self):
- if self.current_page == 1:
- self.first_page_button.disabled = True
- self.prev_button.disabled = True
- self.first_page_button.style = discord.ButtonStyle.gray
- self.prev_button.style = discord.ButtonStyle.gray
- else:
- self.first_page_button.disabled = False
- self.prev_button.disabled = False
- self.first_page_button.style = discord.ButtonStyle.green
- self.prev_button.style = discord.ButtonStyle.primary
- if self.current_page == len(self.data) // self.sep + 1:
- self.next_button.disabled = True
- self.last_page_button.disabled = True
- self.last_page_button.style = discord.ButtonStyle.gray
- self.next_button.style = discord.ButtonStyle.gray
- else:
- self.next_button.disabled = False
- self.last_page_button.disabled = False
- self.last_page_button.style = discord.ButtonStyle.green
- self.next_button.style = discord.ButtonStyle.primary
- @discord.ui.button(label="|<",
- style=discord.ButtonStyle.primary)
- async def first_page_button(self, interaction : discord.Interaction, button : discord.ui.Button):
- await interaction.response.defer()
- self.current_page = 1
- until_item = self.current_page * self.sep
- from_item = until_item - self.sep
- await self.update_message(self.data[:until_item])
- @discord.ui.button(label="<",
- style=discord.ButtonStyle.primary)
- async def prev_button(self, interaction : discord.Interaction, button : discord.ui.Button):
- await interaction.response.defer()
- self.current_page -= 1
- until_item = self.current_page * self.sep
- from_item = until_item - self.sep
- await self.update_message(self.data[from_item:until_item])
- @discord.ui.button(label=">",
- style=discord.ButtonStyle.primary)
- async def next_button(self, interaction : discord.Interaction, button : discord.ui.Button):
- await interaction.response.defer()
- self.current_page += 1
- until_item = self.current_page * self.sep
- from_item = until_item - self.sep
- await self.update_message(self.data[from_item:until_item])
- @discord.ui.button(label=">|",
- style=discord.ButtonStyle.primary)
- async def last_page_button(self, interaction : discord.Interaction, button : discord.ui.Button):
- await interaction.response.defer()
- self.current_page = int(len(self.data) / self.sep) + 1
- until_item = self.current_page * self.sep
- from_item = until_item - self.sep
- await self.update_message(self.data[from_item:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement