Added carbon API!

This commit is contained in:
Robert 2020-01-08 23:49:57 +01:00
parent 75d5a3c664
commit 81c299dffb
5 changed files with 162 additions and 5 deletions

38
cogs/api/carbon.py Normal file
View file

@ -0,0 +1,38 @@
import discord
from discord.ext import commands
from api import carbon
from util import embed
class Carbon(commands.Cog):
def __init__(self, client: discord.Client):
self.client = client
@commands.group(name="carbon", usage="carbon [subcommand]", description="Can accumulate data about carbon levels in the UK")
async def carbon(self, ctx):
pass
@carbon.command(name="now", usage="carbon now", description="Gets the latest available carbon intensity data")
async def now(self, ctx):
data = carbon.current_level()
await ctx.send(embed=embed.make_embed_fields_footer("Carbon intensity in the UK", f"The current carbon intensity is considered **{data[3]}**.", f"{data[0]} | All values in gCO2/kWh", ("Measured", data[2]), ("Predicted", data[1])))
@carbon.command(name="at", usage="carbon at <Date>", description="Gets the carbon levels at the given date (YYYY-MM-DD)")
async def at(self, ctx: discord.Client, date: str):
data = carbon.level_at(date)
if len(data) == 0:
await ctx.send(embed=embed.make_error_embed(f"There is no data available for {date}."))
else:
await ctx.send(embed=embed.make_embed_fields_footer("Carbon intensity in the UK", f"The carbon intensity for that date is considered **{data[3]}**.", f"{data[0]} | All values in gCO2/kWh", ("Measured", data[2]), ("Predicted", data[1])))
@carbon.command(name="during", usage="carbon during <Start> <Stop>", description="Creates a diagram about carbon levels in the given time period (YYYY-MM-DD)")
async def during(self, ctx: discord.Client, start: str, stop: str):
path = carbon.level_from_to(start, stop)
if path == "":
await ctx.send(embed=embed.make_error_embed(f"There is no data available for the given time period."))
else:
data = embed.make_embed_image("Carbon intensity in the UK", path)
await ctx.send(embed=data[0], file=data[1])
def setup(client: discord.Client):
client.add_cog(Carbon(client))

View file

@ -20,7 +20,29 @@ class Help(commands.MinimalHelpCommand):
await self.context.send(embed=embed.make_error_embed("Command not found"))
async def send_group_help(self, group):
await self.context.send(embed=embed.make_error_embed("Command not found"))
try:
subcmds = ""
if group.commands != []:
for command in group.commands:
subcmds += "`" + command.name + "`, "
subcmds = subcmds[:-2]
else:
subcmds = "`None`"
alias = ""
if group.aliases != []:
for i in range(len(group.aliases)):
if i == len(group.aliases) - 1:
alias = alias + '`' + group.aliases[i] + '`'
else:
alias = alias + '`' + group.aliases[i] + '`' + ', '
else:
alias = "`None`"
await self.context.send(embed=embed.make_embed_fields_ninl(group.name, group.description, ("Usage", f"`{config.settings['prefix']}{group.usage}`"), ("Aliases", alias), ("Subcommands", subcmds)))
except Exception as e:
logging.error(str(e))
await self.context.send(embed=embed.make_error_embed("Command not found"))
async def send_command_help(self, command):
try:
@ -34,7 +56,10 @@ class Help(commands.MinimalHelpCommand):
else:
alias = "`None`"
await self.context.send(embed=embed.make_embed_fields_ninl(command.name, command.description, ("Usage", f"`{config.settings['prefix']}{command.usage}`"), ("Aliases", alias)))
command_name = command.name
if command.parent != None:
command_name += f" ({command.parent})"
await self.context.send(embed=embed.make_embed_fields_ninl(command_name, command.description, ("Usage", f"`{config.settings['prefix']}{command.usage}`"), ("Aliases", alias)))
except Exception as e:
logging.error(str(e))
await self.context.send(embed=embed.make_error_embed("Command not found"))