dragon-bot/app/cogs/actual_utils.py
2022-10-25 11:43:25 -07:00

166 lines
4.9 KiB
Python

from discord.ext import commands
from discord import option
import discord
import os
import get_from_reddit
import core_utils
import requests
class ActualUtils(commands.Cog):
def __init__(self, bot):
self.bot: commands.Bot = bot
@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.command(name="issue")
async def issue(self, ctx: commands.Context):
import gitlab
await ctx.send(gitlab.parse_message(ctx.message))
@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 ask(self, ctx, query):
import questions
await ctx.defer()
await ctx.send_followup(
questions.answer_question(query),
)
@commands.command(name="openai")
async def openai(self, ctx: commands.Context, *, query):
if ctx.message.author.id != core_utils.my_id:
await ctx.send("Sorry, this is a paid dale-bot feature")
return
import questions
await ctx.reply(
questions.open_ai(query),
)
@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",
)
async def trackdays(self, ctx: commands.Context):
import trackdays
shid = await trackdays.get_msreg()
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**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.send(embed=embed)
@commands.slash_command(
guild_ids=None,
name="corona",
description="Query Johns hopkins online covid stats. Requires a location",
)
@option(
name="location",
required=True,
description="The location you want to see covid stats for",
default="California",
)
async def corona(self, ctx: commands.Context, location):
import corona
await ctx.defer()
result = corona.parse_message(location)
await ctx.send_followup(embed=result)
@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))