From a909ce176edfeeff652a913be62fe1ba42523369 Mon Sep 17 00:00:00 2001 From: Josh Stockin Date: Sun, 6 Sep 2020 17:35:02 -0500 Subject: [PATCH 1/2] Add inspirobot command to misc cog --- futaba/cogs/misc/core.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/futaba/cogs/misc/core.py b/futaba/cogs/misc/core.py index c0182d12..f30efccb 100644 --- a/futaba/cogs/misc/core.py +++ b/futaba/cogs/misc/core.py @@ -19,6 +19,7 @@ from datetime import datetime from hashlib import sha1 +import aiohttp import discord from discord.ext import commands @@ -34,6 +35,7 @@ __all__ = ["Miscellaneous"] SHA1_ERROR_MESSAGE = "Error downloading file".ljust(40) +INSPIROBOT_API = "https://inspirobot.me/api?generate=true" class Miscellaneous(AbstractCog): @@ -134,3 +136,17 @@ async def sha1sum(self, ctx, *urls: str): for content in contents: await ctx.send(content=str(content)) + + @commands.command(name="inspirobot", aliases=["inspire", "inspireme"]) + async def inspirobot(self, ctx): + """ Returns an image generated by InspiroBot. """ + async with aiohttp.ClientSession() as session: + async with session.get(INSPIROBOT_API) as api_resp: + if api_resp.status == 200: + image_url = await api_resp.text() + embed = discord.Embed() + embed.title = "Be inspired." + embed.set_image(url=image_url) + await ctx.send(embed=embed) + else: + raise CommandFailed() From dda1e69d7886cb0ab648bcdc3ee7f83abce2982d Mon Sep 17 00:00:00 2001 From: Josh Stockin Date: Sun, 6 Sep 2020 18:56:31 -0500 Subject: [PATCH 2/2] Move InspiroBot command to subcog of misc --- futaba/cogs/misc/__init__.py | 12 ++++++++ futaba/cogs/misc/core.py | 16 ---------- futaba/cogs/misc/inspirobot.py | 53 ++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 16 deletions(-) create mode 100644 futaba/cogs/misc/inspirobot.py diff --git a/futaba/cogs/misc/__init__.py b/futaba/cogs/misc/__init__.py index 9dba5380..d70a2be9 100644 --- a/futaba/cogs/misc/__init__.py +++ b/futaba/cogs/misc/__init__.py @@ -13,12 +13,14 @@ from .core import Miscellaneous from .debug import Debugging from .mentionable import Mentionable +from .inspirobot import InspiroBot # Setup for when cog is loaded def setup(bot): setup_miscellaneous(bot) setup_debugging(bot) setup_mentionable(bot) + setup_inspirobot(bot) def setup_miscellaneous(bot): @@ -38,11 +40,17 @@ def setup_mentionable(bot): bot.add_cog(cog) +def setup_inspirobot(bot): + cog = InspiroBot(bot) + bot.add_cog(cog) + + # Remove all the cogs when cog is unloaded def teardown(bot): teardown_miscellaneous(bot) teardown_debugging(bot) teardown_mentionable(bot) + teardown_inspirobot(bot) def teardown_miscellaneous(bot): @@ -55,3 +63,7 @@ def teardown_debugging(bot): def teardown_mentionable(bot): bot.remove_cog(Mentionable.__name__) + + +def teardown_inspirobot(bot): + bot.remove_cog(InspiroBot.__name__) diff --git a/futaba/cogs/misc/core.py b/futaba/cogs/misc/core.py index f30efccb..c0182d12 100644 --- a/futaba/cogs/misc/core.py +++ b/futaba/cogs/misc/core.py @@ -19,7 +19,6 @@ from datetime import datetime from hashlib import sha1 -import aiohttp import discord from discord.ext import commands @@ -35,7 +34,6 @@ __all__ = ["Miscellaneous"] SHA1_ERROR_MESSAGE = "Error downloading file".ljust(40) -INSPIROBOT_API = "https://inspirobot.me/api?generate=true" class Miscellaneous(AbstractCog): @@ -136,17 +134,3 @@ async def sha1sum(self, ctx, *urls: str): for content in contents: await ctx.send(content=str(content)) - - @commands.command(name="inspirobot", aliases=["inspire", "inspireme"]) - async def inspirobot(self, ctx): - """ Returns an image generated by InspiroBot. """ - async with aiohttp.ClientSession() as session: - async with session.get(INSPIROBOT_API) as api_resp: - if api_resp.status == 200: - image_url = await api_resp.text() - embed = discord.Embed() - embed.title = "Be inspired." - embed.set_image(url=image_url) - await ctx.send(embed=embed) - else: - raise CommandFailed() diff --git a/futaba/cogs/misc/inspirobot.py b/futaba/cogs/misc/inspirobot.py new file mode 100644 index 00000000..157efb3d --- /dev/null +++ b/futaba/cogs/misc/inspirobot.py @@ -0,0 +1,53 @@ +# +# cogs/misc/inspirobot.py +# +# futaba - A Discord Mod bot for the Programming server +# Copyright (c) 2017-2020 Jake Richardson, Ammon Smith, jackylam5 +# +# futaba is available free of charge under the terms of the MIT +# License. You are free to redistribute and/or modify it under those +# terms. It is distributed in the hopes that it will be useful, but +# WITHOUT ANY WARRANTY. See the LICENSE file for more details. +# + +""" Cog for InspiroBot image generation. """ + +import logging + +import aiohttp +import discord +from discord.ext import commands + +from ..abc import AbstractCog + +logger = logging.getLogger(__name__) + +__all__ = ["InspiroBot"] + +INSPIROBOT_API = "https://inspirobot.me/api?generate=true" + + +class InspiroBot(AbstractCog): + __slots__ = ("journal",) + + def __init__(self, bot): + super().__init__(bot) + self.journal = bot.get_broadcaster("/inspirobot") + + def setup(self): + pass + + @commands.command(name="inspirobot", aliases=["inspire", "inspireme"]) + async def inspirobot(self, ctx): + """ Returns an image generated by InspiroBot. """ + async with aiohttp.ClientSession(raise_for_status=True) as session: + async with session.get(INSPIROBOT_API) as api_resp: + image_url = await api_resp.text() + + content = f"Got generated image {image_url} from InspiroBot" + self.journal.send("api/success", ctx.guild, content, icon="ok") + + embed = discord.Embed() + embed.title = "Be inspired." + embed.set_image(url=image_url) + await ctx.send(embed=embed)