Created
November 11, 2021 14:42
-
-
Save SpoopyTim/af68446a4c648831ac98ef434e0de07c to your computer and use it in GitHub Desktop.
Updated
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SearchResults(discord.ui.View): # Create an interaction wrapper thing | |
def __init__(self, search_query: str, max_pages: int = None): # Call the init method so we can store data | |
"""Creates a search results viewer based on discord interactions | |
Args: | |
search_query (str): The query to search with | |
max_pages (int, optional): The maximum number of results to show. Set to None to disable the limit. Defaults to None. | |
""" | |
super().__init__() # Make sure to keep init methods from discord.ui.View and dont overwrite them | |
self.page = 0 # Set a global page number | |
self.resource = build("customsearch", "v1", developerKey=api_key).cse() # Only grab the data here for stability | |
self.back_button = None # Set variables for controlling buttons from each other's interactions | |
self.next_button = None | |
self.max_pages = max_pages | |
self.search = search_query | |
@discord.ui.button(label="Back", style=discord.ButtonStyle.green) # Create the back button | |
async def previous_page(self, button: discord.ui.Button, interaction: discord.Interaction): # Pass required parameters | |
self.back_button = button # Save this instance of the button globally | |
try: | |
if self.next_button.disabled is True: # Check if the next button is disabled | |
self.next_button.disabled = False # If it is then enable it | |
except: | |
pass | |
result = self.resource.list(q=f"{self.search}", cx="9f5b481aa553e3a4e", searchType="image").execute() # Grab the data from the resource | |
if self.page == 1: # If this is the first page | |
button.disabled = True # Disable the button and continue with the page still being 1 | |
# Used to artificially finish the interaction (theres probs a method for this but i cba rn) | |
await interaction.response.edit_message(view=self) # Used to artific | |
else: # If this isnt the first page | |
self.page -= 1 # Increment the global page number | |
url = result["items"][self.page]["link"] # Grab the link for this page | |
embed = discord.Embed(title=f"Here is what i found : `{self.search.title()}`", url=url) | |
embed.set_image(url=url) | |
await interaction.response.edit_message(embed=embed, view=self) | |
@discord.ui.button(label="Next", style=discord.ButtonStyle.green) # Create the next button | |
async def next_page(self, button: discord.ui.Button, interaction: discord.Interaction): # Pass required parameters | |
self.next_button = button # Save this instance of the button globally | |
try: | |
if self.back_button.disabled is True: # Check if the back button is disabled | |
self.back_button.disabled = False # If it is then enable it | |
except: | |
pass | |
result = self.resource.list(q=f"{self.search}", cx="9f5b481aa553e3a4e", searchType="image").execute() # Grab the data from the resource here | |
# Just incase for whatever reason | |
# results have changed (probs unneeded) | |
if self.max_pages is not None: # Make sure there is actually a numerical limit | |
if self.page == self.max_pages - 1: # Check if the limit has been hit | |
button.disabled = True | |
await interaction.response.edit_message(view=self) | |
return | |
self.page += 1 # Increment the global page number | |
url = result["items"][self.page]["link"] # Grab the link for this page | |
embed = discord.Embed(title=f"Here is what i found : `{self.search.title()}`", url=url) # Form the embed again with the updated link | |
embed.set_image(url=url) | |
await interaction.response.edit_message(embed=embed, view=self) # Edit the original message with the updated content | |
@commands.command() | |
async def img(self, ctx, *, search): | |
start_index = 0 | |
resource = build("customsearch", "v1", developerKey=api_key).cse() | |
result = resource.list(q=f"{search}", cx="9f5b481aa553e3a4e", searchType="image").execute() | |
url = result["items"][0]["link"] | |
embed1 = discord.Embed(title=f"Here is what i found : `{search.title()}`", url=url) | |
embed1.set_image(url=url) | |
embed1.set_footer(text= f'Requested by {ctx.author.name}') | |
print(f"{ctx.author.name}#{ctx.author.discriminator} Searched: {search.title()}") | |
await ctx.send(embed=embed1, view=SearchResults(search_query=search)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment