dragon-bot/app/cogs/actual_utils.py

199 lines
6.2 KiB
Python
Executable File

from discord.ext import commands
from discord import option
import discord
import os
import core_utils
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=core_utils.my_guilds,
name="issue",
description="Files an issue on gitlab",
)
@option(name="title", requried=True, description="The title of the issue")
@option(name="description", require=True, description="The body of the issue")
async def issue(self, ctx: commands.Context):
post_args = {"title": title, "description": description}
headers = {"PRIVATE-TOKEN": os.getenv("gitlab_token")}
r = requests.post(
"https://git.luker.fr/api/v4/projects/3/issues",
data=post_args,
headers=headers,
)
await ctx.respond(r.json()["web_url"])
@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
embed = define_word.get_definition(word)
await ctx.respond(embed=embed)
@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.has_role("Track day gamers")
@commands.slash_command(
guild_ids=core_utils.my_guilds,
name="trackdays",
description="Query motorsportsreg.com for a list of trackdays going on at Buttonwillow and Thunderhill",
)
@option(
"track",
description="Choose a track to see the trackdays at, or leave blank for all",
choices=["Buttonwillow", "Thunderhill", "Both"],
default="Both",
)
async def trackdays(self, ctx: commands.Context, track: str):
import trackdays
shid = await trackdays.get_msreg(track=track)
for track, events in shid.items():
embed = discord.Embed(
description=":checkered_flag: **Upcoming events at %s**:checkered_flag: "
% track,
color=0x428BCA,
type="rich",
)
for track_day in events:
embed.add_field(
name="-------------\n**Organizer**",
value=track_day["event_organizer"],
inline=False,
)
embed.add_field(
name="**Event Name**",
value=track_day["event_name"],
inline=False,
)
embed.add_field(
name="Date", value=track_day["event_date"], inline=False
)
embed.add_field(
name="Event URL", value=track_day["event_url"], inline=False
)
await ctx.defer()
await ctx.send_followup(embed=embed)
@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.send_followup(embed=res)
def setup(bot):
bot.add_cog(ActualUtils(bot))