Added Google-Command

This commit is contained in:
Lukas Blacha
2023-02-08 11:42:24 +01:00
parent fadb42bbf6
commit c08bad409f

View File

@@ -1,5 +1,6 @@
import asyncio import asyncio
from datetime import timedelta from datetime import timedelta
from urllib.parse import quote_plus
import yt_dlp import yt_dlp
import discord import discord
import logging import logging
@@ -82,6 +83,22 @@ else:
before_options="-threads 2", options=ffmpeg_options), data=data) before_options="-threads 2", options=ffmpeg_options), data=data)
class Google(discord.ui.View):
def __init__(self, query: str):
super().__init__()
# We need to quote the query string to make a valid url. Discord will raise an error if it isn't valid.
query = quote_plus(query)
url = f"https://www.google.com/search?q={query}"
# Link buttons cannot be made with the
# decorator, so we have to manually create one.
# We add the quoted url to the button, and add the button to the view.
self.add_item(discord.ui.Button(label="Click Here", url=url))
# Initializing the view and adding the button can actually be done in a one-liner at the start if preferred:
# super().__init__(discord.ui.Button(label="Click Here", url=url))
intents = discord.Intents.default() intents = discord.Intents.default()
intents.message_content = True intents.message_content = True
@@ -198,6 +215,12 @@ else:
await ctx.voice_client.disconnect(force=True) await ctx.voice_client.disconnect(force=True)
@bot.slash_command()
async def google(ctx: discord.ApplicationContext, query: str):
"""Returns a google link for a query."""
await ctx.respond(f"Google Result for: `{query}`", view=Google(query))
@play.before_invoke @play.before_invoke
@stream.before_invoke @stream.before_invoke
async def ensure_voice(ctx: commands.Context): async def ensure_voice(ctx: commands.Context):