Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(commands): update docs command #352

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
beautifulsoup4==4.12.3
Flask==3.0.3
GitPython==3.1.43
igdb-api-v4==0.3.3
Expand Down
179 changes: 9 additions & 170 deletions src/discord/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
from typing import Tuple

# lib imports
from bs4 import BeautifulSoup
import discord
from discord.ui.select import Select
from discord.ui.button import Button
import requests

# local imports
from src.common import avatar, bot_name
Expand All @@ -26,7 +24,7 @@
A list of `discord.SelectOption` objects.
"""
def __init__(self):
self.projects = get_json(url='https://app.lizardbyte.dev/uno/readthedocs/projects.json')
self.projects = get_json(url='https://app.lizardbyte.dev/dashboard/readthedocs/projects.json')
self.projects_options = []
for project in self.projects:
try:
Expand All @@ -53,24 +51,6 @@
The project name.
self.docs_version : str
The url to the documentation of the selected version.
self.docs_category : str
The name of the selected category.
self.docs_page : str
The name of the selected page.
self.docs_section : str
The name of the selected section.
self.html : bytes
Content of `requests.get()` in bytes.
self.soup : bs4.BeautifulSoup
BeautifulSoup object of `self.html`
self.toc : ResultSet
Docs table of contents.
self.categories : list
A list of Docs categories.
self.pages : list
A list of pages for the selected category.
self.sections : list
A list of sections for the selected page.
"""
def __init__(self, ctx: discord.ApplicationContext):
super().__init__(timeout=45)
Expand All @@ -81,17 +61,6 @@
# final values
self.docs_project = None
self.docs_version = None
self.docs_category = None
self.docs_page = None
self.docs_section = None

# intermediate values
self.html = None
self.soup = None
self.toc = None
self.categories = None
self.pages = None
self.sections = None

# reset the first select menu because it remembers the last selected value
self.children[0].options = DocsCommandDefaultProjects().projects_options
Expand All @@ -112,20 +81,15 @@
embed = discord.Embed()
embed.set_footer(text=bot_name, icon_url=avatar)

url = f'{self.docs_version}{self.docs_section}'
url = self.docs_version

Check warning on line 84 in src/discord/views.py

View check run for this annotation

Codecov / codecov/patch

src/discord/views.py#L84

Added line #L84 was not covered by tests

if self.docs_project and self.docs_version: # the project and version are selected
if self.docs_category is not None: # category has a value, which may be ""
if self.docs_category: # category is selected, so the next item must not be blank
if self.docs_page is not None and self.docs_section is not None: # info is complete
complete = True
else: # info is complete IF category is ""
complete = True
complete = True

Check warning on line 87 in src/discord/views.py

View check run for this annotation

Codecov / codecov/patch

src/discord/views.py#L87

Added line #L87 was not covered by tests

if complete:
embed.title = f'{self.docs_project} | {self.docs_category}' if self.docs_category else self.docs_project
embed.title = self.docs_project

Check warning on line 90 in src/discord/views.py

View check run for this annotation

Codecov / codecov/patch

src/discord/views.py#L90

Added line #L90 was not covered by tests
embed.description = f'The selected docs are available at {url}'
embed.color = 0x39FF14 # PyCharm complains that the color is read only, but this works anyway
embed.color = 0x39FF14

Check warning on line 92 in src/discord/views.py

View check run for this annotation

Codecov / codecov/patch

src/discord/views.py#L92

Added line #L92 was not covered by tests
embed.url = url
else:
# info is not complete
Expand Down Expand Up @@ -214,7 +178,7 @@
readthedocs = self.children[0].values[0]

versions = get_json(
url=f'https://app.lizardbyte.dev/uno/readthedocs/versions/{readthedocs}.json')
url=f'https://app.lizardbyte.dev/dashboard/readthedocs/versions/{readthedocs}.json')

options = []
for version in versions:
Expand All @@ -227,83 +191,6 @@

child.options = options

if child == self.children[2]: # choose the docs category
url = self.children[1].values[0]

self.html = requests.get(url=url).content
self.soup = BeautifulSoup(self.html, 'html.parser')

self.toc = self.soup.select("div[class*=toctree-wrapper]")

self.categories = []
for item in self.toc:
self.categories.extend(item.select("p[role=heading]"))

options = [discord.SelectOption(label='None')]
for category in self.categories:

options.append(discord.SelectOption(
label=category.string
))

child.options = options

if child == self.children[3]: # choose the docs page
category_value = self.children[2].values[0]

for category in self.categories:
if category.string == category_value:
category_section = self.toc[self.categories.index(category)]

page_sections = category_section.findChild('ul')
self.sections = page_sections.find_all('li', class_="toctree-l1")

break

options = []
self.pages = []
if category_value == 'None':
options.append(discord.SelectOption(label='None', value=category_value, default=True))

# enable the final menu
self.children[-1].disabled = False
self.children[-1].options = options
else:
for section in self.sections:
page = section.findNext('a')
self.pages.append(page)

options.append(discord.SelectOption(
label=page.string,
value=page['href']
))

child.options = options

if category_value == 'None':
break

if child == self.children[4]: # choose the docs page section
page_value = self.children[3].values[0]

if page_value == 'None':
options = [discord.SelectOption(label='None', value=page_value, default=True)]
else:
options = [discord.SelectOption(label='None', value=page_value)]
for section in self.sections:
page = section.findNext('a')
if page_value == page['href']:
page_sections = section.find_all('a')
del page_sections[0] # delete first item from list

for page_section in page_sections:
options.append(discord.SelectOption(
label=page_section.string,
value=page_section['href']
))

child.options = options

index += 1

# set the currently selected value to the default item
Expand All @@ -316,32 +203,14 @@
# reset values
try:
self.docs_project = self.children[0].values[0]
self.docs_version = self.children[1].values[0]

if self.children[2].values[0] == 'None':
self.docs_category = ''
self.docs_page = ''
self.docs_section = ''
if self.children[1].values:
self.docs_version = self.children[1].values[0]

Check warning on line 207 in src/discord/views.py

View check run for this annotation

Codecov / codecov/patch

src/discord/views.py#L206-L207

Added lines #L206 - L207 were not covered by tests
else:
self.docs_category = self.children[2].values[0]
self.docs_page = self.children[3].values[0] if self.children[3].values[0] != 'None' else ''
self.docs_section = self.children[4].values[0] if self.children[4].values[0] != 'None' else ''
self.docs_version = None

Check warning on line 209 in src/discord/views.py

View check run for this annotation

Codecov / codecov/patch

src/discord/views.py#L209

Added line #L209 was not covered by tests
except IndexError:
pass
if select == self.children[0]: # chose the docs project
self.docs_version = None
self.docs_category = None
self.docs_page = None
self.docs_section = None
elif select == self.children[1]: # chose the docs version
self.docs_category = None
self.docs_page = None
self.docs_section = None
elif select == self.children[2]: # chose the docs category
self.docs_page = None if self.children[2].values[0] != 'None' else ''
self.docs_section = None if self.children[2].values[0] != 'None' else ''
elif select == self.children[3]: # chose the docs page
self.docs_section = None

complete, embed = self.check_completion_status()

Expand All @@ -367,36 +236,6 @@
async def version_callback(self, select: Select, interaction: discord.Interaction):
await self.callback(select=select, interaction=interaction)

@discord.ui.select(
placeholder="Choose category...",
disabled=True,
min_values=1,
max_values=1,
options=[discord.SelectOption(label='error')]
)
async def category_callback(self, select: Select, interaction: discord.Interaction):
await self.callback(select=select, interaction=interaction)

@discord.ui.select(
placeholder="Choose page...",
disabled=True,
min_values=1,
max_values=1,
options=[discord.SelectOption(label='error')]
)
async def page_callback(self, select: Select, interaction: discord.Interaction):
await self.callback(select=select, interaction=interaction)

@discord.ui.select(
placeholder="Choose section...",
disabled=True,
min_values=1,
max_values=1,
options=[discord.SelectOption(label='error')]
)
async def section_callback(self, select: Select, interaction: discord.Interaction):
await self.callback(select=select, interaction=interaction)


class DonateCommandView(discord.ui.View):
"""
Expand Down
Loading