1
0
Fork 0
mirror of synced 2024-05-09 23:22:27 +12:00

Compare commits

...

4 commits

Author SHA1 Message Date
Dan Hess 13e20f73ad Clean up API method, man I must have made this a while ago, it's ugly 2020-04-13 15:07:54 -05:00
Dan Hess 04c029b368 Use asyncio's subprocess module 2020-04-13 14:15:37 -05:00
Dan Hess 86546dec16 Move horse noodle API to its own method, add more images 2020-04-13 14:10:03 -05:00
Dan Hess a2cd241265 Correct how to get horse/snek 2020-04-13 14:02:41 -05:00
2 changed files with 63 additions and 42 deletions

View file

@ -11,6 +11,24 @@ import utils
class Images(commands.Cog):
"""Commands that post images, or look up images"""
async def horse_noodle_api(self, ctx, animal):
data = await utils.request(f"http://hrsendl.com/api/{animal}")
try:
url = data["data"]["file_url_size_large"]
filename = data["data"]["file_name"]
except (KeyError, TypeError):
return await ctx.send(f"I couldn't connect! Sorry no {animal}s right now ;w;")
else:
image = await utils.download_image(url)
f = discord.File(image, filename=filename)
try:
await ctx.send(file=f)
except discord.HTTPException:
await ctx.send(
f"File to large to send as attachment, here is the URL: {url}"
)
@commands.command(aliases=['rc'])
@utils.can_run(send_messages=True)
async def cat(self, ctx):
@ -66,26 +84,7 @@ class Images(commands.Cog):
EXAMPLE: !snek
RESULT: A beautiful picture of a snek o3o"""
data = await utils.request("http://hrsendl.com/api/snake")
if data is None:
await ctx.send("I couldn't connect! Sorry no snakes right now ;w;")
return
result = data['data']
filename = result.get('file_url_size_large', None)
if filename is None:
await ctx.send("I couldn't connect! Sorry no snakes right now ;w;")
return
image = await utils.download_image(filename)
filename = re.search('.*/optimized/large/(.*)', filename).group(1)
f = discord.File(image, filename=filename)
try:
await ctx.send(file=f)
except discord.HTTPException:
await ctx.send(
f"File to large to send as attachment, here is the URL: {url}"
)
await self.horse_noodle_api(ctx, "snake")
@commands.command()
@utils.can_run(send_messages=True)
@ -94,26 +93,43 @@ class Images(commands.Cog):
EXAMPLE: !horse
RESULT: A beautiful picture of a horse o3o"""
data = await utils.request("http://hrsendl.com/api/horse")
await self.horse_noodle_api(ctx, "horse")
if data is None:
await ctx.send("I couldn't connect! Sorry no horses right now ;w;")
return
result = data['data']
filename = result.get('file_url_size_large', None)
if filename is None:
await ctx.send("I couldn't connect! Sorry no horses right now ;w;")
return
@commands.command()
@utils.can_run(send_messages=True)
async def duck(self, ctx):
"""Use this to print a random duck image.
image = await utils.download_image(filename)
filename = re.search('.*/optimized/large/(.*)', filename).group(1)
f = discord.File(image, filename=filename)
try:
await ctx.send(file=f)
except discord.HTTPException:
await ctx.send(
f"File to large to send as attachment, here is the URL: {url}"
)
EXAMPLE: !duck
RESULT: A beautiful picture of a duck o3o"""
await self.horse_noodle_api(ctx, "duck")
@commands.command()
@utils.can_run(send_messages=True)
async def snail(self, ctx):
"""Use this to print a random snail image.
EXAMPLE: !snail
RESULT: A beautiful picture of a snail o3o"""
await self.horse_noodle_api(ctx, "snail")
@commands.command()
@utils.can_run(send_messages=True)
async def pleco(self, ctx):
"""Use this to print a random pleco image.
EXAMPLE: !pleco
RESULT: A beautiful picture of a pleco o3o"""
await self.horse_noodle_api(ctx, "pleco")
@commands.command()
@utils.can_run(send_messages=True)
async def moth(self, ctx):
"""Use this to print a random moth image.
EXAMPLE: !moth
RESULT: A beautiful picture of a moth o3o"""
await self.horse_noodle_api(ctx, "moth")
@commands.command()
@commands.guild_only()

View file

@ -188,11 +188,16 @@ class Owner(commands.Cog):
@commands.command()
async def bash(self, ctx, *, cmd: str):
"""Runs a bash command"""
output = subprocess.check_output("{}; exit 0".format(cmd), stderr=subprocess.STDOUT, shell=True)
if output:
await ctx.send("```\n{}\n```".format(output.decode("utf-8", "ignore").strip()))
proc = await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT
)
stdout = (await proc.communicate())[0]
if stdout:
await ctx.send(f'[stdout]\n{stdout.decode()}')
else:
await ctx.send("No output for `{}`".format(cmd))
await ctx.send("Process finished, no output")
@commands.command()
async def shutdown(self, ctx):