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/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)