dragon-bot/app/cogs/actual_utils.py

168 lines
4.9 KiB
Python
Executable File

from discord import option
from discord.ext import commands
import discord
import os
import tempfile
class ActualUtils(commands.Cog):
def __init__(self, bot):
self.bot: commands.Bot = bot
@commands.slash_command(
guild_ids=None,
name="translate",
description="Translate a string",
)
@option(
"query",
description="What you're tryna translate",
input_type="str",
requred=True,
)
async def translate(self, ctx, query: str):
from googletrans import Translator
translator = Translator()
result = translator.translate(query, dest="en").text
await ctx.defer()
await ctx.followup.send(result)
@commands.slash_command(
guild_ids=None,
name="youtube",
description="Search youtube for the passed in query",
)
@option(
"query",
description="The search string you want to enter on youtube",
input_type="str",
requred=True,
)
async def youtube(self, ctx, query: str):
import re
from urllib import parse, request
query_string = parse.urlencode({"search_query": query})
html_content = request.urlopen("http://www.youtube.com/results?" + query_string)
search_results = re.findall("\/watch\?v=(.{11})", html_content.read().decode())
result = "https://www.youtube.com/watch?v=" + search_results[0]
await ctx.defer()
await ctx.followup.send(result)
@commands.slash_command(
guild_ids=None,
name="define",
description="Grabs the highest rated definition from urban dictionary",
)
@option(
name="word",
description="The word to define. Tries UD first then an actual dictionary",
required=True,
)
async def define(self, ctx, word):
import define_word
await ctx.defer()
await ctx.send_followup(embed=define_word.get_definition(word))
@commands.command(name="tts")
async def tts(self, ctx: commands.Context):
import tts
if ctx.message.content.split()[1] == "langs":
await ctx.send("Ok {}, check your DMs".format(ctx.message.author.mention))
return await ctx.message.author.send(tts.get_all_langs())
file_path = tts.text_to_speech(ctx.message.content)
await ctx.send(
file=discord.File(
file_path,
filename="A Message From {}.mp3".format(ctx.message.author.name),
)
)
await ctx.message.delete()
os.remove(file_path)
@commands.slash_command(
guild_ids=None, name="wolfram", description="Send a query to wolfram alpha"
)
@option(
name="query",
required=True,
description="The query you want to pass to wolfram alpha",
)
async def wolfram(self, ctx, query):
import wolframalpha
client = wolframalpha.Client(os.getenv("wolfram_token"))
await ctx.defer()
try:
res = client.query(query)
return await ctx.send_followup(
"You asked: %s\n\n%s" % (query, next(res.results).text)
)
except Exception:
return await ctx.send_followup("Sorry, I'm unable to answer that")
@commands.slash_command(
guild_ids=None,
name="wifiqr",
description="Generate a qr code for your wifi network",
)
@option(
name="network_name",
required=True,
description="The name of your network",
)
@option(
name="password", required=True, description="Your wifi password", min_length=8
)
async def wifiqr(self, ctx, network_name, password):
import wifi_qrcode_generator.generator
qr_code = wifi_qrcode_generator.generator.wifi_qrcode(
ssid=network_name,
hidden=False,
authentication_type="WPA",
password=password,
)
await ctx.defer()
try:
file, file_path = tempfile.mkstemp()
qr_code.make_image().save(file_path + ".png")
return await ctx.send_followup(file=discord.File(file_path + ".png"))
os.remove(file_path + ".png")
except Exception as e:
return await ctx.send_followup(f"Something went wrong,\n{e}")
@commands.slash_command(
guld_ids=None,
name="stock",
description="Enter one or many stock tickers to get info about them",
)
@option(
name="symbols", description="Stocks to look up the price for", required=True
)
@option(
name="verbose",
description="Return extended info about the stocks",
type=bool,
default=False,
)
async def stock(self, ctx, symbols, verbose):
import stock
results = stock.parse_message(symbols, verbose)
await ctx.defer()
for res in results:
await ctx.respond(embed=res)
def setup(bot):
bot.add_cog(ActualUtils(bot))