+ Upload
This commit is contained in:
65
cogs/search.py
Normal file
65
cogs/search.py
Normal file
@@ -0,0 +1,65 @@
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from discord import app_commands
|
||||
import wikipedia
|
||||
from googlesearch import search
|
||||
|
||||
class Search(commands.Cog):
|
||||
def __init__(self, bot: commands.Bot):
|
||||
self.bot = bot
|
||||
# Set Wikipedia language to German
|
||||
wikipedia.set_lang("de")
|
||||
|
||||
@app_commands.command(name="google", description="Sucht bei Google und zeigt die Top-Ergebnisse an.")
|
||||
@app_commands.describe(query="Wonach möchtest du suchen?")
|
||||
async def google_search(self, interaction: discord.Interaction, query: str):
|
||||
await interaction.response.defer(ephemeral=True)
|
||||
try:
|
||||
results = []
|
||||
# We use a generator and stop after 5 results
|
||||
for j in search(query, num=5, stop=5, pause=1.0):
|
||||
results.append(j)
|
||||
|
||||
if not results:
|
||||
await interaction.followup.send("Ich konnte keine Ergebnisse für deine Suche finden.")
|
||||
return
|
||||
|
||||
description = ""
|
||||
for i, result in enumerate(results):
|
||||
description += f"{i+1}. [{result.split('//')[1].split('/')[0]}]({result})\n"
|
||||
|
||||
embed = discord.Embed(
|
||||
title=f"Google-Ergebnisse für: `{query}`",
|
||||
description=description,
|
||||
color=discord.Color.from_rgb(66, 133, 244) # Google Blue
|
||||
)
|
||||
await interaction.followup.send(embed=embed)
|
||||
|
||||
except Exception as e:
|
||||
await interaction.followup.send(f"Ein Fehler ist bei der Google-Suche aufgetreten: {e}")
|
||||
|
||||
@app_commands.command(name="wikipedia", description="Durchsucht Wikipedia nach einem Artikel.")
|
||||
@app_commands.describe(query="Wonach möchtest du suchen?")
|
||||
async def wikipedia_search(self, interaction: discord.Interaction, query: str):
|
||||
await interaction.response.defer()
|
||||
try:
|
||||
# Get the summary of the first result, auto-suggesting corrections
|
||||
page = wikipedia.page(query, auto_suggest=True, redirect=True)
|
||||
summary = wikipedia.summary(query, sentences=3, auto_suggest=True, redirect=True)
|
||||
|
||||
embed = discord.Embed(
|
||||
title=f"Wikipedia: {page.title}",
|
||||
url=page.url,
|
||||
description=summary,
|
||||
color=discord.Color.light_grey()
|
||||
)
|
||||
await interaction.followup.send(embed=embed)
|
||||
|
||||
except wikipedia.exceptions.PageError:
|
||||
await interaction.followup.send(f"Ich konnte keinen Wikipedia-Artikel für `{query}` finden.", ephemeral=True)
|
||||
except wikipedia.exceptions.DisambiguationError as e:
|
||||
options_list = "\n".join(e.options[:5])
|
||||
await interaction.followup.send(f"Deine Suche ist mehrdeutig. Meintest du vielleicht:\n`{options_list}`", ephemeral=True)
|
||||
|
||||
async def setup(bot: commands.Bot):
|
||||
await bot.add_cog(Search(bot))
|
Reference in New Issue
Block a user