lmgtfy-bot/bot.py

88 lines
2.5 KiB
Python
Raw Normal View History

2023-01-27 01:34:28 +00:00
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()
2023-01-27 20:43:27 +00:00
@bot.event
async def on_message(message: discord.Message):
if message.author == bot.user:
return
if message.reference is None:
if message.content.startswith("!lmgtfy"):
if len(message.content.split()) > 1:
await message.reply('I will do it for you :)', view=LMGTFY(
message.content.split(' ', 1)[1])
)
return
if "google it" in message.content.lower():
if message.reference.message_id is None:
return
repl = await message.channel.fetch_message(
message.reference.message_id
)
await repl.reply('I will do it for you :)', view=LMGTFY(repl.content))
if message.content == "!lmgtfy":
if message.reference.message_id is None:
return
repl = await message.channel.fetch_message(
message.reference.message_id
)
await repl.reply('I will do it for you :)', view=LMGTFY(repl.content))
2023-01-27 01:34:28 +00:00
@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)