Skip to content

Commit

Permalink
[Units] Return list of WikiArticle results for search_wiki function
Browse files Browse the repository at this point in the history
[Discord] Add and use utilities.views and WikiArticlesView to show and allow selection of multiple article results for commands that search wikis
  • Loading branch information
Harmon758 committed Jul 28, 2023
1 parent 98a9f98 commit fa34406
Show file tree
Hide file tree
Showing 4 changed files with 278 additions and 156 deletions.
26 changes: 16 additions & 10 deletions Discord/cogs/runescape.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys

from utilities import checks
from utilities.views import WikiArticlesView

sys.path.insert(0, "..")
from units.runescape import get_ge_data, get_item_id, get_monster_data
Expand Down Expand Up @@ -162,21 +163,26 @@ async def wiki(self, ctx, *, query):
"""
await ctx.defer()
try:
article = await search_wiki(
articles = await search_wiki(
"https://runescape.wiki/", query,
aiohttp_session = ctx.bot.aiohttp_session
)
except ValueError as e:
await ctx.embed_reply(f"{ctx.bot.error_emoji} {e}")
else:
await ctx.embed_reply(
title = article.title,
title_url = article.url,
description = article.extract,
image_url = article.image_url,
footer_icon_url = article.wiki.logo,
footer_text = article.wiki.name
)
return

view = WikiArticlesView(articles)
message = await ctx.reply(
"",
embed = view.initial_embed(ctx),
view = view
)

if ctx.interaction:
# Fetch Message, as InteractionMessage token expires after 15 min.
message = await message.fetch()
view.message = message
ctx.bot.views.append(view)

@runescape.command(hidden = True, with_app_command = False)
async def zybez(self, ctx):
Expand Down
107 changes: 65 additions & 42 deletions Discord/cogs/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from utilities import checks
from utilities.menu_sources import WolframAlphaSource
from utilities.paginators import ButtonPaginator
from utilities.views import WikiArticlesView

sys.path.insert(0, "..")
from units.wikis import search_wiki
Expand Down Expand Up @@ -209,21 +210,26 @@ async def startpage(self, ctx, *search: str):
async def uesp(self, ctx, *, search: str):
"""Look something up on the Unofficial Elder Scrolls Pages"""
try:
article = await search_wiki(
articles = await search_wiki(
"https://en.uesp.net/", search,
aiohttp_session = ctx.bot.aiohttp_session
)
except ValueError as e:
await ctx.embed_reply(f"{ctx.bot.error_emoji} {e}")
else:
await ctx.embed_reply(
title = article.title,
title_url = article.url,
description = article.extract,
image_url = article.image_url,
footer_icon_url = article.wiki.logo,
footer_text = article.wiki.name
)
return

view = WikiArticlesView(articles)
message = await ctx.reply(
"",
embed = view.initial_embed(ctx),
view = view
)

if ctx.interaction:
# Fetch Message, as InteractionMessage token expires after 15 min.
message = await message.fetch()
view.message = message
ctx.bot.views.append(view)

@uesp.command(name = "random")
async def uesp_random(self, ctx):
Expand All @@ -233,7 +239,7 @@ async def uesp_random(self, ctx):
'''
# Note: random uesp command invokes this command
try:
article = await search_wiki(
articles = await search_wiki(
"https://en.uesp.net/", None,
aiohttp_session = ctx.bot.aiohttp_session,
random = True,
Expand All @@ -244,6 +250,7 @@ async def uesp_random(self, ctx):
except ValueError as e:
await ctx.embed_reply(f"{ctx.bot.error_emoji} {e}")
else:
article = articles[0]
await ctx.embed_reply(
title = article.title,
title_url = article.url,
Expand All @@ -260,36 +267,42 @@ async def uesp_random(self, ctx):
async def wikipedia(self, ctx, *, query: str):
"""Search for an article on Wikipedia"""
try:
article = await search_wiki(
articles = await search_wiki(
"https://en.wikipedia.org/", query,
aiohttp_session = ctx.bot.aiohttp_session
)
except ValueError as e:
await ctx.embed_reply(f"{ctx.bot.error_emoji} {e}")
else:
await ctx.embed_reply(
title = article.title,
title_url = article.url,
description = article.extract,
image_url = article.image_url,
footer_icon_url = article.wiki.logo,
footer_text = article.wiki.name
)
return

view = WikiArticlesView(articles)
message = await ctx.reply(
"",
embed = view.initial_embed(ctx),
view = view
)

if ctx.interaction:
# Fetch Message, as InteractionMessage token expires after 15 min.
message = await message.fetch()
view.message = message
ctx.bot.views.append(view)

@wikipedia.command(name = "random")
async def wikipedia_random(self, ctx):
"""Random Wikipedia article"""
# Note: random wikipedia command invokes this command
await ctx.defer()
try:
article = await search_wiki(
articles = await search_wiki(
"https://en.wikipedia.org/", None,
aiohttp_session = ctx.bot.aiohttp_session,
random = True
)
except ValueError as e:
await ctx.embed_reply(f"{ctx.bot.error_emoji} {e}")
else:
article = articles[0]
await ctx.embed_reply(
title = article.title,
title_url = article.url,
Expand Down Expand Up @@ -325,41 +338,51 @@ async def fandom(self, ctx):
async def lotr(self, ctx, *, query: str):
"""Search for an article on The Lord of The Rings Wiki"""
try:
article = await search_wiki(
articles = await search_wiki(
"https://lotr.fandom.com/", query,
aiohttp_session = ctx.bot.aiohttp_session
)
except ValueError as e:
await ctx.embed_reply(f"{ctx.bot.error_emoji} {e}")
else:
await ctx.embed_reply(
title = article.title,
title_url = article.url,
description = article.extract,
image_url = article.image_url,
footer_icon_url = article.wiki.logo,
footer_text = article.wiki.name
)
return

view = WikiArticlesView(articles)
message = await ctx.reply(
"",
embed = view.initial_embed(ctx),
view = view
)

if ctx.interaction:
# Fetch Message, as InteractionMessage token expires after 15 min.
message = await message.fetch()
view.message = message
ctx.bot.views.append(view)

@commands.command()
async def tolkien(self, ctx, *, query: str):
"""Search for an article on Tolkien Gateway"""
try:
article = await search_wiki(
articles = await search_wiki(
"https://tolkiengateway.net/", query,
aiohttp_session = ctx.bot.aiohttp_session
)
except ValueError as e:
await ctx.embed_reply(f"{ctx.bot.error_emoji} {e}")
else:
await ctx.embed_reply(
title = article.title,
title_url = article.url,
description = article.extract,
image_url = article.image_url,
footer_icon_url = article.wiki.logo,
footer_text = article.wiki.name
)
return

view = WikiArticlesView(articles)
message = await ctx.reply(
"",
embed = view.initial_embed(ctx),
view = view
)

if ctx.interaction:
# Fetch Message, as InteractionMessage token expires after 15 min.
message = await message.fetch()
view.message = message
ctx.bot.views.append(view)

@commands.group(
aliases = ["wa", "wolfram_alpha"],
Expand Down
62 changes: 62 additions & 0 deletions Discord/utilities/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

import discord


class WikiArticlesView(discord.ui.View):

def __init__(self, articles):
super().__init__(timeout = None)
# TODO: Timeout?

self.articles = articles

for number, article in enumerate(articles):
self.article.add_option(label = article.title, value = number)

self.article.options[0].default = True

def initial_embed(self, ctx):
article = self.articles[0]
return discord.Embed(
color = ctx.bot.bot_color,
title = article.title,
url = article.url,
description = article.extract
).set_image(
url = article.image_url
).set_footer(
icon_url = article.wiki.logo,
text = article.wiki.name
)

@discord.ui.select()
async def article(self, interaction, select):
for option in select.options:
option.default = False

selected = int(select.values[0])
article = self.articles[selected]

embed = discord.Embed(
color = interaction.client.bot_color,
title = article.title,
url = article.url,
description = article.extract
).set_image(
url = article.image_url
).set_footer(
icon_url = article.wiki.logo,
text = article.wiki.name
)

select.options[selected].default = True

await interaction.response.edit_message(embed = embed, view = self)

async def stop(self):
self.article.disabled = True

await self.message.edit(view = self)

super().stop()

Loading

0 comments on commit fa34406

Please sign in to comment.