lmgtfy-bot/bot.py
jjohnstondev 1c748308f9 initia
2023-01-26 17:34:28 -08:00

61 lines
1.5 KiB
Python

import sys
from urllib.parse import quote_plus
import discord
import os
from discord.ext import commands
intents = discord.Intents.default()
class LMGTFYBot(commands.Bot):
def __init__(self):
intents = discord.Intents.default()
intents.message_content = True
super().__init__(
command_prefix=commands.when_mentioned_or('!'),
intents=intents
)
async def on_ready(self):
print(f'Logged in as {self.user})')
print('------')
class LMGTFY(discord.ui.View):
def __init__(self, query: str):
super().__init__()
query = quote_plus(query)
search_url = f'https://lmgtfy.asdf.cafe/?search={query}'
self.add_item(discord.ui.Button(
label='Google Search',
url=search_url)
)
self.add_item(discord.ui.Button(
label='Feeling Lucky',
url=search_url+"&lucky=true")
)
bot = LMGTFYBot()
@bot.command()
async def lmgtfy(ctx: commands.Context, search: str | None):
if search is None:
if ctx.message.reference is None:
return
if ctx.message.reference.message_id is None:
return
repl = await ctx.fetch_message(ctx.message.reference.message_id)
await repl.reply('I will do it for you :)', view=LMGTFY(repl.content))
if search is not None:
await ctx.reply('I will do it for you :)', view=LMGTFY(search))
return
TOKEN = os.getenv("TOKEN")
if TOKEN is None:
sys.exit("TOKEN INVALID")
bot.run(TOKEN)