From c08bad409f27ece3d2f029f6d6f1c3ba96c8c0b4 Mon Sep 17 00:00:00 2001 From: Lukas Blacha Date: Wed, 8 Feb 2023 11:42:24 +0100 Subject: [PATCH] Added Google-Command --- Music-Bot.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Music-Bot.py b/Music-Bot.py index d47d106..7f164d8 100644 --- a/Music-Bot.py +++ b/Music-Bot.py @@ -1,5 +1,6 @@ import asyncio from datetime import timedelta +from urllib.parse import quote_plus import yt_dlp import discord import logging @@ -82,6 +83,22 @@ else: 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.message_content = True @@ -198,6 +215,12 @@ else: 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 @stream.before_invoke async def ensure_voice(ctx: commands.Context):