From 2f1687617717a2d630a74cbc7a4be27004309837 Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 7 Jan 2020 04:07:39 +0100 Subject: [PATCH] Added .gitignore, Added Inspirobot --- .gitignore | 3 +++ api/inspirobot.py | 11 +++++++++++ bot.py | 22 ++++++++++++++++++---- cogs/api/inspirobot.py | 23 +++++++++++++++++++++++ cogs/api/steam.py | 10 ++++++---- 5 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 .gitignore create mode 100644 api/inspirobot.py create mode 100644 cogs/api/inspirobot.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aeaf289 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.json +*.txt +__pycache__ \ No newline at end of file diff --git a/api/inspirobot.py b/api/inspirobot.py new file mode 100644 index 0000000..8547e15 --- /dev/null +++ b/api/inspirobot.py @@ -0,0 +1,11 @@ +import requests +from util import logging + +def get_inspirational_quote() -> str: + response = requests.get("http://inspirobot.me/api?generate=true") + + if not response.ok: + logging.error(f"Steam API response not OK: {response.status_code} [http://inspirobot.me/api?generate=true]") + return None + + return response.text diff --git a/bot.py b/bot.py index ffefff9..59e57d9 100644 --- a/bot.py +++ b/bot.py @@ -11,15 +11,29 @@ client = commands.Bot(command_prefix='.', case_insensitive=True) @client.command() @commands.is_owner() async def load(ctx, extension): - client.load_extension(f"cogs.{extension}") - await ctx.message.add_reaction("👍") + try: + client.load_extension(f"cogs.{extension}") + await ctx.message.add_reaction("👍") + except: + await ctx.message.add_reaction("👎") @client.command() @commands.is_owner() async def unload(ctx, extension): - client.unload_extension(f"cogs.{extension}") - await ctx.message.add_reaction("👍") + try: + client.unload_extension(f"cogs.{extension}") + await ctx.message.add_reaction("👍") + except: + await ctx.message.add_reaction("👎") +@client.command() +@commands.is_owner() +async def reload(ctx, extension): + try: + client.reload_extension(f"cogs.{extension}") + await ctx.message.add_reaction("👍") + except: + await ctx.message.add_reaction("👎") @client.event diff --git a/cogs/api/inspirobot.py b/cogs/api/inspirobot.py new file mode 100644 index 0000000..50a0b27 --- /dev/null +++ b/cogs/api/inspirobot.py @@ -0,0 +1,23 @@ +import discord + +from discord.ext import commands +from api import inspirobot + +class Inspirobot(commands.Cog): + + def __init__(self, client): + self.client = client + + @commands.command(name="Inspirobot", description="Sends a randomly generated inspirational quote", aliases=["inspiration", "inspiro"]) + @commands.cooldown(1, 5) + async def inspirobot(self, ctx): + image = inspirobot.get_inspirational_quote() + if image is None: + await ctx.message.add_reaction("⚠️") + else: + embed = discord.Embed(title="InspiroBot", color=0x111387) + embed.set_image(url=image) + await ctx.send(embed=embed) + +def setup(client): + client.add_cog(Inspirobot(client)) \ No newline at end of file diff --git a/cogs/api/steam.py b/cogs/api/steam.py index 5be733e..b8aee1a 100644 --- a/cogs/api/steam.py +++ b/cogs/api/steam.py @@ -11,11 +11,13 @@ class Steam(commands.Cog): @commands.cooldown(1, 2) async def SteamLevel(self, ctx, vanity_url): level = steam.get_steam_level(vanity_url) - percentile = round(float(steam.get_steam_level_distribution(level)), 2) - if level is not None: - await ctx.send(f"{vanity_url} is level {level}! This makes him better than {percentile}% of Steam users!") - else: + + if level is None: await ctx.send(f"There is nobody named \"{vanity_url}\" on Steam, or their profile might be pivate.") + else: + percentile = round(float(steam.get_steam_level_distribution(level)), 2) + await ctx.send(f"{vanity_url} is level {level}! This makes him better than {percentile}% of Steam users!") +